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 *****
47 def GetCygwinPath(dos_path
):
48 """Helper function to get the Cygwin path from a dos path.
49 Used to generate a Firefox command line piped through the
53 dos_path: String containing the dos path
56 String containing the cygwin path
59 # Convert the path to a cygwin path. Assumes the path starts with
60 # /cygdrive/driveletter
61 cygwin_path
= '/' + dos_path
[3:] # Remove 'C:\'
62 cygwin_path
= cygwin_path
.replace('\\', '/') # Backslashes->slashes
63 cygwin_path
= cygwin_path
.replace(' ', '\\ ') # Escape spaces
64 cygwin_path
= '/cygdrive/' + dos_path
[0] + cygwin_path
# Add drive letter
68 def GenerateFirefoxCommandLine(firefox_path
, profile_dir
, url
):
69 """Generates the command line for a process to run Firefox, wrapped
70 by cygwin so that we can read the output from dump() statements.
73 firefox_path: String containing the path to the firefox exe to use
74 profile_dir: String containing the directory of the profile to run Firefox in
75 url: String containing url to start with.
80 profile_dir
= profile_dir
.replace('\\', '\\\\\\')
81 profile_arg
= '-profile %s' % profile_dir
85 url_arg
= '-url %s' % url
87 cmd
= '%s "%s %s %s"' % (config
.CYGWIN
,
88 GetCygwinPath(firefox_path
),
94 def TerminateProcess(pid
):
95 """Helper function to terminate a process, given the pid
98 pid: integer process id of the process to terminate.
101 PROCESS_TERMINATE
= 1
102 handle
= win32api
.OpenProcess(PROCESS_TERMINATE
, False, pid
)
103 win32api
.TerminateProcess(handle
, -1)
104 win32api
.CloseHandle(handle
)
107 def ProcessesWithNameExist(process_name
):
108 """Returns true if there are any processes running with the
109 given name. Useful to check whether a Firefox process is still running
112 process_name: String containing the process name, i.e. "firefox"
115 True if any processes with that name are running, False otherwise.
119 pids
= win32pdhutil
.FindPerformanceAttributesByName(process_name
, counter
="ID Process")
122 # Might get an exception if there are no instances of the process running.
126 def TerminateAllProcesses(process_name
):
127 """Helper function to terminate all processes with the given process name
130 process_name: String containing the process name, i.e. "firefox"
133 # Get all the process ids of running instances of this process, and terminate them.
135 pids
= win32pdhutil
.FindPerformanceAttributesByName(process_name
, counter
="ID Process")
137 TerminateProcess(pid
)
139 # Might get an exception if there are no instances of the process running.
143 def NonBlockingReadProcessOutput(handle
):
144 """Does a non-blocking read from the output of the process
145 with the given handle.
148 handle: The process handle returned from os.popen()
151 A tuple (bytes, output) containing the number of output
152 bytes read, and the actual output.
158 osfhandle
= msvcrt
.get_osfhandle(handle
.fileno())
159 (read
, num_avail
, num_message
) = win32pipe
.PeekNamedPipe(osfhandle
, 0)
161 (error_code
, output
) = win32file
.ReadFile(osfhandle
, num_avail
, None)
163 return (num_avail
, output
)