-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpyDeps.cmd
223 lines (169 loc) · 5.79 KB
/
pyDeps.cmd
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
@setlocal enableextensions & python -x "%~f0" %* & goto :EOF
try:
import wingdbstub
except: pass
import os
import sys
import dependencies
from consoleChroma import Warn, Error, Good, setConsoleColour, BRIGHT, FG_WHITE, FG_YELLOW
from filesystem import Path
HELP_ARGS = '-h', '-help', '/h', '/help', '/?'
UPDATE_ARGS = '/u', '/update'
REBUILD_ARGS = '/f', '/force', '/rebuild'
PRINT_TREE_ARGS = '/t', '/tree'
PRINT_DEPENDENTS = '/d', '/dependents'
PRINT_DEPENDENCIES = '/i', '/imports'
PRINT_TESTS_ARGS = '/s', '/tests'
PACKAGE_SCRIPTS = '/p', '/package'
def printHelp():
print """Prints a list of scripts dependent on the given script/s. ie: prints
the scripts that directly import the one/s specified on the cmd-line
USAGE: pydeps <flags> someScript.py
to update the dependency cache: %s
to rebuild the dependency cache: %s
to print the dependency tree: %s
prints dependents (scripts that import this one) [depth, default 0]: %s
prints import dependencies [depth, default 1]: %s
prints out the tests that exercise the given scripts %s
packages up the given scripts and all import dependencies into a zip %s
""" % (' '.join( UPDATE_ARGS ), ' '.join( REBUILD_ARGS ), ' '.join( PRINT_TREE_ARGS ), ' '.join( PRINT_DEPENDENTS ), ' '.join( PRINT_DEPENDENCIES ), ' '.join( PRINT_TESTS_ARGS ), ' '.join( PACKAGE_SCRIPTS ))
def logWarning( *a ):
print >> Warn, ' '.join( map( str, a ) )
def logHighlight( *a ):
print >> Good, ' '.join( map( str, a ) )
def logError( *a ):
print >> Error, ' '.join( map( str, a ) )
#insert the log warning function above into the dependencies namespace so warnings get logged in the appropriate colour
dependencies.logWarning = logWarning
def main():
args = sys.argv[ 1: ] #first arg is always this script
if not args:
printHelp()
return
for arg in HELP_ARGS:
if arg in args:
printHelp()
args.remove( arg )
updateCache = False
for arg in UPDATE_ARGS:
if arg in args:
updateCache = True
args.remove( arg )
rebuild = False
for arg in REBUILD_ARGS:
if arg in args:
rebuild = True
args.remove( arg )
printDependents = False
printDependentsDepth = 0
for arg in PRINT_DEPENDENTS:
if arg in args:
idx = args.index( arg )
if args[ idx+1 ].isdigit():
printDependentsDepth = int( args[ idx+1 ] )
if printDependentsDepth < 1:
printDependentsDepth = 0
args.pop( idx+1 )
printDependents = True
args.remove( arg )
printDependencies = False
printDependenciesDepth = 1
for arg in PRINT_DEPENDENCIES:
if arg in args:
idx = args.index( arg )
if args[ idx+1 ].isdigit():
printDependenciesDepth = int( args[ idx+1 ] )
if printDependenciesDepth < 1:
printDependenciesDepth = None
args.pop( idx+1 )
printDependencies = True
args.remove( arg )
printTree = False
printTreeDepth = None
for arg in PRINT_TREE_ARGS:
if arg in args:
idx = args.index( arg )
if args[ idx+1 ].isdigit():
printTreeDepth = int( args[ idx+1 ] )
if printTreeDepth < 1:
printTreeDepth = None
args.pop( idx+1 )
printTree = True
args.remove( arg )
printTests = False
for arg in PRINT_TESTS_ARGS:
if arg in args:
printTests = True
args.remove( arg )
package = False
for arg in PACKAGE_SCRIPTS:
if arg in args:
package = True
args.remove( arg )
files = []
for f in args:
if not os.path.isabs( f ):
f = os.path.abspath( f )
if not os.path.exists( f ):
print >> Warn, 'FYI: %s cannot be found on disk. Dependency query can still be performed, but you should know' % f
files.append( f )
depTree = None
if not files:
if updateCache:
depTree = dependencies.generateDepTree( rebuildCache=rebuild )
return
if depTree is None:
depTree = dependencies.generateDepTree( rebuildCache=rebuild )
for n, f in enumerate( files ):
if printTree:
logHighlight( '-- DEPENDENCY TREE (DEPTH %s) FOR %s' % (printTreeDepth if printTreeDepth else 'deep', f) )
dependencies.printDepTree( f, depTree, printTreeDepth )
print
if printDependents:
pf, sf = depTree.findDependents( f )
deps = pf
logHighlight( '-- %d DEPENDENTS THAT IMMEDIATELY IMPORT %s' % (len( deps ), f) )
for ff in sorted( deps ):
print ff
if printDependentsDepth > 0:
deps = sf.difference( pf )
logHighlight( '-- %d ADDITIONAL DEPENDENTS THAT INDIRECTLY IMPORT %s' % (len( deps ), f) )
for ff in sorted( deps ):
print ff
print
if printDependencies:
deps = depTree.findDependencies( f, printDependenciesDepth, False )
if deps:
logHighlight( '-- %d IMPORT DEPENDENCIES FOR %s' % (len( deps ), f) )
for ff in deps:
print ff
else:
logHighlight( '-- NO IMPORT DEPENDENCIES FOR %s' % f )
print
if printTests:
tests = dependencies.getScriptTests( f, depTree )
if not tests:
logHighlight( '-- THERE ARE NO TESTS FOR %s' % f )
else:
logHighlight( '-- THE FOLLOWING TESTS PROBABLY EXERCISE %s' % f )
for ff in tests:
print ff
print
setConsoleColour( BRIGHT | FG_YELLOW )
pf, sf = depTree.findDependents( f )
setConsoleColour( FG_WHITE )
if n:
logHighlight( '---------------------' )
print
if package:
packageFilepath = dependencies.packageScripts( files[:], files[0], depTree )
if packageFilepath is None:
logWarning( '-- NO IMPORT DEPENDENCIES - no package file was written' )
else:
logHighlight( '-- PACKAGE WRITTEN TO %s' % packageFilepath )
try:
main()
except KeyboardInterrupt:
logWarning( "Aborted by user" )
sys.exit( 0 )
#end