예제 #1
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)
예제 #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)
예제 #3
0
 def __make_root_dir(self):
     self.mapping = _WorkingDirMapping(self.source_dir, self.output_dir,
                                       self.mem_dir, self.env_dir,
                                       self.alt_configs)
     self.root_dir = MappedDirectory(self.mapping)
예제 #4
0
class Configuration(object):
    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 __make_root_dir(self):
        self.mapping = _WorkingDirMapping(self.source_dir, self.output_dir,
                                          self.mem_dir, self.env_dir,
                                          self.alt_configs)
        self.root_dir = MappedDirectory(self.mapping)

    def save(self):
        if not self.mem_dir.empty():
            _save_pickle(self.mem_dir, self.root_dir, "mem.pickle")
        if not self.env_dir.empty():
            _save_pickle(self.env_dir, self.root_dir, "env.pickle")

    def getenv(self, name):
        if self.root_dir.read("env/set/" + name) == "true":
            return self.root_dir.read("env/" + name)
        else:
            return None

    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 get_all_linked_configs(self):
        result = set()
        self.__get_all_linked_configs_recursive(result)
        return result

    def __get_all_linked_configs_recursive(self, result):
        if self in result:
            return

        result.add(self)
        for link in self.alt_configs.values():
            link.__get_all_linked_configs_recursive(result)
예제 #5
0
 def setUp(self):
     self.virtual_dir = VirtualDirectory()
     self.dir = MappedDirectory(
         MappedDirectoryTest.MappingImpl("mapped_prefix", self.virtual_dir))
     super(MappedDirectoryTest, self).setUp()
예제 #6
0
class MappedDirectoryTest(DirectoryTest, unittest.TestCase):
    class MappingImpl(MappedDirectory.Mapping):
        def __init__(self, prefix, real_dir):
            super(MappedDirectoryTest.MappingImpl, self).__init__()
            self.__prefix = prefix
            self.__real_dir = real_dir

        def map(self, filename):
            return (self.__real_dir, os.path.join(self.__prefix, filename))

    def setUp(self):
        self.virtual_dir = VirtualDirectory()
        self.dir = MappedDirectory(
            MappedDirectoryTest.MappingImpl("mapped_prefix", self.virtual_dir))
        super(MappedDirectoryTest, self).setUp()

    def addFile(self, name, mtime, content):
        self.virtual_dir.add(os.path.join("mapped_prefix", name), mtime,
                             content)

    def addDirectory(self, name):
        self.virtual_dir.add_directory(os.path.join("mapped_prefix", name))

    def testGetDiskPath(self):
        self.assertEquals(None, self.dir.get_disk_path("foo/bar"))

    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)
예제 #7
0
 def __make_root_dir(self):
   self.mapping = _WorkingDirMapping(self.source_dir, self.output_dir,
                                     self.mem_dir, self.env_dir,
                                     self.alt_configs)
   self.root_dir = MappedDirectory(self.mapping)
예제 #8
0
class Configuration(object):
  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 __make_root_dir(self):
    self.mapping = _WorkingDirMapping(self.source_dir, self.output_dir,
                                      self.mem_dir, self.env_dir,
                                      self.alt_configs)
    self.root_dir = MappedDirectory(self.mapping)

  def save(self):
    if not self.mem_dir.empty():
      _save_pickle(self.mem_dir, self.root_dir, "mem.pickle")
    if not self.env_dir.empty():
      _save_pickle(self.env_dir, self.root_dir, "env.pickle")

  def getenv(self, name):
    if self.root_dir.read("env/set/" + name) == "true":
      return self.root_dir.read("env/" + name)
    else:
      return None

  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 get_all_linked_configs(self):
    result = set()
    self.__get_all_linked_configs_recursive(result)
    return result

  def __get_all_linked_configs_recursive(self, result):
    if self in result:
      return

    result.add(self)
    for link in self.alt_configs.values():
      link.__get_all_linked_configs_recursive(result)
예제 #9
0
 def setUp(self):
   self.virtual_dir = VirtualDirectory()
   self.dir = MappedDirectory(
       MappedDirectoryTest.MappingImpl("mapped_prefix", self.virtual_dir))
   super(MappedDirectoryTest, self).setUp()
예제 #10
0
class MappedDirectoryTest(DirectoryTest, unittest.TestCase):
  class MappingImpl(MappedDirectory.Mapping):
    def __init__(self, prefix, real_dir):
      super(MappedDirectoryTest.MappingImpl, self).__init__()
      self.__prefix = prefix
      self.__real_dir = real_dir

    def map(self, filename):
      return (self.__real_dir, os.path.join(self.__prefix, filename))

  def setUp(self):
    self.virtual_dir = VirtualDirectory()
    self.dir = MappedDirectory(
        MappedDirectoryTest.MappingImpl("mapped_prefix", self.virtual_dir))
    super(MappedDirectoryTest, self).setUp()

  def addFile(self, name, mtime, content):
    self.virtual_dir.add(os.path.join("mapped_prefix", name), mtime, content)

  def addDirectory(self, name):
    self.virtual_dir.add_directory(os.path.join("mapped_prefix", name))

  def testGetDiskPath(self):
    self.assertEquals(None, self.dir.get_disk_path("foo/bar"))

  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)