From 0555178fec5f15489e7641e0ef5657db69611649 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 2 Dec 2015 08:27:38 -0800 Subject: [PATCH] Accept multiple files on command line. Fixes issue #935. --- mypy/build.py | 10 +++++++++- mypy/main.py | 30 ++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 3dc0dddd5b20..c9bb7d22c149 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -269,7 +269,7 @@ def default_lib_path(data_dir: str, pyversion: Tuple[int, int], def lookup_program(module: str, lib_path: List[str]) -> str: - # Modules are .py and not .pyi + # Modules are .py or .pyi path = find_module(module, lib_path) if path: return path @@ -544,12 +544,20 @@ def log(self, message: str) -> None: def remove_cwd_prefix_from_path(p: str) -> str: """Remove current working directory prefix from p, if present. + Also crawl up until a directory without __init__.py is found. + If the result would be empty, return '.' instead. """ cur = os.getcwd() # Add separator to the end of the path, unless one is already present. if basename(cur) != '': cur += os.sep + # Compute root path. + while p and os.path.isfile(os.path.join(p, '__init__.py')): + dir, base = os.path.split(p) + if not base: + break + p = dir # Remove current directory prefix from the path, if present. if p.startswith(cur): p = p[len(cur):] diff --git a/mypy/main.py b/mypy/main.py index 45d209088108..99b7f5bb0b4a 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -99,6 +99,7 @@ def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]: module to run as script (or None), parsed flags) """ + # TODO: Rewrite using argparse. options = Options() help = False ver = False @@ -171,14 +172,30 @@ def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]: if not args: usage('Missing target file or module') - if args[1:]: - usage('Extra argument: {}'.format(args[1])) - if options.python_path and options.pyversion[0] == 2: usage('Python version 2 (or --py2) specified, ' 'but --use-python-path will search in sys.path of Python 3') - return [BuildSource(args[0], None, None)], options + return [BuildSource(arg, file_to_mod(arg), None) for arg in args], options + + +def file_to_mod(arg: str) -> str: + """Convert a .py filename to a module name. + + We crawl up the path until we find a directory without __init__.py. + """ + if not arg.endswith('.py'): + return '__main__' # Special case for entry scripts. + dir, mod = os.path.split(arg) + if mod.endswith('.py'): + mod = mod[:-3] + assert '.' not in mod + while dir and os.path.isfile(os.path.join(dir, '__init__.py')): + dir, base = os.path.split(dir) + if not base: + break + mod = base + '.' + mod + return mod # Don't generate this from mypy.reports, not all are meant to be public. @@ -200,15 +217,16 @@ def is_report(arg: str) -> bool: def usage(msg: str = None) -> None: + # TODO: Add other supported options (--package, -f/--dirty-stubs, ...) if msg: sys.stderr.write('%s\n' % msg) sys.stderr.write("""\ -usage: mypy [option ...] [-c cmd | -m mod | file] +usage: mypy [option ...] [-c cmd | -m mod | file ...] Try 'mypy -h' for more information. """) else: sys.stderr.write("""\ -usage: mypy [option ...] [-m mod | file] +usage: mypy [option ...] [-c cmd | -m mod | file ...] Optional arguments: -h, --help print this help message and exit