Santiago Seifert | 41a78d6 | 2021-09-29 17:50:37 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | """Downloads a new ExoPlayer copy into platform/exoplayer/external. |
| 4 | |
| 5 | The copy is put in tree_<SHA>/ directory, where <SHA> identifies the downloaded |
| 6 | commit. |
| 7 | """ |
| 8 | import argparse |
| 9 | import atexit |
| 10 | import datetime |
| 11 | import logging |
| 12 | import os |
| 13 | import re |
| 14 | import shutil |
| 15 | import subprocess |
| 16 | import sys |
| 17 | import tempfile |
| 18 | |
| 19 | EXOPLAYER_SOURCE_REPO = "https://github.com/google/ExoPlayer.git" |
| 20 | METADATA_FILE = "METADATA" |
| 21 | SCRIPT_PARENT_DIRECTORY = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 22 | TREE_LOCATION = "tree/" |
| 23 | |
| 24 | |
| 25 | def cd_to_script_parent_directory(): |
| 26 | os.chdir(SCRIPT_PARENT_DIRECTORY) |
| 27 | |
| 28 | |
| 29 | def 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 | |
Santiago Seifert | db0062f | 2021-10-08 15:54:35 +0100 | [diff] [blame] | 36 | def 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: |
Kevin Rocard | 54adf1c | 2021-11-09 15:05:00 +0000 | [diff] [blame] | 40 | print("Please confirm [Y/n] ") |
Santiago Seifert | db0062f | 2021-10-08 15:54:35 +0100 | [diff] [blame] | 41 | choice = input().lower() |
Kevin Rocard | 54adf1c | 2021-11-09 15:05:00 +0000 | [diff] [blame] | 42 | if choice in ["y", "yes", ""]: |
Santiago Seifert | db0062f | 2021-10-08 15:54:35 +0100 | [diff] [blame] | 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 | |
Santiago Seifert | 41a78d6 | 2021-09-29 17:50:37 +0100 | [diff] [blame] | 50 | logging.basicConfig(level=logging.INFO) |
| 51 | |
| 52 | parser = 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.") |
Kevin Rocard | 54adf1c | 2021-11-09 15:05:00 +0000 | [diff] [blame] | 57 | refGroup = parser.add_mutually_exclusive_group(required=True) |
| 58 | refGroup.add_argument( |
Santiago Seifert | 41a78d6 | 2021-09-29 17:50:37 +0100 | [diff] [blame] | 59 | "--tag", help="The tag that identifies the ExoPlayer commit to download.") |
Kevin Rocard | 54adf1c | 2021-11-09 15:05:00 +0000 | [diff] [blame] | 60 | refGroup.add_argument( |
Santiago Seifert | 41a78d6 | 2021-09-29 17:50:37 +0100 | [diff] [blame] | 61 | "--commit", help="The commit SHA of the ExoPlayer version to download.") |
| 62 | parser.add_argument( |
| 63 | "--branch", |
| 64 | help="The branch to create for the change.", |
Kevin Rocard | 54adf1c | 2021-11-09 15:05:00 +0000 | [diff] [blame] | 65 | default="download-exoplayer") |
Santiago Seifert | 41a78d6 | 2021-09-29 17:50:37 +0100 | [diff] [blame] | 66 | args = parser.parse_args() |
| 67 | |
Santiago Seifert | 41a78d6 | 2021-09-29 17:50:37 +0100 | [diff] [blame] | 68 | cd_to_script_parent_directory() |
| 69 | |
| 70 | # Check whether the branch exists, and abort if it does. |
| 71 | if 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 | |
Kevin Rocard | 54adf1c | 2021-11-09 15:05:00 +0000 | [diff] [blame] | 75 | if run(f"repo start {args.branch}"): |
Santiago Seifert | 41a78d6 | 2021-09-29 17:50:37 +0100 | [diff] [blame] | 76 | sys.exit(f"\nFailed to repo start {args.branch}. Check you don't have " |
| 77 | "uncommited changes in your current branch.") |
| 78 | |
| 79 | with tempfile.TemporaryDirectory() as tmpdir: |
| 80 | logging.info(f"Created temporary directory {tmpdir}") |
Kevin Rocard | 54adf1c | 2021-11-09 15:05:00 +0000 | [diff] [blame] | 81 | run("git clone --no-checkout --filter=tree:0 " |
| 82 | f"{EXOPLAYER_SOURCE_REPO} {tmpdir}") |
| 83 | os.chdir(tmpdir) |
Santiago Seifert | 41a78d6 | 2021-09-29 17:50:37 +0100 | [diff] [blame] | 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) |
Santiago Seifert | db0062f | 2021-10-08 15:54:35 +0100 | [diff] [blame] | 96 | |
Santiago Seifert | 0239bae | 2021-10-04 16:04:55 +0100 | [diff] [blame] | 97 | # Remove all Android.mk files in the exoplayer tree to avoid licensing issues. |
Santiago Seifert | db0062f | 2021-10-08 15:54:35 +0100 | [diff] [blame] | 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 | |
Santiago Seifert | 41a78d6 | 2021-09-29 17:50:37 +0100 | [diff] [blame] | 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 | |
| 107 | with open(METADATA_FILE) as metadata_file: |
| 108 | metadata_lines = metadata_file.readlines() |
| 109 | |
| 110 | # Update the metadata file. |
| 111 | today = datetime.date.today() |
| 112 | with 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 | |
| 121 | run(f"git add {METADATA_FILE}") |
| 122 | logging.info("All done. Ready to commit.") |