Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2021, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | import argparse |
| 7 | import jdk |
| 8 | import os.path |
| 9 | import re |
| 10 | import subprocess |
| 11 | import sys |
| 12 | import urllib.request |
| 13 | |
| 14 | import utils |
| 15 | |
| 16 | # Grep match string for 'Version X.Y.Z[-dev]' |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 17 | VERSION_EXP = '^Version [[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+\(\|-dev\)$' |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 18 | |
| 19 | # R8 is located in the 'builder' library. |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 20 | AGP_MAVEN = "https://dl.google.com/android/maven2/com/android/tools/build/builder" |
| 21 | |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 22 | |
| 23 | def parse_options(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 24 | parser = argparse.ArgumentParser(description='Tag R8 Versions') |
| 25 | parser.add_argument('--branch', |
| 26 | help='The R8 branch to tag versions on, eg, origin/3.0') |
| 27 | parser.add_argument('--agp', |
| 28 | help='The AGP to compute the tag for, eg, 4.2.0-beta03') |
| 29 | parser.add_argument( |
| 30 | '--dry-run', |
| 31 | default=False, |
| 32 | action='store_true', |
| 33 | help='Print the state changing commands without running them.') |
| 34 | return parser.parse_args() |
| 35 | |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 36 | |
| 37 | def run(options, cmd): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 38 | print(' '.join(cmd)) |
| 39 | if not options.dry_run: |
| 40 | subprocess.check_call(cmd) |
| 41 | |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 42 | |
| 43 | def main(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 44 | args = parse_options() |
| 45 | if args.branch: |
| 46 | tag_r8_branch(args.branch, args) |
| 47 | elif args.agp: |
| 48 | if (args.agp == 'all'): |
| 49 | tag_all_agp_versions(args) |
| 50 | else: |
| 51 | tag_agp_version(args.agp, args) |
Morten Krogh-Jespersen | a23fa10 | 2022-07-01 08:51:59 +0200 | [diff] [blame] | 52 | else: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 53 | print("Should use a top-level option, such as --branch or --agp.") |
| 54 | return 1 |
| 55 | return 0 |
| 56 | |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 57 | |
| 58 | def prepare_print_version(dist, temp): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 59 | wrapper_file = os.path.join( |
| 60 | utils.REPO_ROOT, |
| 61 | 'src/main/java/com/android/tools/r8/utils/PrintR8Version.java') |
| 62 | cmd = [ |
| 63 | jdk.GetJavacExecutable(), |
| 64 | wrapper_file, |
| 65 | '-d', |
| 66 | temp, |
| 67 | '-cp', |
| 68 | dist, |
| 69 | ] |
| 70 | utils.PrintCmd(cmd) |
| 71 | subprocess.check_output(cmd) |
| 72 | return temp |
| 73 | |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 74 | |
| 75 | def get_tag_info_on_origin(tag): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 76 | output = subprocess.check_output( |
| 77 | ['git', 'ls-remote', '--tags', 'origin', tag]).decode('utf-8') |
| 78 | if len(output.strip()) == 0: |
| 79 | return None |
| 80 | return output |
| 81 | |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 82 | |
Morten Krogh-Jespersen | a23fa10 | 2022-07-01 08:51:59 +0200 | [diff] [blame] | 83 | def tag_all_agp_versions(args): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 84 | with utils.TempDir() as temp: |
| 85 | url = "%s/maven-metadata.xml" % AGP_MAVEN |
| 86 | metadata = os.path.join(temp, "maven-metadata.xml") |
| 87 | try: |
| 88 | urllib.request.urlretrieve(url, metadata) |
| 89 | except urllib.error.HTTPError as e: |
| 90 | print('Could not find maven-metadata.xml for agp') |
| 91 | print(e) |
| 92 | return 1 |
| 93 | with open(metadata, 'r') as file: |
| 94 | data = file.read() |
| 95 | pattern = r'<version>(.+)</version>' |
| 96 | matches = re.findall(pattern, data) |
| 97 | matches.reverse() |
| 98 | for version in matches: |
| 99 | print('Tagging agp version ' + version) |
| 100 | tag_agp_version(version, args) |
Morten Krogh-Jespersen | a23fa10 | 2022-07-01 08:51:59 +0200 | [diff] [blame] | 101 | |
| 102 | |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 103 | def tag_agp_version(agp, args): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 104 | tag = 'agp-%s' % agp |
| 105 | result = get_tag_info_on_origin(tag) |
| 106 | if result: |
| 107 | print('Tag %s is already present' % tag) |
| 108 | print(result) |
| 109 | subprocess.call(['git', 'show', '--oneline', '-s', tag]) |
| 110 | return 0 |
| 111 | with utils.TempDir() as temp: |
| 112 | url = "%s/%s/builder-%s.jar" % (AGP_MAVEN, agp, agp) |
| 113 | jar = os.path.join(temp, "agp.jar") |
| 114 | try: |
| 115 | urllib.request.urlretrieve(url, jar) |
| 116 | except urllib.error.HTTPError as e: |
| 117 | print('Could not find jar for agp %s' % agp) |
| 118 | print(e) |
| 119 | return 1 |
| 120 | print_version_helper = prepare_print_version(utils.R8_JAR, temp) |
| 121 | output = subprocess.check_output([ |
| 122 | jdk.GetJavaExecutable(), '-cp', |
| 123 | ':'.join([jar, print_version_helper]), |
| 124 | 'com.android.tools.r8.utils.PrintR8Version' |
| 125 | ]).decode('utf-8') |
| 126 | version = output.split(' ')[0] |
| 127 | run(args, ['git', 'tag', '-f', tag, '-m', tag, '%s^{}' % version]) |
Rico Wind | 94b7764 | 2023-11-27 08:22:45 +0100 | [diff] [blame] | 128 | run(args, ['git', 'push', '-o', 'push-justification=b/313360935', |
| 129 | 'origin', tag]) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 130 | |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 131 | |
| 132 | def tag_r8_branch(branch, args): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 133 | if not branch.startswith('origin/'): |
| 134 | print('Expected branch to start with origin/') |
| 135 | return 1 |
| 136 | output = subprocess.check_output( |
| 137 | ['git', 'log', '--pretty=format:%H\t%s', '--grep', VERSION_EXP, |
| 138 | branch]).decode('utf-8') |
| 139 | for l in output.split('\n'): |
| 140 | (hash, subject) = l.split('\t') |
| 141 | m = re.search('Version (.+)', subject) |
| 142 | if not m: |
| 143 | print('Unable to find a version for line: %s' % l) |
| 144 | continue |
| 145 | version = m.group(1) |
| 146 | result = get_tag_info_on_origin(version) |
| 147 | if not result: |
| 148 | run(args, ['git', 'tag', '-a', version, '-m', version, hash]) |
Rico Wind | 94b7764 | 2023-11-27 08:22:45 +0100 | [diff] [blame] | 149 | run(args, ['git', 'push', '-o', 'push-justification=b/313360935', |
| 150 | 'origin', version]) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 151 | if args.dry_run: |
| 152 | print('Dry run complete. None of the above have been executed.') |
Ian Zerny | a2703f8 | 2021-05-31 11:11:51 +0200 | [diff] [blame] | 153 | |
| 154 | |
| 155 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 156 | sys.exit(main()) |