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 Windows 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)
24 # Alternatively, the contents of this file may be used under the terms of
25 # either the GNU General Public License Version 2 or later (the "GPL"), or
26 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 # in which case the provisions of the GPL or the LGPL are applicable instead
28 # of those above. If you wish to allow use of your version of this file only
29 # under the terms of either the GPL or the LGPL, and not to allow others to
30 # use your version of this file under the terms of the MPL, indicate your
31 # decision by deleting the provisions above and replace them with the notice
32 # and other provisions required by the GPL or the LGPL. If you do not delete
33 # the provisions above, a recipient may use your version of this file under
34 # the terms of any one of the MPL, the GPL or the LGPL.
36 # ***** END LICENSE BLOCK *****
41 from select
import select
46 def GenerateFirefoxCommandLine(firefox_path
, profile_dir
, url
):
47 """Generates the command line for a process to run Firefox
50 firefox_path: String containing the path to the firefox exe to use
51 profile_dir: String containing the directory of the profile to run Firefox in
52 url: String containing url to start with.
57 profile_arg
= '-profile %s' % profile_dir
59 cmd
= '%s %s %s' % (firefox_path
,
65 def GetPidsByName(process_name
):
66 """Searches for processes containing a given string.
67 This function is UNIX specific.
70 process_name: The string to be searched for
73 A list of PIDs containing the string. An empty list is returned if none are
79 command
= ['ps', 'ax']
80 handle
= subprocess
.Popen(command
, stdout
=subprocess
.PIPE
)
82 # wait for the process to terminate
84 data
= handle
.stdout
.read()
86 # find all matching processes and add them to the list
87 for line
in data
.splitlines():
88 if line
.find(process_name
) >= 0:
89 # splits by whitespace, the first one should be the pid
90 pid
= int(line
.split()[0])
91 matchingPids
.append(pid
)
96 def ProcessesWithNameExist(*process_names
):
97 """Returns true if there are any processes running with the
98 given name. Useful to check whether a Firefox process is still running
101 process_names: String or strings containing the process name, i.e. "firefox"
104 True if any processes with that name are running, False otherwise.
107 for process_name
in process_names
:
108 pids
= GetPidsByName(process_name
)
114 def TerminateProcess(pid
):
115 """Helper function to terminate a process, given the pid
118 pid: integer process id of the process to terminate.
121 if ProcessesWithNameExist(str(pid
)):
122 os
.kill(pid
, signal
.SIGTERM
)
124 if ProcessesWithNameExist(str(pid
)):
125 os
.kill(pid
, signal
.SIGKILL
)
126 except OSError, (errno
, strerror
):
127 print 'WARNING: failed os.kill: %s : %s' % (errno
, strerror
)
129 def TerminateAllProcesses(*process_names
):
130 """Helper function to terminate all processes with the given process name
133 process_names: String or strings containing the process name, i.e. "firefox"
136 # Get all the process ids of running instances of this process,
138 for process_name
in process_names
:
139 pids
= GetPidsByName(process_name
)
141 TerminateProcess(pid
)
144 def NonBlockingReadProcessOutput(handle
):
145 """Does a non-blocking read from the output of the process
146 with the given handle.
149 handle: The process handle returned from os.popen()
152 A tuple (bytes, output) containing the number of output
153 bytes read, and the actual output.
160 # select() does not seem to work well with pipes.
161 # after data is available once it *always* thinks there is data available
162 # readline() will continue to return an empty string however
163 # so we can use this behavior to work around the problem
164 while select([handle
], [], [], 0)[0]:
165 line
= handle
.readline()
170 # this statement is true for encodings that have 1byte/char
171 num_avail
= len(output
)
173 return (num_avail
, output
)