Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import os | 6 import os |
| 7 import shutil | 7 import shutil |
| 8 import sys | 8 import sys |
| 9 import subprocess | 9 import subprocess |
| 10 import tarfile | 10 import tarfile |
| 11 import tempfile | 11 import tempfile |
| 12 import urllib2 | 12 import urllib2 |
| 13 | 13 |
| 14 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | 14 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 15 | 15 |
| 16 DEFAULT_VERSION = '4.5.0' | 16 DEFAULT_VERSION = '4.5.0' |
| 17 BUCKET = 'chromium-nodejs' | 17 BUCKET = 'chromium-nodejs' |
| 18 | 18 RUNNING_AS_HOOK_FLAG = '--running-as-hook' |
| 19 | 19 |
| 20 def install_latest_node_js(version, tmp_dir): | 20 def install_latest_node_js(version, tmp_dir): |
| 21 target_dir = os.path.join(THIS_DIR, 'runtimes', version) | 21 target_dir = os.path.join(THIS_DIR, 'runtimes', version) |
| 22 version_file = os.path.join(target_dir, 'VERSION') | 22 version_file = os.path.join(target_dir, 'VERSION') |
| 23 | 23 |
| 24 if sys.platform == 'win32': | 24 if sys.platform == 'win32': |
| 25 bin_location = os.path.join(target_dir, 'node.exe') | 25 bin_location = os.path.join(target_dir, 'node.exe') |
| 26 else: | 26 else: |
| 27 bin_location = os.path.join(target_dir, 'bin', 'node') | 27 bin_location = os.path.join(target_dir, 'bin', 'node') |
| 28 | 28 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 write_version = False | 93 write_version = False |
| 94 | 94 |
| 95 if write_version: | 95 if write_version: |
| 96 with open(version_file, 'w') as f: | 96 with open(version_file, 'w') as f: |
| 97 f.write(version) | 97 f.write(version) |
| 98 | 98 |
| 99 return bin_location | 99 return bin_location |
| 100 | 100 |
| 101 | 101 |
| 102 def main(mode=None): | 102 def main(mode=None): |
| 103 # Exit early if this is being invoked from `gclient runhooks` | |
| 104 # and INSTALL_NODE_FOR_DEVTOOLS=1 isn't present in the environment. | |
| 105 if (RUNNING_AS_HOOK_FLAG in sys.argv[1:] and | |
| 106 os.environ.get('INSTALL_NODE_FOR_DEVTOOLS', '0') != '1'): | |
| 107 return 0 | |
| 108 | |
|
Dirk Pranke
2016/11/11 02:11:20
I'd rewrite this section to use argparse, e.g.:
chenwilliam
2016/12/06 18:22:09
Done.
| |
| 103 version = os.environ.get('NODE_VERSION', DEFAULT_VERSION) | 109 version = os.environ.get('NODE_VERSION', DEFAULT_VERSION) |
| 104 try: | 110 try: |
| 105 tmp_dir = tempfile.mkdtemp(dir=THIS_DIR) | 111 tmp_dir = tempfile.mkdtemp(dir=THIS_DIR) |
| 106 bin_location = install_latest_node_js(version, tmp_dir) | 112 bin_location = install_latest_node_js(version, tmp_dir) |
| 107 finally: | 113 finally: |
| 108 if os.path.exists(tmp_dir): | 114 if os.path.exists(tmp_dir): |
| 109 shutil.rmtree(tmp_dir) | 115 shutil.rmtree(tmp_dir) |
| 110 | 116 |
| 111 if mode == 'npm': | 117 if mode == 'npm': |
| 112 # TODO(hinoka): How about Windows...? | 118 # TODO(hinoka): How about Windows...? |
| 113 bin_location = os.path.join(os.path.dirname(bin_location), 'npm') | 119 bin_location = os.path.join(os.path.dirname(bin_location), 'npm') |
| 114 | 120 |
| 115 return subprocess.call([bin_location, ] + sys.argv[1:]) | 121 node_args = [arg for arg in sys.argv[1:] if arg != RUNNING_AS_HOOK_FLAG] |
| 122 return subprocess.call([bin_location, ] + node_args) | |
| 116 | 123 |
| 117 | 124 |
| 118 if __name__ == '__main__': | 125 if __name__ == '__main__': |
| 119 sys.exit(main()) | 126 sys.exit(main()) |
| OLD | NEW |