1111import logging
1212import os
1313import sys
14+ import subprocess
1415import textwrap
1516from . import __version__
1617
@@ -42,6 +43,17 @@ def _verbose(n, *args, **kwargs):
4243 return _verbose
4344
4445
46+ def _find_current_package ():
47+ startdir = cwd = os .getcwd ()
48+ while 'setup.py' not in os .listdir (cwd ) and cwd != os .path .dirname (cwd ):
49+ cwd = os .path .dirname (cwd )
50+ if 'setup.py' not in os .listdir (cwd ):
51+ raise Exception ("--find-package didn't find setup.py in current, or any parent, directory" )
52+ os .chdir (cwd )
53+ package_name = subprocess .check_output ("python setup.py --name" , shell = True ).decode ('u8' ).strip ()
54+ return package_name
55+
56+
4557def base_argparser (argv = ()):
4658 """Initial parser that can set values for the rest of the parsing process.
4759 """
@@ -56,6 +68,7 @@ def base_argparser(argv=()):
5668 _p .add_argument ('-L' , '--log' , help = textwrap .dedent ('''
5769 set log-level to one of CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET.
5870 ''' ))
71+ _p .add_argument ('--find-package' , action = 'store_true' , help = "tries to automatically find the name of the current package." )
5972 _args , argv = _p .parse_known_args (argv )
6073
6174 if _args .log :
@@ -82,7 +95,7 @@ def parse_args(argv=()):
8295 """Parse command line arguments, and return a dict.
8396 """
8497 _p , _args , argv = base_argparser (argv )
85-
98+ find_package = _args . find_package
8699 config_files = []
87100
88101 if not _args .no_config : # process config files
@@ -102,7 +115,12 @@ def parse_args(argv=()):
102115 config_files .append (home_pydeps )
103116
104117 args = Arguments (config_files , debug = True , parents = [_p ])
105- args .add ('fname' , kind = "FNAME:input" , help = 'filename' )
118+
119+ if not find_package :
120+ args .add ('fname' , kind = "FNAME:input" , help = 'filename' )
121+ else :
122+ args .add ('--fname' , kind = "FNAME:input" , help = 'filename' )
123+
106124 args .add ('-v' , '--verbose' , default = 0 , action = 'count' , help = "be more verbose (-vv, -vvv for more verbosity)" )
107125 args .add ('-o' , default = None , kind = "FNAME:output" , dest = 'output' , metavar = "file" , help = "write output to 'file'" )
108126 args .add ('-T' , default = 'svg' , dest = 'format' , help = "output format (svg|png)" )
@@ -141,7 +159,7 @@ def parse_args(argv=()):
141159 noise_level = 200 , noshow = True , output = None , pylib = False , pylib_all = False ,
142160 show = False , show_cycles = False , show_deps = False , show_dot = False ,
143161 show_raw_deps = False , verbose = 0 , include_missing = True , reverse = False ,
144- start_color = 0
162+ start_color = 0 , find_package = False ,
145163 )
146164
147165 _args .show = True
@@ -158,6 +176,8 @@ def parse_args(argv=()):
158176 _args .max_bacon = sys .maxsize
159177 if _args .keep_target_cluster or _args .min_cluster_size > 0 or _args .max_cluster_size > 0 :
160178 _args .cluster = True
179+ if find_package :
180+ _args .fname = _find_current_package ()
161181
162182 _args .format = getattr (_args , 'T' , getattr (_args , 'format' , None ))
163183
0 commit comments