-
Notifications
You must be signed in to change notification settings - Fork 3
/
uniq-compdb.py
executable file
·41 lines (30 loc) · 1.28 KB
/
uniq-compdb.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
#!/usr/bin/env python
import json
import argparse
import sys
import os
def main():
## Argparse boilerplat
parser = argparse.ArgumentParser(description='Dedup compdb entries for AST export. Renames original json file as compile_commands_orig.json')
parser.add_argument('infilename', nargs='?', help="Path to compile database json file", default='compile_commands.json')
parser.add_argument('outfilename', nargs='?', help="Output json file name", default='compile_commands.json')
args = parser.parse_args()
infilename = args.infilename
outfilename = args.outfilename
## Read compile_commands.json to infile
if not os.path.isfile(infilename):
print 'compile_commands.json does not exist in working dir. Need help? Use the -h flag'
exit()
with open(infilename) as infile:
buffer = infile.read()
ds = json.loads(buffer)
infile.close()
## Rename 'compile_commands.json' to 'compile_commands_orig.json'
os.rename(infilename, 'compile_commands_orig.json')
all_ids = [each['file'] for each in ds]
unique_stuff = [ds[all_ids.index(id)] for id in set(all_ids)]
with open(outfilename, 'w') as outfile:
json.dump(unique_stuff, outfile, indent=4)
outfile.close()
if __name__ == "__main__":
main()