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

Unified Diff: ensure_dependencies.py

Issue 29589691: Issue 5757 - Update buildtools dependency, (re)move legacy extensions (Closed)
Patch Set: Created Oct. 26, 2017, 10:50 p.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
Index: ensure_dependencies.py
diff --git a/ensure_dependencies.py b/ensure_dependencies.py
index b5231e49ce2215c823775403a3c562a333d13b62..1cbbf09522eef68283123f634370064352ec49f1 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
@@ -43,6 +44,8 @@ SKIP_DEPENDENCY_UPDATES = os.environ.get(
'SKIP_DEPENDENCY_UPDATES', ''
).lower() not in ('', '0', 'false')
+NPM_LOCKFILE = '.npm_install_lock'
+
class Mercurial():
def istype(self, repodir):
@@ -135,6 +138,7 @@ class Git():
return 'ssh://' + url.replace(':', '/', 1)
return url
+
repo_types = OrderedDict((
('hg', Mercurial()),
('git', Git()),
@@ -245,14 +249,60 @@ 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:
+ # Create an empty file, which gets deleted after successfully
+ # installing Node.js dependencies.
+ lockfile_path = os.path.join(target, NPM_LOCKFILE)
+ open(lockfile_path, 'a').close()
+
+ cmd = ['npm', 'install', '--only=production', '--loglevel=warn',
+ '--no-package-lock', '--no-optional']
+ subprocess.check_output(cmd, cwd=target)
+
+ repo_types[vcs].ignore(os.path.join(target, NPM_LOCKFILE), target)
+ repo_types[vcs].ignore(os.path.join(target, 'node_modules'), target)
+
+ os.remove(lockfile_path)
+ 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
+ return False
if SKIP_DEPENDENCY_UPDATES:
logging.warning('SKIP_DEPENDENCY_UPDATES environment variable set, '
'%s not cloned', target)
- return
+ return False
postprocess_url = repo_types[type].postprocess_url
root = postprocess_url(root)
@@ -266,6 +316,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)
+ return True
def update_repo(target, type, revision):
@@ -276,7 +327,7 @@ def update_repo(target, type, revision):
if SKIP_DEPENDENCY_UPDATES:
logging.warning('SKIP_DEPENDENCY_UPDATES environment variable set, '
'%s not checked out to %s', target, revision)
- return
+ return False
if not resolved_revision:
logging.info('Revision %s is unknown, downloading remote changes' % revision)
@@ -287,6 +338,8 @@ 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)
+ return True
+ return False
def resolve_deps(repodir, level=0, self_update=True, overrideroots=None, skipdependencies=set()):
@@ -302,7 +355,7 @@ def resolve_deps(repodir, level=0, self_update=True, overrideroots=None, skipdep
if overrideroots is not None:
config['_root'] = overrideroots
- for dir, sources in config.iteritems():
+ for dir, sources in sorted(config.iteritems()):
if (dir.startswith('_') or
skipdependencies.intersection([s[0] for s in sources if s[0]])):
continue
@@ -320,8 +373,12 @@ def resolve_deps(repodir, level=0, self_update=True, overrideroots=None, skipdep
logging.warning('No valid source / revision found to create %s' % target)
continue
- ensure_repo(repodir, parenttype, target, vcs, _root.get(vcs, ''), source)
- update_repo(target, vcs, rev)
+ repo_cloned = ensure_repo(repodir, parenttype, target, vcs,
+ _root.get(vcs, ''), source)
+ repo_updated = update_repo(target, vcs, rev)
+ recent_npm_failed = os.path.exists(os.path.join(target, NPM_LOCKFILE))
+ if repo_cloned or repo_updated or recent_npm_failed:
+ resolve_npm_dependencies(target, vcs)
resolve_deps(target, level + 1, self_update=False,
overrideroots=overrideroots, skipdependencies=skipdependencies)
@@ -362,6 +419,7 @@ def _ensure_line_exists(path, pattern):
for l in file_content:
print >>f, l
+
if __name__ == '__main__':
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)

Powered by Google App Engine
This is Rietveld