-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.py
executable file
·154 lines (108 loc) · 2.88 KB
/
files.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
import re
import sys
import io
import os
from pprint import pprint
import subprocess
import glob
def grep_includes (path, recursive=False):
args = ["grep", path, "-e", "#include"]
if recursive:
args.append("-r")
grepout_bin = b''
try:
grepout_bin = subprocess.check_output(args)
except subprocess.CalledProcessError as err:
# print(err.output)
# print(err.returncode)
# print(err.cmd)
print()
if not grepout_bin:
return []
grepout = grepout_bin.decode("utf-8").split(os.linesep)
regex = re.compile('#include.*["<].*\.h[">]')
trueincs = []
incfiles = set([])
for s in grepout:
inc = regex.findall(s)
if inc:
trueincs.append(inc[0])
f = inc[0].replace("#include", "").strip(" \"<>./")
incfiles.add(f)
# else:
# print("\"%s\" not match" % s)
# print()
# print(path)
# print()
# pprint(incfiles)
# print()
return list(incfiles)
'''
incfiles - list of filenames with path
'''
def basenames (incfiles):
path = []
for f in incfiles:
path.append(f.split("/"))
return list(map(lambda x: x[-1], path))
'''
incfiles - list of stripped filenames without path
'''
def filter_project_deps (prj_path, subrelpath, incfiles):
depincs = set([])
for f in incfiles:
find = subprocess.check_output(["find", prj_path, "-name", f])
s = find.decode("utf-8").strip(os.linesep)
if s and not subrelpath in s and os.path.isfile(s):
depincs.add(s)
return list(depincs)
def merge_lists (l1, l2):
m = set(l1)
for e in l2:
m.add(e)
return list(m)
def find_more_deps (prj_path, subrelpath, deps):
count = 0
while True:
# pprint(sorted(deps))
# grep includes from deps
incs = set([])
for d in deps:
names = basenames(grep_includes(d))
for n in names:
incs.add(n)
# filter only includes, that belongs to project, but not to subproject
new_deps = filter_project_deps(prj_path, subrelpath, list(incs))
deps = merge_lists(deps, new_deps)
if len(deps) == count:
break
else:
count = len(deps)
return deps
def subproject_deps (prj_path, subrelpath):
# grep includes from subproject
includes = basenames(grep_includes(subrelpath, recursive=True))
# filter only includes, that belongs to project, but not to subproject
deps = filter_project_deps(prj_path, subrelpath, includes)
# find deps for deps:
deps = find_more_deps(prj_path, subrelpath, deps)
return deps
'''
TODO: show full picture:
walk trough subproject
find all code files by filename extension
save in tree structure
grep for includes
filter external, internal for project and internal for subproject
save in tree structure
repeat until not found new dependencies
tree_element:
type: subproject file | project file | external
incs: list of included files
'''
PROJECT_PATH = sys.argv[1]
SUBPROJECT_PATH = sys.argv[2]
subprj_rel_path = SUBPROJECT_PATH.replace(PROJECT_PATH, "")
deps = subproject_deps(PROJECT_PATH, SUBPROJECT_PATH)
pprint(sorted(deps))