Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / testing / performance / talos / ffprocess_mac.py
blobd066f4edfb6f83d85d6f9decdfc2ac5e6e77b050
1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
12 # License.
14 # The Original Code is standalone Firefox Mac performance test.
16 # The Initial Developer of the Original Code is Google Inc.
17 # Portions created by the Initial Developer are Copyright (C) 2006
18 # the Initial Developer. All Rights Reserved.
20 # Contributor(s):
21 # Annie Sullivan <annie.sullivan@gmail.com> (original author)
22 # Ben Hearsum <bhearsum@wittydomain.com> (OS independence)
23 # Zach Lipton <zach@zachlipton.com> (Mac port)
25 # Alternatively, the contents of this file may be used under the terms of
26 # either the GNU General Public License Version 2 or later (the "GPL"), or
27 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 # in which case the provisions of the GPL or the LGPL are applicable instead
29 # of those above. If you wish to allow use of your version of this file only
30 # under the terms of either the GPL or the LGPL, and not to allow others to
31 # use your version of this file under the terms of the MPL, indicate your
32 # decision by deleting the provisions above and replace them with the notice
33 # and other provisions required by the GPL or the LGPL. If you do not delete
34 # the provisions above, a recipient may use your version of this file under
35 # the terms of any one of the MPL, the GPL or the LGPL.
37 # ***** END LICENSE BLOCK *****
39 import subprocess
40 #HACK - http://www.gossamer-threads.com/lists/python/bugs/593800
41 #To stop non-threadsafe popen nonsense, should be removed when we upgrade to
42 #python 2.5 or later
43 subprocess._cleanup = lambda: None
44 import signal
45 import os
46 import time
47 from select import select
50 def GenerateFirefoxCommandLine(firefox_path, profile_dir, url):
51 """Generates the command line for a process to run Firefox
53 Args:
54 firefox_path: String containing the path to the firefox binary to use
55 profile_dir: String containing the directory of the profile to run Firefox in
56 url: String containing url to start with.
57 """
59 profile_arg = ''
60 if profile_dir:
61 profile_arg = '-profile %s' % profile_dir
63 cmd = '%s -foreground %s %s' % (firefox_path,
64 profile_arg,
65 url)
66 return cmd
69 def GetPidsByName(process_name):
70 """Searches for processes containing a given string.
72 Args:
73 process_name: The string to be searched for
75 Returns:
76 A list of PIDs containing the string. An empty list is returned if none are
77 found.
78 """
80 matchingPids = []
82 command = ['ps -Ac']
83 handle = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True, shell=True)
85 # wait for the process to terminate
86 handle.wait()
87 data = handle.stdout.readlines()
89 # find all matching processes and add them to the list
90 for line in data:
91 #overlook the mac crashreporter daemon
92 if line.find("crashreporterd") >= 0:
93 continue
94 if line.find(process_name) >= 0:
95 # splits by whitespace, the first one should be the pid
96 pid = int(line.split()[0])
97 matchingPids.append(pid)
99 return matchingPids
102 def ProcessesWithNameExist(*process_names):
103 """Returns true if there are any processes running with the
104 given name. Useful to check whether a Firefox process is still running
106 Args:
107 process_names: String or strings containing the process name, i.e. "firefox"
109 Returns:
110 True if any processes with that name are running, False otherwise.
112 for process_name in process_names:
113 pids = GetPidsByName(process_name)
114 if len(pids) > 0:
115 return True
116 return False
119 def TerminateProcess(pid):
120 """Helper function to terminate a process, given the pid
122 Args:
123 pid: integer process id of the process to terminate.
125 try:
126 if ProcessesWithNameExist(str(pid)):
127 os.kill(pid, signal.SIGTERM)
128 time.sleep(5)
129 if ProcessesWithNameExist(str(pid)):
130 os.kill(pid, signal.SIGKILL)
131 except OSError, (errno, strerror):
132 print 'WARNING: failed os.kill: %s : %s' % (errno, strerror)
134 def TerminateAllProcesses(*process_names):
135 """Helper function to terminate all processes with the given process name
137 Args:
138 process_names: String or strings containing the process name, i.e. "firefox"
140 for process_name in process_names:
141 pids = GetPidsByName(process_name)
142 for pid in pids:
143 TerminateProcess(pid)
145 def NonBlockingReadProcessOutput(handle):
146 """Does a non-blocking read from the output of the process
147 with the given handle.
149 Args:
150 handle: The process handle returned from os.popen()
152 Returns:
153 A tuple (bytes, output) containing the number of output
154 bytes read, and the actual output.
157 output = ""
158 num_avail = 0
160 # check for data
161 # select() does not seem to work well with pipes.
162 # after data is available once it *always* thinks there is data available
163 # readline() will continue to return an empty string however
164 # so we can use this behavior to work around the problem
165 while select([handle], [], [], 0)[0]:
166 line = handle.readline()
167 if line:
168 output += line
169 else:
170 break
171 # this statement is true for encodings that have 1byte/char
172 num_avail = len(output)
174 return (num_avail, output)