2017-08-18 21:42:06 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
2020-01-30 02:35:44 +01:00
|
|
|
import glob
|
2017-08-18 21:42:06 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2017-08-28 02:59:49 +02:00
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2017-08-18 21:42:06 +02:00
|
|
|
sys.path.append(ZULIP_PATH)
|
2017-08-25 22:48:41 +02:00
|
|
|
from scripts.lib.hash_reqs import expand_reqs, hash_deps
|
2020-06-11 00:54:34 +02:00
|
|
|
from scripts.lib.zulip_tools import (
|
|
|
|
get_environment,
|
|
|
|
get_recent_deployments,
|
|
|
|
parse_cache_script_args,
|
|
|
|
purge_unused_caches,
|
|
|
|
)
|
2017-08-18 21:42:06 +02:00
|
|
|
|
|
|
|
ENV = get_environment()
|
2021-02-12 08:20:45 +01:00
|
|
|
VENV_CACHE_DIR = "/srv/zulip-venv-cache"
|
2017-08-18 21:42:06 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_caches_in_use(threshold_days: int) -> set[str]:
|
2020-04-09 21:51:58 +02:00
|
|
|
setups_to_check = {ZULIP_PATH}
|
2017-08-18 21:42:06 +02:00
|
|
|
caches_in_use = set()
|
2017-08-25 22:48:41 +02:00
|
|
|
|
2018-02-20 01:14:20 +01:00
|
|
|
def add_current_venv_cache(venv_name: str) -> None:
|
|
|
|
CACHE_SYMLINK = os.path.join(os.path.dirname(ZULIP_PATH), venv_name)
|
|
|
|
CURRENT_CACHE = os.path.dirname(os.path.realpath(CACHE_SYMLINK))
|
|
|
|
caches_in_use.add(CURRENT_CACHE)
|
|
|
|
|
2017-08-18 21:42:06 +02:00
|
|
|
if ENV == "prod":
|
|
|
|
setups_to_check |= get_recent_deployments(threshold_days)
|
2017-08-25 23:29:01 +02:00
|
|
|
if ENV == "dev":
|
2018-02-20 01:14:20 +01:00
|
|
|
add_current_venv_cache("zulip-py3-venv")
|
2017-08-25 22:48:41 +02:00
|
|
|
|
2017-08-18 21:42:06 +02:00
|
|
|
for path in setups_to_check:
|
2017-10-29 21:21:46 +01:00
|
|
|
reqs_dir = os.path.join(path, "requirements")
|
|
|
|
# If the target directory doesn't contain a requirements
|
|
|
|
# directory, skip it to avoid throwing an exception trying to
|
|
|
|
# list its requirements subdirectory.
|
|
|
|
if not os.path.exists(reqs_dir):
|
|
|
|
continue
|
2020-01-30 02:35:44 +01:00
|
|
|
requirements_files = glob.glob(os.path.join(reqs_dir, "*.txt"))
|
|
|
|
for requirements_file in requirements_files:
|
2017-08-25 22:48:41 +02:00
|
|
|
deps = expand_reqs(requirements_file)
|
|
|
|
hash_val = hash_deps(deps)
|
|
|
|
caches_in_use.add(os.path.join(VENV_CACHE_DIR, hash_val))
|
|
|
|
|
2017-08-18 21:42:06 +02:00
|
|
|
return caches_in_use
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-06-18 16:28:34 +02:00
|
|
|
def main(args: argparse.Namespace) -> None:
|
2017-08-18 21:42:06 +02:00
|
|
|
caches_in_use = get_caches_in_use(args.threshold_days)
|
2021-02-12 08:19:30 +01:00
|
|
|
purge_unused_caches(VENV_CACHE_DIR, caches_in_use, "venv cache", args)
|
|
|
|
|
2017-08-18 21:42:06 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-10-23 02:43:28 +02:00
|
|
|
args = parse_cache_script_args("This script cleans unused Zulip venv caches.")
|
2018-06-18 16:28:34 +02:00
|
|
|
main(args)
|