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 *****
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_dir
= profile_dir
.replace('\\', '\\\\\\')
58 profile_arg
= '-profile %s' % profile_dir
60 cmd
= '%s %s %s' % (firefox_path
,
66 def TerminateProcess(pid
):
67 """Helper function to terminate a process, given the pid
70 pid: integer process id of the process to terminate.
74 handle
= win32api
.OpenProcess(PROCESS_TERMINATE
, False, pid
)
75 win32api
.TerminateProcess(handle
, -1)
76 win32api
.CloseHandle(handle
)
79 def ProcessesWithNameExist(*process_names
):
80 """Returns true if there are any processes running with the
81 given name. Useful to check whether a Firefox process is still running
84 process_name: String or strings containing the process name, i.e. "firefox"
87 True if any processes with that name are running, False otherwise.
90 for process_name
in process_names
:
92 # refresh list of processes
93 win32pdh
.EnumObjects(None, None, 0, 1)
94 pids
= win32pdhutil
.FindPerformanceAttributesByName(process_name
, counter
="ID Process")
98 # Might get an exception if there are no instances of the process running.
103 def TerminateAllProcesses(*process_names
):
104 """Helper function to terminate all processes with the given process name
107 process_name: String or strings containing the process name, i.e. "firefox"
109 for process_name
in process_names
:
110 # Get all the process ids of running instances of this process, and terminate them.
112 pids
= win32pdhutil
.FindPerformanceAttributesByName(process_name
, counter
="ID Process")
114 TerminateProcess(pid
)
116 # Might get an exception if there are no instances of the process running.
120 def NonBlockingReadProcessOutput(handle
):
121 """Does a non-blocking read from the output of the process
122 with the given handle.
125 handle: The process handle returned from os.popen()
128 A tuple (bytes, output) containing the number of output
129 bytes read, and the actual output.
135 osfhandle
= msvcrt
.get_osfhandle(handle
.fileno())
136 (read
, num_avail
, num_message
) = win32pipe
.PeekNamedPipe(osfhandle
, 0)
138 (error_code
, output
) = win32file
.ReadFile(osfhandle
, num_avail
, None)
140 return (num_avail
, output
)