2017-08-19 09:26:52 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2019-07-23 23:58:11 +02:00
|
|
|
from typing import Set
|
2017-08-19 09:26:52 +02:00
|
|
|
|
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 09:26:52 +02:00
|
|
|
sys.path.append(ZULIP_PATH)
|
2019-02-02 23:53:23 +01:00
|
|
|
from scripts.lib.zulip_tools import \
|
2017-09-23 20:54:19 +02:00
|
|
|
get_environment, get_recent_deployments, parse_cache_script_args, \
|
2017-08-19 09:26:52 +02:00
|
|
|
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:
|
2017-11-23 20:27:18 +01:00
|
|
|
subprocess.check_output(["/home/travis/zulip-yarn/bin/yarn", '--version'])
|
2017-08-19 09:26:52 +02:00
|
|
|
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)
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +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 09:26:52 +02:00
|
|
|
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:
|
2018-03-29 00:32:47 +02:00
|
|
|
node_modules_link_path = os.path.join(setup_dir, "node_modules")
|
2018-03-29 19:42:55 +02:00
|
|
|
if not os.path.islink(node_modules_link_path):
|
2017-08-19 09:26:52 +02:00
|
|
|
# If 'package.json' file doesn't exist then no node_modules
|
|
|
|
# cache is associated with this setup.
|
2018-03-29 00:32:47 +02:00
|
|
|
continue
|
2018-12-03 20:59:08 +01:00
|
|
|
# The actual cache path doesn't include the /node_modules
|
|
|
|
caches_in_use.add(os.path.dirname(os.readlink(node_modules_link_path)))
|
2017-08-19 09:26:52 +02:00
|
|
|
|
|
|
|
return caches_in_use
|
|
|
|
|
2018-06-18 16:28:34 +02:00
|
|
|
def main(args: argparse.Namespace) -> None:
|
2017-08-19 09:26:52 +02:00
|
|
|
caches_in_use = get_caches_in_use(args.threshold_days)
|
|
|
|
purge_unused_caches(
|
2017-09-23 21:21:55 +02:00
|
|
|
NODE_MODULES_CACHE_PATH, caches_in_use, "node modules cache", args)
|
2017-08-19 09:26:52 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-06-18 16:28:34 +02:00
|
|
|
args = parse_cache_script_args("This script cleans unused zulip npm caches.")
|
|
|
|
main(args)
|