forked from paracrawl/cirrus-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisector.py
executable file
·195 lines (153 loc) · 4.67 KB
/
bisector.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
import sys
import subprocess
from threading import Thread
from queue import Queue, Empty
from io import TextIOWrapper
from itertools import islice
from math import ceil
def prefix(prefix, lines):
return "\n".join(prefix + line for line in lines.split("\n"))
class Reader(Thread):
def __init__(self, file, queue):
super().__init__()
self.file = file
self.queue = queue
def run(self):
while True:
line = self.file.readline()
if not line:
break
self.queue.put(line.rstrip(b"\n"))
class CrashingChild(Exception):
pass
class UnexpectedOutput(Exception):
def __init__(self, lines, expected):
super().__init__("Expected {} line{}, got {}".format(expected, "s" if expected != 1 else "", len(lines)))
self.lines = lines
class TroublesomeInput(Exception):
def __init__(self, lines):
super().__init__("Troublesome input on line {start} to {end}".format(start=lines[0][0], end=lines[-1][0]))
self.lines = lines
class Child:
def __init__(self, argv):
self.argv = argv
self.queue = Queue()
self.alive = False
def __enter__(self):
self.start()
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is not None:
self.kill()
else:
self.close()
def start(self):
self.proc = subprocess.Popen(self.argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
self.reader = Reader(self.proc.stdout, self.queue)
self.reader.start()
self.alive = True
def kill(self):
if not self.alive:
return
self.alive = False
self.proc.stdin.close()
self.proc.kill()
# Don't wait.
def close(self):
if not self.alive:
return
self.alive = False
# Indicate we're done with the child
self.proc.stdin.close()
# Wait for process to finish
exit_code = self.proc.wait()
if exit_code != 0:
raise CrashingChild("{command} crashed with exit code {exit_code}".format(
command=" ".join(self.argv),
exit_code=exit_code))
# Wait for reading to finish
self.reader.join()
def readlines(self):
output = []
while not self.queue.empty():
output.append(self.queue.get())
return output
def process_chunk(argv, lines):
with Child(argv) as child:
for line in lines:
child.proc.stdin.write(line + b"\n")
child.close()
output = child.readlines()
if len(output) != len(lines):
raise UnexpectedOutput(output, len(lines))
return output
def try_chunk(argv, lines, target_size=1, report=None):
try:
output = process_chunk(argv, list(line for _, line in lines))
except (UnexpectedOutput, CrashingChild) as e:
print("Trouble between lines {} to {}: {}".format(lines[0][0], lines[-1][0], e), file=sys.stderr)
if len(lines) <= target_size:
try:
raise TroublesomeInput(lines=lines) from e
except TroublesomeInput as e:
if report:
report(e)
return [text for _, text in lines] # Just return the input as output
else:
raise
else:
output = []
for n, chunk in enumerate(grouper(lines, int(ceil(len(lines) / 2)))):
output += try_chunk(argv, chunk, target_size=target_size, report=report)
print("While processing the chunk {} to {} in smaller chunks instead, no errors occurred".format(lines[0][0], lines[-1][0]), file=sys.stderr)
return output
def grouper(iterable, n):
iterator = iter(iterable)
try:
while True:
chunk = []
for i in range(n):
chunk.append(next(iterator))
yield chunk
except StopIteration:
if len(chunk):
yield chunk
def print_error(e):
if isinstance(e.__cause__, UnexpectedOutput):
sys.stderr.write("{error!s}:\n{input}\nOutput: ({len} line{plural})\n{output}\n".format(
error=e,
len=len(e.__cause__.lines),
plural="s" if len(e.__cause__.lines) != 1 else "",
input=prefix("> ", b"\n".join(line[1] for line in e.lines).decode()),
output=prefix("> ", b"\n".join(e.__cause__.lines).decode())))
else:
sys.stderr.write("{error!s}:\n{input}\nNo output because child crashed\n".format(
error=e,
input=prefix("> ", b"\n".join(line[1] for line in e.lines).decode())))
def main(argv):
argv = argv[1:] # Ditch program name
target_size = 1
report = None
# optional extract target size argument
while len(argv) > 0:
if argv[0].isnumeric():
target_size = int(argv[0])
argv = argv[1:]
elif argv[0] == "--ignore":
report = print_error
argv = argv[1:]
else:
break
if len(argv) == 0:
raise ValueError("Missing child command")
for chunk in grouper(enumerate(sys.stdin.buffer, start=1), 8192):
try:
for line in try_chunk(argv, [(line_no, line.rstrip(b"\n")) for line_no, line in chunk], target_size=target_size, report=report):
sys.stdout.buffer.write(line + b"\n")
except TroublesomeInput as e:
print_error(e)
return 1
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))