def __init__(self, output_path, all_configs=None): # We want to make sure to construct only one copy of each config, even # if configs refer to each other or multiple configs refer to a shared # config. So, all_configs maps names to configs that we have already # constructed. if all_configs is None: # Note that if we just make all_configs default to {} in the method # signature, then Python will create a single empty map to use as the # default value for all calls rather than create a new one every call. # Since we modify all_configs during this method, we would be modifying # the shared default value, which would be bad. If you don't understand # what I mean, try typing the following into the interpreter and then # calling it several times with no argument: # def f(l = []): # l.append("foo") # return l # Ouchies. all_configs = {} if output_path is None: all_configs[""] = self else: all_configs[output_path] = self self.name = output_path self.source_dir = DiskDirectory(".") if output_path is None: self.output_dir = self.source_dir else: self.source_dir.mkdir(output_path) self.output_dir = DiskDirectory(output_path) self.mem_dir = VirtualDirectory() self.env_dir = VirtualDirectory() _restore_pickle(self.mem_dir, self.output_dir, "mem.pickle") _restore_pickle(self.env_dir, self.output_dir, "env.pickle") self.alt_configs = {} self.__make_root_dir() self.alt_configs["host"] = self if self.env_dir.exists("$mappings"): mappings = self.env_dir.read("$mappings").split(":") for mapping in mappings: if mapping == "": continue alias, name = mapping.split("=", 1) if name in all_configs: self.alt_configs[alias] = all_configs[name] else: if name == "": name = None self.alt_configs[alias] = Configuration(name, all_configs)
def clean(self, expunge=False): for dir in ["tmp", "bin", "lib", "share", "include", "mem", "env"]: if self.root_dir.exists(dir): shutil.rmtree(self.root_dir.get_disk_path(dir)) for file in ["mem.pickle", "env.pickle"]: if self.root_dir.exists(file): os.remove(self.root_dir.get_disk_path(file)) self.mem_dir = VirtualDirectory() if expunge: # Try to remove the output directory itself -- will fail if not empty. outdir = self.root_dir.get_disk_path(".") if outdir.endswith("/."): # rmdir doesn't like a trailing "/.". outdir = outdir[:-2] try: os.rmdir(outdir) except os.error: pass else: # Restore the parts of env.pickle that were set explicitly. new_env_dir = VirtualDirectory() if self.env_dir.exists("$mappings"): new_env_dir.write("$mappings", self.env_dir.read("$mappings")) if self.env_dir.exists("$config"): locked_vars = self.env_dir.read("$config") new_env_dir.write("$config", locked_vars) for var in locked_vars.split(","): if var != "": new_env_dir.write(var, self.env_dir.read(var)) new_env_dir.write("set/" + var, self.env_dir.read("set/" + var)) self.env_dir = new_env_dir self.__make_root_dir()
def testEnvironmentCommandWithDefault(self): bar = Artifact("env/BAR", None) bar_set = Artifact("env/set/BAR", None) default = Artifact("default", None) output = Artifact("foo", None) command = EnvironmentCommand(MockRuleContext(bar, bar_set), "BAR", output, default) enumerator = MockArtifactEnumerator({bar_set: "false"}) command.enumerate_artifacts(enumerator) self.assertEquals([bar_set], enumerator.reads) self.assertEquals([default], enumerator.inputs) self.assertEquals([output], enumerator.outputs) enumerator = MockArtifactEnumerator({bar_set: "true"}) command.enumerate_artifacts(enumerator) self.assertEquals([bar_set], enumerator.reads) self.assertEquals([bar], enumerator.inputs) self.assertEquals([output], enumerator.outputs) dir = VirtualDirectory() dir.write("env/set/BAR", "true") dir.write("env/BAR", "baz") context = MockCommandContext(dir) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("baz", dir.read("foo")) self.assertEquals("", log.getvalue()) dir = VirtualDirectory() dir.write("env/set/BAR", "false") dir.write("default", "qux") context = MockCommandContext(dir) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("qux", dir.read("foo")) self.assertEquals("", log.getvalue()) self.assertEquals("echo ${BAR:$(default)} > foo\n", _print_command(command))
def testEchoCommand(self): dir = VirtualDirectory() output = Artifact("foo", None) command = EchoCommand("bar", output) enumerator = MockArtifactEnumerator() command.enumerate_artifacts(enumerator) self.assertEquals([], enumerator.reads) self.assertEquals([], enumerator.inputs) self.assertEquals([output], enumerator.outputs) context = MockCommandContext(dir) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("bar", dir.read("foo")) self.assertEquals("", log.getvalue()) self.assertEquals("echo 'bar' > foo\n", _print_command(command))
def testDoAllCommand(self): dir = VirtualDirectory() inputs = [Artifact("input1", None), Artifact("input2", None)] outputs = [Artifact("output1", None), Artifact("output2", None)] mock_command1 = MockCommand("command1", inputs[:1], outputs[:1]) mock_command2 = MockCommand("command2", inputs[1:], outputs[1:]) command = DoAllCommand([mock_command1, mock_command2]) enumerator = MockArtifactEnumerator() command.enumerate_artifacts(enumerator) self.assertEquals([], enumerator.reads) self.assertEquals(inputs, enumerator.inputs) self.assertEquals(outputs, enumerator.outputs) context = MockCommandContext(dir) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals( "Ran MockCommand command1\n" "Ran MockCommand command2\n", log.getvalue()) mock_command2.fails = True log = cStringIO.StringIO() self.assertFalse(command.run(context, log)) self.assertEquals( "Ran MockCommand command1\n" "Ran MockCommand command2\n", log.getvalue()) mock_command1.fails = True log = cStringIO.StringIO() self.assertFalse(command.run(context, log)) self.assertEquals("Ran MockCommand command1\n", log.getvalue()) self.assertEquals("mock_command command1\n" "mock_command command2\n", _print_command(command))
def testEnvironmentCommand(self): dir = VirtualDirectory() bar = Artifact("env/BAR", None) bar_set = Artifact("env/set/BAR", None) output = Artifact("foo", None) command = EnvironmentCommand(MockRuleContext(bar, bar_set), "BAR", output) enumerator = MockArtifactEnumerator({bar_set: "false"}) command.enumerate_artifacts(enumerator) self.assertEquals([bar_set], enumerator.reads) self.assertEquals([], enumerator.inputs) self.assertEquals([output], enumerator.outputs) enumerator = MockArtifactEnumerator({bar_set: "true"}) command.enumerate_artifacts(enumerator) self.assertEquals([bar_set], enumerator.reads) self.assertEquals([bar], enumerator.inputs) self.assertEquals([output], enumerator.outputs) context = MockCommandContext(dir) dir.write("env/set/BAR", "false") log = cStringIO.StringIO() self.assertFalse(command.run(context, log)) self.assertFalse(dir.exists("foo")) self.assertEquals("Environment variable not set: BAR\n", log.getvalue()) dir.write("env/set/BAR", "true") dir.write("env/BAR", "baz") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("baz", dir.read("foo")) self.assertEquals("", log.getvalue()) self.assertEquals("echo $BAR > foo\n", _print_command(command))
def setUp(self): self.dir = VirtualDirectory() self.loader = Loader(self.dir)
def setUp(self): self.dir = VirtualDirectory() self.context = MockContext("mock.sebs", "src/mock.sebs") self.rule = Rule(self.context) self.console = make_console(cStringIO.StringIO()) # ignore output
def setUp(self): self.virtual_dir = VirtualDirectory() self.dir = MappedDirectory( MappedDirectoryTest.MappingImpl("mapped_prefix", self.virtual_dir)) super(MappedDirectoryTest, self).setUp()
def setUp(self): self.dir = VirtualDirectory() super(VirtualDirectoryTest, self).setUp()
def setUp(self): self.__action = Action(None, "dummy", "dummy") self.__artifact = Artifact("filename", None) self.__dir = VirtualDirectory()
def testConditionalCommandWithNoElse(self): dir = VirtualDirectory() inputs = [Artifact("input1", None), Artifact("input2", None)] outputs = [Artifact("output1", None), Artifact("output2", None)] condition = Artifact("condition", None) mock_command1 = MockCommand("command1", inputs[:1], outputs[:1]) command = ConditionalCommand(condition, mock_command1) enumerator = MockArtifactEnumerator() command.enumerate_artifacts(enumerator) self.assertEquals([condition], enumerator.reads) self.assertEquals([], enumerator.inputs) self.assertEquals([], enumerator.outputs) enumerator = MockArtifactEnumerator({condition: "true"}) command.enumerate_artifacts(enumerator) self.assertEquals([condition], enumerator.reads) self.assertEquals(inputs[:1], enumerator.inputs) self.assertEquals(outputs[:1], enumerator.outputs) enumerator = MockArtifactEnumerator({condition: "false"}) command.enumerate_artifacts(enumerator) self.assertEquals([condition], enumerator.reads) self.assertEquals([], enumerator.inputs) self.assertEquals([], enumerator.outputs) enumerator = MockArtifactEnumerator({condition: "blah"}) command.enumerate_artifacts(enumerator) self.assertEquals([condition], enumerator.reads) self.assertEquals([], enumerator.inputs) self.assertEquals([], enumerator.outputs) context = MockCommandContext(dir) dir.write("condition", "true") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("Ran MockCommand command1\n", log.getvalue()) mock_command1.fails = True log = cStringIO.StringIO() self.assertFalse(command.run(context, log)) self.assertEquals("Ran MockCommand command1\n", log.getvalue()) dir.write("condition", "false") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("", log.getvalue()) dir.write("condition", "blah") log = cStringIO.StringIO() self.assertFalse(command.run(context, log)) self.assertEquals( "Condition artifact was not true or false: %s\n" % condition, log.getvalue()) self.assertEquals("if condition {\n" " mock_command command1\n" "}\n", _print_command(command))