blob: 05f48a62325ab2158d632ac63745e51833f4b173 [file] [log] [blame]
Santiago Seifert41a78d62021-09-29 17:50:37 +01001#!/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
Santiago Seifertdb0062f2021-10-08 15:54:35 +010036def 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 Rocard54adf1c2021-11-09 15:05:00 +000040 print("Please confirm [Y/n] ")
Santiago Seifertdb0062f2021-10-08 15:54:35 +010041 choice = input().lower()
Kevin Rocard54adf1c2021-11-09 15:05:00 +000042 if choice in ["y", "yes", ""]:
Santiago Seifertdb0062f2021-10-08 15:54:35 +010043 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 Seifert41a78d62021-09-29 17:50:37 +010050logging.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.")
Kevin Rocard54adf1c2021-11-09 15:05:00 +000057refGroup = parser.add_mutually_exclusive_group(required=True)
58refGroup.add_argument(
Santiago Seifert41a78d62021-09-29 17:50:37 +010059 "--tag", help="The tag that identifies the ExoPlayer commit to download.")
Kevin Rocard54adf1c2021-11-09 15:05:00 +000060refGroup.add_argument(
Santiago Seifert41a78d62021-09-29 17:50:37 +010061 "--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.",
Kevin Rocard54adf1c2021-11-09 15:05:00 +000065 default="download-exoplayer")
Santiago Seifert41a78d62021-09-29 17:50:37 +010066args = parser.parse_args()
67
Santiago Seifert41a78d62021-09-29 17:50:37 +010068cd_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
Kevin Rocard54adf1c2021-11-09 15:05:00 +000075if run(f"repo start {args.branch}"):
Santiago Seifert41a78d62021-09-29 17:50:37 +010076 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}")
Kevin Rocard54adf1c2021-11-09 15:05:00 +000081 run("git clone --no-checkout --filter=tree:0 "
82 f"{EXOPLAYER_SOURCE_REPO} {tmpdir}")
83 os.chdir(tmpdir)
Santiago Seifert41a78d62021-09-29 17:50:37 +010084
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 Seifertdb0062f2021-10-08 15:54:35 +010096
Santiago Seifert0239bae2021-10-04 16:04:55 +010097 # Remove all Android.mk files in the exoplayer tree to avoid licensing issues.
Santiago Seifertdb0062f2021-10-08 15:54:35 +010098 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 Seifert41a78d62021-09-29 17:50:37 +0100102 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.")