From 919a27a22788755c63a7460ec5a97b936e177755 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 8 Jan 2011 22:10:53 -0500 Subject: [PATCH] Use optparse.OptionParser to parse script arguments. --- depgraph2dot.py | 46 ++++++++++++++++++++++++++++++---------------- py2depgraph.py | 24 +++++++++++++++++------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/depgraph2dot.py b/depgraph2dot.py index 23ad7f3..c347947 100755 --- a/depgraph2dot.py +++ b/depgraph2dot.py @@ -22,7 +22,10 @@ # 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 @@ -31,7 +34,7 @@ USER=getpwuid(getuid())[0] # get effective user name 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) : @@ -315,8 +318,8 @@ class pydepgraphdot (object) : 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) @@ -362,8 +365,8 @@ class pydepgraphdot (object) : 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 @@ -405,22 +408,33 @@ class pydepgraphdot (object) : 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() diff --git a/py2depgraph.py b/py2depgraph.py index cb2bc86..b17c4dd 100755 --- a/py2depgraph.py +++ b/py2depgraph.py @@ -22,7 +22,9 @@ # 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): @@ -55,13 +57,21 @@ 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}) -- 2.26.2