Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / native_client / tools / browser_tester / browsertester / browserprocess.py
blobe10e6b5336ff679076010e0116b31047b4f0bdad
1 #!/usr/bin/python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import os
7 import signal
8 import subprocess
9 import time
11 class BrowserProcessBase(object):
13 def __init__(self, handle):
14 self.handle = handle
15 print 'PID', self.handle.pid
17 def GetReturnCode(self):
18 return self.handle.returncode
20 def IsRunning(self):
21 return self.handle.poll() is None
23 def Wait(self, wait_steps, sleep_time):
24 try:
25 self.term()
26 except Exception:
27 # Terminating the handle can raise an exception. There is likely no point
28 # in waiting if the termination didn't succeed.
29 return
31 i = 0
32 # subprocess.wait() doesn't have a timeout, unfortunately.
33 while self.IsRunning() and i < wait_steps:
34 time.sleep(sleep_time)
35 i += 1
37 def Kill(self):
38 if self.IsRunning():
39 print 'KILLING the browser'
40 try:
41 self.kill()
42 # If it doesn't die, we hang. Oh well.
43 self.handle.wait()
44 except Exception:
45 # If it is already dead, then it's ok.
46 # This may happen if the browser dies after the first poll, but
47 # before the kill.
48 if self.IsRunning():
49 raise
51 class BrowserProcess(BrowserProcessBase):
53 def term(self):
54 self.handle.terminate()
56 def kill(self):
57 self.handle.kill()
60 class BrowserProcessPosix(BrowserProcessBase):
61 """ This variant of BrowserProcess uses process groups to manage browser
62 life time. """
64 def term(self):
65 os.killpg(self.handle.pid, signal.SIGTERM)
67 def kill(self):
68 os.killpg(self.handle.pid, signal.SIGKILL)
71 def RunCommandWithSubprocess(cmd, env=None):
72 handle = subprocess.Popen(cmd, env=env)
73 return BrowserProcess(handle)
76 def RunCommandInProcessGroup(cmd, env=None):
77 def SetPGrp():
78 os.setpgrp()
79 print 'I\'M THE SESSION LEADER!'
80 handle = subprocess.Popen(cmd, env=env, preexec_fn=SetPGrp)
81 return BrowserProcessPosix(handle)