def download_build_if_needed(dest, url):
  """Download and extract a build (if it's not already there)."""
  if os.path.exists(dest):
    return dest

  logger.info('Downloading build data...')

  gsutil_path = url.replace(
      'https://storage.cloud.google.com/', 'gs://')
  common.gsutil('cp %s .' % gsutil_path, common.CLUSTERFUZZ_CACHE_DIR)

  filename = os.path.basename(gsutil_path)
  saved_file = os.path.join(common.CLUSTERFUZZ_CACHE_DIR, filename)

  tmp_dir_path = tempfile.mkdtemp(dir=common.CLUSTERFUZZ_TMP_DIR)
  common.execute('unzip', '-q %s -d %s' % (saved_file, tmp_dir_path), cwd='.')

  # args.gn is guaranteed to be in the wanted folder. In Chrome, it's under a
  # sub-directory. In Android, it's in the top dir.
  args_gn_path = common.find_file('args.gn', tmp_dir_path)
  shutil.copytree(os.path.dirname(args_gn_path), dest)

  logger.info('Cleaning up...')
  common.delete_if_exists(saved_file)
  common.delete_if_exists(tmp_dir_path)
def download_build(dest, url, binary_name):
  """Download and extract a build (if it's not already there)."""
  if os.path.exists(dest):
    return dest

  logger.info('Downloading build data...')
  common.ensure_dir(common.CLUSTERFUZZ_BUILDS_DIR)

  gsutil_path = url.replace(
      'https://storage.cloud.google.com/', 'gs://')
  common.gsutil('cp %s .' % gsutil_path, common.CLUSTERFUZZ_CACHE_DIR)

  filename = os.path.basename(gsutil_path)
  saved_file = os.path.join(common.CLUSTERFUZZ_CACHE_DIR, filename)

  common.execute(
      'unzip', '-q %s -d %s' % (saved_file, common.CLUSTERFUZZ_BUILDS_DIR),
      cwd=common.CLUSTERFUZZ_DIR)

  logger.info('Cleaning up...')
  os.remove(saved_file)
  os.rename(os.path.join(
      common.CLUSTERFUZZ_BUILDS_DIR, os.path.splitext(filename)[0]), dest)

  binary_location = os.path.join(dest, binary_name)
  stats = os.stat(binary_location)
  os.chmod(binary_location, stats.st_mode | stat.S_IEXEC)
Example #3
0
    def download_build_data(self):
        """Downloads a build and saves it locally."""

        build_dir = self.build_dir_name()
        binary_location = os.path.join(build_dir, self.binary_name)
        if os.path.exists(build_dir):
            return build_dir

        logger.info('Downloading build data...')
        if not os.path.exists(common.CLUSTERFUZZ_BUILDS_DIR):
            os.makedirs(common.CLUSTERFUZZ_BUILDS_DIR)

        gsutil_path = self.build_url.replace(
            'https://storage.cloud.google.com/', 'gs://')
        common.gsutil('cp %s .' % gsutil_path, common.CLUSTERFUZZ_CACHE_DIR)

        filename = os.path.split(gsutil_path)[1]
        saved_file = os.path.join(common.CLUSTERFUZZ_CACHE_DIR, filename)

        common.execute('unzip',
                       '-q %s -d %s' %
                       (saved_file, common.CLUSTERFUZZ_BUILDS_DIR),
                       cwd=common.CLUSTERFUZZ_DIR)

        logger.info('Cleaning up...')
        os.remove(saved_file)
        os.rename(
            os.path.join(common.CLUSTERFUZZ_BUILDS_DIR,
                         os.path.splitext(filename)[0]), build_dir)
        stats = os.stat(binary_location)
        os.chmod(binary_location, stats.st_mode | stat.S_IEXEC)
    def test_fail(self):
        """Test failing with NotInstalledError."""
        self.mock.execute.side_effect = error.NotInstalledError('gsutil')
        with self.assertRaises(error.GsutilNotInstalledError):
            common.gsutil('test', cwd='source')

        self.mock.execute.assert_called_once_with('gsutil',
                                                  'test',
                                                  cwd='source')
    def test_fail_other(self):
        """Test failing with other exception."""
        self.mock.execute.side_effect = subprocess.CalledProcessError(
            1, 'cmd', 'o')
        with self.assertRaises(subprocess.CalledProcessError) as cm:
            common.gsutil('test', cwd='source')

        self.assertEqual(self.mock.execute.side_effect, cm.exception)
        self.mock.execute.assert_called_once_with('gsutil',
                                                  'test',
                                                  cwd='source')
def get_clank_sha(revision_url):
  """Get Clank SHA."""
  tmp_file = tempfile.NamedTemporaryFile(delete=False)
  tmp_file.close()

  common.gsutil('cp %s %s' % (revision_url, tmp_file.name), cwd='.')

  with open(tmp_file.name, 'r') as file_handle:
    body = file_handle.read()
  common.delete_if_exists(tmp_file.name)

  match = re.search('"clank_revision": "([a-fA-F0-9]+)"', body)
  if match:
    return match.group(1)

  raise Exception('Clank SHA is not found in:\n%s' % body)
    def test_succeed(self):
        """Test suceeding."""
        self.mock.execute.return_value = (0, None)
        self.assertEqual((0, None), common.gsutil('test', cwd='source'))

        self.mock.execute.assert_called_once_with('gsutil',
                                                  'test',
                                                  cwd='source')