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
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.
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 *****
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
43 subprocess
._cleanup
= lambda: None
47 from select
import select
50 def GenerateFirefoxCommandLine(firefox_path
, profile_dir
, url
):
51 """Generates the command line for a process to run Firefox
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.
61 profile_arg
= '-profile %s' % profile_dir
63 cmd
= '%s -foreground %s %s' % (firefox_path
,
69 def GetPidsByName(process_name
):
70 """Searches for processes containing a given string.
73 process_name: The string to be searched for
76 A list of PIDs containing the string. An empty list is returned if none are
83 handle
= subprocess
.Popen(command
, stdout
=subprocess
.PIPE
, universal_newlines
=True, shell
=True)
85 # wait for the process to terminate
87 data
= handle
.stdout
.readlines()
89 # find all matching processes and add them to the list
91 #overlook the mac crashreporter daemon
92 if line
.find("crashreporterd") >= 0:
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
)
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
107 process_names: String or strings containing the process name, i.e. "firefox"
110 True if any processes with that name are running, False otherwise.
112 for process_name
in process_names
:
113 pids
= GetPidsByName(process_name
)
119 def TerminateProcess(pid
):
120 """Helper function to terminate a process, given the pid
123 pid: integer process id of the process to terminate.
126 if ProcessesWithNameExist(str(pid
)):
127 os
.kill(pid
, signal
.SIGTERM
)
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
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
)
143 TerminateProcess(pid
)
145 def NonBlockingReadProcessOutput(handle
):
146 """Does a non-blocking read from the output of the process
147 with the given handle.
150 handle: The process handle returned from os.popen()
153 A tuple (bytes, output) containing the number of output
154 bytes read, and the actual output.
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()
171 # this statement is true for encodings that have 1byte/char
172 num_avail
= len(output
)
174 return (num_avail
, output
)