| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """ | 7 """ |
| 8 Ensure node.js and npm modules are installed | 8 Ensure node.js and npm modules are installed |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 import os | 11 import os |
| 12 from os import path | 12 from os import path |
| 13 import shutil | 13 import shutil |
| 14 import subprocess | 14 import subprocess |
| 15 import sys | 15 import sys |
| 16 | 16 |
| 17 import utils |
| 18 |
| 17 MIN_NODE_VERSION = 4 | 19 MIN_NODE_VERSION = 4 |
| 18 LOCAL_NODE_VERSION = '4.5.0' | 20 LOCAL_NODE_VERSION = '4.5.0' |
| 19 | 21 |
| 20 scripts_path = path.dirname(path.abspath(__file__)) | 22 scripts_path = path.dirname(path.abspath(__file__)) |
| 21 install_local_node_path = path.join(scripts_path, 'local_node', 'node.py') | 23 install_local_node_path = path.join(scripts_path, 'local_node', 'node.py') |
| 22 local_node_runtimes_path = path.join(scripts_path, 'local_node', 'runtimes') | 24 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') | 25 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') | 26 local_npm_binary_path = path.join(local_node_runtimes_path, LOCAL_NODE_VERSION,
'bin', 'npm') |
| 25 | 27 |
| 26 | 28 |
| 27 def main(): | 29 def main(): |
| 28 (node_path, npm_path) = resolve_node_paths() | 30 (node_path, npm_path) = resolve_node_paths() |
| 29 npm_install(npm_path) | 31 npm_install(npm_path) |
| 30 | 32 |
| 31 | 33 |
| 32 def resolve_node_paths(): | 34 def resolve_node_paths(): |
| 33 if has_valid_global_node(): | 35 if has_valid_global_node(): |
| 34 return (which('node'), which('npm')) | 36 if sys.platform == "win32": |
| 37 return (utils.which('node'), utils.which('npm.cmd')) |
| 38 return (utils.which('node'), utils.which('npm')) |
| 35 has_installed_local_node = path.isfile(local_node_binary_path) | 39 has_installed_local_node = path.isfile(local_node_binary_path) |
| 36 if has_installed_local_node: | 40 if has_installed_local_node: |
| 37 return (local_node_binary_path, local_npm_binary_path) | 41 return (local_node_binary_path, local_npm_binary_path) |
| 38 if path.isdir(local_node_runtimes_path): | 42 if path.isdir(local_node_runtimes_path): |
| 39 shutil.rmtree(local_node_runtimes_path) | 43 shutil.rmtree(local_node_runtimes_path) |
| 40 if sys.platform == 'linux2' or sys.platform == 'darwin': | 44 if sys.platform == 'linux2' or sys.platform == 'darwin': |
| 41 install_node() | 45 install_node() |
| 42 return (local_node_binary_path, local_npm_binary_path) | 46 return (local_node_binary_path, local_npm_binary_path) |
| 43 print('ERROR: Please install the latest node.js LTS version using the Window
s installer:') | 47 print('ERROR: Please install the latest node.js LTS version using the Window
s installer:') |
| 44 print('https://nodejs.org/en/download/') | 48 print('https://nodejs.org/en/download/') |
| 45 raise | 49 raise |
| 46 | 50 |
| 47 | 51 |
| 48 def has_valid_global_node(): | 52 def has_valid_global_node(): |
| 49 node_path = which('node') | 53 node_path = utils.which('node') |
| 50 if not node_path: | 54 if not node_path: |
| 51 return False | 55 return False |
| 52 node_process = popen([node_path, '--version']) | 56 node_process = popen([node_path, '--version']) |
| 53 (node_process_out, _) = node_process.communicate() | 57 (node_process_out, _) = node_process.communicate() |
| 54 if node_process.returncode != 0: | 58 if node_process.returncode != 0: |
| 55 return False | 59 return False |
| 56 major_version = node_process_out[1] | 60 major_version = node_process_out[1] |
| 57 return int(major_version) >= MIN_NODE_VERSION | 61 return int(major_version) >= MIN_NODE_VERSION |
| 58 | 62 |
| 59 | 63 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 72 | 76 |
| 73 def npm_install(npm_path): | 77 def npm_install(npm_path): |
| 74 print('Runing npm install using {}'.format(npm_path)) | 78 print('Runing npm install using {}'.format(npm_path)) |
| 75 npm_process = popen([npm_path, 'install']) | 79 npm_process = popen([npm_path, 'install']) |
| 76 (npm_process_out, _) = npm_process.communicate() | 80 (npm_process_out, _) = npm_process.communicate() |
| 77 if npm_process.returncode != 0: | 81 if npm_process.returncode != 0: |
| 78 print('WARNING: npm install had an issue') | 82 print('WARNING: npm install had an issue') |
| 79 print(npm_process_out) | 83 print(npm_process_out) |
| 80 | 84 |
| 81 | 85 |
| 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): | 86 def popen(arguments, env=None): |
| 101 return subprocess.Popen( | 87 return subprocess.Popen( |
| 102 arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) | 88 arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) |
| 103 | 89 |
| 104 | 90 |
| 105 if __name__ == '__main__': | 91 if __name__ == '__main__': |
| 106 sys.exit(main()) | 92 sys.exit(main()) |
| OLD | NEW |