This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.py
executable file
·447 lines (335 loc) · 10.3 KB
/
prepare.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/env python3
#
# Copyright (c) 2011 Timo Savola
# Reverse topological sort function is Copyright (c) 2004, 2005 Nokia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
import ast
import optparse
import os
import re
import sys
class Project:
def __init__(self, outputdir):
self.outputdir = outputdir
self.units = []
def add(self, sourcename, template):
if template:
targetname = os.path.join(self.outputdir, sourcename[:-1])
unit = TemplateUnit(sourcename, targetname)
else:
unit = CodeUnit(sourcename)
self.units.append(unit)
def process(self):
for unit in self.units:
unit.parse()
symbolmap = {}
for unit in self.units:
for symbol in unit.declsyms:
assert symbol.name not in symbolmap
symbolmap[symbol.name] = symbol
datamap = {}
for unit in self.units:
unit.analyze_references(symbolmap, datamap)
depends = set()
for unit in self.units:
for name in unit.refnames:
symbol = symbolmap[name]
depends.add((unit, symbol.unit))
for data in datamap.values():
for prod in data.producers:
for cons in data.consumers:
depends.add((cons, prod))
globaldict = {}
for unit in reverse_topological_sort(self.units, depends):
unit.evaluate(globaldict)
def adjust(self, filename):
assert self.outputdir # TODO
prefix = os.path.join(self.outputdir, "")
source = ""
with open(filename) as file:
for line in file:
if line.startswith("#"):
match = re.match(r'^(#\s*\d+\s+")([^"]+)(".*)$', line)
if match:
head, name, tail = match.groups()
if name.startswith(prefix):
name = name[len(prefix):] + "y"
line = "".join([head, name, tail]) + "\n"
source += line
with open(filename, "w") as file:
file.write(source)
class AbstractUnit:
def __init__(self, sourcename):
self.sourcename = sourcename
def parse(self):
self.declsyms = set()
self.refnames = set()
with open(self.sourcename) as file:
self.do_parse(file)
self.refnames -= set(s.name for s in self.declsyms)
self.refnames -= set(dir(__builtins__))
def analyze_references(self, symbolmap, datamap):
def get_data(unit):
data = datamap.get(unit)
if data is None:
data = Data()
datamap[unit] = data
return data
for name in self.refnames:
symbol = symbolmap[name]
if symbol.producer:
get_data(symbol.unit).producers.add(self)
if symbol.consumer:
get_data(symbol.unit).consumers.add(self)
class TemplateUnit(AbstractUnit):
def __init__(self, sourcename, targetname):
super().__init__(sourcename)
self.targetname = targetname
def do_parse(self, file):
self.blocks = []
code_block = False
code_indent = None
code_source = ""
for text in file:
while text:
if code_block:
i = text.find("}}}")
if i >= 0:
code_block = False
if text[:i].strip():
code_source += text[:i]
self.blocks.append(TemplateCodeBlock(code_source, code_indent, self))
code_indent = None
code_source = ""
text = text[i+3:]
else:
if text.strip():
code_source += text
text = ""
else:
i = text.find("{{{")
if i >= 0:
code_block = True
self.blocks.append(TextBlock(text[:i]))
code_indent = re.sub(r"[^\t]", " ", text[:i])
text = code_indent + " " + text[i+3:]
if not text.strip():
text = ""
text = "if True:\n" + text
else:
self.blocks.append(TextBlock(text))
text = ""
def evaluate(self, globaldict):
dirname = os.path.dirname(self.targetname)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
tempname = self.targetname + ".tmp"
done = False
try:
with open(tempname, "w") as outputfile:
for block in self.blocks:
block.evaluate(globaldict, outputfile)
done = True
finally:
if done:
self.deploy(tempname)
else:
os.remove(tempname)
def deploy(self, tempname):
changed = True
if os.path.exists(self.targetname):
with open(tempname) as file:
newdata = file.read()
with open(self.targetname) as file:
olddata = file.read()
changed = (newdata != olddata)
if changed:
print(" Prepare ", self.targetname)
os.rename(tempname, self.targetname)
else:
if int(os.environ.get("PREPARE_VERBOSE", 0)) > 0:
print(" Unchanged", self.targetname)
os.remove(tempname)
class CodeUnit(AbstractUnit):
def do_parse(self, file):
self.block = CodeBlock(file.read(), self)
def analyze_references(self, symbolmap, datamap):
def get_data(unit):
data = datamap.get(unit)
if data is None:
data = Data()
datamap[unit] = data
return data
for name in self.refnames:
symbol = symbolmap[name]
if symbol.producer:
get_data(symbol.unit).producers.add(self)
if symbol.consumer:
get_data(symbol.unit).consumers.add(self)
def evaluate(self, globaldict):
self.block.evaluate(globaldict)
class TextBlock:
def __init__(self, text):
self.text = text
def evaluate(self, globaldict, outputfile):
print(self.text, end="", file=outputfile)
class CodeBlock:
source_transformations = [(re.compile(pat, re.MULTILINE), sub) for pat, sub in [
(r"^(\s*)for\s+([^\s:]+)\s+in\s+([^:]+)\s+if\s+([^:]+):",
r"\1for \2 in (\2 for \2 in \3 if \4):"),
(r"(^|\W)echo\s*\((.*)\)(\s*)$",
r"\1_echo(locals(), \2)\3"),
]]
def __init__(self, source, unit):
for pat, sub in self.source_transformations:
source = pat.sub(sub, source)
self.ast = ast.parse(source, unit.sourcename)
self.unit = unit
self.declnames = set()
self.analyze_symbols(self.ast)
def analyze_symbols(self, node, toplevel=True):
if isinstance(node, (ast.FunctionDef, ast.ClassDef)):
if toplevel and node.name[0].isupper():
producer = False
consumer = False
for decor in node.decorator_list:
if isinstance(decor, ast.Name):
if decor.id == "producer":
producer = True
if decor.id == "consumer":
consumer = True
self.unit.declsyms.add(Symbol(node.name, self.unit, producer, consumer))
self.declnames.add(node.name)
toplevel = False
if isinstance(node, ast.Name) and node.id[0].isupper():
if isinstance(node.ctx, ast.Store):
self.unit.declsyms.add(Symbol(node.id, self.unit))
self.declnames.add(node.id)
if isinstance(node.ctx, ast.Load):
self.unit.refnames.add(node.id)
for child in ast.iter_child_nodes(node):
self.analyze_symbols(child, toplevel)
def evaluate(self, globaldict):
localdict = {
"producer": lambda func: func,
"consumer": lambda func: func,
}
exec(compile(self.ast, self.unit.sourcename, "exec"), globaldict, localdict)
for name, value in localdict.items():
if name in self.declnames:
globaldict[name] = value
class TemplateCodeBlock(CodeBlock):
def __init__(self, source, indent, unit):
super().__init__(source, unit)
self.indent = indent
def evaluate(self, globaldict, outputfile):
lines = []
def _echo(_dict, line, delim=None, newline=True):
lines.append((line.format(**_dict), delim, newline))
globaldict["_echo"] = _echo
localdict = {
"producer": lambda func: func,
"consumer": lambda func: func,
}
super().evaluate(globaldict)
for i, (line, delim, newline) in enumerate(lines):
if i > 0:
line = self.indent + line
if delim and i < len(lines) - 1:
line += delim
if newline and i < len(lines) - 1:
print(line, file=outputfile)
else:
print(line, end="", file=outputfile)
class Symbol:
def __init__(self, name, unit, producer=False, consumer=False):
assert not (producer and consumer)
self.name = name
self.unit = unit
self.producer = producer
self.consumer = consumer
class Data:
def __init__(self):
self.consumers = set()
self.producers = set()
def reverse_topological_sort(vertices, edges):
def get_out_degree(vertex):
value = 0
for head, tail in edges:
if vertex == head:
value += 1
return value
def get_in_edges(vertex):
list = []
for edge in edges:
head, tail = edge
if vertex == tail:
list.append(edge)
return list
queue = []
out_degrees = {}
for vertex in vertices:
out_degree = get_out_degree(vertex)
if out_degree == 0:
queue.append(vertex)
else:
out_degrees[vertex] = out_degree
list = []
while queue:
vertex = queue.pop(0)
list.append(vertex)
for edge in get_in_edges(vertex):
head, tail = edge
out_degrees[head] -= 1
if out_degrees[head] == 0:
queue.append(head)
if len(list) != len(vertices):
raise Exception("\n\t".join(["Cyclic dependencies:"] + find_cycles(vertices, edges)))
return list
def find_cycles(vertices, edges):
cycles = set()
for vertex in vertices:
find_cycle([vertex], vertex, edges, cycles)
return list(sorted(cycles))
def find_cycle(seen, vertex, edges, cycles):
for head, tail in edges:
if head == vertex:
list = seen + [tail]
if tail in seen:
cycles.add(" -> ".join([unit.sourcename for unit in list]))
else:
find_cycle(list, tail, edges, cycles)
def main():
parser = optparse.OptionParser(usage="Usage: %prog [options] FILE...")
parser.add_option("-d", "--outputdir", metavar="DIR", default="", dest="outputdir", help="output directory")
parser.add_option("--post", action="store_const", const=postprocess, default=process, dest="mode", help="adjust postprocessed file")
options, args = parser.parse_args()
project = Project(options.outputdir)
options.mode(project, args)
def process(project, args):
for filename in args:
base = os.path.basename(filename)
if not ("." in base and base.endswith("y")):
print("Bad filename extension:", filename, file=sys.stdout)
sys.exit(1)
project.add(filename, not base.endswith(".py"))
project.process()
def postprocess(project, args):
for filename in args:
project.adjust(filename)
if __name__ == "__main__":
main()