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