summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/third_party_build/fetch_github_repo.py
blob: bfa40f5c7b902f5d176b702239bf684b3d3ff1f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import argparse
import os
import re
import shutil

from run_operations import run_git, run_shell

# This script fetches the moz-libwebrtc github repro with the expected
# upstream remote and branch-heads setup.  This is used by both the
# prep_repo.sh script as well as the restore_patch_stack.py script.
#
# For speed and conservation of network resources, after fetching all
# the data, a tar of the repo is made and used if available.


def fetch_repo(github_path, force_fetch, tar_path):
    capture_output = False

    # check for pre-existing repo - make sure we force the removal
    if force_fetch and os.path.exists(github_path):
        print("Removing existing repo: {}".format(github_path))
        shutil.rmtree(github_path)

    # clone https://github.com/mozilla/libwebrtc
    if not os.path.exists(github_path):
        # check for pre-existing tar, use it if we have it
        if os.path.exists(tar_path):
            print("Using tar file to reconstitute repo")
            cmd = "cd {} ; tar --extract --gunzip --file={}".format(
                os.path.dirname(github_path), os.path.basename(tar_path)
            )
            run_shell(cmd, capture_output)
        else:
            print("Cloning github repo")
            run_shell(
                "git clone https://github.com/mozilla/libwebrtc {}".format(github_path),
                capture_output,
            )

    # setup upstream (https://webrtc.googlesource.com/src)
    stdout_lines = run_git("git config --local --list", github_path)
    stdout_lines = [
        path for path in stdout_lines if re.findall("^remote.upstream.url.*", path)
    ]
    if len(stdout_lines) == 0:
        print("Fetching upstream")
        run_git("git checkout master", github_path)
        run_git(
            "git remote add upstream https://webrtc.googlesource.com/src", github_path
        )
        run_git("git fetch upstream", github_path)
        run_git("git merge upstream/master", github_path)
    else:
        print(
            "Upstream remote (https://webrtc.googlesource.com/src) already configured"
        )

    # setup upstream branch-heads
    stdout_lines = run_git(
        "git config --local --get-all remote.upstream.fetch", github_path
    )
    if len(stdout_lines) == 1:
        print("Fetching upstream branch-heads")
        run_git(
            "git config --local --add remote.upstream.fetch +refs/branch-heads/*:refs/remotes/branch-heads/*",
            github_path,
        )
        run_git("git fetch upstream", github_path)
    else:
        print("Upstream remote branch-heads already configured")

    # do a sanity fetch in case this was not a freshly cloned copy of the
    # repo, meaning it may not have all the mozilla branches present.
    run_git("git fetch --all", github_path)

    # create tar to avoid time refetching
    if not os.path.exists(tar_path):
        print("Creating tar file for quicker restore")
        cmd = "cd {} ; tar --create --gzip --file={} {}".format(
            os.path.dirname(github_path),
            os.path.basename(tar_path),
            os.path.basename(github_path),
        )
        run_shell(cmd, capture_output)


if __name__ == "__main__":
    default_state_dir = ".moz-fast-forward"
    default_tar_name = "moz-libwebrtc.tar.gz"

    parser = argparse.ArgumentParser(
        description="Restore moz-libwebrtc github patch stack"
    )
    parser.add_argument(
        "--repo-path",
        required=True,
        help="path to libwebrtc repo",
    )
    parser.add_argument(
        "--force-fetch",
        action="store_true",
        default=False,
        help="force rebuild an existing repo directory",
    )
    parser.add_argument(
        "--tar-name",
        default=default_tar_name,
        help="name of tar file (defaults to {})".format(default_tar_name),
    )
    parser.add_argument(
        "--state-path",
        default=default_state_dir,
        help="path to state directory (defaults to {})".format(default_state_dir),
    )
    args = parser.parse_args()

    fetch_repo(
        args.repo_path, args.force_fetch, os.path.join(args.state_path, args.tar_name)
    )