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
|
#!/usr/bin/env python
# vim: ts=2 sw=2
import optparse
import re
import sys
class Dependency:
def __init__(self, tgt):
self.tgt = tgt
self.pos = ""
self.prereqs = set()
self.visit = 0
def add(self, prereq):
self.prereqs.add(prereq)
class Dependencies:
def __init__(self):
self.lines = {}
self.__visit = 0
self.count = 0
def add(self, tgt, prereq):
t = self.lines.get(tgt)
if not t:
t = Dependency(tgt)
self.lines[tgt] = t
p = self.lines.get(prereq)
if not p:
p = Dependency(prereq)
self.lines[prereq] = p
t.add(p)
self.count = self.count + 1
def setPos(self, tgt, pos):
t = self.lines.get(tgt)
if not t:
t = Dependency(tgt)
self.lines[tgt] = t
t.pos = pos
def get(self, tgt):
if self.lines.has_key(tgt):
return self.lines[tgt]
else:
return None
def __iter__(self):
return self.lines.iteritems()
def trace(self, tgt, prereq):
self.__visit = self.__visit + 1
d = self.lines.get(tgt)
if not d:
return
return self.__trace(d, prereq)
def __trace(self, d, prereq):
if d.visit == self.__visit:
return d.trace
if d.tgt == prereq:
return [ [ d ], ]
d.visit = self.__visit
result = []
for pre in d.prereqs:
recursed = self.__trace(pre, prereq)
for r in recursed:
result.append([ d ] + r)
d.trace = result
return result
def help():
print "Commands:"
print " dep TARGET Print the prerequisites for TARGET"
print " trace TARGET PREREQ Print the paths from TARGET to PREREQ"
def main(argv):
opts = optparse.OptionParser()
opts.add_option("-i", "--interactive", action="store_true", dest="interactive",
help="Interactive mode")
(options, args) = opts.parse_args()
deps = Dependencies()
filename = args[0]
print "Reading %s" % filename
if True:
f = open(filename)
for line in f:
line = line.strip()
if len(line) > 0:
if line[0] == '#':
pos,tgt = line.rsplit(":", 1)
pos = pos[1:].strip()
tgt = tgt.strip()
deps.setPos(tgt, pos)
else:
(tgt,prereq) = line.split(':', 1)
tgt = tgt.strip()
prereq = prereq.strip()
deps.add(tgt, prereq)
f.close()
print "Read %d dependencies. %d targets." % (deps.count, len(deps.lines))
while True:
line = raw_input("target> ")
if not line.strip():
continue
split = line.split()
cmd = split[0]
if len(split) == 2 and cmd == "dep":
tgt = split[1]
d = deps.get(tgt)
if d:
for prereq in d.prereqs:
print prereq.tgt
elif len(split) == 3 and cmd == "trace":
tgt = split[1]
prereq = split[2]
if False:
print "from %s to %s" % (tgt, prereq)
trace = deps.trace(tgt, prereq)
if trace:
width = 0
for g in trace:
for t in g:
if len(t.tgt) > width:
width = len(t.tgt)
for g in trace:
for t in g:
if t.pos:
print t.tgt, " " * (width-len(t.tgt)), " #", t.pos
else:
print t.tgt
print
else:
help()
if __name__ == "__main__":
try:
main(sys.argv)
except KeyboardInterrupt:
print
except EOFError:
print
|