For bug 379240, update bootstrap.cfg for RC2
[mozilla-1.9.git] / testing / performance / talos / ffprocess_win32.py
blob15c227be68fd51626e36c4da2ba962df60b29a41
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 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.
20 # Contributor(s):
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 *****
38 import win32api
39 import win32file
40 import win32pdhutil
41 import win32pipe
42 import msvcrt
44 import config
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
50 Cygwin bash shell
52 Args:
53 dos_path: String containing the dos path
55 Returns:
56 String containing the cygwin path
57 """
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
65 return cygwin_path
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.
72 Args:
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.
76 """
78 profile_arg = ''
79 if profile_dir:
80 profile_dir = profile_dir.replace('\\', '\\\\\\')
81 profile_arg = '-profile %s' % profile_dir
83 url_arg = ''
84 if url:
85 url_arg = '-url %s' % url
87 cmd = '%s "%s %s %s"' % (config.CYGWIN,
88 GetCygwinPath(firefox_path),
89 profile_arg,
90 url_arg)
91 return cmd
94 def TerminateProcess(pid):
95 """Helper function to terminate a process, given the pid
97 Args:
98 pid: integer process id of the process to terminate.
99 """
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
111 Args:
112 process_name: String containing the process name, i.e. "firefox"
114 Returns:
115 True if any processes with that name are running, False otherwise.
118 try:
119 pids = win32pdhutil.FindPerformanceAttributesByName(process_name, counter="ID Process")
120 return len(pids) > 0
121 except:
122 # Might get an exception if there are no instances of the process running.
123 return False
126 def TerminateAllProcesses(process_name):
127 """Helper function to terminate all processes with the given process name
129 Args:
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.
134 try:
135 pids = win32pdhutil.FindPerformanceAttributesByName(process_name, counter="ID Process")
136 for pid in pids:
137 TerminateProcess(pid)
138 except:
139 # Might get an exception if there are no instances of the process running.
140 pass
143 def NonBlockingReadProcessOutput(handle):
144 """Does a non-blocking read from the output of the process
145 with the given handle.
147 Args:
148 handle: The process handle returned from os.popen()
150 Returns:
151 A tuple (bytes, output) containing the number of output
152 bytes read, and the actual output.
155 output = ""
157 try:
158 osfhandle = msvcrt.get_osfhandle(handle.fileno())
159 (read, num_avail, num_message) = win32pipe.PeekNamedPipe(osfhandle, 0)
160 if num_avail > 0:
161 (error_code, output) = win32file.ReadFile(osfhandle, num_avail, None)
163 return (num_avail, output)
164 except:
165 return (0, output)