Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # | |
| 3 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 """ | |
| 8 Ensure node.js and npm modules are installed | |
| 9 """ | |
| 10 | |
| 11 import os | |
| 12 from os import path | |
| 13 import subprocess | |
| 14 import sys | |
| 15 | |
| 16 MIN_NODE_VERSION = 4 | |
| 17 | |
| 18 scripts_path = path.dirname(path.abspath(__file__)) | |
| 19 local_node_path = path.join(scripts_path, 'local_node', 'node.py') | |
| 20 | |
| 21 | |
| 22 def main(): | |
| 23 valid_global_node = has_valid_global_node() | |
| 24 if not valid_global_node: | |
| 25 install_node() | |
| 26 npm_install(valid_global_node) | |
| 27 | |
| 28 | |
| 29 def has_valid_global_node(): | |
| 30 node_path = which('node') | |
| 31 if not node_path: | |
| 32 return False | |
| 33 node_process = popen([node_path, '--version']) | |
| 34 (node_process_out, _) = node_process.communicate() | |
| 35 if node_process.returncode != 0: | |
| 36 return False | |
| 37 major_version = node_process_out[1] | |
| 38 version = int(major_version) | |
| 39 if version < MIN_NODE_VERSION: | |
| 40 return False | |
| 41 return True | |
| 42 | |
| 43 | |
| 44 def install_node(): | |
| 45 print('Did not find node.js v4 or later installed globally') | |
| 46 if sys.platform != 'linux2': | |
| 47 print('Please install the latest node.js LTS version using the Mac or Wi ndows installer:') | |
| 48 print('https://nodejs.org/en/download/') | |
| 49 raise | |
| 50 print('Running node.js v4 locally') | |
| 51 print('First time run installs this in devtools/scripts/local_node/runtimes' ) | |
|
lushnikov
2016/09/27 19:01:20
can we output this only as we actually fetch node/
chenwilliam
2016/10/05 01:21:33
Done.
| |
| 52 print('NOTE: this does not add to PATH or affect global node.js installation ') | |
| 53 install_node_process = popen([local_node_path, '--version']) | |
| 54 (node_process_out, error) = install_node_process.communicate() | |
| 55 if install_node_process.returncode != 0: | |
| 56 print('Could not install node locally') | |
| 57 print(error) | |
| 58 raise | |
| 59 print(node_process_out) | |
| 60 | |
| 61 | |
| 62 def npm_install(valid_global_node): | |
| 63 if valid_global_node: | |
| 64 npm_path = which('npm') | |
| 65 else: | |
| 66 npm_path = path.join(scripts_path, 'local_node', 'npm.py') | |
| 67 print('Runing npm install using {}'.format(npm_path)) | |
| 68 npm_process = popen([npm_path, 'install']) | |
| 69 (npm_process_out, _) = npm_process.communicate() | |
| 70 if npm_process.returncode != 0: | |
| 71 print('WARNING: npm install had an issue') | |
| 72 print(npm_process_out) | |
| 73 | |
| 74 | |
| 75 def get_node_path(): | |
| 76 if has_valid_global_node(): | |
| 77 return which('node') | |
| 78 if sys.platform != 'linux2': | |
| 79 print('Please install the latest node.js LTS version using the Mac or Wi ndows installer:') | |
| 80 print('https://nodejs.org/en/download/') | |
| 81 raise | |
| 82 return local_node_path | |
| 83 | |
| 84 | |
| 85 # Based on http://stackoverflow.com/questions/377017/test-if-executable-exists-i n-python. | |
| 86 def which(program): | |
| 87 def is_exe(fpath): | |
| 88 return path.isfile(fpath) and os.access(fpath, os.X_OK) | |
| 89 | |
| 90 fpath, fname = path.split(program) | |
| 91 if fpath: | |
| 92 if is_exe(program): | |
| 93 return program | |
| 94 else: | |
| 95 for part in os.environ['PATH'].split(os.pathsep): | |
| 96 part = part.strip('\"') | |
| 97 exe_file = path.join(part, program) | |
| 98 if is_exe(exe_file): | |
| 99 return exe_file | |
| 100 return None | |
| 101 | |
| 102 | |
| 103 def popen(arguments): | |
| 104 return subprocess.Popen( | |
| 105 arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
| 106 | |
| 107 | |
| 108 if __name__ == '__main__': | |
| 109 sys.exit(main()) | |
| OLD | NEW |