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-07-12 10:33:58 +02:00
|
|
|
stubs/
|
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")
|
2018-05-21 18:34:33 +02:00
|
|
|
parser.add_argument('-d', '--daemon', action='store_true',
|
|
|
|
help="Start and run the mypy fine-grained incremental daemon")
|
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")
|
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)
|
|
|
|
|
2018-05-21 18:34:33 +02:00
|
|
|
command_name = "mypy" if not args.daemon else "dmypy"
|
|
|
|
|
2017-09-26 23:33:21 +02:00
|
|
|
# Use zulip-py3-venv's mypy if it's available.
|
|
|
|
VENV_DIR = "/srv/zulip-py3-venv"
|
2018-05-21 18:34:33 +02:00
|
|
|
MYPY_VENV_PATH = os.path.join(VENV_DIR, "bin", command_name)
|
2017-09-26 23:33:21 +02:00
|
|
|
if os.path.exists(MYPY_VENV_PATH):
|
|
|
|
mypy_command = MYPY_VENV_PATH
|
|
|
|
else:
|
2018-05-21 18:34:33 +02:00
|
|
|
mypy_command = command_name
|
2017-09-26 23:33:21 +02:00
|
|
|
|
|
|
|
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-05-06 23:11:40 +02:00
|
|
|
extra_args = []
|
2017-05-23 20:41:07 +02:00
|
|
|
if args.quick:
|
|
|
|
extra_args.append("--quick")
|
2016-06-02 21:46:24 +02:00
|
|
|
|
2018-05-21 18:34:33 +02:00
|
|
|
mypy_args = extra_args + python_files + pyi_files
|
|
|
|
if args.daemon:
|
|
|
|
rc = subprocess.call([mypy_command, 'run', '--'] + mypy_args)
|
|
|
|
else:
|
|
|
|
rc = subprocess.call([mypy_command] + mypy_args)
|
2016-06-02 21:46:24 +02:00
|
|
|
|
2017-09-26 23:43:34 +02:00
|
|
|
sys.exit(rc)
|