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
|
2020-07-30 22:03:51 +02:00
|
|
|
import random
|
2013-02-19 04:40:57 +01:00
|
|
|
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__)
|
|
|
|
|
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()
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("--force", 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)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
by_lang = linter_config.list_files(
|
|
|
|
groups={
|
2021-02-12 08:20:45 +01:00
|
|
|
"backend": ["py", "sh", "pp", "json", "md", "txt", "text", "yaml", "rst", "yml"],
|
|
|
|
"frontend": ["js", "ts", "css", "hbs", "html", "lock"],
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
exclude=EXCLUDED_FILES,
|
|
|
|
)
|
|
|
|
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"css",
|
|
|
|
["node", "node_modules/.bin/stylelint"],
|
|
|
|
["css"],
|
|
|
|
fix_arg="--fix",
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Standard CSS style and formatting linter (config: stylelint.config.js)",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"eslint",
|
|
|
|
["node", "node_modules/.bin/eslint", "--max-warnings=0", "--cache", "--ext", ".js,.ts"],
|
|
|
|
["js", "ts"],
|
|
|
|
fix_arg="--fix",
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Standard JavaScript style and formatting linter (config: .eslintrc).",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"puppet",
|
|
|
|
["puppet", "parser", "validate"],
|
|
|
|
["pp"],
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Runs the puppet parser validator, checking for syntax errors.",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"puppet-lint",
|
|
|
|
["puppet-lint", "--fail-on-warnings", *PUPPET_CHECK_RULES_TO_EXCLUDE],
|
|
|
|
["pp"],
|
|
|
|
fix_arg="--fix",
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Standard puppet linter (config: tools/linter_lib/exclude.py)",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"templates",
|
|
|
|
["tools/check-templates"],
|
|
|
|
["hbs", "html"],
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Custom linter checks whitespace formatting of HTML templates",
|
2021-02-12 08:20:45 +01:00
|
|
|
fix_arg="--fix",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"openapi",
|
|
|
|
["node", "tools/check-openapi"],
|
|
|
|
["yaml"],
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Validates our OpenAPI/Swagger API documentation "
|
|
|
|
"(zerver/openapi/zulip.yaml) ",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"shellcheck",
|
|
|
|
["shellcheck", "-x", "-P", "SCRIPTDIR"],
|
|
|
|
["sh"],
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Standard shell script linter",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"shfmt",
|
|
|
|
["shfmt"],
|
|
|
|
["sh"],
|
|
|
|
check_arg="-d",
|
|
|
|
fix_arg="-w",
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Formats shell scripts",
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
command = ["tools/run-mypy", "--quiet"]
|
2019-06-18 19:12:54 +02:00
|
|
|
if args.force:
|
2021-02-12 08:20:45 +01:00
|
|
|
command.append("--force")
|
2021-02-12 08:19:30 +01:00
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"mypy",
|
2021-02-12 08:19:30 +01:00
|
|
|
command,
|
2021-02-12 08:20:45 +01:00
|
|
|
["py"],
|
2021-02-12 08:19:30 +01:00
|
|
|
pass_targets=False,
|
|
|
|
description="Static type checker for Python (config: mypy.ini)",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"tsc",
|
|
|
|
["tools/run-tsc"],
|
|
|
|
["ts"],
|
2021-02-12 08:19:30 +01:00
|
|
|
pass_targets=False,
|
|
|
|
description="TypeScript compiler (config: tsconfig.json)",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"yarn-deduplicate",
|
|
|
|
["tools/run-yarn-deduplicate"],
|
|
|
|
["lock"],
|
2021-02-12 08:19:30 +01:00
|
|
|
pass_targets=False,
|
|
|
|
description="Shares duplicate packages in yarn.lock",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"gitlint",
|
|
|
|
["tools/commit-message-lint"],
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Checks commit messages for common formatting errors (config: .gitlint)",
|
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"isort",
|
|
|
|
["isort"],
|
|
|
|
["py"],
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Sorts Python import statements",
|
2021-02-12 08:20:45 +01:00
|
|
|
check_arg=["--check-only", "--diff"],
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"prettier",
|
|
|
|
["node_modules/.bin/prettier", "--check", "--loglevel=warn"],
|
|
|
|
["css", "js", "json", "ts", "yaml", "yml"],
|
|
|
|
fix_arg=["--write"],
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Formats CSS, JavaScript, YAML",
|
|
|
|
)
|
|
|
|
|
|
|
|
semgrep_command = [
|
|
|
|
"semgrep",
|
|
|
|
"--config=./tools/semgrep.yml",
|
|
|
|
"--error",
|
|
|
|
"--disable-version-check",
|
|
|
|
"--quiet",
|
|
|
|
# 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",
|
|
|
|
]
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"semgrep-py",
|
2021-02-12 08:19:30 +01:00
|
|
|
[*semgrep_command, "--lang=python"],
|
2021-02-12 08:20:45 +01:00
|
|
|
["py"],
|
|
|
|
fix_arg="--autofix",
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Syntactic Grep (semgrep) Code Search Tool (config: ./tools/semgrep.yml)",
|
|
|
|
)
|
|
|
|
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"thirdparty",
|
|
|
|
["tools/check-thirdparty"],
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Check docs/THIRDPARTY copyright file syntax",
|
|
|
|
)
|
2020-05-02 02:36:23 +02:00
|
|
|
|
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)"""
|
2021-02-12 08:20:45 +01:00
|
|
|
failed = check_pyflakes(by_lang["py"], args)
|
2017-07-06 07:01:43 +02:00
|
|
|
return 1 if failed else 0
|
|
|
|
|
2021-02-12 08:20:45 +01: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}
|
2018-08-05 20:13:49 +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_1of2() -> int:
|
2020-07-14 01:43:53 +02:00
|
|
|
"""Standard Python style linter on 50% of files (config: setup.cfg)"""
|
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:
|
2020-07-14 01:43:53 +02:00
|
|
|
"""Standard Python style linter on other 50% of files (config: setup.cfg)"""
|
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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if __name__ == "__main__":
|
2016-08-18 18:42:17 +02:00
|
|
|
run()
|