2017-08-19 19:55:42 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
|
|
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-19 19:55:42 +02:00
|
|
|
sys.path.append(ZULIP_PATH)
|
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-19 19:55:42 +02:00
|
|
|
|
|
|
|
ENV = get_environment()
|
|
|
|
EMOJI_CACHE_PATH = "/srv/zulip-emoji-cache"
|
|
|
|
|
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-19 19:55:42 +02:00
|
|
|
caches_in_use = set()
|
|
|
|
|
|
|
|
if ENV == "prod":
|
|
|
|
setups_to_check |= get_recent_deployments(threshold_days)
|
|
|
|
if ENV == "dev":
|
|
|
|
CACHE_SYMLINK = os.path.join(ZULIP_PATH, "static", "generated", "emoji")
|
|
|
|
CURRENT_CACHE = os.path.dirname(os.path.realpath(CACHE_SYMLINK))
|
|
|
|
caches_in_use.add(CURRENT_CACHE)
|
|
|
|
|
|
|
|
for setup_dir in setups_to_check:
|
2018-03-29 00:48:29 +02:00
|
|
|
emoji_link_path = os.path.join(setup_dir, "static/generated/emoji")
|
|
|
|
if not os.path.islink(emoji_link_path):
|
|
|
|
# This happens for a deployment directory extracted from a
|
|
|
|
# tarball, which just has a copy of the emoji data, not a symlink.
|
2017-08-19 19:55:42 +02:00
|
|
|
continue
|
2018-12-03 20:59:08 +01:00
|
|
|
# The actual cache path doesn't include the /emoji
|
|
|
|
caches_in_use.add(os.path.dirname(os.readlink(emoji_link_path)))
|
2017-08-19 19:55:42 +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-19 19:55:42 +02:00
|
|
|
caches_in_use = get_caches_in_use(args.threshold_days)
|
2021-02-12 08:19:30 +01:00
|
|
|
purge_unused_caches(EMOJI_CACHE_PATH, caches_in_use, "emoji cache", args)
|
|
|
|
|
2017-08-19 19:55:42 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-10-23 02:43:28 +02:00
|
|
|
args = parse_cache_script_args("This script cleans unused Zulip emoji caches.")
|
2018-06-18 16:28:34 +02:00
|
|
|
main(args)
|