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