1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3"""Updates the ExoPlayer version in platform/external/exoplayer.""" 4import argparse 5import os 6import sys 7import subprocess 8import atexit 9import shutil 10import re 11import datetime 12 13TEMPORARY_TREE_CHECKOUT_DIR = ".temp_tree/" 14TREE_LOCATION = "tree/" 15METADATA_FILE = "METADATA" 16 17def 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. 24parser = argparse.ArgumentParser( 25 description=f"Update the ExoPlayer version in the {TREE_LOCATION}" 26 " directory and stage changes for commit.") 27parser.add_argument( 28 "--tag", 29 help="The tag to update the ExoPlayer version to.") 30parser.add_argument( 31 "--commit", 32 help="The commit SHA to update the ExoPlayer version to.") 33parser.add_argument( 34 "--branch", 35 help="The branch to create for the change.", 36 default="update-exoplayer") 37args = parser.parse_args() 38 39script_directory = os.path.dirname(os.path.abspath(sys.argv[0])) 40os.chdir(script_directory) 41 42if (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. 47if 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 52run(f"repo start {args.branch}") 53 54# Cleanup function. 55def 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) 59atexit.register(cleanup) 60 61# Update remote branches. 62run("git fetch --all --tags") 63if args.tag: 64 # Get the commit SHA associated to the tag. 65 commit = run(f"git rev-list -n 1 {args.tag}") 66else: # a commit SHA was provided. 67 commit = args.commit 68 69# Checkout the version we want to update to. 70run(f"git checkout {commit}") 71# Checkout all files into a temporary dir. 72run(f"git checkout-index -a --prefix={TEMPORARY_TREE_CHECKOUT_DIR}") 73run(f"git checkout {args.branch}") 74shutil.rmtree(TREE_LOCATION) 75run(f"mv {TEMPORARY_TREE_CHECKOUT_DIR} {TREE_LOCATION}") 76run(f"git add {TREE_LOCATION} {METADATA_FILE}") 77 78with open(METADATA_FILE) as metadata_file: 79 metadata_lines = metadata_file.readlines() 80 81# Update the metadata file. 82today = datetime.date.today() 83with 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 92run(f"git add {METADATA_FILE}") 93print("All done. Ready to commit.") 94