1 | #!/usr/bin/env python |
---|
2 | #vim:set expandtab: |
---|
3 | |
---|
4 | import os |
---|
5 | import sys |
---|
6 | #import copy.deepcopy |
---|
7 | |
---|
8 | class Color: |
---|
9 | """ Ansi colors """ |
---|
10 | black="\033[0;30m\]" |
---|
11 | red="\033[0;31m\]" |
---|
12 | green="\033[0;32m\]" |
---|
13 | brown="\033[0;33m\]" |
---|
14 | blue="\033[0;34m\]" |
---|
15 | purple="\033[0;35m\]" |
---|
16 | cyan="\033[0;36m\]" |
---|
17 | lightGray="\033[0;37m\]" |
---|
18 | |
---|
19 | darkGray="\033[1;30m\]" |
---|
20 | lightRed="\033[1;31m\]" |
---|
21 | lightGreen="\033[1;32m\]" |
---|
22 | yellow="\033[1;33m\]" |
---|
23 | lightBlue="\033[1;34m\]" |
---|
24 | lightPurple="\033[1;35m\]" |
---|
25 | lightCyan="\033[1;36m\]" |
---|
26 | white="\033[1;37m\]" |
---|
27 | |
---|
28 | default="\033[1;0m\]" |
---|
29 | |
---|
30 | class Port: |
---|
31 | """ Collect info from port """ |
---|
32 | _name = "" |
---|
33 | _variants = "" |
---|
34 | _installed = False |
---|
35 | |
---|
36 | _buildDeps = [] |
---|
37 | _runDeps = [] |
---|
38 | _fetchDeps = [] |
---|
39 | _libDeps = [] |
---|
40 | _extractDeps = [] |
---|
41 | _allDeps = [] |
---|
42 | |
---|
43 | def __init__(self, portName): |
---|
44 | self._name=portName |
---|
45 | |
---|
46 | def name(self): |
---|
47 | return self._name |
---|
48 | |
---|
49 | def variants(self): |
---|
50 | return self._variants |
---|
51 | def setVariants(self, x): |
---|
52 | self._variants = x |
---|
53 | self._allDeps += x |
---|
54 | |
---|
55 | def buildDeps(self): |
---|
56 | return self._buildDeps |
---|
57 | def setBuildDeps(self, x): |
---|
58 | self._buildDeps = x |
---|
59 | self._allDeps += x |
---|
60 | |
---|
61 | def runDeps(self): |
---|
62 | return self._runDeps |
---|
63 | def setRunDeps(self, x): |
---|
64 | self._runDeps = x |
---|
65 | self._allDeps += x |
---|
66 | |
---|
67 | def fetchDeps(self): |
---|
68 | return self._fetchDeps |
---|
69 | def setFetchDeps(self, x): |
---|
70 | self._fetchDeps = x |
---|
71 | self._allDeps += x |
---|
72 | |
---|
73 | def libDeps(self): |
---|
74 | return self._libDeps |
---|
75 | def setLibDeps(self, x): |
---|
76 | self._libDeps = x |
---|
77 | self._allDeps += x |
---|
78 | |
---|
79 | def extractDeps(self): |
---|
80 | return self._extractDeps |
---|
81 | def setExtractDeps(self, x): |
---|
82 | self._extractDeps = x |
---|
83 | self._allDeps += x |
---|
84 | |
---|
85 | def allDeps(self): |
---|
86 | return self._allDeps |
---|
87 | |
---|
88 | def isInstalled(self): |
---|
89 | |
---|
90 | return self._installed |
---|
91 | def setInstalled(self, b=True): |
---|
92 | self._installed = b |
---|
93 | |
---|
94 | def parseInfoLine(portInfoLine): |
---|
95 | return portInfoLine.partition(":")[2].strip().split(',') |
---|
96 | |
---|
97 | def getInfo(portName, allPortsStatic = []): |
---|
98 | """ Store results from 'port info' and 'port installed' in Port obj """ |
---|
99 | # Avoid calling port(1) multiple times for same port |
---|
100 | port = next((p for p in allPortsStatic if p.name() == portName), None) |
---|
101 | if port is not None: |
---|
102 | return port |
---|
103 | # else... |
---|
104 | cmd = os.popen("port info " + portName) |
---|
105 | retCmd = cmd.readlines() |
---|
106 | port = Port(portName) |
---|
107 | for line in retCmd: |
---|
108 | if line.startswith("Variants:"): |
---|
109 | port.setVariants(parseInfoLine(line)) |
---|
110 | elif line.startswith("Build Dependencies:"): |
---|
111 | port.setBuildDeps(parseInfoLine(line)) |
---|
112 | elif line.startswith("Runtime Dependencies:"): |
---|
113 | port.setRunDeps(parseInfoLine(line)) |
---|
114 | elif line.startswith("Fetch Dependencies:"): |
---|
115 | port.setFetchDeps(parseInfoLine(line)) |
---|
116 | elif line.startswith("Library Dependencies:"): |
---|
117 | port.setLibDeps(parseInfoLine(line)) |
---|
118 | elif line.startswith("Extract Dependencies:"): |
---|
119 | port.setExtractDeps(parseInfoLine(line)) |
---|
120 | cmd = os.popen("port installed " + portName) |
---|
121 | retCmd = cmd.readlines() |
---|
122 | for line in retCmd: |
---|
123 | if line.startswith("The following ports"): |
---|
124 | port.setInstalled() |
---|
125 | break |
---|
126 | allPortsStatic.append(port) |
---|
127 | return port |
---|
128 | |
---|
129 | def printOutput(port, depth): |
---|
130 | """ Format and display output """ |
---|
131 | line = depth*" " + port.name() |
---|
132 | if port.isInstalled(): |
---|
133 | line += "*" |
---|
134 | print line |
---|
135 | |
---|
136 | def makeTree(portName, n=0): |
---|
137 | """ Build dependency tree """ |
---|
138 | port = getInfo(portName) |
---|
139 | printOutput(port, n) |
---|
140 | for dep in port.libDeps(): |
---|
141 | if dep: |
---|
142 | makeTree(dep, n+1) |
---|
143 | |
---|
144 | def main(argv=None): |
---|
145 | #try: |
---|
146 | arg = sys.argv[1] |
---|
147 | makeTree(arg) |
---|
148 | #except: |
---|
149 | # sys.exit(2) |
---|
150 | |
---|
151 | if __name__ == "__main__": |
---|
152 | main() |
---|
153 | |
---|