Beispiel #1
0
def handle_next_to(args):
    command_dirs = get_command_dirs_from_config(Dodo.get())
    command_map = get_command_map(command_dirs)

    if args.next_to:
        item = command_map.get(args.next_to)
        if not item:
            raise CommandError("Script not found: %s" % args.next_to)
        dest_path = os.path.join(os.path.dirname(item.filename),
                                 args.name + ".py")

        if os.path.exists(dest_path) and not args.force:
            raise CommandError("Destination already exists: %s" % dest_path)

        with open(dest_path, "w") as f:
            f.write(
                script_py.format(parser_args_str="",
                                 params_str="",
                                 description="",
                                 args_str=""))
        print(dest_path)
    else:
        print(
            script_py.format(parser_args_str="",
                             params_str="",
                             description="",
                             args_str=""))
Beispiel #2
0
def _all_hooks():
    command_dirs = get_command_dirs_from_config(Dodo.get_config())
    result = defaultdict(lambda: {})
    for command_dir in command_dirs:
        hooks_path = os.path.join(command_dir, 'git_hooks')
        for prefix in prefixes:
            pattern = os.path.join(hooks_path, '%s-*.py' % prefix)
            for hook_filename in glob.glob(pattern):
                hook_name = os.path.splitext(
                    os.path.basename(hook_filename))[0]
                result[prefix][hook_name] = hook_filename
    return result
Beispiel #3
0
def _which_script(script):
    command_dirs = get_command_dirs_from_config(Dodo.get())
    for item in command_dirs:
        script_path = os.path.join(item, script + ".py")
        if os.path.exists(script_path):
            return os.path.realpath(script_path)

    for item in command_dirs:
        for fn in glob.glob(os.path.join(item, "dodo.*.sh")):
            return os.path.realpath(fn)

    return None
Beispiel #4
0
def _all_decorators(config):
    """Returns a mapping from decorator name to its directory."""
    command_dirs = get_command_dirs_from_config(config)
    extend_sys_path(command_dirs)
    result = {}
    for item in command_dirs:
        try:
            module_path = os.path.basename(item) + ".decorators"
            module = import_module(module_path)
            for decorator in os.listdir(module.__path__[0]):
                name, ext = os.path.splitext(decorator)
                if ext == ".py" and name != "__init__":
                    result[name] = module_path
        except ImportError:
            continue
    return result
Beispiel #5
0
def _drop_dir_by_package_name():
    drop_dir_by_package_name = {}
    command_dirs = get_command_dirs_from_config(Dodo.get())
    for item in command_dirs:
        dot_drop_path = _dot_drop_path(item)
        default_drop_src_dir = _drop_src_dir(item)

        if not dot_drop_path and not default_drop_src_dir:
            continue

        if dot_drop_path and default_drop_src_dir:
            print("Warning: found two drop-ins where only one was expected:\n")
            print("%s\n%s" % (dot_drop_path, default_drop_src_dir))

        package_name = os.path.basename(item)
        drop_src_dir = default_drop_src_dir or _read_dot_drop_path(
            dot_drop_path)
        _register_drop_dir(drop_dir_by_package_name, package_name,
                           drop_src_dir)

    return drop_dir_by_package_name
Beispiel #6
0
def _collect_command_dirs(
    config,
    config_io,
    layer_names_by_command_dir,
    command_aliases,
    metadata_by_layer_name,
    layer_by_target_path,
):
    config_memo = yaml_round_trip_dump(config)

    for layer_name, layer_metadata in metadata_by_layer_name.items():

        def has_command_path(layer):
            return R.path_or(None, "ROOT", "command_path")(layer)

        layer_filenames = layer_filename_superset(
            [layer_metadata.target_path], config_io=config_io
        )
        extra_layers = R.map(config_io.load)(layer_filenames)
        if R.filter(has_command_path)(extra_layers):
            base_config = yaml_round_trip_load(config_memo)
            updated_config, warnings = build_config([base_config] + extra_layers)
            for command_dir in get_command_dirs_from_config(updated_config):
                layer_names = layer_names_by_command_dir[command_dir]
                _add_to_layer_names(layer_names, layer_name)

        layer = layer_by_target_path[layer_metadata.target_path]
        for command_alias in get_aliases(layer).items():
            alias_prefix = (
                ""
                if command_alias[0] in layer_metadata.inferred_commands
                else (layer_name + ".")
            )
            cmd_prefix = layer_name + "."
            command_aliases[alias_prefix + command_alias[0]] = (
                cmd_prefix + command_alias[1]
            )
Beispiel #7
0
def get_command_map(config):
    command_dirs = get_command_dirs_from_config(config)
    return dict(
        command_dirs=command_dirs,
        command_map=_get_command_map(command_dirs),
    )