2016-04-07 07:22:04 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
2016-04-27 09:28:20 +02:00
|
|
|
from os.path import dirname, abspath
|
2016-04-07 07:22:04 +02:00
|
|
|
import sys
|
|
|
|
import lister
|
|
|
|
import argparse
|
|
|
|
import subprocess
|
2016-04-27 09:28:20 +02:00
|
|
|
import six
|
2016-04-07 07:22:04 +02:00
|
|
|
|
2016-05-24 15:42:55 +02:00
|
|
|
exclude = """
|
2016-04-07 07:22:04 +02:00
|
|
|
api/integrations/
|
|
|
|
bots/jabber_mirror_backend.py
|
|
|
|
bots/zephyr_mirror_backend.py
|
|
|
|
tools/deprecated/
|
2016-05-17 17:31:39 +02:00
|
|
|
zproject/settings.py
|
|
|
|
zproject/test_settings.py
|
2016-04-07 07:22:04 +02:00
|
|
|
zerver/migrations/
|
2016-06-03 07:53:28 +02:00
|
|
|
zerver/tests/test_bugdown.py
|
|
|
|
zerver/tests/test_email_mirror.py
|
|
|
|
zerver/tests/test_decorators.py
|
|
|
|
zerver/tests/test_upload.py
|
|
|
|
zerver/tests/test_messages.py
|
2016-06-21 21:05:44 +02:00
|
|
|
zerver/tests/test_narrow.py
|
2016-04-07 07:22:04 +02:00
|
|
|
""".split()
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")
|
|
|
|
parser.add_argument('targets', nargs='*', default=[],
|
|
|
|
help="""files and directories to include in the result.
|
|
|
|
If this is not specified, the current directory is used""")
|
|
|
|
parser.add_argument('-m', '--modified', action='store_true', default=False, help='list only modified files')
|
|
|
|
parser.add_argument('-a', '--all', dest='all', action='store_true', default=False,
|
|
|
|
help="""run mypy on all python files, ignoring the exclude list.
|
|
|
|
This is useful if you have to find out which files fail mypy check.""")
|
2016-06-04 21:03:14 +02:00
|
|
|
parser.add_argument('--linecoverage-report', dest='linecoverage_report', action='store_true', default=False,
|
|
|
|
help="""run the linecoverage report to see annotation coverage""")
|
2016-06-02 21:46:24 +02:00
|
|
|
parser.add_argument('--disallow-untyped-defs', dest='disallow_untyped_defs', action='store_true', default=False,
|
|
|
|
help="""throw errors when functions are not annotated""")
|
2016-04-07 07:22:04 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
if args.all:
|
|
|
|
exclude = []
|
|
|
|
|
2016-05-24 15:42:55 +02:00
|
|
|
# find all non-excluded files in current directory
|
2016-06-15 14:50:25 +02:00
|
|
|
files_dict = lister.list_files(targets=args.targets, ftypes=['py', 'pyi'], use_shebang=False,
|
|
|
|
modified_only=args.modified, exclude = exclude + ['stubs'],
|
|
|
|
group_by_ftype=True)
|
|
|
|
pyi_files = set(files_dict['pyi'])
|
|
|
|
python_files = [fpath for fpath in files_dict['py']
|
|
|
|
if fpath.endswith('.py') and fpath + 'i' not in pyi_files]
|
2016-04-07 07:22:04 +02:00
|
|
|
|
2016-04-27 09:28:20 +02:00
|
|
|
# Use zulip-py3-venv's mypy if it's available and we're on python 2
|
|
|
|
PY3_VENV_DIR = "/srv/zulip-py3-venv"
|
|
|
|
MYPY_VENV_PATH = os.path.join(PY3_VENV_DIR, "bin", "mypy")
|
|
|
|
if six.PY2 and os.path.exists(MYPY_VENV_PATH):
|
|
|
|
mypy_command = MYPY_VENV_PATH
|
|
|
|
print("Using mypy from", mypy_command)
|
|
|
|
else:
|
|
|
|
mypy_command = "mypy"
|
|
|
|
|
2016-06-02 21:46:24 +02:00
|
|
|
extra_args = ["--fast-parser", "--silent-imports", "--py2", "--check-untyped-defs"]
|
2016-06-04 21:03:14 +02:00
|
|
|
if args.linecoverage_report:
|
|
|
|
extra_args.append("--linecoverage-report")
|
|
|
|
extra_args.append("linecoverage-report")
|
2016-06-02 21:46:24 +02:00
|
|
|
if args.disallow_untyped_defs:
|
|
|
|
extra_args.append("--disallow-untyped-defs")
|
|
|
|
|
|
|
|
|
2016-05-24 15:42:55 +02:00
|
|
|
# run mypy
|
2016-04-07 07:22:04 +02:00
|
|
|
if python_files:
|
2016-06-02 21:46:24 +02:00
|
|
|
rc = subprocess.call([mypy_command] + extra_args + python_files)
|
2016-06-04 21:03:14 +02:00
|
|
|
if args.linecoverage_report:
|
|
|
|
# Move the coverage report to where coveralls will look for it.
|
|
|
|
try:
|
|
|
|
os.rename('linecoverage-report/coverage.txt', '.coverage')
|
|
|
|
except OSError:
|
|
|
|
# maybe mypy crashed; exit with its error code
|
|
|
|
pass
|
2016-04-07 07:22:04 +02:00
|
|
|
sys.exit(rc)
|
|
|
|
else:
|
|
|
|
print("There are no files to run mypy on.")
|