| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import io |
| 4 |
import re |
| 5 |
import sys |
| 6 |
import zlib |
| 7 |
|
| 8 |
if sys.argv[1] == '-n': |
| 9 |
no_compress = True |
| 10 |
del sys.argv[1] |
| 11 |
else: |
| 12 |
no_compress = False |
| 13 |
|
| 14 |
re_filter = re.compile(sys.argv[1]) |
| 15 |
|
| 16 |
data = io.StringIO(zlib.decompress(sys.stdin.buffer.read()).decode('UTF-8')) |
| 17 |
line = data.readline() |
| 18 |
assert line == 'VERSION 1\n' |
| 19 |
|
| 20 |
bug_list = [] |
| 21 |
bugs = {} |
| 22 |
for line in data: |
| 23 |
assert line[-1] == '\n' |
| 24 |
line = line[:-1] |
| 25 |
if line: |
| 26 |
(name, flags, desc) = line.split(',', 2) |
| 27 |
bug_list.append(name) |
| 28 |
bugs[name] = desc |
| 29 |
else: |
| 30 |
break |
| 31 |
|
| 32 |
|
| 33 |
copied_bugs = [] |
| 34 |
copied_bugs_index = {} |
| 35 |
|
| 36 |
result_main = [] |
| 37 |
for line in data: |
| 38 |
assert line[-1] == '\n' |
| 39 |
line = line[:-1] |
| 40 |
if line: |
| 41 |
line = line.split(',') |
| 42 |
bug_name = line[1] = bug_list[int(line[1])] |
| 43 |
line_combined = ','.join(line) |
| 44 |
if re_filter.match(line_combined): |
| 45 |
if copied_bugs_index.has_key(bug_name): |
| 46 |
line[1] = str(copied_bugs_index[bug_name]) |
| 47 |
else: |
| 48 |
number = len(copied_bugs) |
| 49 |
copied_bugs.append("%s,,%s\n" % (bug_name, bugs[bug_name])) |
| 50 |
copied_bugs_index[bug_name] = number |
| 51 |
line[1] = str(number) |
| 52 |
line = ','.join(line) + '\n' |
| 53 |
result_main.append(line) |
| 54 |
else: |
| 55 |
break |
| 56 |
|
| 57 |
result = ['VERSION 1\n'] + copied_bugs + ['\n'] + result_main + ['\n'] |
| 58 |
|
| 59 |
for line in data: |
| 60 |
result.append(line) |
| 61 |
|
| 62 |
result = ''.join(result) |
| 63 |
if not no_compress: |
| 64 |
result = zlib.compress(result.encode('UTF-8'), 9) |
| 65 |
sys.stdout.buffer.write(result) |