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

Unified Diff: ensure_dependencies.py

Issue 29863604: Issue 6865 - Update ABP dependency to version 3.2 (Closed)
Patch Set: Removing patch file Created Aug. 24, 2018, 8:41 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
===================================================================
--- a/ensure_dependencies.py
+++ b/ensure_dependencies.py
@@ -9,16 +9,17 @@
import posixpath
import re
import io
import errno
import logging
import subprocess
import urlparse
import argparse
+import json
from collections import OrderedDict
from ConfigParser import RawConfigParser
USAGE = '''
A dependencies file should look like this:
# VCS-specific root URLs for the repositories
@@ -38,18 +39,20 @@
# from a specific Git repository, specifying the revision ID.
adblockpluschrome = git:git@github.com:user/adblockpluschrome.git@1fad3a7
'''
SKIP_DEPENDENCY_UPDATES = os.environ.get(
'SKIP_DEPENDENCY_UPDATES', ''
).lower() not in ('', '0', 'false')
+NPM_LOCKFILE = '.npm_install_lock'
-class Mercurial():
+
+class Mercurial:
def istype(self, repodir):
return os.path.exists(os.path.join(repodir, '.hg'))
def clone(self, source, target):
if not source.endswith('/'):
source += '/'
subprocess.check_call(['hg', 'clone', '--quiet', '--noupdate', source, target])
@@ -86,17 +89,17 @@
module = os.path.relpath(target, repo)
_ensure_line_exists(ignore_path, module)
def postprocess_url(self, url):
return url
-class Git():
+class Git:
def istype(self, repodir):
return os.path.exists(os.path.join(repodir, '.git'))
def clone(self, source, target):
source = source.rstrip('/')
if not source.endswith('.git'):
source += '.git'
subprocess.check_call(['git', 'clone', '--quiet', source, target])
@@ -130,16 +133,17 @@
_ensure_line_exists(exclude_file, module)
def postprocess_url(self, url):
# Handle alternative syntax of SSH URLS
if '@' in url and ':' in url and not urlparse.urlsplit(url).scheme:
return 'ssh://' + url.replace(':', '/', 1)
return url
+
repo_types = OrderedDict((
('hg', Mercurial()),
('git', Git()),
))
# [vcs:]value
item_regexp = re.compile(
'^(?:(' + '|'.join(map(re.escape, repo_types.keys())) + '):)?'
@@ -240,74 +244,132 @@
def get_repo_type(repo):
for name, repotype in repo_types.iteritems():
if repotype.istype(repo):
return name
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()
+
+ if os.name == 'nt':
+ # Windows' CreateProcess() (called by subprocess.Popen()) only
+ # resolves executables ending in .exe. The windows installation of
+ # Node.js only provides a npm.cmd, which is executable but won't
+ # be recognized as such by CreateProcess().
+ npm_exec = 'npm.cmd'
+ else:
+ npm_exec = 'npm'
+
+ cmd = [npm_exec, '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)
sourcename = postprocess_url(sourcename)
if os.path.exists(root):
url = os.path.join(root, sourcename)
else:
url = urlparse.urljoin(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):
resolved_revision = repo_types[type].get_revision_id(target, revision)
current_revision = repo_types[type].get_revision_id(target)
if resolved_revision != current_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)
repo_types[type].pull(target)
resolved_revision = repo_types[type].get_revision_id(target, revision)
if not resolved_revision:
raise Exception('Failed to resolve revision %s' % 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()):
config = read_deps(repodir)
if config is None:
if level == 0:
logging.warning('No dependencies file in directory %s, nothing to do...\n%s' % (repodir, USAGE))
return
if level >= 10:
logging.warning('Too much subrepository nesting, ignoring %s' % repo)
return
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
target = safe_join(repodir, dir)
parenttype = get_repo_type(repodir)
_root = config.get('_root', {})
@@ -315,18 +377,22 @@
if key == parenttype or key is None and vcs != '*':
vcs = key
source, rev = merge_seqs(sources.get('*'), sources.get(vcs))
if not (vcs and source and rev):
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)
if self_update and '_self' in config and '*' in config['_self']:
source = safe_join(repodir, config['_self']['*'])
try:
with io.open(source, 'rb') as handle:
sourcedata = handle.read()
@@ -357,16 +423,17 @@
file_content = [l.strip() for l in f.readlines()]
if not pattern in file_content:
file_content.append(pattern)
f.seek(0, os.SEEK_SET)
f.truncate()
for l in file_content:
print >>f, l
+
if __name__ == '__main__':
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
parser = argparse.ArgumentParser(description='Verify dependencies for a set of repositories, by default the repository of this script.')
parser.add_argument('repos', metavar='repository', type=str, nargs='*', help='Repository path')
parser.add_argument('-q', '--quiet', action='store_true', help='Suppress informational output')
args = parser.parse_args()

Powered by Google App Engine
This is Rietveld