blob: e4cd3b5006950bd4db6bda1f7fc9c9dab74c6042 [file] [log] [blame]
#!/usr/bin/env python3
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Generate report files to view and/or compare (diff) milestones.
Size files are located in a Google Cloud Storage bucket for various Chrome
versions. This script generates various HTML report files to view a single
milesone, or to compare two milestones with the same CPU and APK.
Desired CPUs, APKs, and milestone versions are set in constants below. If
specified by the --skip-existing flag, the script checks what HTML report files
have already been uploaded to the GCS bucket, then works on generating the
remaining desired files.
Reports can be uploaded automatically with the --sync flag. Otherwise, they can
be uploaded at a later point.
import argparse
import collections
import contextlib
import errno
import itertools
import json
import logging
import multiprocessing
import os
import re
import shutil
import sys
import subprocess
import tempfile
_DIR_SOURCE_ROOT = os.path.normpath(
os.path.join(os.path.dirname(__file__), '..', '..'))
_GSUTIL = os.path.join(_DIR_SOURCE_ROOT, 'third_party', 'depot_tools',
_PUSH_URL = 'gs://chrome-supersize/milestones/'
_DESIRED_CPUS = ['arm', 'arm_64']
'ChromeModern.apk', 'Monochrome.apk', 'AndroidWebview.apk',
# Versions are manually gathered from
def _VersionMajor(version):
return tuple(int(x) for x in version.split('.'))[0]
def _IsBundle(apk, version):
version = _VersionMajor(version)
if apk == 'ChromeModern.apk' and version >= 73:
return True
if apk == 'Monochrome.apk' and version >= 73:
return True
if apk == 'AndroidWebview.apk' and version >= 89:
return True
return False
def _EnumerateReports():
for cpu, apk in itertools.product(_DESIRED_CPUS, _DESIRED_APKS):
# Webview .size files do not exist before M71.
if apk == 'AndroidWebview.apk':
versions = [v for v in versions if _VersionMajor(v) >= 71]
elif apk == 'TrichromeGoogle':
versions = [v for v in versions if _VersionMajor(v) >= 88]
for version in versions:
yield Report(cpu, apk, version)
class Report(collections.namedtuple('Report', 'cpu,apk,version')):
def GetSizeFileSubpath(self, local):
# TrichromeGoogle at older milestones lived in a subdir.
if not local and self.apk == 'TrichromeGoogle' and _VersionMajor(
self.version) < 91:
template = '{version}/{cpu}/for-signing-only/{apk}.size'
template = '{version}/{cpu}/{apk}.size'
ret = template.format(**self._asdict())
if not local and _IsBundle(self.apk, self.version):
ret = ret.replace('.apk', '.minimal.apks')
return ret
def _MakeDirectory(path):
# Function is safe even from racing fork()ed processes.
except OSError as e:
if e.errno != errno.EEXIST:
def _Shard(func, arg_tuples):
pool = multiprocessing.Pool()
for x in pool.imap_unordered(func, arg_tuples):
yield x
def _DownloadOneSizeFile(arg_tuples):
subpath, temp_dir, base_url = arg_tuples
src = '{}/{}'.format(base_url, subpath)
dest = os.path.join(temp_dir, subpath)
subprocess.check_call([_GSUTIL, '-q', 'cp', src, dest])
def _DownloadSizeFiles(base_url, reports):
temp_dir = tempfile.mkdtemp()
subpaths = set(x.GetSizeFileSubpath(local=False) for x in reports)
arg_tuples = ((p, temp_dir, base_url) for p in subpaths)
for _ in _Shard(_DownloadOneSizeFile, arg_tuples):
yield temp_dir
def _WriteMilestonesJson(path):
with open(path, 'w') as out_file:
# TODO(agrieve): Record the full list of reports rather than three arrays
# so that the UI can prevent selecting non-existent entries.
pushed_reports_obj = {
'pushed': {
json.dump(pushed_reports_obj, out_file, sort_keys=True, indent=2)
def _BuildOneReport(report, output_directory, size_file_directory):
# Newer Monochrome builds are minimal builds, with names like
# "Monochrome.minimal.apks.size". Standardize to "Monochrome.apk.size".
local_size_path = os.path.join(output_directory,
size_file = os.path.join(size_file_directory,
shutil.copyfile(size_file, local_size_path)
def main():
parser = argparse.ArgumentParser(description=__doc__)
help='GCS bucket to find size files in. (e.g. "gs://bucket/subdir")')
help='Sync data files to GCS (otherwise just prints out command to run).')
args = parser.parse_args()
size_file_bucket = args.size_file_bucket.rstrip('/')
if not size_file_bucket.startswith('gs://'):
parser.error('Size file bucket must start with gs://')
reports_to_make = set(_EnumerateReports())
logging.warning('Downloading %d size files.', len(reports_to_make))
with _DownloadSizeFiles(args.size_file_bucket, reports_to_make) as sizes_dir:
staging_dir = os.path.join(sizes_dir, 'staging')
for r in reports_to_make:
_BuildOneReport(r, staging_dir, sizes_dir)
_WriteMilestonesJson(os.path.join(staging_dir, 'milestones.json'))
if args.sync:
[_GSUTIL, '-m', 'rsync', '-r', staging_dir, _PUSH_URL])
milestones_json = _PUSH_URL + 'milestones.json'
# The main index.html page has no authentication code, so make .json file
# world-readable.
[_GSUTIL, 'acl', 'set', '-a', 'public-read', milestones_json])
[_GSUTIL, 'setmeta', '-h', 'Cache-Control:no-cache', milestones_json])
logging.warning('Finished dry run. Run with --sync to upload.')
if __name__ == '__main__':