1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3"""Downloads a new ExoPlayer copy into platform/exoplayer/external. 4 5The copy is put in tree_<SHA>/ directory, where <SHA> identifies the downloaded 6commit. 7""" 8import argparse 9import atexit 10import datetime 11import logging 12import os 13import re 14import shutil 15import subprocess 16import sys 17import tempfile 18 19EXOPLAYER_SOURCE_REPO = "https://github.com/google/ExoPlayer.git" 20METADATA_FILE = "METADATA" 21SCRIPT_PARENT_DIRECTORY = os.path.dirname(os.path.abspath(sys.argv[0])) 22TREE_LOCATION = "tree/" 23 24 25def cd_to_script_parent_directory(): 26 os.chdir(SCRIPT_PARENT_DIRECTORY) 27 28 29def run(command, check=True): 30 logging.info(f"Running: $ {command}") 31 return (subprocess.run( 32 command, shell=True, check=check, capture_output=True, 33 text=True).stdout.strip()) 34 35 36def confirm_deletion_or_exit(files_to_delete): 37 print("The following files will not be added to exoplayer/external: \n" + 38 files_to_delete) 39 while True: 40 print("Please confirm [Y/n] ") 41 choice = input().lower() 42 if choice in ["y", "yes", ""]: 43 return 44 elif choice in ["n", "no"]: 45 sys.exit("User rejected the list of .mk files to exclude from the tree.") 46 else: 47 print("Please select y or n.") 48 49 50logging.basicConfig(level=logging.INFO) 51 52parser = argparse.ArgumentParser( 53 description=f"Downloads an ExoPlayer copy into the tree_<SHA>/ " 54 "directory, where <SHA> identifies the commit to download. This script " 55 "also stages the changes for commit. Either --tag or --commit must be " 56 "provided.") 57refGroup = parser.add_mutually_exclusive_group(required=True) 58refGroup.add_argument( 59 "--tag", help="The tag that identifies the ExoPlayer commit to download.") 60refGroup.add_argument( 61 "--commit", help="The commit SHA of the ExoPlayer version to download.") 62parser.add_argument( 63 "--branch", 64 help="The branch to create for the change.", 65 default="download-exoplayer") 66args = parser.parse_args() 67 68cd_to_script_parent_directory() 69 70# Check whether the branch exists, and abort if it does. 71if run(f"git rev-parse --verify --quiet {args.branch}", check=False): 72 sys.exit(f"\nBranch {args.branch} already exists. Please delete, or change " 73 "branch.") 74 75if run(f"repo start {args.branch}"): 76 sys.exit(f"\nFailed to repo start {args.branch}. Check you don't have " 77 "uncommited changes in your current branch.") 78 79with tempfile.TemporaryDirectory() as tmpdir: 80 logging.info(f"Created temporary directory {tmpdir}") 81 run("git clone --no-checkout --filter=tree:0 " 82 f"{EXOPLAYER_SOURCE_REPO} {tmpdir}") 83 os.chdir(tmpdir) 84 85 if args.tag: 86 # Get the commit SHA associated to the tag. 87 commit_sha = run(f"git rev-list -n 1 {args.tag}") 88 else: # A commit SHA was provided. 89 commit_sha = args.commit 90 91 # Checkout the version we want to update to. 92 run(f"git checkout {commit_sha}") 93 94 # Copy all files in the tree into tree_<SHA>. 95 shutil.rmtree(".git/", ignore_errors=True) 96 97 # Remove all Android.mk files in the exoplayer tree to avoid licensing issues. 98 mk_files_to_remove = run("find . -name \"*.mk\"") 99 confirm_deletion_or_exit(mk_files_to_remove) 100 run("find . -name \"*.mk\" -delete") 101 102 cd_to_script_parent_directory() 103 new_tree_location = f"tree_{commit_sha}" 104 run(f"mv {tmpdir} {new_tree_location}") 105 run(f"git add {new_tree_location}") 106 107with open(METADATA_FILE) as metadata_file: 108 metadata_lines = metadata_file.readlines() 109 110# Update the metadata file. 111today = datetime.date.today() 112with open(METADATA_FILE, "w") as metadata_file: 113 for line in metadata_lines: 114 line = re.sub(r"version: \".+\"", f"version: \"{args.tag or commit_sha}\"", 115 line) 116 line = re.sub( 117 r"last_upgrade_date {.+}", f"last_upgrade_date " 118 f"{{ year: {today.year} month: {today.month} day: {today.day} }}", line) 119 metadata_file.write(line) 120 121run(f"git add {METADATA_FILE}") 122logging.info("All done. Ready to commit.") 123