summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/third_party_build/run_operations.py
blob: 0e86710b5d433b9d9ce7c802c1b7b50c8b50c924 (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
# 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 os
import subprocess
import sys

# This is a collection of helper functions that use the subprocess.run
# command to execute commands.  In the future, if we have cases where
# we find common python functionality in utilizing the data returned
# from these functions, that functionality can live here for easy reuse
# between other python scripts in dom/media/webrtc/third_party_build.


# must run 'hg' with HGPLAIN=1 to ensure aliases don't interfere with
# command output.
env = os.environ.copy()
env["HGPLAIN"] = "1"


def run_hg(cmd):
    cmd_list = cmd.split(" ")
    res = subprocess.run(
        cmd_list,
        capture_output=True,
        text=True,
        env=env,
    )
    if res.returncode != 0:
        print(
            "Hit return code {} running '{}'. Aborting.".format(res.returncode, cmd),
            file=sys.stderr,
        )
        print(res.stderr)
        sys.exit(1)
    stdout = res.stdout.strip()
    return [] if len(stdout) == 0 else stdout.split("\n")


def run_git(cmd, working_dir):
    cmd_list = cmd.split(" ")
    res = subprocess.run(
        cmd_list,
        capture_output=True,
        text=True,
        cwd=working_dir,
    )
    if res.returncode != 0:
        print(
            "Hit return code {} running '{}'. Aborting.".format(res.returncode, cmd),
            file=sys.stderr,
        )
        print(res.stderr)
        sys.exit(1)
    stdout = res.stdout.strip()
    return [] if len(stdout) == 0 else stdout.split("\n")


def run_shell(cmd, capture_output=True):
    res = subprocess.run(
        cmd,
        shell=True,
        capture_output=capture_output,
        text=True,
    )
    if res.returncode != 0:
        print(
            "Hit return code {} running '{}'. Aborting.".format(res.returncode, cmd),
            file=sys.stderr,
        )
        if capture_output:
            print(res.stderr)
        sys.exit(1)
    output_lines = []
    if capture_output:
        stdout = res.stdout.strip()
        output_lines = [] if len(stdout) == 0 else stdout.split("\n")

    return output_lines


def get_last_line(file_path):
    # technique from https://stackoverflow.com/questions/46258499/how-to-read-the-last-line-of-a-file-in-python
    with open(file_path, "rb") as f:
        try:  # catch OSError in case of a one line file
            f.seek(-2, os.SEEK_END)
            while f.read(1) != b"\n":
                f.seek(-2, os.SEEK_CUR)
        except OSError:
            f.seek(0)
        return f.readline().decode().strip()


def update_resume_state(state, resume_state_filename):
    with open(resume_state_filename, "w") as ofile:
        ofile.write(state)
        ofile.write("\n")