blob: f8d81b69355a455ce72c1512cadcd5daef57071d [file] [log] [blame]
Peter Kotwicz2b85bd3d2020-10-01 22:29:491#!/usr/bin/env python3
2
Avi Drissman3f1ea442022-09-08 15:42:013# Copyright 2020 The Chromium Authors
Peter Kotwicz2b85bd3d2020-10-01 22:29:494# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6"""A script to generate build.gradle from template and run fetch_all.py
7
8More specifically, to generate build.gradle:
Peter Wen1a004a02025-01-28 23:04:469 - It downloads the index page for the latest androidx snapshot from
10 https://androidx.dev/snapshots/latest/artifacts
Peter Kotwicz2b85bd3d2020-10-01 22:29:4911 - It replaces {{androidx_repository_url}} with the URL for the latest snapshot
Peter Wen1a004a02025-01-28 23:04:4612 - For each dependency, it looks up the version in the index page's HTML and
Peter Kotwicz2b85bd3d2020-10-01 22:29:4913 substitutes the version into {{androidx_dependency_version}}.
Peter Wen2e5944d2025-02-04 17:02:3214
15Extra args after -- are passed directly to fetch_all.py.
Peter Kotwicz2b85bd3d2020-10-01 22:29:4916"""
17
Haiyang Panace500932022-01-26 19:49:0318import argparse
Peter Kotwicz2b85bd3d2020-10-01 22:29:4919import contextlib
Mohamed Heikal811bb642025-02-25 20:03:5320import json
Haiyang Panace500932022-01-26 19:49:0321import logging
Peter Kotwicz2b85bd3d2020-10-01 22:29:4922import os
Andrew Grieveb5255af2024-06-06 17:03:1823import pathlib
Peter Kotwicz2b85bd3d2020-10-01 22:29:4924import re
Peter Kotwicz2b85bd3d2020-10-01 22:29:4925import shutil
Mohamed Heikal864fa342025-01-08 19:15:4526import sys
Peter Kotwicz2b85bd3d2020-10-01 22:29:4927import subprocess
28import tempfile
Peter Kotwicz4676d062020-10-22 06:04:1329from urllib import request
Peter Wenc90bbb72025-02-25 20:41:4430import zipfile
Peter Kotwicz2b85bd3d2020-10-01 22:29:4931
Mohamed Heikal27da9af62025-03-12 19:59:4532_SRC_PATH = pathlib.Path(__file__).resolve().parents[2]
33_ANDROIDX_PATH = _SRC_PATH / 'third_party/androidx'
34_CIPD_PATH = _ANDROIDX_PATH / 'cipd'
35_BOM_NAME = 'bill_of_materials.json'
Mohamed Heikal9aa653c2025-03-17 21:47:4336_EXTRACT_SCRIPT_PATH = _ANDROIDX_PATH / 'extract_and_commit_extras.py'
Mohamed Heikal864fa342025-01-08 19:15:4537
Mohamed Heikal27da9af62025-03-12 19:59:4538sys.path.insert(1, str(_SRC_PATH / 'build/autoroll'))
39import fetch_util
40
Peter Wen1a004a02025-01-28 23:04:4641# URL to artifacts in latest androidx snapshot.
42_ANDROIDX_LATEST_SNAPSHOT_ARTIFACTS_URL = 'https://androidx.dev/snapshots/latest/artifacts'
Mohamed Heikal864fa342025-01-08 19:15:4543
Peter Wen1a004a02025-01-28 23:04:4644# When androidx roller is breaking, and a fix is not imminent, use this to pin a
Andrew Grieve2515bcbd2022-05-06 03:14:0745# broken library to an old known-working version.
Andrew Grieve105fa4132025-05-16 17:29:5146# * Find working versions from prior androidx roll commit descriptions.
Andrew Grievea03217f2022-05-12 16:05:2747# * The first element of each tuple is the path to the artifact of the latest
48# version of the library. It could change if the version is rev'ed in a new
49# snapshot.
50# * The second element is a URL to replace the file with. Find the URL for older
Peter Wen1a004a02025-01-28 23:04:4651# versions of libraries by looking through the artifacts for the older version
52# (e.g.: https://androidx.dev/snapshots/builds/8545498/artifacts)
Andrew Grieve2515bcbd2022-05-06 03:14:0753_OVERRIDES = [
Andrew Grievea03217f2022-05-12 16:05:2754 # Example:
Andrew Grieve2515bcbd2022-05-06 03:14:0755 #('androidx_core_core/core-1.9.0-SNAPSHOT.aar',
56 # 'https://androidx.dev/snapshots/builds/8545498/artifacts/repository/'
57 # 'androidx/core/core/1.8.0-SNAPSHOT/core-1.8.0-20220505.122105-1.aar'),
Andrew Grieve2515bcbd2022-05-06 03:14:0758]
59
Andrew Grieve105fa4132025-05-16 17:29:5160# Set this to the build_id to pin all libraries to a given version.
61# Useful when pinning a single library would cause issues, but you do not want
62# to pause the auto-roller because other teams want to add / remove libraries.
63# Example: '8545498'
64_LATEST_VERSION_OVERRIDE = ''
65
66
Peter Wenba01695b2025-03-03 18:29:4667_FILES_TO_COMMIT = [
68 'additional_readme_paths.json',
69 'bill_of_materials.json',
70 'BUILD.gn',
71 'build.gradle',
72]
Peter Wen19a68702025-03-03 20:41:3173
Peter Kotwicz3fb0070e2021-02-11 07:37:3574
Mohamed Heikal864fa342025-01-08 19:15:4575def _get_latest_androidx_version():
Peter Wen1a004a02025-01-28 23:04:4676 androidx_artifacts_response = request.urlopen(
77 _ANDROIDX_LATEST_SNAPSHOT_ARTIFACTS_URL)
Mohamed Heikal864fa342025-01-08 19:15:4578 # Get the versioned url from the redirect destination.
Peter Wen1a004a02025-01-28 23:04:4679 androidx_artifacts_url = androidx_artifacts_response.url
80 androidx_artifacts_response.close()
81 logging.info('URL for the latest build info: %s', androidx_artifacts_url)
Mohamed Heikal864fa342025-01-08 19:15:4582 # Strip '/repository' from pattern.
83 resolved_snapshot_repository_url_pattern = (
Peter Wenf52e4a42025-05-07 13:37:2784 fetch_util.make_androidx_maven_url('([0-9]*)').rsplit('/', 1)[0])
Peter Wen1a004a02025-01-28 23:04:4685 match = re.match(resolved_snapshot_repository_url_pattern,
86 androidx_artifacts_url)
87 assert match is not None
88 version = match.group(1)
Andrew Grievef4aa0e262025-05-06 17:21:2389 logging.info('Resolved latest androidx version to %s', version)
Mohamed Heikal864fa342025-01-08 19:15:4590 return version
91
92
Peter Kotwicz2b85bd3d2020-10-01 22:29:4993def main():
Haiyang Panace500932022-01-26 19:49:0394 parser = argparse.ArgumentParser(description=__doc__)
95 parser.add_argument('-v',
96 '--verbose',
97 dest='verbose_count',
98 default=0,
99 action='count',
100 help='Verbose level (multiple times for more)')
Andrew Grieve10bbd552022-05-05 18:10:26101 parser.add_argument('--local-repo',
102 help='Path to a locally androidx maven repo to use '
103 'instead of fetching the latest.')
Mohamed Heikal864fa342025-01-08 19:15:45104 parser.add_argument(
Mohamed Heikal9aa653c2025-03-17 21:47:43105 '--local',
Mohamed Heikal864fa342025-01-08 19:15:45106 action='store_true',
Mohamed Heikal9aa653c2025-03-17 21:47:43107 help='If passed then we will run the extract_and_commit_extras.py '
108 'script and will not try rolling to the latest snapshot but reprocess '
109 'the project at the current androidx.dev snapshot.')
Peter Wen1ba3d4d2025-02-25 21:46:37110 parser.add_argument(
111 '--use-bom',
112 action='store_true',
113 help='If passed then we will use the existing bill_of_materials.json '
Mohamed Heikal9aa653c2025-03-17 21:47:43114 'instead of resolving the latest androidx (faster but might resolve '
115 'incorrect versions if deps are added/removed).')
Peter Wen2e5944d2025-02-04 17:02:32116 args, extra_args = parser.parse_known_args()
Haiyang Panace500932022-01-26 19:49:03117
118 logging.basicConfig(
119 level=logging.WARNING - 10 * args.verbose_count,
120 format='%(levelname).1s %(relativeCreated)6d %(message)s')
121
Andrew Grieve10bbd552022-05-05 18:10:26122 if args.local_repo:
123 version = 'local'
Andrew Grieve10bbd552022-05-05 18:10:26124 androidx_snapshot_repository_url = ('file://' +
125 os.path.abspath(args.local_repo))
126 else:
Andrew Grieve105fa4132025-05-16 17:29:51127 if _LATEST_VERSION_OVERRIDE:
128 version = _LATEST_VERSION_OVERRIDE
129 elif args.local:
Andrew Grievef4aa0e262025-05-06 17:21:23130 version = fetch_util.get_current_androidx_version()
Mohamed Heikal864fa342025-01-08 19:15:45131 else:
132 version = _get_latest_androidx_version()
Mohamed Heikal864fa342025-01-08 19:15:45133
Andrew Grievef4aa0e262025-05-06 17:21:23134 androidx_snapshot_repository_url = (
135 fetch_util.make_androidx_maven_url(version))
Andrew Grieve9c7d4ac2f2022-08-24 17:38:59136 # Prepend '0' to version to avoid conflicts with previous version format.
137 version = 'cr-0' + version
138
Peter Wen1ba3d4d2025-02-25 21:46:37139 if args.use_bom:
Mohamed Heikal9aa653c2025-03-17 21:47:43140 version_map_str = fetch_util.generate_version_map_str(_ANDROIDX_PATH /
Mohamed Heikal27da9af62025-03-12 19:59:45141 _BOM_NAME)
Peter Wen1ba3d4d2025-02-25 21:46:37142 else:
143 version_map_str = ''
144
Mohamed Heikal27da9af62025-03-12 19:59:45145 fetch_util.fill_template(
146 _ANDROIDX_PATH / 'build.gradle.template',
Mohamed Heikal9aa653c2025-03-17 21:47:43147 _ANDROIDX_PATH / 'build.gradle',
Mohamed Heikal27da9af62025-03-12 19:59:45148 version_overrides=version_map_str,
Mohamed Heikal9aa653c2025-03-17 21:47:43149 androidx_repository_url=androidx_snapshot_repository_url)
Peter Kotwicz2b85bd3d2020-10-01 22:29:49150
Andrew Grieve9c7d4ac2f2022-08-24 17:38:59151 # Overrides do not work with local snapshots since the repository_url is
152 # different.
153 if not args.local_repo:
154 for subpath, url in _OVERRIDES:
Mohamed Heikal27da9af62025-03-12 19:59:45155 extra_args += ['--override-artifact', f'{subpath}:{url}']
Peter Kotwicza31339a2020-10-20 16:36:23156
Mohamed Heikal9aa653c2025-03-17 21:47:43157 os.makedirs(_CIPD_PATH, exist_ok=True)
Mohamed Heikal5bd8a842025-03-20 14:55:43158 # gclient/cipd extract files as read only.
159 subprocess.run(['chmod', '-R', '+w', _CIPD_PATH])
Mohamed Heikal9aa653c2025-03-17 21:47:43160
161 fetch_util.run_fetch_all(android_deps_dir=_ANDROIDX_PATH,
162 output_subdir='cipd',
Mohamed Heikal27da9af62025-03-12 19:59:45163 extra_args=extra_args,
164 verbose_count=args.verbose_count)
165
166 version_map_str = fetch_util.generate_version_map_str(_CIPD_PATH /
167 _BOM_NAME)
Mohamed Heikal811bb642025-02-25 20:03:53168
169 # Regenerate the build.gradle file filling in the the version map so that
170 # runs of the main project do not have to revalutate androidx versions.
Mohamed Heikal27da9af62025-03-12 19:59:45171 fetch_util.fill_template(
172 _ANDROIDX_PATH / 'build.gradle.template',
173 _CIPD_PATH / 'build.gradle',
174 version_overrides=version_map_str,
Mohamed Heikal9aa653c2025-03-17 21:47:43175 androidx_repository_url=androidx_snapshot_repository_url)
Mohamed Heikal811bb642025-02-25 20:03:53176
Andrew Grieved8c97182024-06-28 18:37:33177 version_txt_path = os.path.join(_CIPD_PATH, 'VERSION.txt')
Andrew Grieve9c7d4ac2f2022-08-24 17:38:59178 with open(version_txt_path, 'w') as f:
179 f.write(version)
Peter Kotwicz92548aa2021-02-18 15:12:58180
Mohamed Heikal27da9af62025-03-12 19:59:45181 to_commit_zip_path = _CIPD_PATH / 'to_commit.zip'
182 file_map = {f: f'third_party/androidx/{f}' for f in _FILES_TO_COMMIT}
183 fetch_util.create_to_commit_zip(output_path=to_commit_zip_path,
184 package_root=_CIPD_PATH,
185 dirnames=['libs'],
186 absolute_file_map=file_map)
Mohamed Heikal9aa653c2025-03-17 21:47:43187 if args.local:
Mohamed Heikal5bd8a842025-03-20 14:55:43188 subprocess.run([
189 _EXTRACT_SCRIPT_PATH, '--cipd-package-path', _CIPD_PATH,
190 '--no-git-add'
191 ],
192 check=True)
Peter Kotwicz2b85bd3d2020-10-01 22:29:49193
Mohamed Heikal27da9af62025-03-12 19:59:45194 fetch_util.write_cipd_yaml(package_root=_CIPD_PATH,
Andrew Grievef4aa0e262025-05-06 17:21:23195 package_name=fetch_util.ANDROIDX_CIPD_PACKAGE,
Mohamed Heikal27da9af62025-03-12 19:59:45196 version=version,
197 output_path=_CIPD_PATH / 'cipd.yaml')
Peter Wen801b2fe2025-02-25 23:02:46198
Peter Kotwicz2b85bd3d2020-10-01 22:29:49199if __name__ == '__main__':
200 main()