2016-08-18 13:51:25 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import hashlib
|
2018-12-07 23:43:52 +01:00
|
|
|
import json
|
2016-08-18 13:51:25 +02:00
|
|
|
|
|
|
|
if False:
|
2018-12-17 19:52:08 +01:00
|
|
|
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
|
2018-05-10 18:54:59 +02:00
|
|
|
from typing import Optional, List, IO, Tuple, Any
|
2016-08-18 13:51:25 +02:00
|
|
|
|
|
|
|
from scripts.lib.zulip_tools import subprocess_text_output, run
|
|
|
|
|
2017-09-22 08:15:01 +02:00
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2017-07-27 23:22:52 +02:00
|
|
|
ZULIP_SRV_PATH = "/srv"
|
2016-08-18 13:51:25 +02:00
|
|
|
|
|
|
|
if 'TRAVIS' in os.environ:
|
|
|
|
# In Travis CI, we don't have root access
|
2017-07-27 23:22:52 +02:00
|
|
|
ZULIP_SRV_PATH = "/home/travis"
|
2016-08-18 13:51:25 +02:00
|
|
|
|
2017-07-27 23:22:52 +02:00
|
|
|
|
|
|
|
NODE_MODULES_CACHE_PATH = os.path.join(ZULIP_SRV_PATH, 'zulip-npm-cache')
|
|
|
|
YARN_BIN = os.path.join(ZULIP_SRV_PATH, 'zulip-yarn/bin/yarn')
|
2018-12-07 23:43:52 +01:00
|
|
|
YARN_PACKAGE_JSON = os.path.join(ZULIP_SRV_PATH, 'zulip-yarn/package.json')
|
2017-07-27 23:22:52 +02:00
|
|
|
|
2017-07-28 00:18:23 +02:00
|
|
|
DEFAULT_PRODUCTION = False
|
|
|
|
|
|
|
|
def get_yarn_args(production):
|
|
|
|
# type: (bool) -> List[str]
|
|
|
|
if production:
|
|
|
|
yarn_args = ["--prod"]
|
|
|
|
else:
|
|
|
|
yarn_args = []
|
|
|
|
return yarn_args
|
|
|
|
|
2017-08-19 11:06:45 +02:00
|
|
|
def generate_sha1sum_node_modules(setup_dir=None, production=DEFAULT_PRODUCTION):
|
2018-05-10 18:54:59 +02:00
|
|
|
# type: (Optional[str], bool) -> str
|
2017-08-19 11:06:45 +02:00
|
|
|
if setup_dir is None:
|
|
|
|
setup_dir = os.path.realpath(os.getcwd())
|
|
|
|
PACKAGE_JSON_FILE_PATH = os.path.join(setup_dir, 'package.json')
|
|
|
|
YARN_LOCK_FILE_PATH = os.path.join(setup_dir, 'yarn.lock')
|
2016-08-18 13:51:25 +02:00
|
|
|
sha1sum = hashlib.sha1()
|
2017-08-19 11:06:45 +02:00
|
|
|
sha1sum.update(subprocess_text_output(['cat', PACKAGE_JSON_FILE_PATH]).encode('utf8'))
|
2017-09-26 01:52:27 +02:00
|
|
|
if os.path.exists(YARN_LOCK_FILE_PATH):
|
|
|
|
# For backwards compatibility, we can't assume yarn.lock exists
|
|
|
|
sha1sum.update(subprocess_text_output(['cat', YARN_LOCK_FILE_PATH]).encode('utf8'))
|
2018-12-07 23:43:52 +01:00
|
|
|
with open(YARN_PACKAGE_JSON, "r") as f:
|
|
|
|
yarn_version = json.loads(f.read())['version']
|
|
|
|
sha1sum.update(yarn_version.encode("utf8"))
|
2016-08-18 13:51:25 +02:00
|
|
|
sha1sum.update(subprocess_text_output(['node', '--version']).encode('utf8'))
|
2017-07-28 00:18:23 +02:00
|
|
|
yarn_args = get_yarn_args(production=production)
|
|
|
|
sha1sum.update(''.join(sorted(yarn_args)).encode('utf8'))
|
2017-06-17 20:05:41 +02:00
|
|
|
return sha1sum.hexdigest()
|
|
|
|
|
2017-07-28 00:18:23 +02:00
|
|
|
def setup_node_modules(production=DEFAULT_PRODUCTION, stdout=None, stderr=None, copy_modules=False,
|
2017-07-27 23:22:52 +02:00
|
|
|
prefer_offline=False):
|
2017-11-02 05:19:43 +01:00
|
|
|
# type: (bool, Optional[IO[Any]], Optional[IO[Any]], bool, bool) -> None
|
2017-07-28 00:18:23 +02:00
|
|
|
yarn_args = get_yarn_args(production=production)
|
2017-07-27 23:22:52 +02:00
|
|
|
if prefer_offline:
|
|
|
|
yarn_args.append("--prefer-offline")
|
2017-07-28 00:18:23 +02:00
|
|
|
sha1sum = generate_sha1sum_node_modules(production=production)
|
2017-07-22 01:21:01 +02:00
|
|
|
target_path = os.path.join(NODE_MODULES_CACHE_PATH, sha1sum)
|
|
|
|
cached_node_modules = os.path.join(target_path, 'node_modules')
|
|
|
|
success_stamp = os.path.join(target_path, '.success-stamp')
|
2016-08-18 13:51:25 +02:00
|
|
|
# Check if a cached version already exists
|
|
|
|
if not os.path.exists(success_stamp):
|
2017-07-27 23:22:52 +02:00
|
|
|
do_yarn_install(target_path,
|
|
|
|
yarn_args,
|
|
|
|
success_stamp,
|
|
|
|
stdout=stdout,
|
|
|
|
stderr=stderr,
|
|
|
|
copy_modules=copy_modules)
|
2016-08-18 13:51:25 +02:00
|
|
|
|
|
|
|
print("Using cached node modules from %s" % (cached_node_modules,))
|
|
|
|
cmds = [
|
|
|
|
['rm', '-rf', 'node_modules'],
|
2016-09-28 09:09:36 +02:00
|
|
|
["ln", "-nsf", cached_node_modules, 'node_modules'],
|
2016-08-18 13:51:25 +02:00
|
|
|
]
|
|
|
|
for cmd in cmds:
|
|
|
|
run(cmd, stdout=stdout, stderr=stderr)
|
|
|
|
|
2017-07-27 23:22:52 +02:00
|
|
|
def do_yarn_install(target_path, yarn_args, success_stamp, stdout=None, stderr=None,
|
|
|
|
copy_modules=False):
|
2017-11-02 05:19:43 +01:00
|
|
|
# type: (str, List[str], str, Optional[IO[Any]], Optional[IO[Any]], bool) -> None
|
2016-08-18 13:51:25 +02:00
|
|
|
cmds = [
|
2016-10-03 07:45:23 +02:00
|
|
|
['mkdir', '-p', target_path],
|
2017-07-27 23:22:52 +02:00
|
|
|
['cp', 'package.json', "yarn.lock", target_path],
|
2016-08-18 13:51:25 +02:00
|
|
|
]
|
2017-07-22 00:53:58 +02:00
|
|
|
cached_node_modules = os.path.join(target_path, 'node_modules')
|
2016-08-18 13:51:25 +02:00
|
|
|
if copy_modules:
|
|
|
|
print("Cached version not found! Copying node modules.")
|
2017-07-22 00:53:58 +02:00
|
|
|
cmds.append(["cp", "-rT", "prod-static/serve/node_modules", cached_node_modules])
|
2016-08-18 13:51:25 +02:00
|
|
|
else:
|
|
|
|
print("Cached version not found! Installing node modules.")
|
2017-07-27 23:22:52 +02:00
|
|
|
|
|
|
|
# Copy the existing node_modules to speed up install
|
|
|
|
if os.path.exists("node_modules"):
|
|
|
|
cmds.append(["cp", "-R", "node_modules/", cached_node_modules])
|
|
|
|
cd_exec = os.path.join(ZULIP_PATH, "scripts/lib/cd_exec")
|
2018-08-02 16:29:47 +02:00
|
|
|
if os.environ.get('CUSTOM_CA_CERTIFICATES'):
|
|
|
|
cmds.append([YARN_BIN, "config", "set", "cafile", os.environ['CUSTOM_CA_CERTIFICATES']])
|
2017-07-27 23:22:52 +02:00
|
|
|
cmds.append([cd_exec, target_path, YARN_BIN, "install", "--non-interactive"] +
|
|
|
|
yarn_args)
|
2016-10-26 02:38:04 +02:00
|
|
|
cmds.append(['touch', success_stamp])
|
2016-08-18 13:51:25 +02:00
|
|
|
|
|
|
|
for cmd in cmds:
|
|
|
|
run(cmd, stdout=stdout, stderr=stderr)
|