コード例 #1
0
ファイル: utils.py プロジェクト: adamchainz/hashdist
            def replacement(*args, **kw):
                with ctxmgr_func() as ctx:
                    for x in func(ctx, *args, **kw): yield x
        else:
            @functools.wraps(func)
            def replacement(*args, **kw):
                with ctxmgr_func as ctx:
                    return func(ctx, *args, **kw)
        return replacement
    return decorator



VERBOSE = bool(int(os.environ.get('VERBOSE', '0')))
if VERBOSE:
    configure_logging('DEBUG')
    logger = logging.getLogger()
else:
    configure_logging('WARNING')
    logger = logging.getLogger('null_logger')

#
# Mock archives
#
def make_temporary_tarball(files):
    """Make a tarball in a temporary directory under /tmp; returns
    (name of directory, name of archive, source cache key)
    """
    import tarfile
    from ..source_cache import scatter_files
    from ..hasher import format_digest
コード例 #2
0
ファイル: main.py プロジェクト: erdc/hit
def command_line_entry_point(unparsed_argv, env, default_config_filename=None, secondary=False):
    """
    The main ``hit`` command-line entry point

    Arguments:
    ----------

    unparsed_argv : list of str
        The unparsed command line arguments

    env : dict
        Environment

    default_config_filename : unused (TODO)

    secondary : boolean
        Whether this is hit invoking itself. When set, avoids changing
        configuration. This is a bit of a hack and should be done
        better (TODO)
    """
    description = textwrap.dedent('''
    Entry-point for various HashDist command-line tools
    ''')

    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('--config-file',
                        help='Location of hashdist configuration file (default: %s)'
                        % DEFAULT_CONFIG_FILENAME_REPR,
                        default=DEFAULT_CONFIG_FILENAME)
    parser.add_argument('--ipdb',
                        help='Enable IPython debugging on error',
                        action='store_true')
    parser.add_argument('--log', default=None,
                        help='One of [DEBUG, INFO, ERROR, WARNING, CRITICAL]')

    subparser_group = parser.add_subparsers(title='subcommands')

    subcmd_parsers = {}
    for name, cls in sorted(_subcommands.items()):
        help, description = _parse_docstring(cls.__doc__)
        subcmd_parser = subparser_group.add_parser(
            name=name, help=help, description=description,
            formatter_class=argparse.RawDescriptionHelpFormatter)

        cls.setup(subcmd_parser)
        subcmd_parser.add_argument('-v', '--verbose', action='store_true', help='More verbose output')


        subcmd_parser.set_defaults(subcommand_handler=cls.run, parser=parser,
                                   subcommand=name)
        # Can't find an API to access subparsers through parser? Pass along explicitly in ctx
        # (needed by Help)
        subcmd_parsers[name] = subcmd_parser

    if len(unparsed_argv) == 1:
        # Print help by default rather than an error about too few arguments
        parser.print_help()
        return 1
    args = parser.parse_args(unparsed_argv[1:])

    if not secondary:
        configure_logging(args.log)
        if args.verbose:
            set_log_level('INFO')
            if args.log is not None:
                logger.warn('-v overrides --log to INFO')

    ctx = HashDistCommandContext(parser, subcmd_parsers, sys.stdout, args.config_file, env, logger)

    retcode = args.subcommand_handler(ctx, args)
    if retcode is None:
        retcode = 0
    return retcode