Ejemplo n.º 1
0
def _main(sys_args: List[str], unload_plugins: bool = True):
    _repobee.cli.parsing.setup_logging()
    args = sys_args[1:]  # drop the name of the program
    traceback = False
    pre_init = True
    try:
        preparser_args, app_args = separate_args(args)
        parsed_preparser_args = _repobee.cli.preparser.parse_args(
            preparser_args,
            default_config_file=_resolve_config_file(
                pathlib.Path(".").resolve()),
        )

        _initialize_plugins(parsed_preparser_args)

        parsed_args, api = _parse_args(app_args,
                                       parsed_preparser_args.config_file)
        traceback = parsed_args.traceback
        pre_init = False

        with _set_output_verbosity(getattr(parsed_args, "quiet", 0)):
            _repobee.cli.dispatch.dispatch_command(
                parsed_args, api, parsed_preparser_args.config_file)
    except exception.PluginLoadError as exc:
        plug.log.error(f"{exc.__class__.__name__}: {exc}")
        raise
    except exception.ParseError as exc:
        plug.log.error(str(exc))
        raise
    except Exception as exc:
        # FileErrors can occur during pre-init because of reading the config
        # and we don't want tracebacks for those (afaik at this time)
        if traceback or (pre_init
                         and not isinstance(exc, exception.FileError)):
            plug.log.error(str(exc))
            if pre_init:
                plug.echo(_PRE_INIT_ERROR_MESSAGE)
            plug.log.exception("Critical exception")
        else:
            plug.log.error("{.__class__.__name__}: {}".format(exc, str(exc)))
        raise
    finally:
        if unload_plugins:
            plugin.unregister_all_plugins()
Ejemplo n.º 2
0
 def unregister_all_plugins(self):
     plugin.unregister_all_plugins()
Ejemplo n.º 3
0
def run(
    cmd: List[str],
    config_file: Union[str, pathlib.Path] = "",
    plugins: Optional[List[Union[ModuleType, plug.Plugin]]] = None,
    workdir: Union[str, pathlib.Path] = ".",
) -> Mapping[str, List[plug.Result]]:
    """Run RepoBee with the provided options. This function is mostly intended
    to be used for testing plugins.

    .. important::

        This function will always unregister all plugins after execution,
        including anly plugins that may have been registered prior to running
        this function.

    Running this function is almost equivalent to running RepoBee from the CLI,
    with the following exceptions:

    1. Preparser options must be passed as arguments to this function (i.e.
       cannot be given as part of ``cmd``).
    2. There is no error handling at the top level, so exceptions are raised
       instead of just logged.

    As an example, the following CLI call:

    .. code-block:: bash

        $ repobee --plug ext.py --config-file config.ini config show

    Can be executed as follows:

    .. code-block:: python

        import ext
        from repobee import run

        run(["config", "show"], config_file="config.ini", plugins=[ext])

    Args:
        cmd: The command to run.
        config_file: Path to the configuration file.
        plugins: A list of plugin modules and/or plugin classes.
        workdir: The working directory to run RepoBee in.
    Returns:
        A mapping (plugin_name -> plugin_results).
    """
    config_file = pathlib.Path(config_file)
    cur_workdir = pathlib.Path(".").absolute()
    requested_workdir = pathlib.Path(str(workdir)).resolve(strict=True)

    @contextlib.contextmanager
    def _in_requested_workdir():
        try:
            os.chdir(requested_workdir)
            yield
        finally:
            os.chdir(cur_workdir)

    def _ensure_is_module(p: Union[ModuleType, plug.Plugin]):
        if isinstance(p, type) and issubclass(p, plug.Plugin):
            mod = ModuleType(p.__name__.lower())
            mod.__package__ = f"__{p.__name__}"
            setattr(mod, p.__name__, p)
            return mod
        elif isinstance(p, ModuleType):
            return p
        else:
            raise TypeError(f"not plugin or module: {p}")

    wrapped_plugins = list(map(_ensure_is_module, plugins or []))

    with _in_requested_workdir():
        try:
            _repobee.cli.parsing.setup_logging()
            # FIXME calling _initialize_plugins like this is ugly, should be
            # refactored
            _initialize_plugins(argparse.Namespace(no_plugins=False, plug=[]))
            plugin.register_plugins(wrapped_plugins)
            parsed_args, api = _parse_args(cmd, config_file)

            with _set_output_verbosity(getattr(parsed_args, "quiet", 0)):
                return _repobee.cli.dispatch.dispatch_command(
                    parsed_args, api, config_file)
        finally:
            plugin.unregister_all_plugins()
Ejemplo n.º 4
0
def _unregister_plugins_on_exit(unregister: bool = True):
    try:
        yield
    finally:
        if unregister:
            plugin.unregister_all_plugins()