zulip/tools/clean-npm-cache

34 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python
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])