From e5361b440337b252229037c3834687d624f12a26 Mon Sep 17 00:00:00 2001 From: Harshit Bansal Date: Sat, 19 Aug 2017 23:25:42 +0530 Subject: [PATCH] clean_emoji_cache: Expand `clean-emoji-cache`. Expands `clean-emoji-cache` so that it can be used in production environment as well. Also moves it to `scripts/` from `tools/`. --- .travis.yml | 2 +- scripts/clean-emoji-cache | 56 +++++++++++++++++++++++++++++++++++++++ tools/clean-emoji-cache | 23 ---------------- 3 files changed, 57 insertions(+), 24 deletions(-) create mode 100755 scripts/clean-emoji-cache delete mode 100755 tools/clean-emoji-cache diff --git a/.travis.yml b/.travis.yml index 3153224c60..fb73da070b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ install: # Clean any emoji cache which was generated but not required anymore to avoid # our cache becoming huge. - - mispipe "tools/clean-emoji-cache --travis" ts + - mispipe "scripts/clean-emoji-cache --threshold 0" ts script: # We unset GEM_PATH here as a hack to work around Travis CI having # broken running their system puppet with Ruby. See diff --git a/scripts/clean-emoji-cache b/scripts/clean-emoji-cache new file mode 100755 index 0000000000..4b699fbf6c --- /dev/null +++ b/scripts/clean-emoji-cache @@ -0,0 +1,56 @@ +#!/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) +from scripts.lib.zulip_tools import GENERIC_CACHE_SCRIPT_PARSER, \ + generate_sha1sum_emoji, get_caches_to_be_purged, get_environment, \ + get_recent_deployments, purge_unused_caches + +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 parse_args(): + # type: () -> argparse.Namespace + parser = argparse.ArgumentParser(description="This script cleans unused zulip emoji 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() + + 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 + args = parse_args() + caches_in_use = get_caches_in_use(args.threshold_days) + purge_unused_caches(EMOJI_CACHE_PATH, caches_in_use, args.threshold_days, args.dry_run, "emoji") + +if __name__ == "__main__": + main() diff --git a/tools/clean-emoji-cache b/tools/clean-emoji-cache deleted file mode 100755 index 539ac7fb6e..0000000000 --- a/tools/clean-emoji-cache +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python3 -from __future__ import absolute_import -from __future__ import print_function -import os -import subprocess -import sys - -ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - -EMOJI_CACHE_SYMLINK = os.path.join(ZULIP_PATH, 'static', 'generated', 'emoji') -EMOJI_CACHE_PATH = "/srv/zulip-emoji-cache" -if "--travis" in sys.argv: - EMOJI_CACHE_PATH = os.path.join(os.environ["HOME"], "zulip-emoji-cache") - -curr_emoji_cache_dir = os.path.dirname(os.path.realpath(EMOJI_CACHE_SYMLINK)) - -for cache_dir_base in os.listdir(EMOJI_CACHE_PATH): - emoji_cache_dir = os.path.join(EMOJI_CACHE_PATH, cache_dir_base) - if emoji_cache_dir not in curr_emoji_cache_dir: - print("Cleaning unused emoji cache dir %s" % (emoji_cache_dir,)) - subprocess.check_call(["sudo", "rm", "-rf", emoji_cache_dir]) - else: - print("Keeping used emoji cache dir %s" % (emoji_cache_dir,))