Chromium Code Reviews| 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 psutil | 7 import ctypes |
| 8 import pywintypes | |
| 8 import re | 9 import re |
| 10 import win32con | |
| 9 import win32gui | 11 import win32gui |
| 10 import win32process | 12 import win32process |
| 11 | 13 |
| 12 import path_resolver | 14 import path_resolver |
| 13 | 15 |
| 14 | 16 |
| 17 def GetProcessIDAndPathPairs(): | |
| 18 """Returns a list of 2-tuples of (process id, process path). | |
| 19 | |
| 20 This is needed because psutil is not available on Windows slave machines (see | |
| 21 https://code.google.com/p/chromium/issues/detail?id=257696). When it becomes | |
|
Mathieu
2013/08/30 19:34:10
How about:
This is needed because psutil is not a
sukolsak
2013/08/30 20:06:07
Done.
| |
| 22 available, we can use psutil.process_iter() instead. | |
| 23 """ | |
| 24 process_id_and_path_pairs = [] | |
| 25 for process_id in win32process.EnumProcesses(): | |
| 26 process_handle = ctypes.windll.kernel32.OpenProcess( | |
|
Mathieu
2013/08/30 19:34:10
Make sure this never produces an exception
sukolsak
2013/08/30 20:06:07
OpenProcess doesn't produce an exception. It retur
Mathieu
2013/08/30 20:11:15
Do you mean None or the string 'NULL'? Verify that
sukolsak
2013/08/30 20:22:02
Sorry, I mean 0 (int). I have verified it with "pr
| |
| 27 win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, | |
| 28 process_id) | |
| 29 if not process_handle: | |
| 30 continue | |
| 31 try: | |
| 32 process_path = win32process.GetModuleFileNameEx(process_handle, 0) | |
| 33 process_id_and_path_pairs.append((process_id, process_path)) | |
| 34 except pywintypes.error: | |
| 35 # It's normal that some processes are not accessible. | |
| 36 pass | |
| 37 return process_id_and_path_pairs | |
| 38 | |
| 39 | |
| 15 def GetProcessIDs(process_path): | 40 def GetProcessIDs(process_path): |
| 16 """Returns a list of IDs of processes whose path is |process_path|. | 41 """Returns a list of IDs of processes whose path is |process_path|. |
| 17 | 42 |
| 18 Args: | 43 Args: |
| 19 process_path: The path to the process. | 44 process_path: The path to the process. |
| 20 | 45 |
| 21 Returns: | 46 Returns: |
| 22 A list of process IDs. | 47 A list of process IDs. |
| 23 """ | 48 """ |
| 24 process_ids = [] | 49 process_ids = [] |
| 25 for process in psutil.process_iter(): | 50 for found_process_id, found_process_path in GetProcessIDAndPathPairs(): |
| 26 try: | 51 if found_process_path == process_path: |
| 27 found_process_path = process.exe | 52 process_ids.append(found_process_id) |
| 28 if found_process_path == process_path: | |
| 29 process_ids.append(process.pid) | |
| 30 except psutil.AccessDenied: | |
| 31 # It's normal that some processes are not accessible. | |
| 32 pass | |
| 33 return process_ids | 53 return process_ids |
|
Mathieu
2013/08/30 19:34:10
How about:
return [pid for (pid, path) in GetProce
sukolsak
2013/08/30 20:06:07
Done.
| |
| 34 | 54 |
| 35 | 55 |
| 36 def GetWindowHandles(process_ids): | 56 def GetWindowHandles(process_ids): |
| 37 """Returns a list of handles of windows owned by processes in |process_ids|. | 57 """Returns a list of handles of windows owned by processes in |process_ids|. |
| 38 | 58 |
| 39 Args: | 59 Args: |
| 40 process_ids: A list of process IDs. | 60 process_ids: A list of process IDs. |
| 41 | 61 |
| 42 Returns: | 62 Returns: |
| 43 A list of handles of windows owned by processes in |process_ids|. | 63 A list of handles of windows owned by processes in |process_ids|. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 73 | 93 |
| 74 | 94 |
| 75 def GetChromePath(system_level): | 95 def GetChromePath(system_level): |
| 76 """Returns the path to Chrome, at the |system_level| or user level.""" | 96 """Returns the path to Chrome, at the |system_level| or user level.""" |
| 77 chrome_path = None | 97 chrome_path = None |
| 78 if system_level: | 98 if system_level: |
| 79 chrome_path = '$PROGRAM_FILES\\$CHROME_DIR\\Application\\chrome.exe' | 99 chrome_path = '$PROGRAM_FILES\\$CHROME_DIR\\Application\\chrome.exe' |
| 80 else: | 100 else: |
| 81 chrome_path = '$LOCAL_APPDATA\\$CHROME_DIR\\Application\\chrome.exe' | 101 chrome_path = '$LOCAL_APPDATA\\$CHROME_DIR\\Application\\chrome.exe' |
| 82 return path_resolver.ResolvePath(chrome_path) | 102 return path_resolver.ResolvePath(chrome_path) |
| OLD | NEW |