Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/scripts/install_node_deps.py |
| diff --git a/third_party/WebKit/Source/devtools/scripts/install_node_deps.py b/third_party/WebKit/Source/devtools/scripts/install_node_deps.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..2912e949b277b9ad195f24af01535b1a0b791893 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/devtools/scripts/install_node_deps.py |
| @@ -0,0 +1,103 @@ |
| +#!/usr/bin/env python |
| +# |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +""" |
| +Ensure node.js and npm modules are installed |
| +""" |
| + |
| +import os |
| +from os import path |
| +import subprocess |
| +import sys |
| + |
| +from local_node import node |
| + |
| +MIN_NODE_VERSION = 4 |
| +LOCAL_NODE_VERSION = node.DEFAULT_VERSION |
| + |
| +scripts_path = path.dirname(path.abspath(__file__)) |
| +local_node_path = path.join(scripts_path, 'local_node', 'node.py') |
| +local_npm_path = path.join(scripts_path, 'local_node', 'npm.py') |
| +local_node_runtime_path = path.join(scripts_path, 'local_node', 'runtimes', LOCAL_NODE_VERSION) |
| + |
| + |
| +def main(): |
| + (node_path, npm_path) = resolve_node_paths() |
| + npm_install(npm_path) |
| + |
| + |
| +def resolve_node_paths(): |
| + if has_valid_global_node(): |
| + return (which('node'), which('npm')) |
| + has_installed_local_node = path.isdir(local_node_runtime_path) |
| + if has_installed_local_node: |
|
lushnikov
2016/10/05 16:22:49
can we check for the local node binaries and retur
chenwilliam
2016/10/05 17:59:19
Done.
|
| + return (local_node_path, local_npm_path) |
| + if sys.platform == 'linux2': |
| + install_node() |
| + return (local_node_path, local_npm_path) |
| + print('Please install the latest node.js LTS version using the Mac or Windows installer:') |
|
lushnikov
2016/10/05 16:22:49
let's prefix this with "ERROR:" so that it would b
chenwilliam
2016/10/05 17:59:19
Done.
|
| + print('https://nodejs.org/en/download/') |
| + raise |
| + |
| + |
| +def has_valid_global_node(): |
| + node_path = which('node') |
| + if not node_path: |
| + return False |
| + node_process = popen([node_path, '--version']) |
| + (node_process_out, _) = node_process.communicate() |
| + if node_process.returncode != 0: |
| + return False |
| + major_version = node_process_out[1] |
| + return int(major_version) >= MIN_NODE_VERSION |
| + |
| + |
| +def install_node(): |
| + print('Installing node.js locally at devtools/scripts/local_node/runtimes') |
| + print('NOTE: this does not add to PATH or affect global node installation') |
| + install_node_process = popen([local_node_path, '--version']) |
| + (node_process_out, error) = install_node_process.communicate() |
| + if install_node_process.returncode != 0: |
| + print('Could not install node locally') |
| + print(error) |
| + raise |
| + print(node_process_out) |
| + |
| + |
| +def npm_install(npm_path): |
| + print('Runing npm install using {}'.format(npm_path)) |
| + npm_process = popen([npm_path, 'install']) |
| + (npm_process_out, _) = npm_process.communicate() |
| + if npm_process.returncode != 0: |
| + print('WARNING: npm install had an issue') |
| + print(npm_process_out) |
| + |
| + |
| +# Based on http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python. |
| +def which(program): |
| + def is_exe(fpath): |
| + return path.isfile(fpath) and os.access(fpath, os.X_OK) |
| + |
| + fpath, fname = path.split(program) |
| + if fpath: |
| + if is_exe(program): |
| + return program |
| + else: |
| + for part in os.environ['PATH'].split(os.pathsep): |
| + part = part.strip('\"') |
| + exe_file = path.join(part, program) |
| + if is_exe(exe_file): |
| + return exe_file |
| + return None |
| + |
| + |
| +def popen(arguments): |
| + return subprocess.Popen( |
| + arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |