Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / testing / performance / talos / ffprocess_win32.py
bloba4341587ccf32d491d0164f11307e4b8abb94724
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 win32pdh
42 import win32pipe
43 import msvcrt
46 def GenerateFirefoxCommandLine(firefox_path, profile_dir, url):
47 """Generates the command line for a process to run Firefox
49 Args:
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.
53 """
55 profile_arg = ''
56 if profile_dir:
57 profile_dir = profile_dir.replace('\\', '\\\\\\')
58 profile_arg = '-profile %s' % profile_dir
60 cmd = '%s %s %s' % (firefox_path,
61 profile_arg,
62 url)
63 return cmd
66 def TerminateProcess(pid):
67 """Helper function to terminate a process, given the pid
69 Args:
70 pid: integer process id of the process to terminate.
71 """
73 PROCESS_TERMINATE = 1
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
83 Args:
84 process_name: String or strings containing the process name, i.e. "firefox"
86 Returns:
87 True if any processes with that name are running, False otherwise.
88 """
90 for process_name in process_names:
91 try:
92 # refresh list of processes
93 win32pdh.EnumObjects(None, None, 0, 1)
94 pids = win32pdhutil.FindPerformanceAttributesByName(process_name, counter="ID Process")
95 if len(pids) > 0:
96 return True
97 except:
98 # Might get an exception if there are no instances of the process running.
99 continue
100 return False
103 def TerminateAllProcesses(*process_names):
104 """Helper function to terminate all processes with the given process name
106 Args:
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.
111 try:
112 pids = win32pdhutil.FindPerformanceAttributesByName(process_name, counter="ID Process")
113 for pid in pids:
114 TerminateProcess(pid)
115 except:
116 # Might get an exception if there are no instances of the process running.
117 continue
120 def NonBlockingReadProcessOutput(handle):
121 """Does a non-blocking read from the output of the process
122 with the given handle.
124 Args:
125 handle: The process handle returned from os.popen()
127 Returns:
128 A tuple (bytes, output) containing the number of output
129 bytes read, and the actual output.
132 output = ""
134 try:
135 osfhandle = msvcrt.get_osfhandle(handle.fileno())
136 (read, num_avail, num_message) = win32pipe.PeekNamedPipe(osfhandle, 0)
137 if num_avail > 0:
138 (error_code, output) = win32file.ReadFile(osfhandle, num_avail, None)
140 return (num_avail, output)
141 except:
142 return (0, output)