Created
August 10, 2020 14:11
-
-
Save PeterMinin/d1f8a98153fe09e1736528739794dc1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import re | |
rule_regex = re.compile(r'^{$.*?^}$', re.DOTALL | re.MULTILINE) | |
def filter_suppressions(input_file, output_file): | |
''' | |
Takes the output of Valgrind, run with `--gen-suppressions=all` | |
(or `yes`), and extracts the generated rules, making sure each one | |
appears only once. | |
The rules are given names of the form "rule_N". | |
''' | |
with open(input_file) as f: | |
input_text = f.read() | |
unique_rules = set() | |
for m in rule_regex.finditer(input_text): | |
if m: | |
unique_rules.add(m.group(0)) | |
with open(output_file, 'w') as f: | |
for i, rule in enumerate(unique_rules): | |
rule = rule.replace('<insert_a_suppression_name_here>', 'rule_{}'.format(i)) | |
f.write(rule + '\n') | |
def join_suppressions(input_files, output_file): | |
''' | |
Takes multiple Valgrind suppression files and joins them. | |
Each rule is given a new name of the form "rule_N". | |
''' | |
num = 0 | |
with open(output_file, 'w') as out_file: | |
for in_path in input_files: | |
with open(in_path) as in_file: | |
input_text = in_file.read() | |
for m in rule_regex.finditer(input_text): | |
supp = m.group(0) | |
supp_lines = supp.splitlines() | |
indent = re.match(r'\s*', supp_lines[1]).group(0) | |
supp_lines[1] = indent + 'rule_{}'.format(num) | |
num += 1 | |
out_file.writelines(line + '\n' for line in supp_lines) | |
parser = argparse.ArgumentParser() | |
subparsers = parser.add_subparsers() | |
filter_parser = subparsers.add_parser('filter', help=filter_suppressions.__doc__, | |
description=filter_suppressions.__doc__) | |
filter_parser.add_argument('input') | |
filter_parser.add_argument('output') | |
filter_parser.set_defaults(func=filter_suppressions) | |
join_parser = subparsers.add_parser('join', help=join_suppressions.__doc__, | |
description=join_suppressions.__doc__) | |
join_parser.add_argument('input', nargs='+') | |
join_parser.add_argument('output') | |
join_parser.set_defaults(func=join_suppressions) | |
args = parser.parse_args() | |
args.func(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment