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-03-10 17:15:34 +01:00
|
|
|
from __future__ import print_function
|
2016-03-10 18:22:27 +01:00
|
|
|
from __future__ import absolute_import
|
2016-08-18 18:48:08 +02:00
|
|
|
import logging
|
2013-02-19 04:40:57 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2017-08-25 13:55:28 +02:00
|
|
|
import argparse
|
2013-02-19 04:40:57 +01:00
|
|
|
import subprocess
|
2016-08-18 18:42:17 +02:00
|
|
|
|
2017-07-06 06:48:26 +02:00
|
|
|
from linter_lib.printer import print_err, colors
|
|
|
|
|
2017-02-05 21:24:28 +01:00
|
|
|
# check for the venv
|
|
|
|
from lib import sanity_check
|
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
|
|
|
import lister
|
2017-06-05 16:43:16 +02:00
|
|
|
from typing import cast, Callable, Dict, Iterator, List
|
2016-03-22 21:20:33 +01:00
|
|
|
|
2016-08-18 20:05:04 +02:00
|
|
|
|
2017-12-13 19:38:15 +01:00
|
|
|
def run_parallel(lint_functions):
|
|
|
|
# type: (Dict[str, Callable[[], int]]) -> bool
|
2016-08-18 18:48:08 +02:00
|
|
|
pids = []
|
|
|
|
for name, func in lint_functions.items():
|
|
|
|
pid = os.fork()
|
|
|
|
if pid == 0:
|
|
|
|
logging.info("start " + name)
|
|
|
|
result = func()
|
|
|
|
logging.info("finish " + name)
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
|
|
|
os._exit(result)
|
|
|
|
pids.append(pid)
|
|
|
|
failed = False
|
|
|
|
|
|
|
|
for pid in pids:
|
|
|
|
(_, status) = os.waitpid(pid, 0)
|
|
|
|
if status != 0:
|
|
|
|
failed = True
|
|
|
|
return failed
|
|
|
|
|
2017-12-13 19:38:15 +01:00
|
|
|
def run():
|
|
|
|
# type: () -> None
|
2017-08-25 13:55:28 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--force', default=False,
|
|
|
|
action="store_true",
|
|
|
|
help='Run tests despite possible problems.')
|
|
|
|
parser.add_argument('--full',
|
|
|
|
action='store_true',
|
|
|
|
help='Check some things we typically ignore')
|
|
|
|
parser.add_argument('--no-gitlint',
|
|
|
|
action='store_true',
|
|
|
|
help='Disable gitlint')
|
|
|
|
parser.add_argument('--modified', '-m',
|
|
|
|
action='store_true',
|
|
|
|
help='Only check modified files')
|
|
|
|
parser.add_argument('--verbose', '-v',
|
|
|
|
action='store_true',
|
|
|
|
help='Print verbose timing output')
|
|
|
|
parser.add_argument('targets',
|
|
|
|
nargs='*',
|
|
|
|
help='Specify directories to check')
|
2017-08-25 15:23:02 +02:00
|
|
|
limited_tests_group = parser.add_mutually_exclusive_group()
|
|
|
|
limited_tests_group.add_argument('--frontend',
|
|
|
|
action='store_true',
|
|
|
|
help='Only check files relevant to frontend')
|
|
|
|
limited_tests_group.add_argument('--backend',
|
|
|
|
action='store_true',
|
|
|
|
help='Only check files relevant to backend')
|
2017-08-25 13:55:28 +02:00
|
|
|
args = parser.parse_args()
|
2016-08-18 20:05:42 +02:00
|
|
|
|
2016-11-23 18:28:21 +01:00
|
|
|
tools_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
root_dir = os.path.dirname(tools_dir)
|
|
|
|
sys.path.insert(0, root_dir)
|
|
|
|
|
2017-06-05 16:29:04 +02:00
|
|
|
from tools.linter_lib.custom_check import build_custom_checkers
|
2017-06-05 16:34:21 +02:00
|
|
|
from tools.linter_lib.exclude import EXCLUDED_FILES
|
2017-06-05 16:43:16 +02:00
|
|
|
from tools.linter_lib.pyflakes import check_pyflakes
|
2017-06-05 16:49:59 +02:00
|
|
|
from tools.linter_lib.pep8 import check_pep8
|
2017-06-05 16:29:04 +02:00
|
|
|
|
2016-11-23 18:28:21 +01:00
|
|
|
from tools.lib.test_script import (
|
|
|
|
get_provisioning_status,
|
|
|
|
)
|
|
|
|
|
|
|
|
os.chdir(root_dir)
|
|
|
|
|
2017-08-25 13:55:28 +02:00
|
|
|
if not args.force:
|
2016-11-23 18:28:21 +01:00
|
|
|
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)
|
2016-08-18 20:05:42 +02:00
|
|
|
|
2017-11-16 22:20:50 +01:00
|
|
|
backend_file_types = ['py', 'sh', 'pp', 'json', 'md', 'txt', 'text', 'yaml', 'rst']
|
2017-08-25 15:23:02 +02:00
|
|
|
frontend_file_types = ['js', 'css', 'handlebars', 'html']
|
|
|
|
file_types = backend_file_types + frontend_file_types
|
|
|
|
if args.backend:
|
|
|
|
file_types = backend_file_types
|
|
|
|
if args.frontend:
|
|
|
|
file_types = frontend_file_types
|
|
|
|
|
2016-12-11 14:30:45 +01:00
|
|
|
by_lang = cast(Dict[str, List[str]],
|
2017-08-25 13:55:28 +02:00
|
|
|
lister.list_files(args.targets, modified_only=args.modified,
|
2017-08-25 15:23:02 +02:00
|
|
|
ftypes=file_types,
|
2016-12-11 14:30:45 +01:00
|
|
|
use_shebang=True, group_by_ftype=True, exclude=EXCLUDED_FILES))
|
2016-08-18 20:05:42 +02:00
|
|
|
|
|
|
|
# Invoke the appropriate lint checker for each language,
|
|
|
|
# and also check files for extra whitespace.
|
|
|
|
|
|
|
|
logging.basicConfig(format="%(asctime)s %(message)s")
|
|
|
|
logger = logging.getLogger()
|
2017-08-25 13:55:28 +02:00
|
|
|
if args.verbose:
|
2016-08-18 20:05:42 +02:00
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
else:
|
|
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
|
|
|
|
check_custom_checks_py, check_custom_checks_nonpy = build_custom_checkers(by_lang)
|
|
|
|
|
2017-05-17 23:02:38 +02:00
|
|
|
lint_functions = {} # type: Dict[str, Callable[[], int]]
|
2016-08-18 18:42:17 +02:00
|
|
|
|
2017-12-13 19:38:15 +01:00
|
|
|
def lint(func):
|
|
|
|
# type: (Callable[[], int]) -> Callable[[], int]
|
2016-08-18 18:42:17 +02:00
|
|
|
lint_functions[func.__name__] = func
|
|
|
|
return func
|
|
|
|
|
2017-12-13 19:38:15 +01:00
|
|
|
def external_linter(name, command, target_langs=[]):
|
|
|
|
# type: (str, List[str], List[str]) -> None
|
2017-06-02 23:48:40 +02:00
|
|
|
"""Registers an external linter program to be run as part of the
|
|
|
|
linter. This program will be passed the subset of files being
|
|
|
|
linted that have extensions in target_langs. If there are no
|
|
|
|
such files, exits without doing anything.
|
|
|
|
|
|
|
|
If target_langs is empty, just runs the linter unconditionally.
|
|
|
|
"""
|
2017-07-06 06:48:26 +02:00
|
|
|
color = next(colors)
|
|
|
|
|
2017-12-13 19:38:15 +01:00
|
|
|
def run_linter():
|
|
|
|
# type: () -> int
|
2017-07-06 06:48:26 +02:00
|
|
|
targets = [] # type: List[str]
|
|
|
|
if len(target_langs) != 0:
|
|
|
|
targets = [target for lang in target_langs for target in by_lang[lang]]
|
|
|
|
if len(targets) == 0:
|
|
|
|
# If we this linter has a list of languages, and
|
|
|
|
# no files in those languages are to be checked,
|
|
|
|
# then we can safely return success without
|
|
|
|
# invoking the external linter.
|
|
|
|
return 0
|
|
|
|
|
|
|
|
p = subprocess.Popen(command + targets,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
|
2017-08-02 22:17:30 +02:00
|
|
|
assert p.stdout # use of subprocess.PIPE indicates non-None
|
2017-07-06 06:48:26 +02:00
|
|
|
for line in iter(p.stdout.readline, b''):
|
|
|
|
print_err(name, color, line)
|
|
|
|
|
|
|
|
return p.wait() # Linter exit code
|
|
|
|
|
2017-03-19 21:25:20 +01:00
|
|
|
lint_functions[name] = run_linter
|
2016-11-04 23:15:18 +01:00
|
|
|
|
2017-08-27 22:35:11 +02:00
|
|
|
external_linter('add_class', ['tools/find-add-class'], ['js'])
|
2017-07-06 07:01:43 +02:00
|
|
|
external_linter('css', ['tools/check-css'], ['css'])
|
2017-12-07 01:56:39 +01:00
|
|
|
external_linter('eslint', ['node', 'node_modules/.bin/eslint', '--quiet', '--cache'], ['js'])
|
2017-07-06 07:01:43 +02:00
|
|
|
external_linter('tslint', ['node', 'node_modules/.bin/tslint', '-c',
|
|
|
|
'static/ts/tslint.json'], ['ts'])
|
|
|
|
external_linter('puppet', ['puppet', 'parser', 'validate'], ['pp'])
|
|
|
|
external_linter('templates', ['tools/check-templates'], ['handlebars', 'html'])
|
2017-08-27 22:32:31 +02:00
|
|
|
external_linter('urls', ['tools/check-urls'], ['py'])
|
2017-07-06 07:01:43 +02:00
|
|
|
external_linter('swagger', ['node', 'tools/check-swagger'], ['yaml'])
|
|
|
|
|
2017-12-16 11:15:44 +01:00
|
|
|
# Disabled check for imperative mood until it is stabilized
|
|
|
|
if not args.no_gitlint:
|
|
|
|
external_linter('commit_messages', ['tools/commit-message-lint'])
|
2017-07-06 07:01:43 +02:00
|
|
|
|
|
|
|
@lint
|
2017-12-13 19:38:15 +01:00
|
|
|
def custom_py():
|
|
|
|
# type: () -> int
|
2017-07-06 07:01:43 +02:00
|
|
|
failed = check_custom_checks_py()
|
|
|
|
return 1 if failed else 0
|
|
|
|
|
|
|
|
@lint
|
2017-12-13 19:38:15 +01:00
|
|
|
def custom_nonpy():
|
|
|
|
# type: () -> int
|
2017-07-06 07:01:43 +02:00
|
|
|
failed = check_custom_checks_nonpy()
|
|
|
|
return 1 if failed else 0
|
|
|
|
|
|
|
|
@lint
|
2017-12-13 19:38:15 +01:00
|
|
|
def pyflakes():
|
|
|
|
# type: () -> int
|
2017-08-25 13:55:28 +02:00
|
|
|
failed = check_pyflakes(args, by_lang)
|
2017-07-06 07:01:43 +02:00
|
|
|
return 1 if failed else 0
|
|
|
|
|
2017-08-25 15:23:02 +02:00
|
|
|
@lint
|
2017-12-13 19:38:15 +01:00
|
|
|
def pep8():
|
|
|
|
# type: () -> int
|
2017-08-25 15:23:02 +02:00
|
|
|
failed = check_pep8(by_lang['py'])
|
|
|
|
return 1 if failed else 0
|
2016-08-18 18:42:17 +02:00
|
|
|
|
2017-07-06 07:01:43 +02:00
|
|
|
failed = run_parallel(lint_functions)
|
2016-08-18 18:42:17 +02:00
|
|
|
|
2016-08-18 20:05:04 +02:00
|
|
|
sys.exit(1 if failed else 0)
|
2016-08-18 18:42:17 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run()
|