-
Notifications
You must be signed in to change notification settings - Fork 2
/
srcinclude.py
71 lines (49 loc) · 1.9 KB
/
srcinclude.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
from __future__ import print_function
import sys
import re
import argparse
import os
import os.path
import subprocess
def main(argv):
#Process Arguments
parser = argparse.ArgumentParser(description='srcInclude - Program for include analysis and generating srcML based on include order')
parser.add_argument('-o', nargs=1, type=str, help='file to output srcml to')
parser.add_argument('files', metavar='files', type=str, nargs='+', help='files for processing')
#parser.add_argument('--srcml', action='store', nargs=1, required=False, help='generate srcML based on file include order')
args = parser.parse_args()
#Build dictionary of filename -> files it includes
includes = dict()
try:
regex = re.compile('#include\s*["<](.*\.[hc].*)[">]')
for f in args.files:
fn = open(f, "r")
includes[f] = set()
for line in fn:
m = regex.search(line)
if m:
includes[f].add(m.group(1))
fn.close()
except IOError:
print ("Error: file does not exist")
#Create makefile based on dependencies
f = open('srcIncludeMakefile', 'w')
for key, value in includes.items():
if(os.path.isfile(key)):
f.write(key + "_srcinclude: ")
for x in value:
if(os.path.isfile(x)):
f.write(x + "_srcinclude ")
f.write("\n\t @echo " + key + "\n")
f.write("all: ")
for key, value in includes.items():
if(os.path.isfile(key)):
f.write(key + "_srcinclude ")
f.close()
#Execute srcML based on ouput of makefile
if not args.o:
os.system('srcml --in-order $(make all -f srcIncludeMakefile) > out.xml')
else:
os.system('srcml --in-order $(make all -f srcIncludeMakefile) > ' + args.o[0])
if __name__ == "__main__":
main(sys.argv)