OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env 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 """Helper script for GN to run an arbitrary binary. See compiled_action.gni. | 6 """Helper script for GN to run an arbitrary binary. See compiled_action.gni. |
7 | 7 |
8 Run with: | 8 Run with: |
9 python gn_run_binary.py <binary_name> [args ...] | 9 python gn_run_binary.py <invoker> <binary_name> [args ...] |
| 10 |
| 11 Where <invoker> is either "compiled_action" or "exec_script". If it is |
| 12 "compiled_action" the script has a non-zero exit code on a failure. If it is |
| 13 "exec_script" the script has no output on success and produces output otherwise, |
| 14 but always has an exit code of 0. |
10 """ | 15 """ |
11 | 16 |
12 import os | 17 import os |
13 import sys | 18 import sys |
14 import subprocess | 19 import subprocess |
15 | 20 |
16 # Run a command, swallowing the output unless there is an error. | 21 # Run a command, swallowing the output unless there is an error. |
17 def run_command(command): | 22 def run_command(command): |
18 try: | 23 try: |
19 subprocess.check_output(command, stderr=subprocess.STDOUT) | 24 subprocess.check_output(command, stderr=subprocess.STDOUT) |
20 return 0 | 25 return 0 |
21 except subprocess.CalledProcessError as e: | 26 except subprocess.CalledProcessError as e: |
22 return ("Command failed: " + ' '.join(command) + "\n" + | 27 return ("Command failed: " + ' '.join(command) + "\n" + |
23 "output: " + e.output) | 28 "output: " + e.output) |
24 | 29 |
25 def main(argv): | 30 def main(argv): |
| 31 error_exit = 0 |
| 32 if argv[1] == "compiled_action": |
| 33 error_exit = 1 |
| 34 elif argv[1] != "exec_script": |
| 35 print ("The first argument should be either " |
| 36 "'compiled_action' or 'exec_script") |
| 37 return 1 |
| 38 |
26 # Unless the path is absolute, this script is designed to run binaries | 39 # Unless the path is absolute, this script is designed to run binaries |
27 # produced by the current build. We always prefix it with "./" to avoid | 40 # produced by the current build. We always prefix it with "./" to avoid |
28 # picking up system versions that might also be on the path. | 41 # picking up system versions that might also be on the path. |
29 if os.path.isabs(argv[1]): | 42 if os.path.isabs(argv[2]): |
30 path = argv[1] | 43 path = argv[2] |
31 else: | 44 else: |
32 path = './' + argv[1] | 45 path = './' + argv[2] |
33 | 46 |
34 if not os.path.isfile(path): | 47 if not os.path.isfile(path): |
35 print "Binary not found: " + path | 48 print "Binary not found: " + path |
36 return 0 | 49 return error_exit |
37 | 50 |
38 # The rest of the arguements are passed directly to the executable. | 51 # The rest of the arguements are passed directly to the executable. |
39 args = [path] + argv[2:] | 52 args = [path] + argv[3:] |
40 | 53 |
41 result = run_command(args) | 54 result = run_command(args) |
42 if result != 0: | 55 if result != 0: |
43 print result | 56 print result |
| 57 return error_exit |
44 return 0 | 58 return 0 |
45 | 59 |
46 if __name__ == '__main__': | 60 if __name__ == '__main__': |
47 sys.exit(main(sys.argv)) | 61 sys.exit(main(sys.argv)) |
OLD | NEW |