Ejemplo n.º 1
0
def set_main():
    # Some commands work without a bento description file (convert, help)
    if not os.path.exists(BENTO_SCRIPT):
        return []

    pkg_cache = CachedPackage()
    try:
        pkg = pkg_cache.get_package(BENTO_SCRIPT)
    finally:
        pkg_cache.close()
    #create_package_description(BENTO_SCRIPT)

    modules = []
    for f in pkg.hook_files:
        main_file = os.path.abspath(f)
        if not os.path.exists(main_file):
            raise ValueError("Hook file %s not found" % main_file)
        modules.append(create_hook_module(f))
    return modules
Ejemplo n.º 2
0
def run_cmd(cmd_name, cmd_opts):
    root = bento.core.node.Node("", None)
    top = root.find_dir(os.getcwd())

    cmd = get_command(cmd_name)()
    if get_command_override(cmd_name):
        cmd_funcs = get_command_override(cmd_name)
    else:
        cmd_funcs = [(cmd.run, top.abspath())]

    if not os.path.exists(BENTO_SCRIPT):
        raise UsageException("Error: no %s found !" % BENTO_SCRIPT)

    pkg_cache = CachedPackage()
    try:
        package_options = pkg_cache.get_options(BENTO_SCRIPT)
    finally:
        pkg_cache.close()
    cmd.setup_options_parser(package_options)

    if cmd_name == "configure":
        # FIXME: this whole dance to get the user-given flag values is insane
        from bento.commands.configure import set_flag_options
        o, a = cmd.parser.parse_args(cmd_opts)
        flag_values = set_flag_options(cmd.flag_opts, o)
    else:
        flag_values = None
    pkg_cache = CachedPackage()
    try:
        pkg = pkg_cache.get_package(BENTO_SCRIPT, flag_values)
    finally:
        pkg_cache.close()

    if cmd_name == "configure":
        ctx = ConfigureContext(cmd, cmd_opts, pkg, top)
    elif cmd_name == "build":
        ctx = BuildContext(cmd, cmd_opts, pkg, top)
    else:
        ctx = Context(cmd, cmd_opts, pkg, top)

    try:
        spkgs = pkg.subpackages

        def get_subpackage(local_node):
            rpath = local_node.path_from(top)
            k = os.path.join(rpath, "bento.info")
            if local_node == top:
                return pkg
            else:
                if k in spkgs:
                    return spkgs[k]
                else:
                    return None
        def set_local_ctx(ctx, hook, local_dir):
            local_node = top.find_dir(
                    relpath(local_dir, top.abspath()))
            spkg = get_subpackage(local_node)
            ctx.local_dir = local_dir
            ctx.local_node = local_node
            ctx.top_node = top
            ctx.local_pkg = spkg
            ctx.pkg = pkg
            return hook(ctx)

        if get_pre_hooks(cmd_name) is not None:
            for hook, local_dir, help_bypass in get_pre_hooks(cmd_name):
                if not ctx.help and help_bypass:
                    set_local_ctx(ctx, hook, local_dir)

        while cmd_funcs:
            cmd_func, local_dir = cmd_funcs.pop(0)
            set_local_ctx(ctx, cmd_func, local_dir)

        if get_post_hooks(cmd_name) is not None:
            for hook, local_dir, help_bypass in get_post_hooks(cmd_name):
                if not ctx.help and help_bypass:
                    set_local_ctx(ctx, hook, local_dir)
        cmd.shutdown(ctx)
    finally:
        ctx.store()