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
|
2020-06-11 00:54:34 +02:00
|
|
|
import argparse
|
2013-02-19 04:40:57 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2017-07-06 06:48:26 +02:00
|
|
|
|
2017-02-05 21:24:28 +01:00
|
|
|
# check for the venv
|
|
|
|
from lib import sanity_check
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-02-05 21:24:28 +01:00
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
2018-08-05 20:13:49 +02:00
|
|
|
import random
|
2016-08-18 20:05:04 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zulint.command import LinterConfig, add_default_linter_arguments
|
|
|
|
|
|
|
|
from linter_lib.custom_check import non_py_rules, python_rules
|
|
|
|
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def run() -> 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')
|
2018-08-04 23:18:50 +02:00
|
|
|
add_default_linter_arguments(parser)
|
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)
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from tools.lib.test_script import assert_provisioning_status_ok
|
2018-08-07 14:26:46 +02:00
|
|
|
from tools.linter_lib.exclude import EXCLUDED_FILES, PUPPET_CHECK_RULES_TO_EXCLUDE
|
2017-06-05 16:49:59 +02:00
|
|
|
from tools.linter_lib.pep8 import check_pep8
|
2020-06-11 00:54:34 +02:00
|
|
|
from tools.linter_lib.pyflakes import check_pyflakes
|
2016-11-23 18:28:21 +01:00
|
|
|
|
|
|
|
os.chdir(root_dir)
|
|
|
|
|
2019-06-20 18:27:09 +02:00
|
|
|
assert_provisioning_status_ok(args.force)
|
2016-08-18 20:05:42 +02:00
|
|
|
|
|
|
|
# Invoke the appropriate lint checker for each language,
|
|
|
|
# and also check files for extra whitespace.
|
|
|
|
|
2019-06-21 18:40:38 +02:00
|
|
|
linter_config = LinterConfig(args)
|
|
|
|
|
|
|
|
by_lang = linter_config.list_files(groups={
|
2020-03-24 19:06:24 +01:00
|
|
|
'backend': ['py', 'sh', 'pp', 'json', 'md', 'txt', 'text', 'yaml', 'rst', 'yml'],
|
2019-08-31 03:16:37 +02:00
|
|
|
'frontend': ['js', 'ts', 'css', 'scss', 'hbs', 'html', 'lock'],
|
2019-06-21 18:40:38 +02:00
|
|
|
}, exclude=EXCLUDED_FILES)
|
|
|
|
|
2019-06-21 20:37:06 +02:00
|
|
|
linter_config.external_linter('css', ['node', 'node_modules/.bin/stylelint'], ['css', 'scss'],
|
2019-06-24 08:48:31 +02:00
|
|
|
fix_arg='--fix',
|
2019-06-21 20:37:06 +02:00
|
|
|
description="Standard CSS style and formatting linter "
|
|
|
|
"(config: .stylelintrc)")
|
2018-08-04 23:33:19 +02:00
|
|
|
linter_config.external_linter('eslint', ['node', 'node_modules/.bin/eslint',
|
2019-06-21 20:37:06 +02:00
|
|
|
'--quiet', '--cache', '--ext', '.js,.ts'], ['js', 'ts'],
|
2019-06-24 08:48:31 +02:00
|
|
|
fix_arg='--fix',
|
2019-06-21 20:37:06 +02:00
|
|
|
description="Standard JavaScript style and formatting linter"
|
|
|
|
"(config: .eslintrc).")
|
|
|
|
linter_config.external_linter('puppet', ['puppet', 'parser', 'validate'], ['pp'],
|
|
|
|
description="Runs the puppet parser validator, "
|
|
|
|
"checking for syntax errors.")
|
2018-08-07 14:26:46 +02:00
|
|
|
linter_config.external_linter('puppet-lint',
|
2020-02-21 02:02:51 +01:00
|
|
|
['puppet-lint', '--fail-on-warnings'] + PUPPET_CHECK_RULES_TO_EXCLUDE,
|
|
|
|
['pp'],
|
2019-06-24 08:48:31 +02:00
|
|
|
fix_arg='--fix',
|
2019-06-21 20:37:06 +02:00
|
|
|
description="Standard puppet linter"
|
|
|
|
"(config: tools/linter_lib/exclude.py)")
|
2019-07-12 00:52:56 +02:00
|
|
|
linter_config.external_linter('templates', ['tools/check-templates'], ['hbs', 'html'],
|
2019-06-21 20:37:06 +02:00
|
|
|
description="Custom linter checks whitespace formatting"
|
2019-07-18 02:41:47 +02:00
|
|
|
"of HTML templates.",
|
|
|
|
fix_arg='--fix')
|
2019-08-04 17:59:21 +02:00
|
|
|
linter_config.external_linter('openapi', ['node', 'tools/check-openapi'], ['yaml'],
|
2019-06-21 20:37:06 +02:00
|
|
|
description="Validates our OpenAPI/Swagger API documentation"
|
|
|
|
"(zerver/openapi/zulip.yaml) ")
|
2019-12-16 22:16:15 +01:00
|
|
|
linter_config.external_linter('shellcheck', ['shellcheck', '-x', '-P', 'SCRIPTDIR'], ['sh'],
|
2019-06-21 20:37:06 +02:00
|
|
|
description="Standard shell script linter.")
|
2019-11-13 21:55:27 +01:00
|
|
|
command = ['tools/run-mypy', '--quiet']
|
2019-06-18 19:12:54 +02:00
|
|
|
if args.force:
|
|
|
|
command.append('--force')
|
2019-06-21 20:37:06 +02:00
|
|
|
linter_config.external_linter('mypy', command, ['py'], pass_targets=False,
|
|
|
|
description="Static type checker for Python (config: mypy.ini)")
|
2019-07-20 20:40:36 +02:00
|
|
|
linter_config.external_linter('tsc', ['tools/run-tsc'], ['ts'], pass_targets=False,
|
2019-11-04 23:37:32 +01:00
|
|
|
description="TypeScript compiler (config: tsconfig.json)")
|
2019-08-31 03:16:37 +02:00
|
|
|
linter_config.external_linter('yarn-deduplicate', ['tools/run-yarn-deduplicate'], ['lock'],
|
|
|
|
pass_targets=False,
|
|
|
|
description="Shares duplicate packages in yarn.lock")
|
2019-06-21 20:37:06 +02:00
|
|
|
linter_config.external_linter('gitlint', ['tools/commit-message-lint'],
|
|
|
|
description="Checks commit messages for common formatting errors."
|
|
|
|
"(config: .gitlint)")
|
2020-01-08 01:04:54 +01:00
|
|
|
linter_config.external_linter('isort', ['tools/run-isort'], ['py'],
|
|
|
|
description="Sorts Python import statements",
|
|
|
|
fix_arg='--fix')
|
2017-07-06 07:01:43 +02:00
|
|
|
|
2020-05-18 18:01:59 +02:00
|
|
|
semgrep_command = ["semgrep", "--config=./tools/semgrep.yml", "--error",
|
|
|
|
# This option is dangerous in the context of running
|
|
|
|
# semgrep-as-a-service on untrusted user code, since it
|
|
|
|
# causes Python code in the rules configuration to be
|
|
|
|
# executed. From our standpoint, it is required for
|
|
|
|
# `pattern-where-python` rules, and there's no real
|
|
|
|
# security impact, since if you can put arbitrary code
|
|
|
|
# into zulip.git, you can run arbitrary code in a Zulip
|
|
|
|
# development environment anyway.
|
|
|
|
"--dangerously-allow-arbitrary-code-execution-from-rules"]
|
2020-04-27 10:16:16 +02:00
|
|
|
linter_config.external_linter('semgrep-py', [*semgrep_command, "--lang=python"], ['py'],
|
2020-05-01 03:24:55 +02:00
|
|
|
fix_arg='--autofix',
|
2020-04-27 10:16:16 +02:00
|
|
|
description="Syntactic Grep (semgrep) Code Search Tool "
|
|
|
|
"(config: ./tools/semgrep.yml)")
|
2020-03-19 00:32:26 +01:00
|
|
|
|
2020-05-02 02:36:23 +02:00
|
|
|
linter_config.external_linter('thirdparty', ['tools/check-thirdparty'],
|
|
|
|
description="Check docs/THIRDPARTY copyright file syntax")
|
|
|
|
|
2018-08-04 23:33:19 +02:00
|
|
|
@linter_config.lint
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def custom_py() -> int:
|
2019-06-21 20:37:06 +02:00
|
|
|
"""Runs custom checks for python files (config: tools/linter_lib/custom_check.py)"""
|
2019-06-23 13:41:06 +02:00
|
|
|
failed = python_rules.check(by_lang, verbose=args.verbose)
|
2017-07-06 07:01:43 +02:00
|
|
|
return 1 if failed else 0
|
|
|
|
|
2018-08-04 23:33:19 +02:00
|
|
|
@linter_config.lint
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def custom_nonpy() -> int:
|
2019-06-21 20:37:06 +02:00
|
|
|
"""Runs custom checks for non-python files (config: tools/linter_lib/custom_check.py)"""
|
2019-05-30 17:41:23 +02:00
|
|
|
failed = False
|
|
|
|
for rule in non_py_rules:
|
2019-06-23 13:41:06 +02:00
|
|
|
failed = failed or rule.check(by_lang, verbose=args.verbose)
|
2017-07-06 07:01:43 +02:00
|
|
|
return 1 if failed else 0
|
|
|
|
|
2018-08-04 23:33:19 +02:00
|
|
|
@linter_config.lint
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def pyflakes() -> int:
|
2019-06-21 20:37:06 +02:00
|
|
|
"""Standard Python bug and code smell linter (config: tools/linter_lib/pyflakes.py)"""
|
2018-08-05 05:05:15 +02:00
|
|
|
failed = check_pyflakes(by_lang['py'], args)
|
2017-07-06 07:01:43 +02:00
|
|
|
return 1 if failed else 0
|
|
|
|
|
2018-08-05 20:13:49 +02:00
|
|
|
python_part1 = {x for x in by_lang['py'] if random.randint(0, 1) == 0}
|
|
|
|
python_part2 = {y for y in by_lang['py'] if y not in python_part1}
|
|
|
|
|
|
|
|
@linter_config.lint
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def pep8_1of2() -> int:
|
2019-06-21 20:37:06 +02:00
|
|
|
"""Standard Python style linter on 50% of files (config: tools/linter_lib/pep8.py)"""
|
2018-08-05 20:13:49 +02:00
|
|
|
failed = check_pep8(list(python_part1))
|
|
|
|
return 1 if failed else 0
|
|
|
|
|
2018-08-04 23:33:19 +02:00
|
|
|
@linter_config.lint
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def pep8_2of2() -> int:
|
2019-06-21 20:37:06 +02:00
|
|
|
"""Standard Python style linter on other 50% of files (config: tools/linter_lib/pep8.py)"""
|
2018-08-05 20:13:49 +02:00
|
|
|
failed = check_pep8(list(python_part2))
|
2017-08-25 15:23:02 +02:00
|
|
|
return 1 if failed else 0
|
2019-06-21 18:36:41 +02:00
|
|
|
|
|
|
|
linter_config.do_lint()
|
2016-08-18 18:42:17 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run()
|