OLD | NEW |
1 #!/usr/bin/env python | 1 #!ENV/bin/python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 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 """Wrapper for `python -m` to make running tools simpler. | 6 """Wrapper for `python -m` to make running tools simpler. |
7 | 7 |
8 A tool is defined as a python module with a __main__.py file. This latter file | 8 A tool is defined as a python module with a __main__.py file. This latter file |
9 is run by the present script. | 9 is run by the present script. |
10 | 10 |
11 In particular, allows gclient to change directories when running hooks for | 11 In particular, allows gclient to change directories when running hooks for |
12 infra. | 12 infra. |
13 """ | 13 """ |
14 | 14 |
15 assert __name__ == '__main__' | 15 assert __name__ == '__main__' |
16 | 16 |
17 import argparse | 17 import argparse |
18 import os | 18 import os |
19 import runpy | 19 import runpy |
20 import shlex | 20 import shlex |
21 import sys | 21 import sys |
22 import textwrap | 22 import textwrap |
23 | 23 |
24 from infra.ext import argcomplete | 24 |
| 25 ROOT = os.path.dirname(os.path.abspath(__file__)) |
| 26 |
| 27 if os.path.abspath(sys.prefix) != os.path.join(ROOT, 'ENV'): |
| 28 print 'You must use the virtualenv in ENV for scripts in the infra repo.' |
| 29 print 'Please run this as `./ENV/bin/python run.py`. If you do not have an' |
| 30 print 'ENV directory, please make one with `gclient runhooks`.' |
| 31 sys.exit(1) |
| 32 |
| 33 |
| 34 import argcomplete |
25 | 35 |
26 | 36 |
27 def main(args): | 37 def main(args): |
28 os.chdir(os.path.dirname(os.path.abspath(__file__))) | 38 os.chdir(os.path.dirname(os.path.abspath(__file__))) |
29 | 39 |
30 # Impersonate the argcomplete 'protocol' | 40 # Impersonate the argcomplete 'protocol' |
31 completing = os.getenv('_ARGCOMPLETE') == '1' | 41 completing = os.getenv('_ARGCOMPLETE') == '1' |
32 if completing: | 42 if completing: |
33 assert not args | 43 assert not args |
34 line = os.getenv('COMP_LINE') | 44 line = os.getenv('COMP_LINE') |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 argparse.ArgumentParser.parse_args = new_parse_args | 79 argparse.ArgumentParser.parse_args = new_parse_args |
70 else: | 80 else: |
71 # remove the module from sys.argv | 81 # remove the module from sys.argv |
72 del sys.argv[1] | 82 del sys.argv[1] |
73 | 83 |
74 runpy.run_module(args[0], run_name='__main__', alter_sys=True) | 84 runpy.run_module(args[0], run_name='__main__', alter_sys=True) |
75 | 85 |
76 | 86 |
77 if __name__ == '__main__': | 87 if __name__ == '__main__': |
78 sys.exit(main(sys.argv[1:])) | 88 sys.exit(main(sys.argv[1:])) |
OLD | NEW |