def TearDown(self):
        if self.trace_output and self._trace_all:
            instrumentation_tracing.stop_instrumenting()
        elif self.trace_output:
            self.DisableTracing()

        # By default, teardown will invoke ADB. When receiving SIGTERM due to a
        # timeout, there's a high probability that ADB is non-responsive. In these
        # cases, sending an ADB command will potentially take a long time to time
        # out. Before this happens, the process will be hard-killed for not
        # responding to SIGTERM fast enough.
        if self._received_sigterm:
            return

        if not self._devices:
            return

        @handle_shard_failures_with(on_failure=self.BlacklistDevice)
        def tear_down_device(d):
            # Write the cache even when not using it so that it will be ready the
            # first time that it is enabled. Writing it every time is also necessary
            # so that an invalid cache can be flushed just by disabling it for one
            # run.
            cache_path = _DeviceCachePath(d)
            if os.path.exists(os.path.dirname(cache_path)):
                with open(cache_path, 'w') as f:
                    f.write(d.DumpCacheData())
                    logging.info('Wrote device cache: %s', cache_path)
            else:
                logging.warning(
                    'Unable to write device cache as %s directory does not exist',
                    os.path.dirname(cache_path))

        self.parallel_devices.pMap(tear_down_device)

        for m in self._logcat_monitors:
            try:
                m.Stop()
                m.Close()
                _, temp_path = tempfile.mkstemp()
                with open(m.output_file, 'r') as infile:
                    with open(temp_path, 'w') as outfile:
                        for line in infile:
                            outfile.write('Device(%s) %s' %
                                          (m.adb.GetDeviceSerial(), line))
                shutil.move(temp_path, m.output_file)
            except base_error.BaseError:
                logging.exception('Failed to stop logcat monitor for %s',
                                  m.adb.GetDeviceSerial())
            except IOError:
                logging.exception('Failed to locate logcat for device %s',
                                  m.adb.GetDeviceSerial())

        if self._logcat_output_file:
            file_utils.MergeFiles(self._logcat_output_file, [
                m.output_file
                for m in self._logcat_monitors if os.path.exists(m.output_file)
            ])
            shutil.rmtree(self._logcat_output_dir)
  def TearDown(self):
    if self.trace_output and self._trace_all:
      instrumentation_tracing.stop_instrumenting()
    elif self.trace_output:
      self.DisableTracing()

    # By default, teardown will invoke ADB. When receiving SIGTERM due to a
    # timeout, there's a high probability that ADB is non-responsive. In these
    # cases, sending an ADB command will potentially take a long time to time
    # out. Before this happens, the process will be hard-killed for not
    # responding to SIGTERM fast enough.
    if self._received_sigterm:
      return

    if not self._devices:
      return

    @handle_shard_failures_with(on_failure=self.BlacklistDevice)
    def tear_down_device(d):
      # Write the cache even when not using it so that it will be ready the
      # first time that it is enabled. Writing it every time is also necessary
      # so that an invalid cache can be flushed just by disabling it for one
      # run.
      cache_path = _DeviceCachePath(d)
      if os.path.exists(os.path.dirname(cache_path)):
        with open(cache_path, 'w') as f:
          f.write(d.DumpCacheData())
          logging.info('Wrote device cache: %s', cache_path)
      else:
        logging.warning(
            'Unable to write device cache as %s directory does not exist',
            os.path.dirname(cache_path))

    self.parallel_devices.pMap(tear_down_device)

    for m in self._logcat_monitors:
      try:
        m.Stop()
        m.Close()
        _, temp_path = tempfile.mkstemp()
        with open(m.output_file, 'r') as infile:
          with open(temp_path, 'w') as outfile:
            for line in infile:
              outfile.write('Device(%s) %s' % (m.adb.GetDeviceSerial(), line))
        shutil.move(temp_path, m.output_file)
      except base_error.BaseError:
        logging.exception('Failed to stop logcat monitor for %s',
                          m.adb.GetDeviceSerial())
      except IOError:
        logging.exception('Failed to locate logcat for device %s',
                          m.adb.GetDeviceSerial())

    if self._logcat_output_file:
      file_utils.MergeFiles(
          self._logcat_output_file,
          [m.output_file for m in self._logcat_monitors
           if os.path.exists(m.output_file)])
      shutil.rmtree(self._logcat_output_dir)
