Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: ensure_dependencies.py

Issue 29526588: Issue 5559 - include Node.js in ensure_dependencies.py (Closed)
Patch Set: Refactoring according to new integration notes Created Aug. 25, 2017, 9:50 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ensure_dependencies.py
diff --git a/ensure_dependencies.py b/ensure_dependencies.py
index 127e300d213dc13d2888e3ccbaa46fabf83e9990..4b675753a35789f7430e45e12c57b29e726d56f0 100755
--- a/ensure_dependencies.py
+++ b/ensure_dependencies.py
@@ -14,6 +14,7 @@ import logging
import subprocess
import urlparse
import argparse
+import json
from collections import OrderedDict
from ConfigParser import RawConfigParser
@@ -245,6 +246,47 @@ def get_repo_type(repo):
return 'hg'
+def resolve_npm_dependencies(target, vcs):
+ """Install Node.js production-only dependencies if necessary and desired.
+
+ When the target dependency has additional Node.js dependencies declared
+ run "npm install --only=production --loglevel=warn" to resolve the declared
+ dependencies.
+
+ Additionally, make sure that any VCS will ignore the installed files.
+
+ Requires Node.js to be installed locally.
+ """
+ try:
+ with open(os.path.join(target, 'package.json'), 'r') as fp:
+ package_data = json.load(fp)
+
+ # In case a package.json does not exist at all or if there are no
+ # production dependencies declared, we don't need to run npm and can
+ # bail out early
+ if not package_data.get('dependencies', False):
+ return
+ except IOError:
+ return
+
+ try:
+ cmd = ['npm', 'install', '--only=production', '--loglevel=warn']
+ subprocess.check_output(cmd, cwd=target)
+
+ # Make sure Node.js related files / folders are ignored by the VCS in
+ # use
+ repo_types[vcs].ignore(
+ os.path.join(target, 'node_modules'), target
+ )
+ except OSError as e:
+ import errno
+ if e.errno == errno.ENOENT:
+ logging.error('Failed to install Node.js dependencies for %s,'
+ ' please ensure Node.js is installed.', target)
+ else:
+ raise
+
+
def ensure_repo(parentrepo, parenttype, target, type, root, sourcename):
if os.path.exists(target):
return
@@ -266,6 +308,7 @@ def ensure_repo(parentrepo, parenttype, target, type, root, sourcename):
logging.info('Cloning repository %s into %s' % (url, target))
repo_types[type].clone(url, target)
repo_types[parenttype].ignore(target, parentrepo)
+ resolve_npm_dependencies(target, type)
def update_repo(target, type, revision):
@@ -287,6 +330,7 @@ def update_repo(target, type, revision):
logging.info('Updating repository %s to revision %s' % (target, resolved_revision))
repo_types[type].update(target, resolved_revision, revision)
+ resolve_npm_dependencies(target, type)
Wladimir Palant 2017/08/25 10:01:59 For Mercurial repositories, both cloning and updat
tlucas 2017/08/25 10:06:40 Done.
def resolve_deps(repodir, level=0, self_update=True, overrideroots=None, skipdependencies=set()):
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld