OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright 2012 the V8 project authors. All rights reserved. | |
4 # Redistribution and use in source and binary forms, with or without | |
5 # modification, are permitted provided that the following conditions are | |
6 # met: | |
7 # | |
8 # * Redistributions of source code must retain the above copyright | |
9 # notice, this list of conditions and the following disclaimer. | |
10 # * Redistributions in binary form must reproduce the above | |
11 # copyright notice, this list of conditions and the following | |
12 # disclaimer in the documentation and/or other materials provided | |
13 # with the distribution. | |
14 # * Neither the name of Google Inc. nor the names of its | |
15 # contributors may be used to endorse or promote products derived | |
16 # from this software without specific prior written permission. | |
17 # | |
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | |
30 # This script executes the passed command line on Android device | |
31 # using 'adb shell' command. Unfortunately, 'adb shell' always | |
32 # returns exit code 0, ignoring the exit code of executed command. | |
33 # Since we need to return non-zero exit code if the command failed, | |
34 # we augment the passed command line with exit code checking statement | |
35 # and output special error string in case of non-zero exit code. | |
36 # Then we parse the output of 'adb shell' and look for that error string. | |
37 | |
38 import os | |
39 from os.path import join, dirname, abspath | |
40 import string | |
Jakob Kummerow
2012/07/02 14:25:58
unused import
ulan
2012/07/02 16:18:23
Done.
| |
41 import subprocess | |
42 import sys | |
43 import tempfile | |
44 | |
45 def Check(output, errors): | |
46 failed = any([s.startswith('/system/bin/sh:') or s.startswith('Error') | |
47 for s in output.split('\n')]) | |
48 return 1 if failed else 0 | |
49 | |
50 def Execute(cmdline): | |
51 (fd_out, outname) = tempfile.mkstemp() | |
52 (fd_err, errname) = tempfile.mkstemp() | |
53 process = subprocess.Popen( | |
54 args = cmdline, | |
Jakob Kummerow
2012/07/02 14:25:58
nit: styleguide says no spaces around = for keywor
ulan
2012/07/02 16:18:23
Done.
| |
55 shell = True, | |
56 stdout = fd_out, | |
57 stderr = fd_err, | |
58 ) | |
59 exit_code = process.wait() | |
60 os.close(fd_out) | |
61 os.close(fd_err) | |
62 output = file(outname).read() | |
63 errors = file(errname).read() | |
64 os.unlink(outname) | |
65 os.unlink(errname) | |
66 sys.stdout.write(output) | |
67 sys.stderr.write(errors) | |
68 exit_code = exit_code if exit_code != 0 else Check(output, errors) | |
Jakob Kummerow
2012/07/02 14:25:58
shorter:
exit_code = exit_code or Check(output, e
ulan
2012/07/02 16:18:23
Done.
| |
69 return exit_code | |
70 | |
71 def Escape(arg): | |
72 def ShouldEscape(): | |
73 for x in arg: | |
74 if not x.isalnum() and x != '-' and x != '_': | |
75 return True | |
76 return False | |
77 | |
78 return arg if not ShouldEscape() else '"%s"' % (arg.replace('"', '\\"')) | |
79 | |
80 def WriteToTemporaryFile(data): | |
81 (fd, fname) = tempfile.mkstemp() | |
82 os.close(fd) | |
83 tmp_file = open(fname, "w") | |
84 tmp_file.write(data) | |
85 tmp_file.close() | |
86 return fname | |
87 | |
88 def Main(): | |
89 if (len(sys.argv) == 1): | |
90 print("Usage: %s <command-to-run-on-device>" % sys.argv[0]) | |
91 return 1 | |
92 workspace = abspath(join(dirname(sys.argv[0]), '..')) | |
93 android_workspace = os.getenv("ANDROID_V8", "/data/local/v8") | |
94 args = [Escape(arg) for arg in sys.argv[1:]] | |
95 script = (" ".join(args) + "\n" + | |
Jakob Kummerow
2012/07/02 14:25:58
nit: you don't need '+' at the end of a line to ge
ulan
2012/07/02 16:18:23
Done.
| |
96 "if [ $? -ne 0 ]\n" + | |
97 " then echo \"Error returned by test\";\n" + | |
98 "fi\n") | |
99 script = script.replace(workspace, android_workspace) | |
100 script_file = WriteToTemporaryFile(script) | |
101 android_script_file = android_workspace + "/" + script_file | |
102 command = ("adb push '%s' %s;" + | |
Jakob Kummerow
2012/07/02 14:25:58
reformat:
command = ("adb push '%s' %s;" % (scri
ulan
2012/07/02 16:18:23
Done.
| |
103 "adb shell 'sh %s';" + | |
104 "adb shell 'rm %s'") % \ | |
105 (script_file, android_script_file, | |
106 android_script_file, android_script_file) | |
107 error_code = Execute(command) | |
108 os.unlink(script_file) | |
109 return error_code | |
110 | |
111 if __name__ == '__main__': | |
112 sys.exit(Main()) | |
OLD | NEW |