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 | |
|
dgozman
2016/09/14 16:37:41
Let's rename this to install_node_deps
chenwilliam
2016/09/15 00:40:11
Done.
| |
| 9 """ | |
| 10 | |
| 11 import os | |
| 12 from os import path | |
| 13 import subprocess | |
| 14 import sys | |
| 15 | |
| 16 MIN_NODE_VERSION = 4 | |
| 17 | |
| 18 | |
| 19 def main(): | |
| 20 check_node_version() | |
| 21 npm_install() | |
| 22 | |
| 23 | |
| 24 def check_node_version(): | |
| 25 node_path = which('node') | |
| 26 if not node_path: | |
| 27 invalid_node_error_message() | |
| 28 node_process = popen([node_path, '--version']) | |
| 29 (node_process_out, _) = node_process.communicate() | |
| 30 if node_process.returncode != 0: | |
| 31 invalid_node_error_message() | |
| 32 print('Using node.js version: {}'.format(node_process_out)) | |
| 33 major_version = node_process_out[1] | |
| 34 version = int(major_version) | |
| 35 if version < MIN_NODE_VERSION: | |
| 36 invalid_node_error_message() | |
| 37 | |
| 38 | |
| 39 def invalid_node_error_message(): | |
| 40 print('Please install node.js v4 or later') | |
| 41 print('Download the latest LTS version from nodejs.org:') | |
| 42 print('https://nodejs.org/en/download/\n') | |
| 43 raise | |
| 44 | |
| 45 | |
| 46 def npm_install(): | |
| 47 npm_path = which('npm') | |
| 48 if not npm_path: | |
| 49 print('Cannot find npm, which is bundled with node.js') | |
| 50 invalid_node_error_message() | |
| 51 npm_process = popen([npm_path, 'install']) | |
| 52 (npm_process_out, _) = npm_process.communicate() | |
| 53 if npm_process.returncode != 0: | |
| 54 print('WARNING: npm install had an issue') | |
| 55 print(npm_process_out) | |
| 56 | |
| 57 | |
| 58 # Based on http://stackoverflow.com/questions/377017/test-if-executable-exists-i n-python. | |
| 59 def which(program): | |
| 60 def is_exe(fpath): | |
| 61 return path.isfile(fpath) and os.access(fpath, os.X_OK) | |
| 62 | |
| 63 fpath, fname = path.split(program) | |
| 64 if fpath: | |
| 65 if is_exe(program): | |
| 66 return program | |
| 67 else: | |
| 68 for part in os.environ["PATH"].split(os.pathsep): | |
| 69 part = part.strip("\"") | |
| 70 exe_file = path.join(part, program) | |
| 71 if is_exe(exe_file): | |
| 72 return exe_file | |
| 73 return None | |
| 74 | |
| 75 | |
| 76 def popen(arguments): | |
| 77 return subprocess.Popen( | |
| 78 arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
| 79 | |
| 80 | |
| 81 if __name__ == '__main__': | |
| 82 sys.exit(main()) | |
| OLD | NEW |