-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_graph.py
337 lines (286 loc) · 12.1 KB
/
import_graph.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python3
"""
Functionality for creating the import graph of a lecture.
The code has been extracted from `concept_graph.py`.
Unfortunately, the code is not particularly good and needs some clean-up.
"""
import re
import os
import lmh_harvest as harvest
### PART 1 : PARSING OMGROUPS AND MHINPUTREFS
class Context(object):
def __init__(self, mathhub, repo, doc, onlycovered = False):
self.mathhub = mathhub
self.repo = [repo]
self.doc = [doc]
self.inmod = False
self.files = [(repo, doc)]
self.file2mod = []
self.mhinputrefs = [] # mod 2 file
self.onlycovered = onlycovered
def get_path(self):
return os.path.join(self.mathhub, self.repo[-1], "source", self.doc[-1]) + ".tex"
def throw(self, message):
print(f"{self.get_path()}: {message}")
def push_mhinputref(self, repo, doc):
if self.inmod:
self.mhinputrefs.append((self.repo[-1], self.doc[-1], self.file2mod[-1], repo, doc))
else:
self.mhinputrefs.append((self.repo[-1], self.doc[-1], None, repo, doc))
return (repo, doc) not in self.files
def push_doc(self, repo, doc):
if (repo, doc) in self.files:
raise Exception(f"Cycle detected (reached {(repo, doc)} before)")
self.repo.append(repo)
self.doc.append(doc)
self.files.append((repo, doc))
def pop(self):
self.repo.pop()
self.doc.pop()
def push_mod(self, id_, title, type_):
if not id_:
id_ = title.replace(" ", "-")
self.file2mod.append((self.repo[-1], self.doc[-1], id_, title, type_))
self.inmod = True
def pop_mod(self):
self.inmod = False
TOKEN_MHINPUTREF = 0
TOKEN_BEGIN_OMGROUP = 1
TOKEN_END_OMGROUP = 2
TOKEN_PREMATURESTOP = 3
re_arg_core = r"(?:[^\{\}\$]|(?:\$[^\$]+\$)|(\{[^\{\}\$]*\}))+"
re_arg = r"\{(?P<arg>" + re_arg_core + r")\}\s*"
re_param = r"(?:\[(?P<params>[^\]]*)\])?\s*"
re_mhinputref = re.compile(r"\\(mhinputref|input)\*?" + re_param + re_arg)
re_begin_omgroup = re.compile(r"\\begin\{omgroup\}" + re_param + re_arg)
re_end_omgroup = re.compile(r"\\end\{omgroup\}")
re_prematurestop = re.compile(r"\\prematurestop")
regexes = [
(re_mhinputref, TOKEN_MHINPUTREF),
(re_begin_omgroup, TOKEN_BEGIN_OMGROUP),
(re_end_omgroup, TOKEN_END_OMGROUP),
(re_prematurestop, TOKEN_PREMATURESTOP),
]
class PrematureStopException(Exception):
pass
def recurse_omgroup(context, i, tokens):
(match, token_type) = tokens[i]
assert token_type == TOKEN_BEGIN_OMGROUP
params = harvest.get_params(match.group("params"))
if "id" in params:
id_ = params["id"]
else:
id_ = None
title = match.group("arg")
context.push_mod(id_, title, "omgroup")
i += 1
recurse_into = []
found_end = False
should_stop_since_prematurestop = False
while i < len(tokens):
(match, token_type) = tokens[i]
if token_type == TOKEN_END_OMGROUP:
i += 1
found_end = True
break
elif token_type == TOKEN_MHINPUTREF:
repo = match.group("params")
if repo:
assert "," not in repo and "=" not in repo
else:
repo = context.repo[-1]
doc = match.group("arg")
context.push_mhinputref(repo, doc)
recurse_into.append((repo, doc))
i += 1
elif token_type == TOKEN_PREMATURESTOP:
if context.onlycovered:
should_stop_since_prematurestop = True
break
i += 1
else:
context.throw(f"Unexpected token: '{match.group(0)}'")
i += 1
if (not should_stop_since_prematurestop) and not found_end:
context.throw("Missing \\end{omgroup}")
context.pop_mod()
for (repo, doc) in recurse_into:
if (repo, doc) not in context.files:
context.push_doc(repo, doc)
recurse_file(context)
if should_stop_since_prematurestop:
raise PrematureStopException()
return i
def recurse_file(context):
if not os.path.isfile(context.get_path()):
context.throw("File not found: " + context.get_path())
context.pop()
return
with open(context.get_path()) as fp:
string = harvest.preprocess_string(fp.read())
tokens = harvest.parse(string, regexes)
i = 0
while i < len(tokens):
(match, token_type) = tokens[i]
if token_type == TOKEN_BEGIN_OMGROUP:
i = recurse_omgroup(context, i, tokens)
elif token_type == TOKEN_PREMATURESTOP:
raise PrematureStopException()
else:
context.throw(f"Unexpected token: '{match.group(0)}'")
i += 1
else:
pass
context.pop()
### PART 2 : COLLECTING GRAPH DATA
class Graph(object):
def __init__(self):
# values are dictionaries for extra info
self.omgroup_nodes = {} # (file, title) : { }
self.omgroup_edges = {}
self.module_nodes = {}
self.module_edges = {} # \input edges etc
self.omgroup2module_edges = {}
self.g_nodes = {}
self.g_edges = {} # \importmhmodule, \guse, ... edges
def add_omgroup_data(mathhub, root_repo, root_doc, graph, onlycovered = False):
# gather data
context = Context(mathhub, root_repo, root_doc, onlycovered)
try:
recurse_file(context)
except PrematureStopException:
pass
# process data
file2omgroups = {}
for entry in context.file2mod:
if entry[4] == "omgroup":
key = (entry[0], entry[1])
if key not in file2omgroups:
file2omgroups[key] = []
file2omgroups[key].append((entry[0], entry[1], entry[2], entry[3])) # repo, doc, id, title
omgroup2files = {}
for entry in context.mhinputrefs:
if entry[2]:
key = (entry[2][0], entry[2][1], entry[2][2], entry[2][3])
if key not in omgroup2files:
omgroup2files[key] = []
omgroup2files[key].append((entry[3], entry[4]))
else:
print("Skipping:", repr(entry))
omgroup2files[(root_repo, root_doc, "root", "AI Lecture")] = [(root_repo, root_doc)]
# find fringe
potential_modules = []
for v in omgroup2files.values():
for e in v:
if e not in file2omgroups:
e2 = os.path.join(mathhub, e[0], "source", e[1]) + ".tex"
potential_modules.append(e2)
# graph data
omgr2path = lambda omgr : os.path.join(mathhub, omgr[0], "source", omgr[1]) + ".tex"
for omgroup in omgroup2files.keys():
node = (omgr2path(omgroup), omgroup[2])
graph.omgroup_nodes[node] = {
"label" : omgroup[3],
}
for f in omgroup2files[omgroup]:
if f in file2omgroups:
for omg2 in file2omgroups[f]:
graph.omgroup_edges[((omgr2path(omgroup), omgroup[2]), (omgr2path(omg2), omg2[2]))] = { }
else:
path = os.path.join(mathhub, f[0], "source", f[1]) + ".tex"
graph.omgroup2module_edges[(node, path)] = {
"status" : "unconfirmed", # not clear if module exists
}
return potential_modules
def fill_graph(mathhub, root_repo, root_doc, graph, onlycovered = False):
potential_modules = add_omgroup_data(mathhub, root_repo, root_doc, graph, onlycovered)
blocked_nodes = potential_modules[:]
logger = harvest.SimpleLogger(2)
potential_nodes = {}
potential_edges = []
gimports = []
while potential_modules:
gatherer = harvest.DataGatherer()
context = harvest.HarvestContext(logger, gatherer, mathhub)
for pm in potential_modules:
context.repo = "/".join(pm.split("/")[:mathhub.count("/")+3]) # TODO: platform independence
path = pm
root, filename = os.path.split(path)
try:
harvest.harvest_file(root, filename, context)
except FileNotFoundError:
print("couldn't find '" + path + "'")
for mod in gatherer.modules:
node = mod["path"]
if node not in potential_nodes.keys():
name = mod["mod_name"]
if not name:
name = os.path.split(node)[1][:-4]
potential_nodes[node] = {"label" : name, "type" : "module"}
for mod in gatherer.langfiles:
node = mod["path"]
if node not in potential_nodes.keys():
name = mod["mod_name"]
if not name:
name = os.path.split(node)[1][:-4]
potential_nodes[node] = {"label" : name, "type" : "langfile"}
for file_ in gatherer.textfiles:
node = file_["path"]
if node not in potential_nodes.keys():
potential_nodes[node] = {"label" : os.path.split(node)[1], "type" : "text"}
assert not gatherer.sigfiles
potential_modules = [] # includes text files
for inp in gatherer.mhinputrefs:
destnode = inp["dest_path"]
if destnode not in blocked_nodes:
blocked_nodes.append(destnode)
potential_modules.append(destnode)
potential_edges.append((inp["path"], destnode))
for imp in gatherer.importmhmodules:
gimports.append((imp["path"], imp["dest_path"]))
graph.g_edges[gimports[-1]] = {"type":{"importmhmodule":"import","usemhmodule":"use"}[imp["type"]]}
for gimport in gatherer.gimports:
gimports.append((gimport["path"],
os.path.join(gimport["dest_repo"], "source", gimport["dest_mod"]) + ".tex"))
graph.g_edges[gimports[-1]] = {"type":{"gimport":"import","guse":"use"}[gimport["type"]]}
for node in potential_nodes.keys():
graph.module_nodes[node] = {
"label" : potential_nodes[node]["label"],
"type" : potential_nodes[node]["type"],
}
for start, end in potential_edges:
if start in potential_nodes.keys() and end in potential_nodes.keys():
graph.module_edges[(start, end)] = {}
## handle gimports
assert graph.g_nodes == {}
while gimports:
gatherer = harvest.DataGatherer()
context = harvest.HarvestContext(logger, gatherer, mathhub)
for source, dest in gimports:
if dest not in graph.g_nodes.keys() and dest not in potential_nodes.keys():
context.repo = "/".join(dest.split("/")[:mathhub.count("/")+3]) # TODO: platform independence
root, filename = os.path.split(dest)
try:
harvest.harvest_file(root, filename, context)
except FileNotFoundError:
print("couldn't find '" + dest + "'")
assert not gatherer.langfiles
assert not gatherer.textfiles
for mod in gatherer.modules + gatherer.sigfiles:
node = mod["path"]
if node not in potential_nodes.keys() and node not in graph.g_nodes.keys():
name = mod["mod_name"]
if not name:
name = os.path.split(node)[1][:-4]
graph.g_nodes[node] = {"label" : name, "type" : "module"}
gimports = []
for gimport in gatherer.gimports:
pair = (gimport["path"], os.path.join(gimport["dest_repo"], "source", gimport["dest_mod"]) + ".tex")
graph.g_edges[pair] = {"type":{"gimport":"import","guse":"use"}[gimport["type"]]}
if pair[1] not in graph.g_nodes.keys() and pair[1] not in potential_nodes.keys():
gimports.append(pair)
for imp in gatherer.importmhmodules:
pair = (imp["path"], imp["dest_path"])
graph.g_edges[pair] = {"type":{"importmhmodule":"import","usemhmodule":"use"}[imp["type"]]}
if pair[1] not in graph.g_nodes.keys() and pair[1] not in potential_nodes.keys():
gimports.append(pair)