# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-import sys, getopt, colorsys, imp, re, pprint
+"""Convert `py2depgraph` dependency data to Graphviz dot syntax.
+"""
+
+import sys, colorsys, imp, re, pprint
from hashlib import md5
from os import popen, getuid # for finding C extension dependencies with system calls
from pwd import getpwuid
INVISIBLE_MODS=('__future__','copy','doctest','glob','optparse','os','qt','re',
'StringIO','string','sys','textwrap','time','types','unittest')
-INVISIBLE_PATHS=(r'.*',)
+INVISIBLE_PATHS=[] #(r'.*',)
VISIBLE_PATHS=(r'.*%s.*' % USER,r'.*comedi.*')
def _pathmatch(regexp_tuple, path) :
self.reset()
self._debug=False
- def render(self, root_module='__main__'):
- depgraph,types,paths = self.get_data()
+ def render(self, ifile, root_module='__main__'):
+ depgraph,types,paths = self.get_data(ifile)
if root_module != None :
self.add_module_target(root_module)
f.write(self._dotformat.footer())
# data processing methods (input, output, checking)
- def get_data(self):
- t = eval(sys.stdin.read())
+ def get_data(self, ifile):
+ t = eval(ifile.read())
return t['depgraph'],t['types'],t['paths']
def get_output_file(self):
return sys.stdout
return None # no more modules! we're done.
-def main():
- opts,args = getopt.getopt(sys.argv,'',['mono'])
- colored = True
- for o,v in opts:
- if o=='--mono':
- colored = False
+if __name__=='__main__':
+ from optparse import OptionParser
+
+ usage = '%prog [options]'
+ p = OptionParser(usage=usage, description=__doc__)
+ p.add_option('-m', '--mono', dest='color', default=True,
+ action='store_false', help="Don't use color.")
+ p.add_option('-i', '--input', dest='input', default='-',
+ help="Path to input file ('-' for stdin, %default).")
+ options,args = p.parse_args()
# Fancyness with shared hooks instance so we can do slick thinks like
# printing all modules just inside an invisible zone, since we'll need
# the dotformatter to know which nodes are visible.
hk = hooks(link_outside_visited_nodes=False)
#hk._debug = True
- dt = dotformat_Cext(colored=colored, hooks_instance=hk)
+ dt = dotformat_Cext(colored=options.color, hooks_instance=hk)
py = pydepgraphdot(hooks_instance=hk, dotformat_instance=dt)
#py._debug = True
- py.render()
-if __name__=='__main__':
- main()
+ if options.input == '-':
+ ifile = sys.stdin
+ else:
+ ifile = open(options.input, 'r')
+
+ try:
+ py.render(ifile)
+ finally:
+ if options.input != '-':
+ ifile.close()
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-import sys, pprint
+"""Extract a tree of module imports from a Python script.
+"""
+
import modulefinder
class mymf(modulefinder.ModuleFinder):
return r
-def main(argv):
+if __name__=='__main__':
+ from optparse import OptionParser
+ from pprint import pprint
+ import sys
+
+ usage = '%prog [options] path/to/script.py'
+ p = OptionParser(usage=usage, description=__doc__)
+
+ options,args = p.parse_args()
+
+ script = args[0]
+
path = sys.path[:]
debug = 0
exclude = []
mf = mymf(path,debug,exclude)
- mf.run_script(argv[0])
- pprint.pprint({'depgraph':mf._depgraph,'types':mf._types,'paths':mf._paths})
-
-if __name__=='__main__':
- main(sys.argv[1:])
+ mf.run_script(script)
+ pprint({'depgraph':mf._depgraph,'types':mf._types,'paths':mf._paths})