forked from dart-lang/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gn_run_binary.py
executable file
·67 lines (52 loc) · 1.98 KB
/
gn_run_binary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Helper script for GN to run an arbitrary binary. See compiled_action.gni.
Run with:
python3 gn_run_binary.py <invoker> <binary_name> [args ...]
Where <invoker> is either "compiled_action" or "exec_script". If it is
"compiled_action" the script has a non-zero exit code on a failure. If it is
"exec_script" the script has no output on success and produces output otherwise,
but always has an exit code of 0.
"""
import os
import sys
import subprocess
# Run a command, swallowing the output unless there is an error.
def run_command(command):
try:
subprocess.check_output(command, stderr=subprocess.STDOUT)
return 0
except subprocess.CalledProcessError as e:
return ("Command failed: " + ' '.join(command) + "\n" + "output: " +
_decode(e.output))
except OSError as e:
return ("Command failed: " + ' '.join(command) + "\n" + "output: " +
_decode(e.strerror))
def _decode(bytes):
return bytes.decode("utf-8")
def main(argv):
error_exit = 0
if argv[1] == "compiled_action":
error_exit = 1
elif argv[1] != "exec_script":
print("The first argument should be either "
"'compiled_action' or 'exec_script")
return 1
# Unless the path is absolute, this script is designed to run binaries
# produced by the current build, which is the current working directory when
# this script is run.
path = os.path.abspath(argv[2])
if not os.path.isfile(path):
print("Binary not found: " + path)
return error_exit
# The rest of the arguments are passed directly to the executable.
args = [path] + argv[3:]
result = run_command(args)
if result != 0:
print(result)
return error_exit
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))