Exemple #3
0
    def TearDown(self):
        if self.trace_output and self._trace_all:
            instrumentation_tracing.stop_instrumenting()
        elif self.trace_output:
            self.DisableTracing()

        if not self._devices:
            return

        @handle_shard_failures_with(on_failure=self.BlacklistDevice)
        def tear_down_device(d):
            # Write the cache even when not using it so that it will be ready the
            # first time that it is enabled. Writing it every time is also necessary
            # so that an invalid cache can be flushed just by disabling it for one
            # run.
            cache_path = _DeviceCachePath(d)
            if os.path.exists(os.path.dirname(cache_path)):
                with open(cache_path, 'w') as f:
                    f.write(d.DumpCacheData())
                    logging.info('Wrote device cache: %s', cache_path)
            else:
                logging.warning(
                    'Unable to write device cache as %s directory does not exist',
                    os.path.dirname(cache_path))

        self.parallel_devices.pMap(tear_down_device)

        for m in self._logcat_monitors:
            try:
                m.Stop()
                m.Close()
                _, temp_path = tempfile.mkstemp()
                with open(m.output_file, 'r') as infile:
                    with open(temp_path, 'w') as outfile:
                        for line in infile:
                            outfile.write('Device(%s) %s' %
                                          (m.adb.GetDeviceSerial(), line))
                shutil.move(temp_path, m.output_file)
            except base_error.BaseError:
                logging.exception('Failed to stop logcat monitor for %s',
                                  m.adb.GetDeviceSerial())
            except IOError:
                logging.exception('Failed to locate logcat for device %s',
                                  m.adb.GetDeviceSerial())

        if self._logcat_output_file:
            file_utils.MergeFiles(self._logcat_output_file, [
                m.output_file
                for m in self._logcat_monitors if os.path.exists(m.output_file)
            ])
            shutil.rmtree(self._logcat_output_dir)
  def TearDown(self):
    if self.trace_output and self._trace_all:
      instrumentation_tracing.stop_instrumenting()
    elif self.trace_output:
      self.DisableTracing()

    if not self._devices:
      return

    @handle_shard_failures_with(on_failure=self.BlacklistDevice)
    def tear_down_device(d):
      # Write the cache even when not using it so that it will be ready the
      # first time that it is enabled. Writing it every time is also necessary
      # so that an invalid cache can be flushed just by disabling it for one
      # run.
      cache_path = _DeviceCachePath(d)
      if os.path.exists(os.path.dirname(cache_path)):
        with open(cache_path, 'w') as f:
          f.write(d.DumpCacheData())
          logging.info('Wrote device cache: %s', cache_path)
      else:
        logging.warning(
            'Unable to write device cache as %s directory does not exist',
            os.path.dirname(cache_path))

    self.parallel_devices.pMap(tear_down_device)

    for m in self._logcat_monitors:
      try:
        m.Stop()
        m.Close()
        _, temp_path = tempfile.mkstemp()
        with open(m.output_file, 'r') as infile:
          with open(temp_path, 'w') as outfile:
            for line in infile:
              outfile.write('Device(%s) %s' % (m.adb.GetDeviceSerial(), line))
        shutil.move(temp_path, m.output_file)
      except base_error.BaseError:
        logging.exception('Failed to stop logcat monitor for %s',
                          m.adb.GetDeviceSerial())
      except IOError:
        logging.exception('Failed to locate logcat for device %s',
                          m.adb.GetDeviceSerial())

    if self._logcat_output_file:
      file_utils.MergeFiles(
          self._logcat_output_file,
          [m.output_file for m in self._logcat_monitors
           if os.path.exists(m.output_file)])
      shutil.rmtree(self._logcat_output_dir)