import pkgutil
import importlib


def scan_for_agents(do_registration=True):
    """Identify and import ocs Agent plugin scripts.  This will find all
    modules in the current module search path (sys.path) that begin
    with the name 'ocs_plugin\_'.

    Args:
        do_registration (bool): If True, the modules are imported,
            which likely causes them to call register_agent_class on
            each agent they represent.

    Returns:
        The list of discovered module names.

    """
    items = []
    for modinfo in pkgutil.iter_modules():
        if modinfo.name.startswith('ocs_plugin_'):
            items.append(modinfo.name)
            if do_registration:
                importlib.import_module(modinfo.name)
    return items