2016-08-18 13:51:25 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import hashlib
|
|
|
|
from os.path import dirname, abspath
|
|
|
|
|
|
|
|
if False:
|
2017-06-17 20:05:41 +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
|
|
|
|
|
|
|
|
ZULIP_PATH = dirname(dirname(dirname(abspath(__file__))))
|
2017-07-22 00:51:36 +02:00
|
|
|
NODE_MODULES_CACHE_PATH = "/srv/zulip-npm-cache"
|
2016-08-18 13:51:25 +02:00
|
|
|
|
|
|
|
if 'TRAVIS' in os.environ:
|
|
|
|
# In Travis CI, we don't have root access
|
2017-07-22 00:51:36 +02:00
|
|
|
NODE_MODULES_CACHE_PATH = "/home/travis/zulip-npm-cache"
|
2016-08-18 13:51:25 +02:00
|
|
|
|
2017-06-17 20:05:41 +02:00
|
|
|
def generate_sha1sum_node_modules(npm_args=None):
|
|
|
|
# type: (Optional[List[str]]) -> str
|
2016-08-18 13:51:25 +02:00
|
|
|
sha1sum = hashlib.sha1()
|
|
|
|
sha1sum.update(subprocess_text_output(['cat', 'package.json']).encode('utf8'))
|
|
|
|
sha1sum.update(subprocess_text_output(['npm', '--version']).encode('utf8'))
|
|
|
|
sha1sum.update(subprocess_text_output(['node', '--version']).encode('utf8'))
|
|
|
|
if npm_args is not None:
|
|
|
|
sha1sum.update(''.join(sorted(npm_args)).encode('utf8'))
|
|
|
|
|
2017-06-17 20:05:41 +02:00
|
|
|
return sha1sum.hexdigest()
|
|
|
|
|
2017-07-22 01:19:53 +02:00
|
|
|
def setup_node_modules(production=False, stdout=None, stderr=None, copy_modules=False):
|
|
|
|
# type: (bool, Optional[IO], Optional[IO], bool) -> None
|
|
|
|
if production:
|
|
|
|
npm_args = ["--production"]
|
|
|
|
else:
|
|
|
|
npm_args = []
|
2017-06-17 20:05:41 +02:00
|
|
|
sha1sum = generate_sha1sum_node_modules(npm_args)
|
2017-07-22 00:51:36 +02:00
|
|
|
npm_cache = os.path.join(NODE_MODULES_CACHE_PATH, sha1sum)
|
2016-08-18 13:51:25 +02:00
|
|
|
cached_node_modules = os.path.join(npm_cache, 'node_modules')
|
2017-07-22 01:15:15 +02:00
|
|
|
success_stamp = os.path.join(npm_cache, '.success-stamp')
|
2016-08-18 13:51:25 +02:00
|
|
|
# Check if a cached version already exists
|
|
|
|
if not os.path.exists(success_stamp):
|
2016-10-26 02:38:04 +02:00
|
|
|
do_npm_install(npm_cache,
|
2017-07-22 01:19:53 +02:00
|
|
|
npm_args,
|
2017-07-08 05:00:05 +02:00
|
|
|
success_stamp,
|
2016-10-26 02:38:04 +02:00
|
|
|
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-08 05:00:05 +02:00
|
|
|
def do_npm_install(target_path, npm_args, success_stamp, stdout=None, stderr=None,
|
|
|
|
copy_modules=False):
|
2017-07-22 01:27:18 +02:00
|
|
|
# type: (str, List[str], str, Optional[IO], Optional[IO], bool) -> None
|
2016-08-18 13:51:25 +02:00
|
|
|
cmds = [
|
2016-10-03 07:45:23 +02:00
|
|
|
["rm", "-rf", target_path],
|
|
|
|
['mkdir', '-p', target_path],
|
2016-08-18 13:51:25 +02:00
|
|
|
['cp', 'package.json', target_path],
|
|
|
|
]
|
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.")
|
|
|
|
cmds.append(['npm', 'install'] + npm_args + ['--prefix', target_path])
|
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)
|