-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmultirun.py
146 lines (117 loc) · 4.04 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import json
import os
import signal
import shutil
import subprocess
import sys
import threading
import platform
from typing import Dict, List, NamedTuple, Union, Tuple
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 _forward_stdin(procs: List[Tuple[Command, subprocess.Popen]]) -> None:
for line in sys.stdin.readlines():
if not line:
break
for (cmd, proc) in procs:
proc.stdin.write(line.encode())
proc.stdin.flush()
for (cmd, proc) in procs:
proc.stdin.close()
def _perform_concurrently(commands: List[Command], print_command: bool, buffer_output: bool, forward_stdin: bool) -> bool:
kwargs = {}
if buffer_output:
kwargs = {
"stdout" : subprocess.PIPE,
"stderr" : subprocess.STDOUT
}
if forward_stdin:
kwargs["stdin"] = subprocess.PIPE
processes = [
(command, _run_command(command, block=False, **kwargs))
for command
in commands
]
threads = []
if forward_stdin:
stdin_thread = threading.Thread(target=_forward_stdin, args=(processes,))
stdin_thread.start()
threads.append(stdin_thread)
success = True
try:
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
except KeyboardInterrupt:
for command, process in processes:
process.send_signal(signal.SIGINT)
process.wait()
success = False
finally:
for thread in threads:
thread.join()
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
except KeyboardInterrupt:
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(instructions_path: str, extra_args: List[str]) -> None:
with open(instructions_path) as f:
instructions = json.load(f)
workspace_name = instructions["workspace_name"]
commands = [
Command(_script_path(workspace_name, blob["path"]), blob["tag"],
blob["args"] + extra_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"], instructions["forward_stdin"])
else:
success = _perform_serially(commands, print_command, instructions["keep_going"])
sys.exit(0 if success else 1)
if __name__ == "__main__":
_main(sys.argv[1], sys.argv[2:])