OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Common helper module for working with Chrome's processes and windows.""" | 5 """Common helper module for working with Chrome's processes and windows.""" |
6 | 6 |
7 import ctypes | 7 import ctypes |
grt (UTC plus 2)
2014/09/12 15:01:11
unused
huangs
2014/09/12 18:25:33
Done.
| |
8 import psutil | |
8 import pywintypes | 9 import pywintypes |
grt (UTC plus 2)
2014/09/12 15:01:11
unused
huangs
2014/09/12 18:25:33
Done.
| |
9 import re | 10 import re |
10 import win32con | 11 import win32con |
grt (UTC plus 2)
2014/09/12 15:01:11
unused
huangs
2014/09/12 18:25:33
Done.
| |
11 import win32gui | 12 import win32gui |
12 import win32process | 13 import win32process |
13 | 14 |
14 | 15 |
15 def GetProcessIDAndPathPairs(): | 16 def GetProcessIDAndPathPairs(): |
16 """Returns a list of 2-tuples of (process id, process path). | 17 """Returns a list of 2-tuples of (process id, process path). |
17 | |
18 This is needed because psutil is not available on Windows slave machines (see: | |
19 http://crbug.com/257696). | |
20 TODO(sukolsak): Use psutil.process_iter() once it becomes available. | |
21 """ | 18 """ |
22 process_id_and_path_pairs = [] | 19 process_id_and_path_pairs = [] |
23 for process_id in win32process.EnumProcesses(): | 20 for process in psutil.process_iter(): |
24 process_handle = ctypes.windll.kernel32.OpenProcess( | |
25 win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, | |
26 process_id) | |
27 if not process_handle: | |
28 continue | |
29 try: | 21 try: |
30 process_path = win32process.GetModuleFileNameEx(process_handle, 0) | 22 process_id_and_path_pairs.append((process.pid, process.exe)) |
grt (UTC plus 2)
2014/09/12 15:01:11
http://pythonhosted.org/psutil/#psutil.Process say
grt (UTC plus 2)
2014/09/12 15:01:11
process.exe -> process.exe()
huangs
2014/09/12 18:25:33
Catching psutil.Error, per suggested below.
huangs
2014/09/12 18:25:33
From
https://code.google.com/p/psutil/wiki/Documen
grt (UTC plus 2)
2014/09/12 18:35:20
Odd. http://pythonhosted.org/psutil/#psutil.Proces
| |
31 process_id_and_path_pairs.append((process_id, process_path)) | 23 except psutil._error.AccessDenied: |
grt (UTC plus 2)
2014/09/12 15:01:11
perhaps just catch psutil.Error here, since that's
huangs
2014/09/12 18:25:33
Done.
| |
32 except pywintypes.error: | |
33 # It's normal that some processes are not accessible. | 24 # It's normal that some processes are not accessible. |
34 pass | 25 pass |
35 return process_id_and_path_pairs | 26 return process_id_and_path_pairs |
36 | 27 |
37 | 28 |
38 def GetProcessIDs(process_path): | 29 def GetProcessIDs(process_path): |
39 """Returns a list of IDs of processes whose path is |process_path|. | 30 """Returns a list of IDs of processes whose path is |process_path|. |
40 | 31 |
41 Args: | 32 Args: |
42 process_path: The path to the process. | 33 process_path: The path to the process. |
43 | 34 |
44 Returns: | 35 Returns: |
45 A list of process IDs. | 36 A list of process IDs. |
46 """ | 37 """ |
47 return [pid for (pid, path) in GetProcessIDAndPathPairs() if | 38 return [pid for (pid, path) in GetProcessIDAndPathPairs() if |
48 path == process_path] | 39 path == process_path] |
49 | 40 |
50 | 41 |
51 def GetWindowHandles(process_ids): | 42 def GetWindowHandles(process_ids): |
grt (UTC plus 2)
2014/09/12 15:01:11
this looks unnecessarily racy for the case where i
huangs
2014/09/12 18:25:33
EnumWindow() only looks at top-level windows, so t
| |
52 """Returns a list of handles of windows owned by processes in |process_ids|. | 43 """Returns a list of handles of windows owned by processes in |process_ids|. |
53 | 44 |
54 Args: | 45 Args: |
55 process_ids: A list of process IDs. | 46 process_ids: A list of process IDs. |
56 | 47 |
57 Returns: | 48 Returns: |
58 A list of handles of windows owned by processes in |process_ids|. | 49 A list of handles of windows owned by processes in |process_ids|. |
59 """ | 50 """ |
60 hwnds = [] | 51 hwnds = [] |
61 def EnumerateWindowCallback(hwnd, _): | 52 def EnumerateWindowCallback(hwnd, _): |
(...skipping 16 matching lines...) Expand all Loading... | |
78 process_ids: A list of process IDs. | 69 process_ids: A list of process IDs. |
79 class_pattern: The regular expression pattern of the window class name. | 70 class_pattern: The regular expression pattern of the window class name. |
80 | 71 |
81 Returns: | 72 Returns: |
82 A boolean indicating whether such window exists. | 73 A boolean indicating whether such window exists. |
83 """ | 74 """ |
84 for hwnd in GetWindowHandles(process_ids): | 75 for hwnd in GetWindowHandles(process_ids): |
85 if re.match(class_pattern, win32gui.GetClassName(hwnd)): | 76 if re.match(class_pattern, win32gui.GetClassName(hwnd)): |
86 return True | 77 return True |
87 return False | 78 return False |
OLD | NEW |