Ejemplo n.º 1
0
    def stop(self):
        """Restores namespace to the unmocked state."""
        # pylint: disable=W0212
        try:
            if self.__saved_env__ is not None:
                osutils.SetEnvironment(self.__saved_env__)

            if self.started:
                cros_build_lib.SafeRun([self.PreStop, self._stop])
        finally:
            self.started = False
            if getattr(self, 'tempdir', None):
                osutils._TempDirTearDown(self, False)
Ejemplo n.º 2
0
  def stop(self):
    """Restores namespace to the unmocked state."""
    try:
      if self.__saved_env__ is not None:
        osutils.SetEnvironment(self.__saved_env__)

      tasks = ([self.PreStop] + [p.stop for p in self.patchers.itervalues()] +
               [p.stop for p in self.external_patchers])
      if self._tempdir_obj is not None:
        tasks += [self._tempdir_obj.Cleanup]
      cros_build_lib.SafeRun(tasks)
    finally:
      self.started = False
      self.tempdir, self._tempdir_obj = None, None
Ejemplo n.º 3
0
def FixBotoCerts(activate=True, strict=False):
    """Fix for outdated cacerts.txt file in old versions of boto/gsutil."""

    if not activate:
        logging.info('FixBotoCerts skipped')
        yield
        return

    logging.info('FixBotoCerts started')

    orig_env = os.environ.copy()

    boto_cfg_path = None
    try:
        config = configparser.SafeConfigParser()

        # Read existing boto config file(s); this mimics what boto itself does.
        if 'BOTO_CONFIG' in os.environ:
            config.read(os.environ['BOTO_CONFIG'])
        else:
            boto_path = os.environ.get('BOTO_PATH', '/etc/boto.cfg:~/.boto')
            config.read(boto_path.split(':'))

        # Set [Boto] ca_certificates_file = <path to cacerts.txt>.
        if not config.has_section('Boto'):
            config.add_section('Boto')
        config.set('Boto', 'ca_certificates_file', BOTO_CACERTS_ABS_PATH)

        # Write updated boto config to a tempfile.
        fd, boto_cfg_path = tempfile.mkstemp(prefix='fix_certs',
                                             suffix='boto.cfg')
        os.close(fd)
        with open(boto_cfg_path, 'w') as f:
            config.write(f)
        os.chmod(boto_cfg_path, 0o644)

        # Update env to use only our generated boto config.
        os.environ['BOTO_CONFIG'] = boto_cfg_path
        os.environ.pop('BOTO_PATH', None)

    except Exception as e:
        if strict:
            raise e
        logging.warning('FixBotoCerts init failed: %s', e)
        # Don't make things worse; let the build continue.

    try:
        yield
    finally:
        # Restore env.
        osutils.SetEnvironment(orig_env)

        # Clean up the boto.cfg file.
        if boto_cfg_path:
            try:
                os.remove(boto_cfg_path)
            except Exception as e:
                if strict:
                    raise e
                logging.warning('FixBotoCerts failed removing %s: %s',
                                boto_cfg_path, e)