2019-03-05 00:30:58 +01:00
|
|
|
import logging
|
2016-06-21 19:37:36 +02:00
|
|
|
import os
|
2018-07-18 23:50:15 +02:00
|
|
|
import shutil
|
2016-06-21 19:37:36 +02:00
|
|
|
import subprocess
|
2024-07-12 02:30:17 +02:00
|
|
|
from typing import Optional
|
2016-06-21 19:37:36 +02:00
|
|
|
|
2020-07-10 23:02:37 +02:00
|
|
|
from scripts.lib.hash_reqs import expand_reqs, python_version
|
2020-06-11 00:54:34 +02:00
|
|
|
from scripts.lib.zulip_tools import ENDC, WARNING, os_families, run, run_as_root
|
2019-07-23 23:58:11 +02:00
|
|
|
|
2017-09-22 08:15:01 +02:00
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2016-06-21 19:37:36 +02:00
|
|
|
VENV_CACHE_PATH = "/srv/zulip-venv-cache"
|
|
|
|
|
2016-06-22 18:17:46 +02:00
|
|
|
VENV_DEPENDENCIES = [
|
2016-06-24 18:09:36 +02:00
|
|
|
"build-essential",
|
2016-06-22 18:17:46 +02:00
|
|
|
"libffi-dev",
|
|
|
|
"libldap2-dev",
|
2021-02-12 08:19:30 +01:00
|
|
|
"python3-dev", # Needed to install typed-ast dependency of mypy
|
2017-08-04 01:43:32 +02:00
|
|
|
"python3-pip",
|
2019-06-27 00:14:20 +02:00
|
|
|
"virtualenv",
|
2021-03-12 07:06:16 +01:00
|
|
|
"libxml2-dev", # Used for installing talon-core and python-xmlsec
|
|
|
|
"libxslt1-dev", # Used for installing talon-core
|
2021-02-12 08:19:30 +01:00
|
|
|
"libpq-dev", # Needed by psycopg2
|
|
|
|
"libssl-dev", # Needed to build pycurl and other libraries
|
|
|
|
"libmagic1", # Used for install python-magic
|
|
|
|
"libyaml-dev", # For fast YAML parsing in PyYAML
|
2019-09-19 22:31:52 +02:00
|
|
|
# Needed by python-xmlsec:
|
|
|
|
"libxmlsec1-dev",
|
2019-10-03 23:42:13 +02:00
|
|
|
"pkg-config",
|
2023-02-09 01:13:42 +01:00
|
|
|
"jq", # No longer used in production (clean me up later)
|
2021-02-12 08:19:30 +01:00
|
|
|
"libsasl2-dev", # For building python-ldap from source
|
2024-06-12 20:19:15 +02:00
|
|
|
"libvips", # For thumbnailing
|
2016-06-22 18:17:46 +02:00
|
|
|
]
|
|
|
|
|
2018-12-17 20:52:29 +01:00
|
|
|
COMMON_YUM_VENV_DEPENDENCIES = [
|
2018-12-10 14:31:38 +01:00
|
|
|
"libffi-devel",
|
|
|
|
"openldap-devel",
|
2020-08-08 03:17:37 +02:00
|
|
|
"libyaml-devel",
|
2019-09-19 22:31:52 +02:00
|
|
|
# Needed by python-xmlsec:
|
2020-09-04 01:10:32 +02:00
|
|
|
"gcc",
|
2019-09-19 22:31:52 +02:00
|
|
|
"python3-devel",
|
2018-12-10 14:31:38 +01:00
|
|
|
"libxml2-devel",
|
2019-09-19 22:31:52 +02:00
|
|
|
"xmlsec1-devel",
|
|
|
|
"xmlsec1-openssl-devel",
|
|
|
|
"libtool-ltdl-devel",
|
2018-12-10 14:31:38 +01:00
|
|
|
"libxslt-devel",
|
|
|
|
"postgresql-libs", # libpq-dev on apt
|
|
|
|
"openssl-devel",
|
|
|
|
"jq",
|
2024-06-12 20:19:15 +02:00
|
|
|
"libvips", # For thumbnailing
|
2018-12-10 14:31:38 +01:00
|
|
|
]
|
|
|
|
|
2020-09-02 06:59:07 +02:00
|
|
|
REDHAT_VENV_DEPENDENCIES = [
|
|
|
|
*COMMON_YUM_VENV_DEPENDENCIES,
|
2018-12-17 22:04:18 +01:00
|
|
|
"python36-devel",
|
2018-12-19 02:45:32 +01:00
|
|
|
"python-virtualenv",
|
|
|
|
]
|
|
|
|
|
2020-09-02 06:59:07 +02:00
|
|
|
FEDORA_VENV_DEPENDENCIES = [
|
|
|
|
*COMMON_YUM_VENV_DEPENDENCIES,
|
2018-12-19 02:45:32 +01:00
|
|
|
"python3-pip",
|
|
|
|
"virtualenv", # see https://unix.stackexchange.com/questions/27877/install-virtualenv-on-fedora-16
|
|
|
|
]
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_venv_dependencies(vendor: str, os_version: str) -> list[str]:
|
2020-03-21 04:31:01 +01:00
|
|
|
if "debian" in os_families():
|
|
|
|
return VENV_DEPENDENCIES
|
2020-03-22 21:06:37 +01:00
|
|
|
elif "rhel" in os_families():
|
|
|
|
return REDHAT_VENV_DEPENDENCIES
|
|
|
|
elif "fedora" in os_families():
|
|
|
|
return FEDORA_VENV_DEPENDENCIES
|
|
|
|
else:
|
|
|
|
raise AssertionError("Invalid vendor")
|
2020-03-17 20:27:35 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-03-21 04:31:01 +01:00
|
|
|
def install_venv_deps(pip: str, requirements_file: str) -> None:
|
|
|
|
pip_requirements = os.path.join(ZULIP_PATH, "requirements", "pip.txt")
|
2020-09-03 05:58:10 +02:00
|
|
|
run([pip, "install", "--force-reinstall", "--require-hashes", "-r", pip_requirements])
|
2021-05-08 02:27:36 +02:00
|
|
|
run(
|
|
|
|
[
|
|
|
|
pip,
|
|
|
|
"install",
|
|
|
|
"--use-deprecated=legacy-resolver", # https://github.com/pypa/pip/issues/5780
|
|
|
|
"--no-deps",
|
|
|
|
"--require-hashes",
|
|
|
|
"-r",
|
|
|
|
requirements_file,
|
|
|
|
]
|
|
|
|
)
|
2017-06-13 17:04:54 +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 get_index_filename(venv_path: str) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return os.path.join(venv_path, "package_index")
|
2016-08-11 13:32:17 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_package_names(requirements_file: str) -> list[str]:
|
2016-08-11 13:32:17 +02:00
|
|
|
packages = expand_reqs(requirements_file)
|
|
|
|
cleaned = []
|
2021-02-12 08:20:45 +01:00
|
|
|
operators = ["~=", "==", "!=", "<", ">"]
|
2016-08-11 13:32:17 +02:00
|
|
|
for package in packages:
|
2021-02-12 08:20:45 +01:00
|
|
|
if package.startswith("git+https://") and "#egg=" in package:
|
2016-10-16 08:58:09 +02:00
|
|
|
split_package = package.split("#egg=")
|
|
|
|
if len(split_package) != 2:
|
2021-08-02 23:36:06 +02:00
|
|
|
raise Exception(f"Unexpected duplicate #egg in package {package}")
|
2016-10-16 08:58:09 +02:00
|
|
|
# Extract the package name from Git requirements entries
|
|
|
|
package = split_package[1]
|
|
|
|
|
2016-08-11 13:32:17 +02:00
|
|
|
for operator in operators:
|
|
|
|
if operator in package:
|
|
|
|
package = package.split(operator)[0]
|
|
|
|
|
|
|
|
package = package.strip()
|
|
|
|
if package:
|
|
|
|
cleaned.append(package.lower())
|
|
|
|
|
|
|
|
return sorted(cleaned)
|
|
|
|
|
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 create_requirements_index_file(venv_path: str, requirements_file: str) -> str:
|
2016-08-11 13:32:17 +02:00
|
|
|
"""
|
|
|
|
Creates a file, called package_index, in the virtual environment
|
|
|
|
directory that contains all the PIP packages installed in the
|
|
|
|
virtual environment. This file is used to determine the packages
|
|
|
|
that can be copied to a new virtual environment.
|
|
|
|
"""
|
|
|
|
index_filename = get_index_filename(venv_path)
|
|
|
|
packages = get_package_names(requirements_file)
|
2021-02-12 08:20:45 +01:00
|
|
|
with open(index_filename, "w") as writer:
|
|
|
|
writer.write("\n".join(packages))
|
|
|
|
writer.write("\n")
|
2016-08-11 13:32:17 +02:00
|
|
|
|
|
|
|
return index_filename
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_venv_packages(venv_path: str) -> set[str]:
|
2016-08-11 13:32:17 +02:00
|
|
|
"""
|
|
|
|
Returns the packages installed in the virtual environment using the
|
|
|
|
package index file.
|
|
|
|
"""
|
|
|
|
with open(get_index_filename(venv_path)) as reader:
|
2021-02-12 08:20:45 +01:00
|
|
|
return {p.strip() for p in reader.read().split("\n") if p.strip()}
|
2016-08-11 13:32:17 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def try_to_copy_venv(venv_path: str, new_packages: set[str]) -> bool:
|
2016-08-11 13:32:17 +02:00
|
|
|
"""
|
|
|
|
Tries to copy packages from an old virtual environment in the cache
|
|
|
|
to the new virtual environment. The algorithm works as follows:
|
|
|
|
1. Find a virtual environment, v, from the cache that has the
|
|
|
|
highest overlap with the new requirements such that:
|
|
|
|
a. The new requirements only add to the packages of v.
|
|
|
|
b. The new requirements only upgrade packages of v.
|
|
|
|
2. Copy the contents of v to the new virtual environment using
|
|
|
|
virtualenv-clone.
|
|
|
|
3. Delete all .pyc files in the new virtual environment.
|
|
|
|
"""
|
2016-09-20 15:55:08 +02:00
|
|
|
if not os.path.exists(VENV_CACHE_PATH):
|
|
|
|
return False
|
|
|
|
|
2020-07-10 23:02:37 +02:00
|
|
|
desired_python_version = python_version()
|
2016-08-11 13:32:17 +02:00
|
|
|
venv_name = os.path.basename(venv_path)
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
overlaps: list[tuple[int, str, set[str]]] = []
|
|
|
|
old_packages: set[str] = set()
|
2016-08-11 13:32:17 +02:00
|
|
|
for sha1sum in os.listdir(VENV_CACHE_PATH):
|
|
|
|
curr_venv_path = os.path.join(VENV_CACHE_PATH, sha1sum, venv_name)
|
2021-02-12 08:19:30 +01:00
|
|
|
if curr_venv_path == venv_path or not os.path.exists(get_index_filename(curr_venv_path)):
|
2016-08-11 13:32:17 +02:00
|
|
|
continue
|
|
|
|
|
2020-07-10 23:02:37 +02:00
|
|
|
# Check the Python version in the venv matches the version we want to use.
|
|
|
|
venv_python3 = os.path.join(curr_venv_path, "bin", "python3")
|
|
|
|
if not os.path.exists(venv_python3):
|
|
|
|
continue
|
2022-01-22 07:52:54 +01:00
|
|
|
venv_python_version = subprocess.check_output([venv_python3, "-VV"], text=True)
|
2020-07-10 23:02:37 +02:00
|
|
|
if desired_python_version != venv_python_version:
|
|
|
|
continue
|
|
|
|
|
2016-08-11 13:32:17 +02:00
|
|
|
old_packages = get_venv_packages(curr_venv_path)
|
2023-10-09 21:28:43 +02:00
|
|
|
# We only consider using old virtualenvs that only
|
2016-08-11 13:32:17 +02:00
|
|
|
# contain packages that we want in our new virtualenv.
|
|
|
|
if not (old_packages - new_packages):
|
|
|
|
overlap = new_packages & old_packages
|
|
|
|
overlaps.append((len(overlap), curr_venv_path, overlap))
|
|
|
|
|
|
|
|
target_log = get_logfile_name(venv_path)
|
|
|
|
source_venv_path = None
|
|
|
|
if overlaps:
|
|
|
|
# Here, we select the old virtualenv with the largest overlap
|
|
|
|
overlaps = sorted(overlaps)
|
|
|
|
_, source_venv_path, copied_packages = overlaps[-1]
|
2021-08-02 23:36:06 +02:00
|
|
|
print(f"Copying packages from {source_venv_path}")
|
|
|
|
clone_ve = f"{source_venv_path}/bin/virtualenv-clone"
|
2019-03-05 00:28:25 +01:00
|
|
|
cmd = [clone_ve, source_venv_path, venv_path]
|
|
|
|
|
2016-08-11 13:32:17 +02:00
|
|
|
try:
|
2018-07-30 20:54:12 +02:00
|
|
|
# TODO: We can probably remove this in a few months, now
|
|
|
|
# that we can expect that virtualenv-clone is present in
|
|
|
|
# all of our recent virtualenvs.
|
2019-03-05 00:28:25 +01:00
|
|
|
run_as_root(cmd)
|
2019-03-05 00:29:45 +01:00
|
|
|
except subprocess.CalledProcessError:
|
2019-03-05 00:30:58 +01:00
|
|
|
# Virtualenv-clone is either not installed or threw an
|
|
|
|
# error. Just return False: making a new venv is safe.
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.warning("Error cloning virtualenv %s", source_venv_path)
|
2019-03-05 00:30:58 +01:00
|
|
|
return False
|
2016-08-11 13:32:17 +02:00
|
|
|
|
2019-02-16 20:21:14 +01:00
|
|
|
# virtualenv-clone, unfortunately, copies the success stamp,
|
|
|
|
# which means if the upcoming `pip install` phase were to
|
|
|
|
# fail, we'd end up with a broken half-provisioned virtualenv
|
|
|
|
# that's incorrectly tagged as properly provisioned. The
|
|
|
|
# right fix is to use
|
|
|
|
# https://github.com/edwardgeorge/virtualenv-clone/pull/38,
|
|
|
|
# but this rm is almost as good.
|
2021-02-12 08:20:45 +01:00
|
|
|
success_stamp_path = os.path.join(venv_path, "success-stamp")
|
2019-02-26 20:20:46 +01:00
|
|
|
run_as_root(["rm", "-f", success_stamp_path])
|
2019-02-16 20:21:14 +01:00
|
|
|
|
2021-08-02 23:36:06 +02:00
|
|
|
run_as_root(["chown", "-R", f"{os.getuid()}:{os.getgid()}", venv_path])
|
2016-08-11 13:32:17 +02:00
|
|
|
source_log = get_logfile_name(source_venv_path)
|
|
|
|
copy_parent_log(source_log, target_log)
|
2021-02-12 08:19:30 +01:00
|
|
|
create_log_entry(
|
|
|
|
target_log, source_venv_path, copied_packages, new_packages - copied_packages
|
|
|
|
)
|
2016-08-11 13:32:17 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
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 get_logfile_name(venv_path: str) -> str:
|
2021-08-02 23:36:06 +02:00
|
|
|
return f"{venv_path}/setup-venv.log"
|
2016-08-11 13:32:17 +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 create_log_entry(
|
2021-02-12 08:19:30 +01:00
|
|
|
target_log: str,
|
|
|
|
parent: str,
|
2024-07-12 02:30:17 +02:00
|
|
|
copied_packages: set[str],
|
|
|
|
new_packages: set[str],
|
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
|
|
|
) -> None:
|
2017-09-22 08:15:01 +02:00
|
|
|
venv_path = os.path.dirname(target_log)
|
2021-02-12 08:20:45 +01:00
|
|
|
with open(target_log, "a") as writer:
|
2021-08-02 23:36:06 +02:00
|
|
|
writer.write(f"{venv_path}\n")
|
2016-08-11 13:32:17 +02:00
|
|
|
if copied_packages:
|
2021-08-02 23:36:06 +02:00
|
|
|
writer.write(f"Copied from {parent}:\n")
|
|
|
|
writer.write("\n".join(f"- {p}" for p in sorted(copied_packages)))
|
2016-08-11 13:32:17 +02:00
|
|
|
writer.write("\n")
|
|
|
|
|
|
|
|
writer.write("New packages:\n")
|
2021-08-02 23:36:06 +02:00
|
|
|
writer.write("\n".join(f"- {p}" for p in sorted(new_packages)))
|
2016-08-11 13:32:17 +02:00
|
|
|
writer.write("\n\n")
|
|
|
|
|
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 copy_parent_log(source_log: str, target_log: str) -> None:
|
2016-08-11 13:32:17 +02:00
|
|
|
if os.path.exists(source_log):
|
2018-07-18 23:50:15 +02:00
|
|
|
shutil.copyfile(source_log, target_log)
|
2016-08-11 13:32:17 +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 do_patch_activate_script(venv_path: str) -> None:
|
2016-07-20 21:42:33 +02:00
|
|
|
"""
|
|
|
|
Patches the bin/activate script so that the value of the environment variable VIRTUAL_ENV
|
|
|
|
is set to venv_path during the script's execution whenever it is sourced.
|
|
|
|
"""
|
2016-07-20 15:34:31 +02:00
|
|
|
# venv_path should be what we want to have in VIRTUAL_ENV after patching
|
|
|
|
script_path = os.path.join(venv_path, "bin", "activate")
|
|
|
|
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(script_path) as f:
|
2019-07-14 21:37:08 +02:00
|
|
|
lines = f.readlines()
|
2016-07-20 15:34:31 +02:00
|
|
|
for i, line in enumerate(lines):
|
2021-02-12 08:20:45 +01:00
|
|
|
if line.startswith("VIRTUAL_ENV="):
|
2021-08-02 23:36:06 +02:00
|
|
|
lines[i] = f'VIRTUAL_ENV="{venv_path}"\n'
|
2016-07-20 15:34:31 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with open(script_path, "w") as f:
|
2019-07-14 21:37:08 +02:00
|
|
|
f.write("".join(lines))
|
2016-07-20 15:34:31 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-07-09 18:33:16 +02:00
|
|
|
def generate_hash(requirements_file: str) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
path = os.path.join(ZULIP_PATH, "scripts", "lib", "hash_reqs.py")
|
2022-01-22 07:52:54 +01:00
|
|
|
output = subprocess.check_output([path, requirements_file], text=True)
|
2020-07-09 18:33:16 +02:00
|
|
|
return output.split()[0]
|
|
|
|
|
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 setup_virtualenv(
|
|
|
|
target_venv_path: Optional[str],
|
|
|
|
requirements_file: str,
|
|
|
|
patch_activate_script: bool = False,
|
|
|
|
) -> str:
|
2020-07-09 18:33:16 +02:00
|
|
|
sha1sum = generate_hash(requirements_file)
|
2016-06-21 19:37:36 +02:00
|
|
|
# Check if a cached version already exists
|
2016-06-22 20:04:14 +02:00
|
|
|
if target_venv_path is None:
|
2021-02-12 08:20:45 +01:00
|
|
|
cached_venv_path = os.path.join(VENV_CACHE_PATH, sha1sum, "venv")
|
2016-06-22 20:04:14 +02:00
|
|
|
else:
|
2021-02-12 08:19:30 +01:00
|
|
|
cached_venv_path = os.path.join(
|
|
|
|
VENV_CACHE_PATH, sha1sum, os.path.basename(target_venv_path)
|
|
|
|
)
|
2016-06-21 19:37:36 +02:00
|
|
|
success_stamp = os.path.join(cached_venv_path, "success-stamp")
|
|
|
|
if not os.path.exists(success_stamp):
|
2020-03-21 04:31:01 +01:00
|
|
|
do_setup_virtualenv(cached_venv_path, requirements_file)
|
2021-02-12 08:20:45 +01:00
|
|
|
with open(success_stamp, "w") as f:
|
2019-07-14 21:37:08 +02:00
|
|
|
f.close()
|
2016-06-21 19:37:36 +02:00
|
|
|
|
2021-08-02 23:36:06 +02:00
|
|
|
print(f"Using cached Python venv from {cached_venv_path}")
|
2016-06-22 20:04:14 +02:00
|
|
|
if target_venv_path is not None:
|
2019-02-26 20:20:46 +01:00
|
|
|
run_as_root(["ln", "-nsf", cached_venv_path, target_venv_path])
|
2016-07-20 21:42:33 +02:00
|
|
|
if patch_activate_script:
|
|
|
|
do_patch_activate_script(target_venv_path)
|
2016-06-22 20:04:14 +02:00
|
|
|
return cached_venv_path
|
2016-06-21 19:37:36 +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 add_cert_to_pipconf() -> None:
|
2018-08-02 16:29:47 +02:00
|
|
|
conffile = os.path.expanduser("~/.pip/pip.conf")
|
|
|
|
confdir = os.path.expanduser("~/.pip/")
|
|
|
|
os.makedirs(confdir, exist_ok=True)
|
|
|
|
run(["crudini", "--set", conffile, "global", "cert", os.environ["CUSTOM_CA_CERTIFICATES"]])
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-03-21 04:31:01 +01:00
|
|
|
def do_setup_virtualenv(venv_path: str, requirements_file: str) -> None:
|
docs: Add missing space to compound verbs “back up”, “log in”, etc.
Noun: backup, login, logout, lookup, setup.
Verb: back up, log in, log out, look up, set up.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-02-07 20:41:10 +01:00
|
|
|
# Set up Python virtualenv
|
2016-08-11 13:32:17 +02:00
|
|
|
new_packages = set(get_package_names(requirements_file))
|
|
|
|
|
2019-02-26 20:20:46 +01:00
|
|
|
run_as_root(["rm", "-rf", venv_path])
|
2016-08-11 13:32:17 +02:00
|
|
|
if not try_to_copy_venv(venv_path, new_packages):
|
|
|
|
# Create new virtualenv.
|
2019-02-26 20:20:46 +01:00
|
|
|
run_as_root(["mkdir", "-p", venv_path])
|
2021-09-23 22:11:09 +02:00
|
|
|
run_as_root(["virtualenv", "-p", "python3", "--no-download", venv_path])
|
2021-08-02 23:36:06 +02:00
|
|
|
run_as_root(["chown", "-R", f"{os.getuid()}:{os.getgid()}", venv_path])
|
2016-08-11 13:32:17 +02:00
|
|
|
create_log_entry(get_logfile_name(venv_path), "", set(), new_packages)
|
2016-06-21 19:37:36 +02:00
|
|
|
|
2016-08-11 13:32:17 +02:00
|
|
|
create_requirements_index_file(venv_path, requirements_file)
|
2019-02-01 02:05:44 +01:00
|
|
|
|
|
|
|
pip = os.path.join(venv_path, "bin", "pip")
|
2016-06-21 19:37:36 +02:00
|
|
|
|
2018-08-02 16:29:47 +02:00
|
|
|
# use custom certificate if needed
|
2021-02-12 08:20:45 +01:00
|
|
|
if os.environ.get("CUSTOM_CA_CERTIFICATES"):
|
2018-08-02 16:29:47 +02:00
|
|
|
print("Configuring pip to use custom CA certificates...")
|
|
|
|
add_cert_to_pipconf()
|
|
|
|
|
2017-06-13 17:04:54 +02:00
|
|
|
try:
|
2020-03-21 04:31:01 +01:00
|
|
|
install_venv_deps(pip, requirements_file)
|
2017-06-13 17:04:54 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2021-03-03 19:28:21 +01:00
|
|
|
try:
|
|
|
|
# Might be a failure due to network connection issues. Retrying...
|
|
|
|
print(WARNING + "`pip install` failed; retrying..." + ENDC)
|
|
|
|
install_venv_deps(pip, requirements_file)
|
|
|
|
except BaseException as e:
|
|
|
|
# Suppress exception chaining
|
|
|
|
raise e from None
|
2018-12-17 22:04:18 +01:00
|
|
|
|
2019-02-26 20:20:46 +01:00
|
|
|
run_as_root(["chmod", "-R", "a+rX", venv_path])
|