@@ -231,6 +231,104 @@ def normalize(s):
231231 s = '""\n "' + lineterm .join (lines ) + '"'
232232 return s
233233
234+
235+
236+ def containsAny (str , set ):
237+ """ Check whether 'str' contains ANY of the chars in 'set'
238+ """
239+ return 1 in [c in str for c in set ]
240+
241+
242+ def _visit_pyfiles (list , dirname , names ):
243+ """ Helper for getFilesForName().
244+ """
245+ # get extension for python source files
246+ if not globals ().has_key ('_py_ext' ):
247+ import imp
248+ global _py_ext
249+ _py_ext = [triple [0 ] for triple in imp .get_suffixes () if triple [2 ] == imp .PY_SOURCE ][0 ]
250+
251+ # don't recurse into CVS directories
252+ if 'CVS' in names :
253+ names .remove ('CVS' )
254+
255+ # add all *.py files to list
256+ list .extend (
257+ [os .path .join (dirname , file )
258+ for file in names
259+ if os .path .splitext (file )[1 ] == _py_ext ])
260+
261+
262+ def _get_modpkg_path (dotted_name , pathlist = None ):
263+ """ Get the filesystem path for a module or a package.
264+
265+ Return the file system path to a file for a module,
266+ and to a directory for a package. Return None if
267+ the name is not found, or is a builtin or extension module.
268+ """
269+ import imp
270+
271+ # split off top-most name
272+ parts = dotted_name .split ('.' , 1 )
273+
274+ if len (parts ) > 1 :
275+ # we have a dotted path, import top-level package
276+ try :
277+ file , pathname , description = imp .find_module (parts [0 ], pathlist )
278+ if file : file .close ()
279+ except ImportError :
280+ return None
281+
282+ # check if it's indeed a package
283+ if description [2 ] == imp .PKG_DIRECTORY :
284+ # recursively handle the remaining name parts
285+ pathname = _get_modpkg_path (parts [1 ], [pathname ])
286+ else :
287+ pathname = None
288+ else :
289+ # plain name
290+ try :
291+ file , pathname , description = imp .find_module (dotted_name , pathlist )
292+ if file : file .close ()
293+ if description [2 ] not in [imp .PY_SOURCE , imp .PKG_DIRECTORY ]:
294+ pathname = None
295+ except ImportError :
296+ pathname = None
297+
298+ return pathname
299+
300+
301+ def getFilesForName (name ):
302+ """ Get a list of module files for a filename, a module or package name,
303+ or a directory.
304+ """
305+ import imp
306+
307+ if not os .path .exists (name ):
308+ # check for glob chars
309+ if containsAny (name , "*?[]" ):
310+ import glob
311+ files = glob .glob (name )
312+ list = []
313+ for file in files :
314+ list .extend (getFilesForName (file ))
315+ return list
316+
317+ # try to find module or package
318+ name = _get_modpkg_path (name )
319+ if not name :
320+ return []
321+
322+ if os .path .isdir (name ):
323+ # find all python files in directory
324+ list = []
325+ os .path .walk (name , _visit_pyfiles , list )
326+ return list
327+ elif os .path .exists (name ):
328+ # a single file
329+ return [name ]
330+
331+ return []
234332
235333
236334class TokenEater :
@@ -417,13 +515,11 @@ class Options:
417515 else :
418516 options .toexclude = []
419517
420- # on win32, do internal globbing
421- if sys .platform == 'win32' :
422- import glob
423- expanded = []
424- for arg in args :
425- expanded .extend (glob .glob (arg ))
426- args = expanded
518+ # resolve args to module lists
519+ expanded = []
520+ for arg in args :
521+ expanded .extend (getFilesForName (arg ))
522+ args = expanded
427523
428524 # slurp through all the files
429525 eater = TokenEater (options )
0 commit comments