def test_hook(self): root = self.root top_node = self.top_node bento_info = """\ Name: foo HookFile: bar/bscript Recurse: bar """ bento_info2 = """\ Library: Modules: fubar """ bscript = """\ from bento.commands import hooks @hooks.pre_configure def configure(ctx): py_modules = ctx.local_pkg.py_modules ctx.local_node.make_node("test").write(str(py_modules)) """ bentos = {"bento.info": bento_info, os.path.join("bar", "bento.info"): bento_info2} bscripts = {os.path.join("bar", "bscript"): bscript} create_fake_package_from_bento_infos(top_node, bentos, bscripts) conf, configure = prepare_configure(self.run_node, bento_info, ConfigureYakuContext) hook = top_node.search("bar/bscript") m = create_hook_module(hook.abspath()) for hook, local_dir, help_bypass in get_pre_hooks("configure"): conf.pre_recurse(root.find_dir(local_dir)) try: hook(conf) finally: conf.post_recurse() test = top_node.search("bar/test") if test: self.failUnlessEqual(test.read(), "['fubar']") else: self.fail("test dummy not found")
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()