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-06-20 00:07:06 +02:00
|
|
|
import argparse
|
|
|
|
import hashlib
|
2020-06-11 00:54:34 +02:00
|
|
|
import os
|
2020-07-09 18:33:16 +02:00
|
|
|
import subprocess
|
2020-06-11 00:54:34 +02:00
|
|
|
import sys
|
2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Iterable
|
2016-06-20 00:07:06 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def expand_reqs_helper(fpath: str) -> list[str]:
|
|
|
|
result: list[str] = []
|
2016-06-20 00:07:06 +02:00
|
|
|
|
2020-10-24 09:33:54 +02:00
|
|
|
with open(fpath) as f:
|
|
|
|
for line in f:
|
2021-02-12 08:20:45 +01:00
|
|
|
if line.strip().startswith(("#", "--hash")):
|
2020-10-24 09:33:54 +02:00
|
|
|
continue
|
|
|
|
dep = line.split(" \\", 1)[0].strip()
|
|
|
|
if dep:
|
|
|
|
result.append(dep)
|
2016-06-20 00:07:06 +02:00
|
|
|
return result
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def expand_reqs(fpath: str) -> list[str]:
|
2016-06-20 00:07:06 +02:00
|
|
|
"""
|
|
|
|
Returns a sorted list of unique dependencies specified by the requirements file `fpath`.
|
|
|
|
Removes comments from the output and recursively visits files specified inside `fpath`.
|
|
|
|
`fpath` can be either an absolute path or a relative path.
|
|
|
|
"""
|
|
|
|
absfpath = os.path.abspath(fpath)
|
2020-07-09 18:25:27 +02:00
|
|
|
output = expand_reqs_helper(absfpath)
|
2016-06-20 00:07:06 +02:00
|
|
|
return sorted(set(output))
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-07-09 18:33:16 +02:00
|
|
|
def python_version() -> str:
|
|
|
|
"""
|
|
|
|
Returns the Python version as string 'Python major.minor.patchlevel'
|
|
|
|
"""
|
2022-01-22 07:52:54 +01:00
|
|
|
return subprocess.check_output(["/usr/bin/python3", "-VV"], text=True)
|
2020-07-09 18:33:16 +02:00
|
|
|
|
2021-02-12 08:19:30 +01: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 hash_deps(deps: Iterable[str]) -> str:
|
2020-07-09 18:33:16 +02:00
|
|
|
deps_str = "\n".join(deps) + "\n" + python_version()
|
2021-08-02 23:20:39 +02:00
|
|
|
return hashlib.sha1(deps_str.encode()).hexdigest()
|
2016-06-20 00:07:06 +02:00
|
|
|
|
2021-02-12 08:19:30 +01: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 main() -> int:
|
2021-02-12 08:19:30 +01:00
|
|
|
description = (
|
|
|
|
"Finds the SHA1 hash of list of dependencies in a requirements file"
|
|
|
|
" after recursively visiting all files specified in it."
|
|
|
|
)
|
2016-06-20 00:07:06 +02:00
|
|
|
parser = argparse.ArgumentParser(description=description)
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument("fpath", metavar="FILE", help="Path to requirements file")
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--print", dest="print_reqs", action="store_true", help="Print all dependencies"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-06-20 00:07:06 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
deps = expand_reqs(args.fpath)
|
|
|
|
hash = hash_deps(deps)
|
|
|
|
print(hash)
|
|
|
|
if args.print_reqs:
|
|
|
|
for dep in deps:
|
|
|
|
print(dep)
|
|
|
|
return 0
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-06-20 00:07:06 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|