Пример #1
0
  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))
Пример #2
0
    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))
Пример #3
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)
Пример #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)