2017-08-19 19:55:42 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
if False:
|
|
|
|
from typing import Set, Text
|
|
|
|
|
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)
|
2017-09-23 20:54:19 +02:00
|
|
|
from scripts.lib.zulip_tools import generate_sha1sum_emoji, \
|
|
|
|
get_caches_to_be_purged, 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"
|
|
|
|
if ENV == "travis":
|
|
|
|
EMOJI_CACHE_PATH = os.path.join(os.environ["HOME"], "zulip-emoji-cache")
|
|
|
|
|
|
|
|
def get_caches_in_use(threshold_days):
|
|
|
|
# type: (int) -> Set[Text]
|
|
|
|
setups_to_check = set([ZULIP_PATH, ])
|
|
|
|
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:
|
|
|
|
zulip_emoji_dir = os.path.join(setup_dir, "tools", "setup", "emoji")
|
|
|
|
if not os.path.exists(zulip_emoji_dir):
|
|
|
|
# Most probably this is a deployment directory extracted from a
|
|
|
|
# tarball so no emoji cache is associated with this.
|
|
|
|
continue
|
|
|
|
sha1sum_emoji = generate_sha1sum_emoji(setup_dir)
|
|
|
|
caches_in_use.add(os.path.join(EMOJI_CACHE_PATH, sha1sum_emoji))
|
|
|
|
return caches_in_use
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# type: () -> None
|
2017-09-23 20:54:19 +02:00
|
|
|
args = parse_cache_script_args("This script cleans unused zulip emoji caches.")
|
2017-08-19 19:55:42 +02:00
|
|
|
caches_in_use = get_caches_in_use(args.threshold_days)
|
2017-08-30 23:58:00 +02:00
|
|
|
purge_unused_caches(
|
2017-09-23 21:21:55 +02:00
|
|
|
EMOJI_CACHE_PATH, caches_in_use, "emoji cache", args)
|
2017-08-19 19:55:42 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|