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