fetch_github_repo.py (3815B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 import argparse 5 import os 6 import shutil 7 8 from run_operations import run_git, run_shell 9 10 # This script fetches the libwebrtc repro with the expected 11 # upstream remote and branch-heads setup. This is used by both the 12 # prep_repo.sh script as well as the restore_patch_stack.py script. 13 # 14 # For speed and conservation of network resources, after fetching all 15 # the data, a tar of the repo is made and used if available. 16 17 18 def fetch_repo(github_path, force_fetch, tar_path): 19 capture_output = False 20 21 # check for pre-existing repo - make sure we force the removal 22 if force_fetch and os.path.exists(github_path): 23 print(f"Removing existing repo: {github_path}") 24 shutil.rmtree(github_path) 25 26 # clone https://webrtc.googlesource.com/src 27 if not os.path.exists(github_path): 28 # check for pre-existing tar, use it if we have it 29 if os.path.exists(tar_path): 30 print("Using tar file to reconstitute repo") 31 cmd = f"cd {os.path.dirname(github_path)} ; tar --extract --gunzip --file={os.path.basename(tar_path)}" 32 run_shell(cmd, capture_output) 33 else: 34 print("Cloning github repo") 35 run_shell( 36 f"git clone https://webrtc.googlesource.com/src {github_path}", 37 capture_output, 38 ) 39 40 # for sanity, ensure we're on master 41 run_git("git checkout master", github_path) 42 43 # setup upstream branch-heads 44 stdout_lines = run_git( 45 "git config --local --get-all remote.origin.fetch", github_path 46 ) 47 if len(stdout_lines) == 1: 48 print("Fetching upstream branch-heads") 49 run_git( 50 "git config --local --add remote.origin.fetch +refs/branch-heads/*:refs/remotes/branch-heads/*", 51 github_path, 52 ) 53 run_git("git fetch", github_path) 54 else: 55 print("Upstream remote branch-heads already configured") 56 57 # verify that a (quite old) branch-head exists 58 run_git("git show branch-heads/5059", github_path) 59 60 # prevent changing line endings when moving things out of the git repo 61 # (and into hg for instance) 62 run_git("git config --local core.autocrlf false", github_path) 63 64 # do a sanity fetch in case this was not a freshly cloned copy of the 65 # repo. 66 run_git("git fetch --all", github_path) 67 68 # create tar to avoid time refetching 69 if not os.path.exists(tar_path): 70 print("Creating tar file for quicker restore") 71 cmd = f"cd {os.path.dirname(github_path)} ; tar --create --gzip --file={os.path.basename(tar_path)} {os.path.basename(github_path)}" 72 run_shell(cmd, capture_output) 73 74 75 if __name__ == "__main__": 76 default_state_dir = ".moz-fast-forward" 77 default_tar_name = "moz-libwebrtc.tar.gz" 78 79 parser = argparse.ArgumentParser( 80 description="Restore moz-libwebrtc github patch stack" 81 ) 82 parser.add_argument( 83 "--repo-path", 84 required=True, 85 help="path to libwebrtc repo", 86 ) 87 parser.add_argument( 88 "--force-fetch", 89 action="store_true", 90 default=False, 91 help="force rebuild an existing repo directory", 92 ) 93 parser.add_argument( 94 "--tar-name", 95 default=default_tar_name, 96 help=f"name of tar file (defaults to {default_tar_name})", 97 ) 98 parser.add_argument( 99 "--state-path", 100 default=default_state_dir, 101 help=f"path to state directory (defaults to {default_state_dir})", 102 ) 103 args = parser.parse_args() 104 105 fetch_repo( 106 args.repo_path, 107 args.force_fetch, 108 os.path.join(args.state_path, args.tar_name), 109 )