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-09-02 05:22:01 +02:00
|
|
|
import re
|
2013-02-19 04:40:57 +01:00
|
|
|
import sys
|
2017-07-06 06:48:26 +02:00
|
|
|
|
2021-07-03 08:22:44 +02:00
|
|
|
tools_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
root_dir = os.path.join(tools_dir, "..")
|
|
|
|
sys.path.insert(0, root_dir)
|
|
|
|
|
2017-02-05 21:24:28 +01:00
|
|
|
# check for the venv
|
2021-07-03 08:22:44 +02:00
|
|
|
from tools.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
|
|
|
|
|
2021-07-03 08:22:44 +02:00
|
|
|
from tools.linter_lib.custom_check import non_py_rules, python_rules
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
|
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:
|
2021-03-03 05:00:15 +01:00
|
|
|
from tools.lib.test_script import (
|
|
|
|
add_provision_check_override_param,
|
|
|
|
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
|
2016-11-23 18:28:21 +01:00
|
|
|
|
2021-03-03 05:00:15 +01:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
add_provision_check_override_param(parser)
|
|
|
|
parser.add_argument("--full", action="store_true", help="Check some things we typically ignore")
|
2022-06-28 20:12:35 +02:00
|
|
|
parser.add_argument("--use-mypy-daemon", action="store_true", help="Run mypy daemon instead")
|
2021-03-03 05:00:15 +01:00
|
|
|
add_default_linter_arguments(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2016-11-23 18:28:21 +01:00
|
|
|
os.chdir(root_dir)
|
|
|
|
|
2021-03-02 20:59:19 +01:00
|
|
|
assert_provisioning_status_ok(args.skip_provision_check)
|
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-06-25 03:49:14 +02:00
|
|
|
"backend": [
|
|
|
|
"bash",
|
|
|
|
"json",
|
|
|
|
"md",
|
|
|
|
"pp",
|
|
|
|
"py",
|
2021-07-10 02:20:40 +02:00
|
|
|
"pyi",
|
2021-06-25 03:49:14 +02:00
|
|
|
"rst",
|
|
|
|
"sh",
|
|
|
|
"text",
|
|
|
|
"txt",
|
|
|
|
"yaml",
|
|
|
|
"yml",
|
|
|
|
],
|
|
|
|
"frontend": [
|
|
|
|
"css",
|
|
|
|
"flow",
|
|
|
|
"hbs",
|
|
|
|
"html",
|
|
|
|
"js",
|
|
|
|
"lock",
|
|
|
|
"ts",
|
|
|
|
],
|
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",
|
2021-03-29 21:09:38 +02:00
|
|
|
["env", "RUBYOPT=-W0", "puppet", "parser", "validate"],
|
2021-02-12 08:20:45 +01:00
|
|
|
["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) ",
|
2021-09-09 02:02:42 +02:00
|
|
|
fix_arg="--fix",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
linter_config.external_linter(
|
2021-02-12 08:20:45 +01:00
|
|
|
"shellcheck",
|
|
|
|
["shellcheck", "-x", "-P", "SCRIPTDIR"],
|
2021-06-25 03:49:14 +02:00
|
|
|
["bash", "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"],
|
2021-06-25 03:49:14 +02:00
|
|
|
["bash", "sh"],
|
2021-02-12 08:20:45 +01:00
|
|
|
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"]
|
2021-03-02 20:59:19 +01:00
|
|
|
if args.skip_provision_check:
|
|
|
|
command.append("--skip-provision-check")
|
2022-06-28 20:12:35 +02:00
|
|
|
if args.use_mypy_daemon:
|
|
|
|
command.append("--use-daemon")
|
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-07-10 02:20:40 +02:00
|
|
|
["py", "pyi"],
|
2021-02-12 08:19:30 +01:00
|
|
|
pass_targets=False,
|
2021-07-05 23:36:46 +02:00
|
|
|
description="Static type checker for Python (config: pyproject.toml)",
|
2022-06-28 20:12:35 +02:00
|
|
|
suppress_line=(
|
|
|
|
lambda line: line.startswith("Daemon") or line == "Restarting: configuration changed"
|
|
|
|
)
|
|
|
|
if args.use_mypy_daemon
|
|
|
|
else lambda _: False,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2022-10-30 00:07:03 +02:00
|
|
|
linter_config.external_linter(
|
|
|
|
"ruff",
|
|
|
|
["ruff", "--quiet"],
|
|
|
|
["py", "pyi"],
|
|
|
|
fix_arg="--fix",
|
|
|
|
description="Python linter",
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
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"],
|
2021-07-10 02:20:40 +02:00
|
|
|
["py", "pyi"],
|
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",
|
2022-09-21 00:04:57 +02:00
|
|
|
["node_modules/.bin/prettier", "--cache", "--check", "--log-level=warn"],
|
2021-08-20 23:05:13 +02:00
|
|
|
["css", "flow", "js", "json", "md", "ts", "yaml", "yml"],
|
2021-02-12 08:20:45 +01:00
|
|
|
fix_arg=["--write"],
|
2021-02-12 08:19:30 +01:00
|
|
|
description="Formats CSS, JavaScript, YAML",
|
|
|
|
)
|
2020-09-02 05:22:01 +02:00
|
|
|
linter_config.external_linter(
|
|
|
|
"black",
|
|
|
|
["black"],
|
2021-07-10 02:20:40 +02:00
|
|
|
["py", "pyi"],
|
2020-09-02 05:22:01 +02:00
|
|
|
description="Reformats Python code",
|
|
|
|
check_arg=["--check"],
|
|
|
|
suppress_line=lambda line: line == "All done! ✨ 🍰 ✨\n"
|
|
|
|
or re.fullmatch(r"\d+ files? would be left unchanged\.\n", line) is not None,
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
semgrep_command = [
|
|
|
|
"semgrep",
|
|
|
|
"--config=./tools/semgrep.yml",
|
|
|
|
"--error",
|
|
|
|
"--disable-version-check",
|
|
|
|
"--quiet",
|
|
|
|
]
|
|
|
|
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-05-10 07:02:14 +02:00
|
|
|
description="Syntactic grep (semgrep) code search tool (config: ./tools/semgrep.yml)",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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()
|