Exemplo n.º 1
0
def _try(description, last, command, args, call):
    """Try the completion"""
    if command == config.main_command.path:
        args = [config.main_command.path] + list(args)
    else:
        args = [config.main_command.path, command] + list(args)
    complete_envvar = "_{}_COMPLETE".format(config.main_command.path.upper().replace("-", "_"))
    if description:
        extra_env = {
            "COMMANDLINE": " ".join(quote(arg) for arg in args) + ('' if last else ' '),
            complete_envvar: "complete-fish",
        }
    else:
        extra_env = {
            "COMP_WORDS": " ".join(quote(arg) for arg in args),
            "COMP_CWORD": str(max(0, len(args) - (1 if last else 0))),
            complete_envvar: "complete",
        }
    extra_env.update({CASE_INSENSITIVE_ENV: 'ON'} if config.completion.case_insensitive else {})
    with updated_env(**extra_env):
        if call:
            call_me()
        else:
            oldvalue = click_project.completion.IN_COMPLETION
            click_project.completion.IN_COMPLETION = True
            config.main_command()
            click_project.completion.IN_COMPLETION = oldvalue
Exemplo n.º 2
0
def run(cmd, *args, **kwargs):
    """Calls the main_command without polluting the calling config

    Directly calling the main_command has two drawbacks.

    1. A call to main_command redo a parsing of the arguments. This may result in
       rewritting things into the config.
    2. The calling side generally don't want to handle possible changes in
       the config made by the called command.

    This command runs main_command in a new config, avoiding the nasty side
    effects. It extends to call with its own main_command command line
    parameters, so that the new parsing step should have a similar default
    behavior.

    For instance, say the command `a` does `config.me = "a"`, and the command
    `b` does `config.me = "b"`, then calls `a`, and then echo
    config.me. Now, imagine you execute `clk --develop b`. I expect it to run b
    in develop mode, then run a in develop mode and still write b on the
    standard output. This is exactly what run is about.

    """
    cmd = config.commandline_profile.get_settings("parameters")[
        config.main_command.path] + cmd
    with temp_config():
        return config.main_command(cmd, *args, **kwargs)
Exemplo n.º 3
0
def main():
    exitcode = 0
    try:
        try:
            config.main_command()
        except:  # NOQA: E722
            log.exit_on_log_level = None
            raise
    except click.exceptions.Abort:
        LOGGER.debug("Abooooooooort!!")
        exitcode = 1
    except click.ClickException as e:
        if isinstance(e, click.UsageError) and e.ctx is not None:
            click.echo(e.ctx.get_usage())
        log_trace()
        LOGGER.error(e.format_message())
        if isinstance(e, click.exceptions.NoSuchOption):
            click.echo("Hint: If you don't know where this option comes from,"
                       " try checking the parameters (with {} --no-parameters"
                       " parameters show).".format(config.main_command.path))
        post_mortem()
        exitcode = e.exit_code
    except subprocess.CalledProcessError as e:
        if e.returncode < 0 and e.returncode != -2:
            LOGGER.error(str(e))
        log_trace()
        post_mortem()
        exitcode = e.returncode
    except NotImplementedError as e:
        log_trace()
        click.echo(
            "This command reached a part of the code yet to implement. "
            "Please help us by either submitting patches or "
            "sending report files to us. ({} --report-file .../somefile RESTOFCOMMAND, "
            "then send .../somefile to us on"
            " https://github.com/Konubinix/click-project/issues/new)".format(
                config.main_command.path))
        LOGGER.error(str(e))
        post_mortem()
        exitcode = 2
    except EnvironmentError as e:
        log_trace()
        LOGGER.error(str(e))
        post_mortem()
        exitcode = 1
    except (Exception, log.LogLevelExitException) as e:
        log_trace()
        LOGGER.exception(str(e))
        LOGGER.error("Hmm, it looks like we did not properly catch this error."
                     " Please help us improve click-project by telling us what"
                     " caused the error on"
                     " https://github.com/Konubinix/click-project/issues/new ."
                     " If you feel like a pythonista, you can try debugging"
                     " the issue yourself, running the command"
                     " with {cmd} --post-mortem or {cmd} --develop".format(
                         cmd=config.main_command.path))
        post_mortem()
        exitcode = 1
    finally:
        if profiling is not None:
            profiling.disable()
            sortby = 'cumulative'
            s = StringIO()
            ps = pstats.Stats(profiling, stream=s).sort_stats(sortby)
            ps.print_stats()
            print(s.getvalue())
        trigger()
        end_time = datetime.now()
        LOGGER.debug("command run in %s" %
                     natural_delta(end_time - startup_time))
    exit(exitcode)
Exemplo n.º 4
0
def main():
    exitcode = 0
    try:
        try:
            config.main_command()
        except:  # NOQA: E722
            log.exit_on_log_level = None
            raise
    except click.exceptions.Abort:
        LOGGER.debug("Abooooooooort!!")
        exitcode = 1
    except click.ClickException as e:
        if isinstance(e, click.UsageError) and e.ctx is not None:
            click.echo(e.ctx.get_usage())
        log_trace()
        LOGGER.error(e.format_message())
        if isinstance(e, click.exceptions.NoSuchOption):
            click.echo(
                "Hint: If you don't know where this option comes from,"
                " try checking the parameters (with {} --no-parameters"
                " parameters show).".format(config.main_command.path)
            )
        post_mortem()
        exitcode = e.exit_code
    except subprocess.CalledProcessError as e:
        if e.returncode < 0 and e.returncode != -2:
            LOGGER.error(str(e))
        log_trace()
        post_mortem()
        exitcode = e.returncode
    except NotImplementedError as e:
        log_trace()
        click.echo(
            "This command reached a part of the code yet to implement. "
            "Please help us by either submitting patches or "
            "sending report files to us. ({} --report-file .../somefile RESTOFCOMMAND, "
            "then send .../somefile to us)".format(config.main_command.path)
        )
        LOGGER.error(str(e))
        post_mortem()
        exitcode = 2
    except EnvironmentError as e:
        log_trace()
        LOGGER.error(str(e))
        post_mortem()
        exitcode = 1
    except (Exception, log.LogLevelExitException) as e:
        log_trace()
        LOGGER.exception(str(e))
        LOGGER.error(
            "{} reached an error that was not properly caught."
            " Please tell us.".format(
                config.main_command.path)
        )
        post_mortem()
        exitcode = 1
    finally:
        if profiling is not None:
            profiling.disable()
            sortby = 'cumulative'
            s = StringIO()
            ps = pstats.Stats(profiling, stream=s).sort_stats(sortby)
            ps.print_stats()
            print(s.getvalue())
        trigger()
        end_time = datetime.now()
        LOGGER.debug("command run in %s" % natural_delta(end_time - startup_time))
    exit(exitcode)
Exemplo n.º 5
0
def help(args):
    """Display help information"""
    config.main_command(list(args) + ['--help'])