py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-04-07 07:22:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import subprocess
|
|
|
|
|
2017-02-05 21:24:28 +01:00
|
|
|
import lister
|
2016-07-24 09:38:08 +02:00
|
|
|
from typing import cast, Dict, List
|
|
|
|
|
2016-07-12 19:27:13 +02:00
|
|
|
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
os.chdir(os.path.dirname(TOOLS_DIR))
|
|
|
|
|
2017-08-06 01:16:07 +02:00
|
|
|
sys.path.append(os.path.dirname(TOOLS_DIR))
|
|
|
|
from lib.test_script import get_provisioning_status
|
|
|
|
|
2017-08-09 02:17:30 +02:00
|
|
|
exclude = """
|
2018-01-23 23:27:14 +01:00
|
|
|
docs/conf.py
|
2016-05-17 17:31:39 +02:00
|
|
|
zproject/settings.py
|
|
|
|
zproject/test_settings.py
|
2016-04-07 07:22:04 +02:00
|
|
|
""".split()
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")
|
2017-09-26 23:10:20 +02:00
|
|
|
parser.add_argument('targets', nargs='*',
|
2017-09-26 23:28:10 +02:00
|
|
|
help="files and directories to check (default: .)")
|
2017-09-26 23:33:21 +02:00
|
|
|
parser.add_argument('--version', action='store_true',
|
2017-09-26 23:38:56 +02:00
|
|
|
help="show mypy version information and exit")
|
2017-09-26 23:30:05 +02:00
|
|
|
parser.add_argument('--quick', action='store_true',
|
|
|
|
help="pass --quick to mypy")
|
2017-09-26 23:10:20 +02:00
|
|
|
parser.add_argument('-m', '--modified', action='store_true',
|
2017-09-26 23:28:10 +02:00
|
|
|
help="check only modified files")
|
2017-09-26 23:30:05 +02:00
|
|
|
parser.add_argument('--scripts-only', action='store_true',
|
|
|
|
help="only check extensionless python scripts")
|
2017-09-26 23:10:20 +02:00
|
|
|
parser.add_argument('-a', '--all', action='store_true',
|
2017-09-26 23:28:10 +02:00
|
|
|
help="check all files, bypassing the default exclude list")
|
2017-09-26 23:30:05 +02:00
|
|
|
parser.add_argument('--force', action="store_true",
|
|
|
|
help="run tests despite possible provisioning problems")
|
2017-09-26 23:10:20 +02:00
|
|
|
parser.add_argument('--linecoverage-report', action='store_true',
|
2017-09-26 23:28:10 +02:00
|
|
|
help="emit a coverage report under var/")
|
2017-09-26 23:10:20 +02:00
|
|
|
parser.add_argument('--no-disallow-untyped-defs',
|
|
|
|
dest='disallow_untyped_defs', action='store_false',
|
2017-09-26 23:28:10 +02:00
|
|
|
help="don't pass --disallow-untyped-defs to mypy")
|
2017-09-26 23:10:20 +02:00
|
|
|
parser.add_argument('--warn-unused-ignores', action='store_true',
|
2017-09-26 23:28:10 +02:00
|
|
|
help="pass --warn-unused-ignores to mypy")
|
2017-09-26 23:10:20 +02:00
|
|
|
parser.add_argument('--no-ignore-missing-imports',
|
|
|
|
dest='ignore_missing_imports', action='store_false',
|
2017-09-26 23:28:10 +02:00
|
|
|
help="don't pass --ignore-missing-imports to mypy")
|
2016-04-07 07:22:04 +02:00
|
|
|
args = parser.parse_args()
|
2016-07-01 18:38:57 +02:00
|
|
|
|
2017-08-06 01:16:07 +02:00
|
|
|
if not args.force:
|
|
|
|
ok, msg = get_provisioning_status()
|
|
|
|
if not ok:
|
|
|
|
print(msg)
|
|
|
|
print('If you really know what you are doing, use --force to run anyway.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-09-26 23:33:21 +02:00
|
|
|
# Use zulip-py3-venv's mypy if it's available.
|
|
|
|
VENV_DIR = "/srv/zulip-py3-venv"
|
|
|
|
MYPY_VENV_PATH = os.path.join(VENV_DIR, "bin", "mypy")
|
|
|
|
if os.path.exists(MYPY_VENV_PATH):
|
|
|
|
mypy_command = MYPY_VENV_PATH
|
|
|
|
else:
|
|
|
|
mypy_command = "mypy"
|
|
|
|
|
|
|
|
if args.version:
|
2017-09-26 23:38:56 +02:00
|
|
|
print("mypy command:", mypy_command)
|
2017-09-26 23:33:21 +02:00
|
|
|
sys.exit(subprocess.call([mypy_command, "--version"]))
|
|
|
|
|
2016-04-07 07:22:04 +02:00
|
|
|
if args.all:
|
2017-08-09 02:17:30 +02:00
|
|
|
exclude = []
|
2016-04-07 07:22:04 +02:00
|
|
|
|
2016-05-24 15:42:55 +02:00
|
|
|
# find all non-excluded files in current directory
|
2016-07-24 09:38:08 +02:00
|
|
|
files_dict = cast(Dict[str, List[str]],
|
|
|
|
lister.list_files(targets=args.targets, ftypes=['py', 'pyi'],
|
2016-10-16 07:48:08 +02:00
|
|
|
use_shebang=True, modified_only=args.modified,
|
2017-09-26 23:17:58 +02:00
|
|
|
exclude=exclude, group_by_ftype=True,
|
2016-07-24 09:38:08 +02:00
|
|
|
extless_only=args.scripts_only))
|
2018-03-13 21:00:49 +01:00
|
|
|
pyi_files = list(files_dict['pyi'])
|
2016-06-15 14:50:25 +02:00
|
|
|
python_files = [fpath for fpath in files_dict['py']
|
2016-07-21 21:56:18 +02:00
|
|
|
if not fpath.endswith('.py') or fpath + 'i' not in pyi_files]
|
2018-03-13 21:00:49 +01:00
|
|
|
if not python_files and not pyi_files:
|
2017-09-26 23:43:34 +02:00
|
|
|
print("There are no files to run mypy on.")
|
|
|
|
sys.exit(0)
|
2016-04-07 07:22:04 +02:00
|
|
|
|
2018-03-23 21:32:12 +01:00
|
|
|
extra_args = ["--follow-imports=silent",
|
2018-05-06 23:06:34 +02:00
|
|
|
"--cache-dir=var/mypy-cache"]
|
2016-06-04 21:03:14 +02:00
|
|
|
if args.linecoverage_report:
|
|
|
|
extra_args.append("--linecoverage-report")
|
2016-07-16 16:44:41 +02:00
|
|
|
extra_args.append("var/linecoverage-report")
|
2016-06-02 21:46:24 +02:00
|
|
|
if args.disallow_untyped_defs:
|
|
|
|
extra_args.append("--disallow-untyped-defs")
|
2017-07-28 01:09:37 +02:00
|
|
|
if args.warn_unused_ignores:
|
|
|
|
extra_args.append("--warn-unused-ignores")
|
2017-05-23 20:41:07 +02:00
|
|
|
if args.ignore_missing_imports:
|
|
|
|
extra_args.append("--ignore-missing-imports")
|
|
|
|
if args.quick:
|
|
|
|
extra_args.append("--quick")
|
2016-06-02 21:46:24 +02:00
|
|
|
|
2018-03-13 21:00:49 +01:00
|
|
|
rc = subprocess.call([mypy_command] + extra_args + python_files + pyi_files)
|
2016-06-02 21:46:24 +02:00
|
|
|
|
2017-09-26 23:43:34 +02:00
|
|
|
if args.linecoverage_report:
|
|
|
|
# Move the coverage report to where codecov will look for it.
|
|
|
|
try:
|
|
|
|
os.rename('var/linecoverage-report/coverage.txt', 'var/.coverage')
|
|
|
|
except OSError:
|
|
|
|
# maybe mypy crashed; exit with its error code
|
|
|
|
pass
|
|
|
|
|
|
|
|
sys.exit(rc)
|