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 from local_node import node | |
| 17 | |
| 18 MIN_NODE_VERSION = 4 | |
| 19 LOCAL_NODE_VERSION = node.DEFAULT_VERSION | |
| 20 | |
| 21 scripts_path = path.dirname(path.abspath(__file__)) | |
| 22 local_node_path = path.join(scripts_path, 'local_node', 'node.py') | |
| 23 local_npm_path = path.join(scripts_path, 'local_node', 'npm.py') | |
| 24 local_node_runtime_path = path.join(scripts_path, 'local_node', 'runtimes', LOCA L_NODE_VERSION) | |
| 25 | |
| 26 | |
| 27 def main(): | |
| 28 (node_path, npm_path) = resolve_node_paths() | |
| 29 npm_install(npm_path) | |
| 30 | |
| 31 | |
| 32 def resolve_node_paths(): | |
| 33 if has_valid_global_node(): | |
| 34 return (which('node'), which('npm')) | |
| 35 has_installed_local_node = path.isdir(local_node_runtime_path) | |
| 36 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.
| |
| 37 return (local_node_path, local_npm_path) | |
| 38 if sys.platform == 'linux2': | |
| 39 install_node() | |
| 40 return (local_node_path, local_npm_path) | |
| 41 print('Please install the latest node.js LTS version using the Mac or Window s 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.
| |
| 42 print('https://nodejs.org/en/download/') | |
| 43 raise | |
| 44 | |
| 45 | |
| 46 def has_valid_global_node(): | |
| 47 node_path = which('node') | |
| 48 if not node_path: | |
| 49 return False | |
| 50 node_process = popen([node_path, '--version']) | |
| 51 (node_process_out, _) = node_process.communicate() | |
| 52 if node_process.returncode != 0: | |
| 53 return False | |
| 54 major_version = node_process_out[1] | |
| 55 return int(major_version) >= MIN_NODE_VERSION | |
| 56 | |
| 57 | |
| 58 def install_node(): | |
| 59 print('Installing node.js locally at devtools/scripts/local_node/runtimes') | |
| 60 print('NOTE: this does not add to PATH or affect global node installation') | |
| 61 install_node_process = popen([local_node_path, '--version']) | |
| 62 (node_process_out, error) = install_node_process.communicate() | |
| 63 if install_node_process.returncode != 0: | |
| 64 print('Could not install node locally') | |
| 65 print(error) | |
| 66 raise | |
| 67 print(node_process_out) | |
| 68 | |
| 69 | |
| 70 def npm_install(npm_path): | |
| 71 print('Runing npm install using {}'.format(npm_path)) | |
| 72 npm_process = popen([npm_path, 'install']) | |
| 73 (npm_process_out, _) = npm_process.communicate() | |
| 74 if npm_process.returncode != 0: | |
| 75 print('WARNING: npm install had an issue') | |
| 76 print(npm_process_out) | |
| 77 | |
| 78 | |
| 79 # Based on http://stackoverflow.com/questions/377017/test-if-executable-exists-i n-python. | |
| 80 def which(program): | |
| 81 def is_exe(fpath): | |
| 82 return path.isfile(fpath) and os.access(fpath, os.X_OK) | |
| 83 | |
| 84 fpath, fname = path.split(program) | |
| 85 if fpath: | |
| 86 if is_exe(program): | |
| 87 return program | |
| 88 else: | |
| 89 for part in os.environ['PATH'].split(os.pathsep): | |
| 90 part = part.strip('\"') | |
| 91 exe_file = path.join(part, program) | |
| 92 if is_exe(exe_file): | |
| 93 return exe_file | |
| 94 return None | |
| 95 | |
| 96 | |
| 97 def popen(arguments): | |
| 98 return subprocess.Popen( | |
| 99 arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
| 100 | |
| 101 | |
| 102 if __name__ == '__main__': | |
| 103 sys.exit(main()) | |
| OLD | NEW |