1"""
2Parses the id of the process that ran with ASAN from the output logs.
3"""
4import sys, argparse, re
5
6def main():
7    parser = argparse.ArgumentParser()
8    parser.add_argument('--infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help='The sanitizer output to get the pid from')
9    parser.add_argument('--outfile', nargs='?', type=argparse.FileType('r'), default=sys.stdout, help='Where to write the result')
10    args = parser.parse_args()
11
12    pid = process_file(args.infile)
13    args.outfile.write(pid)
14    args.infile.close()
15    args.outfile.close()
16
17
18
19def process_file(infile):
20    # check first line is just ==== divider
21    first_line_pattern = re.compile(r'=*')
22    assert first_line_pattern.match(infile.readline())
23
24    # parse out pid from 2nd line
25    # `==PID==ERROR: SanitizerName: error-type on address...`
26    pid_pattern = re.compile(r'==([0-9]*)==ERROR:')
27    pid = pid_pattern.search(infile.readline()).group(1)
28
29    # ignore the rest
30
31    assert pid and pid.isdigit()
32
33    return pid
34
35if __name__ == '__main__':
36    main()
37