Santiago Seifert | b6c9d6d | 2020-04-07 18:31:57 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | """Updates the ExoPlayer version in platform/external/exoplayer.""" |
| 4 | import argparse |
| 5 | import os |
| 6 | import sys |
| 7 | import subprocess |
| 8 | import atexit |
| 9 | import shutil |
| 10 | import re |
| 11 | import datetime |
| 12 | |
Santiago Seifert | b6c9d6d | 2020-04-07 18:31:57 +0100 | [diff] [blame] | 13 | TEMPORARY_TREE_CHECKOUT_DIR = ".temp_tree/" |
| 14 | TREE_LOCATION = "tree/" |
| 15 | METADATA_FILE = "METADATA" |
| 16 | |
| 17 | def run(command, check=True): |
| 18 | print(f"Running: {command}") |
| 19 | return (subprocess.run( |
| 20 | command, shell=True, check=check, capture_output=True, text=True) |
| 21 | .stdout.strip()) |
| 22 | |
| 23 | # Argument parsing. |
| 24 | parser = argparse.ArgumentParser( |
| 25 | description=f"Update the ExoPlayer version in the {TREE_LOCATION}" |
| 26 | " directory and stage changes for commit.") |
| 27 | parser.add_argument( |
| 28 | "--tag", |
| 29 | help="The tag to update the ExoPlayer version to.") |
| 30 | parser.add_argument( |
| 31 | "--commit", |
| 32 | help="The commit SHA to update the ExoPlayer version to.") |
| 33 | parser.add_argument( |
| 34 | "--branch", |
| 35 | help="The branch to create for the change.", |
| 36 | default="update-exoplayer") |
| 37 | args = parser.parse_args() |
| 38 | |
| 39 | script_directory = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 40 | os.chdir(script_directory) |
| 41 | |
| 42 | if (args.tag is None) == (args.commit is None): |
| 43 | parser.print_help() |
| 44 | sys.exit("\nError: Please provide the tag or the commit. But not both.") |
| 45 | |
| 46 | # Check whether the branch exists, and abort if it does. |
| 47 | if run(f"git rev-parse --verify --quiet {args.branch}", check=False): |
| 48 | parser.print_help() |
| 49 | sys.exit(f"\nBranch {args.branch} already exists. Please delete, or change " |
| 50 | "branch.") |
| 51 | |
| 52 | run(f"repo start {args.branch}") |
| 53 | |
| 54 | # Cleanup function. |
| 55 | def cleanup(): |
| 56 | print(f"Restoring branch {args.branch}") |
| 57 | run(f"git checkout {args.branch}") |
| 58 | shutil.rmtree(TEMPORARY_TREE_CHECKOUT_DIR, ignore_errors=True) |
| 59 | atexit.register(cleanup) |
| 60 | |
Santiago Seifert | 9789298 | 2020-04-19 14:34:47 +0100 | [diff] [blame] | 61 | # Update remote branches. |
| 62 | run("git fetch --all --tags") |
Santiago Seifert | b6c9d6d | 2020-04-07 18:31:57 +0100 | [diff] [blame] | 63 | if args.tag: |
Santiago Seifert | b6c9d6d | 2020-04-07 18:31:57 +0100 | [diff] [blame] | 64 | # Get the commit SHA associated to the tag. |
| 65 | commit = run(f"git rev-list -n 1 {args.tag}") |
| 66 | else: # a commit SHA was provided. |
| 67 | commit = args.commit |
| 68 | |
| 69 | # Checkout the version we want to update to. |
| 70 | run(f"git checkout {commit}") |
| 71 | # Checkout all files into a temporary dir. |
| 72 | run(f"git checkout-index -a --prefix={TEMPORARY_TREE_CHECKOUT_DIR}") |
| 73 | run(f"git checkout {args.branch}") |
| 74 | shutil.rmtree(TREE_LOCATION) |
| 75 | run(f"mv {TEMPORARY_TREE_CHECKOUT_DIR} {TREE_LOCATION}") |
| 76 | run(f"git add {TREE_LOCATION} {METADATA_FILE}") |
| 77 | |
| 78 | with open(METADATA_FILE) as metadata_file: |
| 79 | metadata_lines = metadata_file.readlines() |
| 80 | |
| 81 | # Update the metadata file. |
| 82 | today = datetime.date.today() |
| 83 | with open(METADATA_FILE, "w") as metadata_file: |
| 84 | for line in metadata_lines: |
| 85 | line = re.sub( |
| 86 | r"version: \".+\"", f"version: \"{args.tag or commit}\"", line) |
| 87 | line = re.sub(r"last_upgrade_date {.+}", f"last_upgrade_date " |
| 88 | f"{{ year: {today.year} month: {today.month} day: {today.day} }}", |
| 89 | line) |
| 90 | metadata_file.write(line) |
| 91 | |
| 92 | run(f"git add {METADATA_FILE}") |
| 93 | print("All done. Ready to commit.") |