Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/scripts/node.py |
| diff --git a/third_party/WebKit/Source/devtools/scripts/node.py b/third_party/WebKit/Source/devtools/scripts/node.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..6ef6435b0e6d8b2f9ea1ef9efe498307350470b7 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/devtools/scripts/node.py |
| @@ -0,0 +1,82 @@ |
| +#!/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 |
|
dgozman
2016/09/14 16:37:41
Let's rename this to install_node_deps
chenwilliam
2016/09/15 00:40:11
Done.
|
| +""" |
| + |
| +import os |
| +from os import path |
| +import subprocess |
| +import sys |
| + |
| +MIN_NODE_VERSION = 4 |
| + |
| + |
| +def main(): |
| + check_node_version() |
| + npm_install() |
| + |
| + |
| +def check_node_version(): |
| + node_path = which('node') |
| + if not node_path: |
| + invalid_node_error_message() |
| + node_process = popen([node_path, '--version']) |
| + (node_process_out, _) = node_process.communicate() |
| + if node_process.returncode != 0: |
| + invalid_node_error_message() |
| + print('Using node.js version: {}'.format(node_process_out)) |
| + major_version = node_process_out[1] |
| + version = int(major_version) |
| + if version < MIN_NODE_VERSION: |
| + invalid_node_error_message() |
| + |
| + |
| +def invalid_node_error_message(): |
| + print('Please install node.js v4 or later') |
| + print('Download the latest LTS version from nodejs.org:') |
| + print('https://nodejs.org/en/download/\n') |
| + raise |
| + |
| + |
| +def npm_install(): |
| + npm_path = which('npm') |
| + if not npm_path: |
| + print('Cannot find npm, which is bundled with node.js') |
| + invalid_node_error_message() |
| + 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()) |