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