2017-08-18 21:42:06 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
if False:
|
|
|
|
from typing import Set, Text
|
|
|
|
|
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
sys.path.append(ZULIP_PATH)
|
2017-08-25 22:48:41 +02:00
|
|
|
from scripts.lib.hash_reqs import expand_reqs, hash_deps
|
2017-08-18 21:42:06 +02:00
|
|
|
from scripts.lib.zulip_tools import GENERIC_CACHE_SCRIPT_PARSER, \
|
2017-08-23 23:21:22 +02:00
|
|
|
get_caches_to_be_purged, get_environment, get_recent_deployments, \
|
|
|
|
purge_unused_caches
|
2017-08-18 21:42:06 +02:00
|
|
|
|
|
|
|
ENV = get_environment()
|
|
|
|
VENV_CACHE_DIR = '/srv/zulip-venv-cache'
|
|
|
|
if ENV == "travis":
|
|
|
|
VENV_CACHE_DIR = os.path.join(os.environ["HOME"], "zulip-venv-cache")
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
# type: () -> argparse.Namespace
|
|
|
|
parser = argparse.ArgumentParser(description="This script cleans unused zulip venv caches.",
|
|
|
|
parents=[GENERIC_CACHE_SCRIPT_PARSER, ])
|
|
|
|
args = parser.parse_args()
|
|
|
|
return args
|
|
|
|
|
|
|
|
def get_caches_in_use(threshold_days):
|
|
|
|
# type: (int) -> Set[Text]
|
|
|
|
setups_to_check = set([ZULIP_PATH, ])
|
|
|
|
caches_in_use = set()
|
2017-08-25 22:48:41 +02:00
|
|
|
|
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":
|
|
|
|
CACHE_SYMLINK = os.path.join(os.path.dirname(ZULIP_PATH), "zulip-py3-venv")
|
|
|
|
CURRENT_CACHE = os.path.dirname(os.path.realpath(CACHE_SYMLINK))
|
|
|
|
caches_in_use.add(CURRENT_CACHE)
|
2017-08-25 22:48:41 +02:00
|
|
|
|
2017-08-18 21:42:06 +02:00
|
|
|
for path in setups_to_check:
|
|
|
|
for filename in os.listdir(os.path.join(path, "requirements")):
|
|
|
|
requirements_file = os.path.join(path, "requirements", filename)
|
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
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# type: () -> None
|
|
|
|
args = parse_args()
|
|
|
|
caches_in_use = get_caches_in_use(args.threshold_days)
|
2017-08-23 23:21:22 +02:00
|
|
|
purge_unused_caches(VENV_CACHE_DIR, caches_in_use, args.threshold_days, args.dry_run, "venv")
|
2017-08-18 21:42:06 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|