clean-npm-cache: Expand clean-npm-cache.

Expands `clean-npm-cache` so that it can be used in production
environment as well. Also moves it to `scripts/` from `tools/`.
This commit is contained in:
Harshit Bansal
2017-08-19 07:26:52 +00:00
committed by Tim Abbott
parent 8e41bbe2b0
commit 948cf54ee3
3 changed files with 68 additions and 34 deletions

View File

@@ -20,7 +20,7 @@ install:
# Clean any npm cache which was generated but not required anymore to avoid
# our cache becoming huge.
- mispipe "tools/clean-npm-cache --travis" ts
- mispipe "scripts/clean-npm-cache --threshold 0" ts
# Clean any emoji cache which was generated but not required anymore to avoid
# our cache becoming huge.

67
scripts/clean-npm-cache Executable file
View File

@@ -0,0 +1,67 @@
#!/usr/bin/env python3
import argparse
import os
import subprocess
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.node_cache import generate_sha1sum_node_modules
from scripts.lib.zulip_tools import GENERIC_CACHE_SCRIPT_PARSER, \
get_caches_to_be_purged, get_environment, get_recent_deployments, \
purge_unused_caches
ENV = get_environment()
NODE_MODULES_CACHE_PATH = "/srv/zulip-npm-cache"
if ENV == "travis":
NODE_MODULES_CACHE_PATH = os.path.join(os.environ["HOME"], "zulip-npm-cache")
try:
subprocess.check_output(["/srv/zulip-yarn/bin/yarn", '--version'])
except OSError:
print('yarn not found. Most probably we are running static-analysis and '
'hence yarn is not installed. Exiting without cleaning npm cache.')
sys.exit(0)
def parse_args():
# type: () -> argparse.Namespace
parser = argparse.ArgumentParser(description="This script cleans unused zulip npm 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":
# In dev always include the currently active cache in order
# not to break current installation in case dependencies
# are updated with bumping the provision version.
CURRENT_CACHE = os.path.dirname(os.path.realpath(os.path.join(ZULIP_PATH, "node_modules")))
caches_in_use.add(CURRENT_CACHE)
for setup_dir in setups_to_check:
PACKAGES_FILE = os.path.join(setup_dir, "package.json")
if os.path.exists(PACKAGES_FILE):
# If 'package.json' file doesn't exist then no node_modules
# cache is associated with this setup.
sha1sum = generate_sha1sum_node_modules(setup_dir=setup_dir)
caches_in_use.add(os.path.join(NODE_MODULES_CACHE_PATH, sha1sum))
return caches_in_use
def main():
# type: () -> None
args = parse_args()
caches_in_use = get_caches_in_use(args.threshold_days)
purge_unused_caches(
NODE_MODULES_CACHE_PATH, caches_in_use, args.threshold_days, args.dry_run, "node modules")
if __name__ == "__main__":
main()

View File

@@ -1,33 +0,0 @@
#!/usr/bin/env python3
from __future__ import absolute_import
from __future__ import print_function
import os
import subprocess
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from scripts.lib.node_cache import generate_sha1sum_node_modules
NODE_MODULES_CACHE_PATH = "/srv/zulip-npm-cache"
if "--travis" in sys.argv:
NODE_MODULES_CACHE_PATH = os.path.join(os.environ["HOME"], "zulip-npm-cache")
try:
subprocess.check_output([os.path.join(NODE_MODULES_CACHE_PATH, "yarn/bin/yarn"), '--version'])
except OSError:
print('yarn not found. Most probably we are running static-analysis and '
'hence yarn is not installed. Exiting without cleaning npm cache.')
sys.exit(0)
sha1sum = generate_sha1sum_node_modules()
current_cache_dir_base = os.path.join(NODE_MODULES_CACHE_PATH, sha1sum)
current_success_stamp = os.path.join(current_cache_dir_base, '.success-stamp')
print("Current cache stamp at %s" % (current_success_stamp,))
for cache_dir_base in os.listdir(NODE_MODULES_CACHE_PATH):
node_modules_dir = os.path.join(NODE_MODULES_CACHE_PATH, cache_dir_base)
if node_modules_dir == current_cache_dir_base and os.path.exists(current_success_stamp):
print("Keeping used node_modules cache dir %s" % (node_modules_dir,))
else:
print("Cleaning unused node_modules cache dir %s" % (node_modules_dir,))
subprocess.check_call(["sudo", "rm", "-rf", node_modules_dir])