• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# for py2/py3 compatibility
39from __future__ import print_function
40
41import os
42from os.path import join, dirname, abspath
43import subprocess
44import sys
45import tempfile
46
47def Check(output, errors):
48  failed = any([s.startswith('/system/bin/sh:') or s.startswith('ANDROID')
49                for s in output.split('\n')])
50  return 1 if failed else 0
51
52def Execute(cmdline):
53  (fd_out, outname) = tempfile.mkstemp()
54  (fd_err, errname) = tempfile.mkstemp()
55  process = subprocess.Popen(
56    args=cmdline,
57    shell=True,
58    stdout=fd_out,
59    stderr=fd_err,
60  )
61  exit_code = process.wait()
62  os.close(fd_out)
63  os.close(fd_err)
64  output = open(outname).read()
65  errors = open(errname).read()
66  os.unlink(outname)
67  os.unlink(errname)
68  sys.stdout.write(output)
69  sys.stderr.write(errors)
70  return exit_code or Check(output, errors)
71
72def Escape(arg):
73  def ShouldEscape():
74    for x in arg:
75      if not x.isalnum() and x != '-' and x != '_':
76        return True
77    return False
78
79  return arg if not ShouldEscape() else '"%s"' % (arg.replace('"', '\\"'))
80
81def WriteToTemporaryFile(data):
82  (fd, fname) = tempfile.mkstemp()
83  os.close(fd)
84  tmp_file = open(fname, "w")
85  tmp_file.write(data)
86  tmp_file.close()
87  return fname
88
89def Main():
90  if (len(sys.argv) == 1):
91    print("Usage: %s <command-to-run-on-device>" % sys.argv[0])
92    return 1
93  workspace = abspath(join(dirname(sys.argv[0]), '..'))
94  v8_root = "/data/local/tmp/v8"
95  android_workspace = os.getenv("ANDROID_V8", v8_root)
96  args = [Escape(arg) for arg in sys.argv[1:]]
97  script = (" ".join(args) + "\n"
98            "case $? in\n"
99            "  0) ;;\n"
100            "  *) echo \"ANDROID: Error returned by test\";;\n"
101            "esac\n")
102  script = script.replace(workspace, android_workspace)
103  script_file = WriteToTemporaryFile(script)
104  android_script_file = android_workspace + "/" + script_file
105  command =  ("adb push '%s' %s;" % (script_file, android_script_file) +
106              "adb shell 'cd %s && sh %s';" % (v8_root, android_script_file) +
107              "adb shell 'rm %s'" % android_script_file)
108  error_code = Execute(command)
109  os.unlink(script_file)
110  return error_code
111
112if __name__ == '__main__':
113  sys.exit(Main())
114