Beispiel #1
0
    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)
Beispiel #2
0
    def testGlob(self):
        # Must test with disk directory...
        tempdir = tempfile.mkdtemp()
        try:
            disk_dir = DiskDirectory(tempdir)

            disk_dir.write("foo.qux", "")
            disk_dir.write("bar.qux", "")
            disk_dir.write("baz.qux", "")
            disk_dir.write("foo.corge", "")
            disk_dir.write("bar.corge", "")
            disk_dir.write("baz.corge", "")

            class PrefixRemovingMapping(MappedDirectory.Mapping):
                def __init__(self, prefix, real_dir):
                    super(PrefixRemovingMapping, self).__init__()
                    self.__prefix = prefix
                    self.__real_dir = real_dir

                def map(self, filename):
                    assert filename.startswith(self.__prefix)
                    return (self.__real_dir, filename[len(self.__prefix):])

            dir = MappedDirectory(PrefixRemovingMapping("a/", disk_dir))

            self.assertEquals(set(["a/foo.qux"]),
                              set(dir.expand_glob("a/foo.qux")))
            self.assertEquals(set(["a/foo.qux", "a/bar.qux", "a/baz.qux"]),
                              set(dir.expand_glob("a/*.qux")))
            self.assertEquals(
                set(["a/foo.corge", "a/bar.corge", "a/baz.corge"]),
                set(dir.expand_glob("a/*.corge")))
            self.assertEquals(set(["a/foo.qux", "a/foo.corge"]),
                              set(dir.expand_glob("a/foo.*")))
            self.assertEquals(
                set([
                    "a/foo.qux", "a/bar.qux", "a/baz.qux", "a/foo.corge",
                    "a/bar.corge", "a/baz.corge"
                ]), set(dir.expand_glob("a/*")))
            self.assertEquals(set([]), set(dir.expand_glob("a/grault")))
        finally:
            shutil.rmtree(tempdir)
Beispiel #3
0
 def setUp(self):
     self.tempdir = tempfile.mkdtemp()
     self.dir = DiskDirectory(self.tempdir)
     super(DiskDirectoryTest, self).setUp()