xref: /aosp_15_r20/external/perfetto/tools/download_changed_screenshots.py (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1#!/usr/bin/env python3
2# Copyright (C) 2022 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import base64
18import json
19import os
20import io
21import re
22import zipfile
23import urllib.request
24from os import path
25
26ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
27
28
29def get_artifact_url(run, name):
30  return f'https://storage.googleapis.com/perfetto-ci-artifacts/{run}/ui-test-artifacts/{name}'
31
32
33def main():
34  os.chdir(ROOT_DIR)
35  parser = argparse.ArgumentParser()
36  parser.add_argument(
37      'run',
38      metavar='RUN',
39      help='CI run identifier, e.g. ' +
40      '\'20240923144821--cls-3258215-15--ui-clang-x86_64-release\'')
41  args = parser.parse_args()
42  url = get_artifact_url(args.run, 'index.html')
43  with urllib.request.urlopen(url) as resp:
44    handle_report(resp.read().decode('utf-8'), args.run)
45
46
47def sanitize(name):
48  return re.sub('[ _]', '-', name)
49
50
51def handle_report(report: str, run: str):
52  m = re.findall(
53      r'playwrightReportBase64 = "data:application/zip;base64,([^"]+)"', report)
54  bin = base64.b64decode(m[0])
55  z = zipfile.ZipFile(io.BytesIO(bin))
56  report = json.loads(z.open('report.json').read().decode())
57  pngs = {}
58  for f in report['files']:
59    test_file = f['fileName'].removeprefix('test/')
60    for t in f['tests']:
61      title = sanitize(t['title'])
62      for r in t['results']:
63        for a in r['attachments']:
64          png_name = sanitize(a['name'])
65          if not png_name.endswith('-actual.png'):
66            continue
67          path = 'test/data/ui-screenshots/%s/%s/%s' % (
68              test_file, title, png_name.replace('-actual', ''))
69          pngs[path] = a['path']
70
71  for local_path, remote_path in pngs.items():
72    url = get_artifact_url(run, remote_path)
73    print(f'Downloading {local_path} from {url}')
74    urllib.request.urlretrieve(url, local_path)
75
76  print('Done. Now run:')
77  print('./tools/test_data upload  (or status)')
78
79
80if __name__ == "__main__":
81  main()
82