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 argparse |
6 import os | 7 import os |
7 import shutil | 8 import shutil |
8 import sys | 9 import sys |
9 import subprocess | 10 import subprocess |
10 import tarfile | 11 import tarfile |
11 import tempfile | 12 import tempfile |
12 import urllib2 | 13 import urllib2 |
13 | 14 |
14 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | 15 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
15 | 16 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 os.rename(os.path.join(tmp_dir, target[:-len('.tar.gz')]), | 81 os.rename(os.path.join(tmp_dir, target[:-len('.tar.gz')]), |
81 target_dir) | 82 target_dir) |
82 except OSError: | 83 except OSError: |
83 write_version = False | 84 write_version = False |
84 os.remove(dest) | 85 os.remove(dest) |
85 else: | 86 else: |
86 try: | 87 try: |
87 # Still potentiall racy, from python docs: | 88 # Still potentiall racy, from python docs: |
88 # "On Windows...there may be no way to implement an atomic rename wh
en dst | 89 # "On Windows...there may be no way to implement an atomic rename wh
en dst |
89 # names an existing file." | 90 # names an existing file." |
90 os.mkdir(target_dir) | 91 os.makedirs(target_dir) |
91 os.rename(dest, bin_location) | 92 os.rename(dest, bin_location) |
92 except OSError: | 93 except OSError: |
93 write_version = False | 94 write_version = False |
94 | 95 |
95 if write_version: | 96 if write_version: |
96 with open(version_file, 'w') as f: | 97 with open(version_file, 'w') as f: |
97 f.write(version) | 98 f.write(version) |
98 | 99 |
99 return bin_location | 100 return bin_location |
100 | 101 |
101 | 102 |
102 def main(mode=None): | 103 def main(mode=None): |
| 104 parser = argparse.ArgumentParser() |
| 105 parser.add_argument('--running-as-hook', action='store_true') |
| 106 args, rest_args = parser.parse_known_args() |
| 107 |
| 108 # Exit early if this is being invoked from `gclient runhooks` |
| 109 # and not on a bot (CHROME_HEADLESS is set on all build bots). |
| 110 if (args.running_as_hook and |
| 111 os.environ.get('CHROME_HEADLESS', '0') != '1'): |
| 112 return 0 |
| 113 |
103 version = os.environ.get('NODE_VERSION', DEFAULT_VERSION) | 114 version = os.environ.get('NODE_VERSION', DEFAULT_VERSION) |
104 try: | 115 try: |
105 tmp_dir = tempfile.mkdtemp(dir=THIS_DIR) | 116 tmp_dir = tempfile.mkdtemp(dir=THIS_DIR) |
106 bin_location = install_latest_node_js(version, tmp_dir) | 117 bin_location = install_latest_node_js(version, tmp_dir) |
107 finally: | 118 finally: |
108 if os.path.exists(tmp_dir): | 119 if os.path.exists(tmp_dir): |
109 shutil.rmtree(tmp_dir) | 120 shutil.rmtree(tmp_dir) |
110 | 121 |
111 if mode == 'npm': | 122 if mode == 'npm': |
112 # TODO(hinoka): How about Windows...? | 123 # TODO(hinoka): How about Windows...? |
113 bin_location = os.path.join(os.path.dirname(bin_location), 'npm') | 124 bin_location = os.path.join(os.path.dirname(bin_location), 'npm') |
114 | 125 |
115 return subprocess.call([bin_location, ] + sys.argv[1:]) | 126 return subprocess.call([bin_location, ] + rest_args) |
116 | 127 |
117 | 128 |
118 if __name__ == '__main__': | 129 if __name__ == '__main__': |
119 sys.exit(main()) | 130 sys.exit(main()) |
OLD | NEW |