author | Sverre Rabbelier <srabbelier@gmail.com> |
Sat, 29 Nov 2008 19:44:48 +0000 | |
changeset 613 | 4880ffa9f3ba |
parent 600 | f6abfcbff3a4 |
permissions | -rwxr-xr-x |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
600
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
2 |
# |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
4 |
# |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
7 |
# You may obtain a copy of the License at |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
8 |
# |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
10 |
# |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
15 |
# limitations under the License. |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
16 |
|
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
17 |
"""TODO(SRabbelier) Update __doc__ string |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
18 |
""" |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
19 |
|
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
20 |
__authors__ = [ |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
21 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
22 |
] |
f6abfcbff3a4
Apache license and __author__ added. Also added a todo for the __doc__ string.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
599
diff
changeset
|
23 |
|
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
import sys |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
import cPickle |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
import os |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
import graph |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
|
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
31 |
|
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
32 |
ROOTDIR = "soc" |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
33 |
|
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
34 |
|
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
def parseFile(file_name): |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
36 |
if os.path.exists("imports.dat"): |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
37 |
log = open("imports.dat", "r") |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
38 |
all_imports = cPickle.load(log) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
39 |
log.close() |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
40 |
else: |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
all_imports = {} |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
if file_name in all_imports: |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
print "Overwriting previous data on '%s'." % file_name |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
imports = [] |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
file = open(file_name) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
for line in file: |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
if line.lstrip().startswith('import soc'): |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
splitline = line[:-1].split(' ') |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
mod = splitline[1] |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
imports.append(mod) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
if line.lstrip().startswith('from soc'): |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
splitline = line[:-1].split(' ') |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
mod = splitline[1] + '.' + splitline[3] |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
imports.append(mod) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
61 |
for idx, imp in enumerate(imports): |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
62 |
if imp in set(imports[idx+1:]): |
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
63 |
sys.stderr.write("Warning: file '%s' has a redundant import: '%s'.\n" % (file_name, imp)) |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
64 |
|
597
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
65 |
if file_name.endswith("__init__.py"): |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
66 |
normalized = file_name[:-12].replace('/', '.') |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
67 |
else: |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
68 |
normalized = file_name[:-3].replace('/', '.') |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
70 |
print "Writing imports for file %s (%s)." % (file_name, normalized) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
all_imports[normalized] = imports |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
log = open("imports.dat", "w") |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
cPickle.dump(all_imports, log) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
log.close() |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
return 0 |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
79 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
80 |
def buildGraph(): |
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
81 |
if not os.path.exists("imports.dat"): |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
82 |
sys.stderr.write("Missing imports.dat file, run 'build' first\n") |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
83 |
return |
597
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
84 |
|
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
log = open("imports.dat", "r") |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
all_imports = cPickle.load(log) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
|
597
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
88 |
gr = graph.digraph() |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
89 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
90 |
gr.add_nodes(all_imports.keys()) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
91 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
for file_name, imports in all_imports.iteritems(): |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
for imp in imports: |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
gr.add_edge(file_name, imp) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
95 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
96 |
return gr |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
97 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
98 |
|
597
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
99 |
def showGraph(gr): |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
100 |
for node in gr: |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
101 |
print "%s: " % node |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
102 |
for edge in gr[node]: |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
103 |
print "\t%s" % edge |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
104 |
|
597
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
105 |
return 0 |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
106 |
|
597
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
107 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
108 |
def getParents(gst, target): |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
109 |
parents = [] |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
110 |
current = target |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
111 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
112 |
while True: |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
113 |
for node in gst: |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
114 |
if current in gst[node]: |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
115 |
parents.append(node) |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
116 |
current = node |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
117 |
break |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
118 |
else: |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
119 |
break |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
120 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
121 |
return parents |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
122 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
123 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
124 |
def pathFrom(parents, first, target): |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
125 |
idx = parents.index(first) |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
126 |
path = parents[idx::-1] |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
127 |
return [target] + path + [target] |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
128 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
129 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
130 |
def findCycle(gr, gst, target): |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
131 |
parents = getParents(gst, target) |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
132 |
cycles = [] |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
133 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
134 |
for node in gr[target]: |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
135 |
if node in parents: |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
136 |
cycles.append(pathFrom(parents, node, target)) |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
137 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
138 |
return cycles |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
139 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
140 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
141 |
def findCycles(gr): |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
142 |
st, pre, post = gr.depth_first_search() |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
143 |
gst = graph.digraph() |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
144 |
gst.add_spanning_tree(st) |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
145 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
146 |
cycles = [] |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
147 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
148 |
for node in gr: |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
149 |
cycles += findCycle(gr, gst, node) |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
150 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
151 |
return cycles |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
152 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
153 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
154 |
def drawGraph(gr): |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
155 |
st, pre, post = gr.depth_first_search() |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
156 |
gst = graph.digraph() |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
157 |
gst.add_spanning_tree(st) |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
158 |
|
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
159 |
sys.path.append('/usr/lib/graphviz/python/') |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
160 |
import gv |
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
161 |
dot = gst.write(fmt='dot') |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
162 |
gvv = gv.readstring(dot) |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
163 |
gv.layout(gvv,'dot') |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
164 |
gv.render(gvv,'png','imports.png') |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
165 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
166 |
|
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
167 |
def accumulate(arg, dirname, fnames): |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
168 |
for fname in fnames: |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
169 |
if not fname.endswith(".py"): |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
170 |
continue |
597
66088092f849
Proper working implementation of a cycle detection algorithm, that
Sverre Rabbelier <srabbelier@gmail.com>
parents:
595
diff
changeset
|
171 |
|
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
172 |
arg.append(os.path.join(dirname, fname)) |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
173 |
|
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
174 |
|
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
175 |
def main(args): |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
176 |
if len(args) != 1: |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
177 |
print "Supported options: 'print', 'build', 'find', and 'draw'." |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
178 |
return -1 |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
179 |
|
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
180 |
action = args[0] |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
181 |
|
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
182 |
if action == "build": |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
183 |
fnames = [] |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
184 |
os.path.walk(ROOTDIR, accumulate, fnames) |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
185 |
|
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
186 |
for fname in fnames: |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
187 |
parseFile(fname) |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
188 |
|
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
189 |
print "Done parsing." |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
190 |
return 0 |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
191 |
|
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
192 |
gr = buildGraph() |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
193 |
if not gr: |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
194 |
return 1 |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
195 |
|
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
196 |
if action == "show": |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
197 |
return showGraph(gr) |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
198 |
|
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
199 |
if action == "draw": |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
200 |
return drawGraph(gr) |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
201 |
|
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
202 |
if action == "find": |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
203 |
cycles = findCycles(gr) |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
204 |
for cycle in cycles: |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
205 |
print cycle |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
206 |
return 0 |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
207 |
|
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
208 |
print "Unknown option." |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
209 |
return -1 |
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
210 |
|
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
211 |
|
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
212 |
if __name__ == '__main__': |
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
213 |
import sys |
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
214 |
os.chdir("../app") |
595
3c4d5b7d4391
Added rudementry include checker
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
215 |
res = main(sys.argv[1:]) |
598
c1f9435bb645
Change command parsing and do file walking manually
Sverre Rabbelier <srabbelier@gmail.com>
parents:
597
diff
changeset
|
216 |
sys.exit(0) |