def run(self, args): def warn_invalid(msg): print("the configuration is invalid, the argument parser got the" " following error:") print(msg) sandbox = SandBox() sandbox.root_dir = args.sandbox try: cmd_line = sandbox.get_configuration()["cmd_line"] except (TypeError, KeyError) as msg: warn_invalid(msg) return argument_parser = main(get_argument_parser=True) def error(message): raise SandBoxError(message) argument_parser.error = error try: args = argument_parser.parse_args(cmd_line[2:]) except SandBoxError as msg: warn_invalid(msg) return for k, v in vars(args).items(): if k in self.keys: print("%s = %s" % (k, v))
def test_configure_opts(): """Check configure options.""" class AnodConf(Anod): def shell(self, *cmd, **kwargs): """Mock for Anod.shell that does not spawn processes.""" return (cmd, kwargs) @Anod.primitive() def build(self): c = Configure(self) c.add("--with-opt") c.add_env("OPT", "VAL") return [c.cmdline(), c()] os.environ["CONFIG_SHELL"] = "ksh" Anod.sandbox = SandBox(root_dir=os.getcwd()) Anod.sandbox.create_dirs() ac = AnodConf(qualifier="", kind="build", jobs=10) AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) ac.build_space.create() result = ac.build() assert result[0]["cmd"][:-1] == ["ksh", "../src/configure", "--with-opt"] assert result[0]["options"]["env"] == {"OPT": "VAL"} assert result[1][0][:-1] == tuple(result[0]["cmd"][:-1]) assert result[1][1]["env"] == result[0]["options"]["env"]
def test_make(): class AnodMake(Anod): @Anod.primitive() def build(self): m1 = Make(self, makefile='/tmp/makefile') m1.set_var('prefix', '/foo') m2 = Make(self, jobs=2) m2.set_default_target('install') m2.set_var('profiles', ['dev', 'prod']) return (m1.cmdline()['cmd'], m2.cmdline()['cmd'], m2.cmdline(['clean', 'install'])['cmd'], m2.cmdline('all')['cmd']) Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() am = AnodMake(qualifier='', kind='build', jobs=10) AnodDriver(anod_instance=am, store=None).activate() am.build_space.create() assert am.build() == ([ 'make', '-f', '/tmp/makefile', '-j', '10', 'prefix=/foo' ], ['make', '-j', '2', 'profiles=dev prod', 'install' ], ['make', '-j', '2', 'profiles=dev prod', 'clean', 'install'], ['make', '-j', '2', 'profiles=dev prod', 'all'])
def test_configure_opts(): """Check configure options.""" class AnodConf(Anod): def shell(self, *cmd, **kwargs): """Mock for Anod.shell that does not spawn processes.""" return (cmd, kwargs) @Anod.primitive() def build(self): c = Configure(self) c.add('--with-opt') c.add_env('OPT', 'VAL') return [c.cmdline(), c()] os.environ['CONFIG_SHELL'] = 'ksh' Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() ac = AnodConf(qualifier='', kind='build', jobs=10) AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) ac.build_space.create() result = ac.build() assert result[0]['cmd'][:-1] == [ 'ksh', '../src/configure', '--with-opt'] assert result[0]['options']['env'] == {'OPT': 'VAL'} assert result[1][0][:-1] == tuple(result[0]['cmd'][:-1]) assert result[1][1]['env'] == result[0]['options']['env']
def test_primitive(): class NoPrimitive(Anod): def build(self): return 2 no_primitive = NoPrimitive("", "build") assert has_primitive(no_primitive, "build") is False class WithPrimitive(Anod): build_qualifier_format = (("error", False),) package = Anod.Package(prefix="mypackage", version=lambda: "42") @Anod.primitive() def build(self): if "error" in self.parsed_qualifier: raise ValueError(self.parsed_qualifier["error"]) elif "error2" in self.parsed_qualifier: self.shell(sys.executable, "-c", "import sys; sys.exit(2)") else: hello = self.shell( sys.executable, "-c", 'print("world")', output=subprocess.PIPE ) return hello.out.strip() with_primitive = WithPrimitive("", "build") with_primitive2 = WithPrimitive("error=foobar", "build") with_primitive3 = WithPrimitive("error2", "build") with_primitive4 = WithPrimitive("error3", "build") Anod.sandbox = SandBox(root_dir=os.getcwd()) Anod.sandbox.spec_dir = os.path.join(os.path.dirname(__file__), "data") Anod.sandbox.create_dirs() # Activate the logging AnodDriver(anod_instance=with_primitive, store=None).activate(Anod.sandbox, None) AnodDriver(anod_instance=with_primitive2, store=None).activate(Anod.sandbox, None) AnodDriver(anod_instance=with_primitive3, store=None).activate(Anod.sandbox, None) AnodDriver(anod_instance=with_primitive4, store=None) # don't activate with_primitive.build_space.create() assert has_primitive(with_primitive, "build") is True assert with_primitive.build() == "world" with_primitive2.build_space.create() with pytest.raises(AnodError): with_primitive2.build() assert with_primitive2.package.name.startswith("mypackage") # Check __getitem__ # PKG_DIR returns the path to the pkg directory assert with_primitive2["PKG_DIR"].endswith("pkg") with_primitive3.build_space.create() with pytest.raises(AnodError): with_primitive3.build()
def test_custom_repr(): """Test yaml custom repr for Make.""" Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() ac = Anod(qualifier='', kind='build', jobs=1) AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) m = Make(ac, exec_dir='/here', makefile='/tmp/makefile') AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) ac.build_space.create() assert "cmd: [make, -f, /tmp/makefile, -j, '1']" in yaml.dump(m)
def run(self, args): sandbox = SandBox(root_dir=args.sandbox) sandbox.create_dirs() if args.spec_git_url: g = GitRepository(sandbox.specs_dir) if e3.log.default_output_stream is not None: g.log_stream = e3.log.default_output_stream g.init() g.update(args.spec_git_url, args.spec_git_branch, force=True) sandbox.dump_configuration() sandbox.write_scripts()
def test_configure(): class AnodConf(Anod): @Anod.primitive() def build(self): c = Configure(self) return c.cmdline() Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() ac = AnodConf(qualifier="", kind="build", jobs=10) AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) ac.build_space.create() # Configure() can add $CONFIG_SHELL in the command line # Check that the two other arguments are as expected assert ac.build()["cmd"][-2:] == [ "../src/configure", "--build=%s" % ac.env.build.triplet, ] # Check with canadian env canadian_env = BaseEnv() canadian_env.set_build("x86-windows") canadian_env.set_host("x86-linux") canadian_env.set_target("arm-elf") assert canadian_env.is_canadian ac2 = AnodConf(qualifier="", kind="build", jobs=10, env=canadian_env) AnodDriver(anod_instance=ac2, store=None).activate(Anod.sandbox, None) ac2.build_space.create() ac2_cmd = ac2.build()["cmd"] assert "--build=i686-pc-mingw32" in ac2_cmd assert "--host=i686-pc-linux-gnu" in ac2_cmd assert "--target=arm-eabi" in ac2_cmd # Check with cross env cross_env = BaseEnv() cross_env.set_target("arm-elf") ac3 = AnodConf(qualifier="", kind="build", jobs=10, env=cross_env) AnodDriver(anod_instance=ac3, store=None).activate(Anod.sandbox, None) ac3.build_space.create() assert "--target=arm-eabi" in ac3.build()["cmd"]
def test_configure_opts(): """Check configure options.""" class AnodConf(Anod): @Anod.primitive() def build(self): c = Configure(self) c.add('--with-opt') c.add_env('OPT', 'VAL') return c.cmdline() os.environ['CONFIG_SHELL'] = 'ksh' Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() ac = AnodConf(qualifier='', kind='build', jobs=10) AnodDriver(anod_instance=ac, store=None).activate() ac.build_space.create() assert ac.build()['cmd'][:-1] == ['ksh', '../src/configure', '--with-opt'] assert ac.build()['options']['env'] == {'OPT': 'VAL'}
def test_make(): class AnodMake(Anod): def shell(self, *cmd, **kwargs): """Mock for Anod.shell that does not spawn processes.""" return (cmd, kwargs) @Anod.primitive() def build(self): m1 = Make(self, makefile="/tmp/makefile") m1.set_var("prefix", "/foo") m2 = Make(self, exec_dir="/tmp/exec_dir", jobs=2) m2.set_default_target("install") m2.set_var("profiles", ["dev", "prod"]) return ( m1.cmdline()["cmd"], m1()[0], m1(exec_dir="/foo", timeout=2)[1], m2.cmdline()["cmd"], m2.cmdline(["clean", "install"])["cmd"], m2.cmdline("all")["cmd"], m2()[1], ) Anod.sandbox = SandBox(root_dir=os.getcwd()) Anod.sandbox.create_dirs() am = AnodMake(qualifier="", kind="build", jobs=10) AnodDriver(anod_instance=am, store=None).activate(Anod.sandbox, None) am.build_space.create() assert am.build() == ( ["make", "-f", "/tmp/makefile", "-j", "10", "prefix=/foo"], ("make", "-f", "/tmp/makefile", "-j", "10", "prefix=/foo"), {"cwd": "/foo", "timeout": 2}, ["make", "-j", "2", "profiles=dev prod", "install"], ["make", "-j", "2", "profiles=dev prod", "clean", "install"], ["make", "-j", "2", "profiles=dev prod", "all"], {"cwd": "/tmp/exec_dir", "timeout": None}, )
def run(self, args): sandbox = SandBox() sandbox.root_dir = args.sandbox cmd_line = sandbox.get_configuration()['cmd_line'] args_namespace = argparse.Namespace() args_namespace.python = cmd_line[0] argument_parser = main(get_argument_parser=True) def error(message): raise SandBoxError(message) argument_parser.error = error try: args = argument_parser.parse_args(cmd_line[2:]) except SandBoxError as msg: print('the configuration is invalid, the argument parser got the' 'following error:') print(msg) for k, v in vars(args).iteritems(): if k in self.keys: print('%s = %s' % (k, v))
def test_make(): class AnodMake(Anod): def shell(self, *cmd, **kwargs): """Mock for Anod.shell that does not spawn processes.""" return (cmd, kwargs) @Anod.primitive() def build(self): m1 = Make(self, makefile='/tmp/makefile') m1.set_var('prefix', '/foo') m2 = Make(self, jobs=2) m2.set_default_target('install') m2.set_var('profiles', ['dev', 'prod']) return (m1.cmdline()['cmd'], m1()[0], m1(exec_dir='/foo', timeout=2)[1], m2.cmdline()['cmd'], m2.cmdline(['clean', 'install'])['cmd'], m2.cmdline('all')['cmd']) Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() am = AnodMake(qualifier='', kind='build', jobs=10) AnodDriver(anod_instance=am, store=None).activate(Anod.sandbox, None) am.build_space.create() assert am.build() == ( ['make', '-f', '/tmp/makefile', '-j', '10', 'prefix=/foo'], ('make', '-f', '/tmp/makefile', '-j', '10', 'prefix=/foo'), {'cwd': '/foo', 'timeout': 2}, ['make', '-j', '2', 'profiles=dev prod', 'install'], ['make', '-j', '2', 'profiles=dev prod', 'clean', 'install'], ['make', '-j', '2', 'profiles=dev prod', 'all'])
def run(self, args): sandbox = SandBox() sandbox.root_dir = args.sandbox if args.specs_dir: sandbox.specs_dir = args.specs_dir if args.create_sandbox: sandbox.create_dirs() if args.create_sandbox and args.spec_git_url: mkdir(sandbox.specs_dir) g = GitRepository(sandbox.specs_dir) if e3.log.default_output_stream is not None: g.log_stream = e3.log.default_output_stream g.init() g.update(args.spec_git_url, args.spec_git_branch, force=True) sandbox.dump_configuration() sandbox.write_scripts() asr = AnodSpecRepository(sandbox.specs_dir) check_api_version(asr.api_version) # Load plan content if needed if args.plan: if not os.path.isfile(args.plan): raise SandBoxError("plan file %s does not exist" % args.plan, origin="SandBoxExec.run") with open(args.plan, "r") as plan_fd: plan_content = ["def main_entry_point():"] plan_content += [ " %s" % line for line in plan_fd.read().splitlines() ] plan_content = "\n".join(plan_content) env = BaseEnv() cm = PlanContext(server=env) store = None resolver = getattr( AnodContext, str(args.resolver), AnodContext.always_create_source_resolver, ) logger.debug("Using resolver %s", resolver.__name__) # Declare available actions and their signature def anod_action(module, build=None, host=None, target=None, qualifier=None): pass # all: no cover for a in ("anod_install", "anod_build", "anod_test"): cm.register_action(a, anod_action) # Load the plan and execute plan = Plan(data={}) plan.load_chunk(plan_content) actions = cm.execute(plan, "main_entry_point") ac = AnodContext(asr, default_env=env) for action in actions: ac.add_anod_action( action.module, action, action.action.replace("anod_", "", 1), action.qualifier, ) # Check if machine plan is locally schedulable action_list = ac.schedule(resolver) e = ElectrolytJobFactory(sandbox, asr, store, dry_run=args.dry_run) e.run(action_list)
def test_primitive(): class NoPrimitive(Anod): def build(self): return 2 no_primitive = NoPrimitive('', 'build') assert has_primitive(no_primitive, 'build') is False class WithPrimitive(Anod): build_qualifier_format = (('error', False), ) package = Anod.Package(prefix='mypackage', version=lambda: '42') @Anod.primitive() def build(self): if 'error' in self.parsed_qualifier: raise ValueError(self.parsed_qualifier['error']) elif 'error2' in self.parsed_qualifier: self.shell(sys.executable, '-c', 'import sys; sys.exit(2)') else: hello = self.shell(sys.executable, '-c', 'print("world")', output=subprocess.PIPE) return hello.out.strip() with_primitive = WithPrimitive('', 'build') with_primitive2 = WithPrimitive('error=foobar', 'build') with_primitive3 = WithPrimitive('error2', 'build') with_primitive4 = WithPrimitive('error3', 'build') Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.spec_dir = os.path.join(os.path.dirname(__file__), 'data') Anod.sandbox.create_dirs() # Activate the logging AnodDriver(anod_instance=with_primitive, store=None).activate() AnodDriver(anod_instance=with_primitive2, store=None).activate() AnodDriver(anod_instance=with_primitive3, store=None).activate() AnodDriver(anod_instance=with_primitive4, store=None) # don't activate with_primitive.build_space.create() assert has_primitive(with_primitive, 'build') is True assert with_primitive.build() == 'world' assert with_primitive.has_nsis is False with_primitive2.build_space.create() with pytest.raises(AnodError) as err: with_primitive2.build() assert 'foobar' in str(err.value) assert with_primitive2.package.name.startswith('mypackage') # Check __getitem__ # PKG_DIR returns the path to the pkg directory assert with_primitive2['PKG_DIR'].endswith('pkg') # Check access to build_space config dict directly in Anod instance with_primitive2.build_space.config['config-key'] = 'config-value' assert with_primitive2['config-key'] == 'config-value' with_primitive3.build_space.create() with pytest.raises(ShellError) as err: with_primitive3.build() assert 'build fails' in str(err.value) with_primitive3.build_space.set_logging() with pytest.raises(ShellError) as err: with_primitive3.build() assert 'build fails' in str(err.value) with open(with_primitive3.build_space.log_file) as f: assert 'import sys; sys.exit(2)' in f.read() with_primitive3.build_space.end() with pytest.raises(AnodError) as err: with_primitive4.build() assert 'AnodDriver.activate() has not been run' in str(err)
def create_anod_sandbox(sbx_dir: str, spec_dir: str) -> SandBox: sbx = SandBox() sbx.root_dir = sbx_dir sbx.specs_dir = spec_dir return sbx