def run_cmd_in_context(cmd_klass, cmd_name, cmd_opts, ctx_klass, run_node, top_node, pkg): """Run the given Command instance inside its context, including any hook and/or override.""" cmd = cmd_klass() options_ctx = OPTIONS_REGISTRY.get_options(cmd_name) ctx = ctx_klass(cmd_opts, options_ctx, pkg, run_node) # FIXME: hack to pass package_options to configure command - most likely # this needs to be known in option context ? ctx.package_options = __get_package_options(top_node) if get_command_override(cmd_name): cmd_funcs = get_command_override(cmd_name) else: cmd_funcs = [(cmd.run, top_node.abspath())] try: def _run_hooks(hook_iter): for hook, local_dir, help_bypass in hook_iter: local_node = top_node.find_dir(relpath(local_dir, top_node.abspath())) ctx.pre_recurse(local_node) try: if not ctx.help: hook(ctx) finally: ctx.post_recurse() _run_hooks(get_pre_hooks(cmd_name)) while cmd_funcs: cmd_func, local_dir = cmd_funcs.pop(0) local_node = top_node.find_dir(relpath(local_dir, top_node.abspath())) ctx.pre_recurse(local_node) try: cmd_func(ctx) finally: ctx.post_recurse() _run_hooks(get_post_hooks(cmd_name)) cmd.shutdown(ctx) finally: ctx.shutdown()
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()