forked from keith/rules_multirun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultirun.py
112 lines (87 loc) · 2.96 KB
/
multirun.py
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
import json
import os
import shutil
import subprocess
import sys
import platform
from typing import Dict, List, NamedTuple, Union
from python.runfiles import runfiles
_R = runfiles.Create()
class Command(NamedTuple):
path: str
tag: str
args: List[str]
env: Dict[str, str]
def _run_command(command: Command, block: bool, **kwargs) -> Union[int, subprocess.Popen]:
if platform.system() == "Windows":
bash = shutil.which("bash.exe")
if not bash:
raise SystemExit("error: bash.exe not found in PATH")
args = [bash, "-c", f'{command.path} "$@"', "--"] + command.args
else:
args = [command.path] + command.args
env = dict(os.environ)
env.update(command.env)
if block:
return subprocess.check_call(args, env=env)
else:
return subprocess.Popen(args, env=env, **kwargs)
def _perform_concurrently(commands: List[Command], print_command: bool, buffer_output: bool) -> bool:
kwargs = {}
if buffer_output:
kwargs = {
"stdout" : subprocess.PIPE,
"stderr" : subprocess.STDOUT
}
processes = [
(command, _run_command(command, block=False, **kwargs))
for command
in commands
]
success = True
for command, process in processes:
process.wait()
if print_command and buffer_output:
print(command.tag, flush=True)
stdout = process.communicate()[0]
if stdout:
print(stdout.decode().strip(), flush=True)
if process.returncode != 0:
success = False
return success
def _perform_serially(commands: List[Command], print_command: bool, keep_going: bool) -> bool:
success = True
for command in commands:
if print_command:
print(command.tag, flush=True)
try:
_run_command(command, block=True)
except subprocess.CalledProcessError:
if keep_going:
success = False
else:
return False
return success
def _script_path(workspace_name: str, path: str) -> str:
# Even on Windows runfiles require forward slashes.
if path.startswith("../"):
return _R.Rlocation(path[3:])
else:
return _R.Rlocation(f"{workspace_name}/{path}")
def _main(path: str) -> None:
with open(path) as f:
instructions = json.load(f)
workspace_name = instructions["workspace_name"]
commands = [
Command(_script_path(workspace_name, blob["path"]), blob["tag"], blob["args"], blob["env"])
for blob in instructions["commands"]
]
parallel = instructions["jobs"] == 0
print_command: bool = instructions["print_command"]
if parallel:
success = _perform_concurrently(commands, print_command, instructions["buffer_output"])
else:
success = _perform_serially(commands, print_command, instructions["keep_going"])
sys.exit(0 if success else 1)
if __name__ == "__main__":
_main(sys.argv[-1])