• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6#     * Redistributions of source code must retain the above copyright
7#       notice, this list of conditions and the following disclaimer.
8#     * Redistributions in binary form must reproduce the above
9#       copyright notice, this list of conditions and the following
10#       disclaimer in the documentation and/or other materials provided
11#       with the distribution.
12#     * Neither the name of Google Inc. nor the names of its
13#       contributors may be used to endorse or promote products derived
14#       from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29import subprocess
30import sys
31from threading import Timer
32
33from ..local import utils
34from ..objects import output
35
36
37SEM_INVALID_VALUE = -1
38SEM_NOGPFAULTERRORBOX = 0x0002  # Microsoft Platform SDK WinBase.h
39
40
41def Win32SetErrorMode(mode):
42  prev_error_mode = SEM_INVALID_VALUE
43  try:
44    import ctypes
45    prev_error_mode = \
46        ctypes.windll.kernel32.SetErrorMode(mode)  #@UndefinedVariable
47  except ImportError:
48    pass
49  return prev_error_mode
50
51
52def RunProcess(verbose, timeout, args, **rest):
53  if verbose: print "#", " ".join(args)
54  popen_args = args
55  prev_error_mode = SEM_INVALID_VALUE
56  if utils.IsWindows():
57    popen_args = subprocess.list2cmdline(args)
58    # Try to change the error mode to avoid dialogs on fatal errors. Don't
59    # touch any existing error mode flags by merging the existing error mode.
60    # See http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx.
61    error_mode = SEM_NOGPFAULTERRORBOX
62    prev_error_mode = Win32SetErrorMode(error_mode)
63    Win32SetErrorMode(error_mode | prev_error_mode)
64
65  try:
66    process = subprocess.Popen(
67      args=popen_args,
68      stdout=subprocess.PIPE,
69      stderr=subprocess.PIPE,
70      **rest
71    )
72  except Exception as e:
73    sys.stderr.write("Error executing: %s\n" % popen_args)
74    raise e
75
76  if (utils.IsWindows() and prev_error_mode != SEM_INVALID_VALUE):
77    Win32SetErrorMode(prev_error_mode)
78
79  def kill_process(process, timeout_result):
80    timeout_result[0] = True
81    try:
82      if utils.IsWindows():
83        if verbose:
84          print "Attempting to kill process %d" % process.pid
85          sys.stdout.flush()
86        tk = subprocess.Popen(
87            'taskkill /T /F /PID %d' % process.pid,
88            stdout=subprocess.PIPE,
89            stderr=subprocess.PIPE,
90        )
91        stdout, stderr = tk.communicate()
92        if verbose:
93          print "Taskkill results for %d" % process.pid
94          print stdout
95          print stderr
96          print "Return code: %d" % tk.returncode
97          sys.stdout.flush()
98      else:
99        process.kill()
100    except OSError:
101      sys.stderr.write('Error: Process %s already ended.\n' % process.pid)
102
103  # Pseudo object to communicate with timer thread.
104  timeout_result = [False]
105
106  timer = Timer(timeout, kill_process, [process, timeout_result])
107  timer.start()
108  stdout, stderr = process.communicate()
109  timer.cancel()
110  return process.returncode, timeout_result[0], stdout, stderr
111
112
113def Execute(args, verbose=False, timeout=None):
114  args = [ c for c in args if c != "" ]
115  exit_code, timed_out, stdout, stderr = RunProcess(
116    verbose,
117    timeout,
118    args=args,
119  )
120  return output.Output(exit_code, timed_out, stdout, stderr)
121