bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4988.10.3
by John Arbash Meinel
Merge bzr.dev 5007, resolve conflict, update NEWS |
1 |
# Copyright (C) 2005-2010 Canonical Ltd
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
2 |
#
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
3 |
# This program is free software; you can redistribute it and/or modify
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
16 |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
17 |
"""Win32-specific helper functions
|
18 |
||
19 |
Only one dependency: ctypes should be installed.
|
|
20 |
"""
|
|
21 |
||
4786.1.1
by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows. |
22 |
import glob |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
23 |
import os |
1185.16.86
by mbp at sourcefrog
- win32 get_console_size from Alexander |
24 |
import struct |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
25 |
import sys |
26 |
||
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
27 |
from breezy import ( |
6362.2.2
by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths |
28 |
cmdline, |
29 |
)
|
|
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
30 |
from breezy.i18n import gettext |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
31 |
|
32 |
||
33 |
# Special Win32 API constants
|
|
34 |
# Handles of std streams
|
|
1704.2.3
by Martin Pool
(win32) Detect terminal width using GetConsoleScreenBufferInfo (Alexander) |
35 |
WIN32_STDIN_HANDLE = -10 |
36 |
WIN32_STDOUT_HANDLE = -11 |
|
37 |
WIN32_STDERR_HANDLE = -12 |
|
38 |
||
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
39 |
# CSIDL constants (from MSDN 2003)
|
40 |
CSIDL_APPDATA = 0x001A # Application Data folder |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
41 |
# <user name>\Local Settings\Application Data (non roaming)
|
42 |
CSIDL_LOCAL_APPDATA = 0x001c |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
43 |
CSIDL_PERSONAL = 0x0005 # My Documents folder |
44 |
||
45 |
# from winapi C headers
|
|
46 |
MAX_PATH = 260 |
|
47 |
UNLEN = 256 |
|
48 |
MAX_COMPUTERNAME_LENGTH = 31 |
|
49 |
||
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
50 |
# Registry data type ids
|
51 |
REG_SZ = 1 |
|
52 |
REG_EXPAND_SZ = 2 |
|
53 |
||
1704.2.3
by Martin Pool
(win32) Detect terminal width using GetConsoleScreenBufferInfo (Alexander) |
54 |
|
4011.1.1
by John Arbash Meinel
Implement -Dmemory for win32 |
55 |
def debug_memory_win32api(message='', short=True): |
56 |
"""Use trace.note() to dump the running memory info.""" |
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
57 |
import ctypes |
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
58 |
from breezy import trace |
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
59 |
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure): |
60 |
"""Used by GetProcessMemoryInfo""" |
|
61 |
_fields_ = [('cb', ctypes.c_ulong), |
|
62 |
('PageFaultCount', ctypes.c_ulong), |
|
63 |
('PeakWorkingSetSize', ctypes.c_size_t), |
|
64 |
('WorkingSetSize', ctypes.c_size_t), |
|
65 |
('QuotaPeakPagedPoolUsage', ctypes.c_size_t), |
|
66 |
('QuotaPagedPoolUsage', ctypes.c_size_t), |
|
67 |
('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t), |
|
68 |
('QuotaNonPagedPoolUsage', ctypes.c_size_t), |
|
69 |
('PagefileUsage', ctypes.c_size_t), |
|
70 |
('PeakPagefileUsage', ctypes.c_size_t), |
|
71 |
('PrivateUsage', ctypes.c_size_t), |
|
72 |
]
|
|
73 |
cur_process = ctypes.windll.kernel32.GetCurrentProcess() |
|
74 |
mem_struct = PROCESS_MEMORY_COUNTERS_EX() |
|
75 |
ret = ctypes.windll.psapi.GetProcessMemoryInfo( |
|
76 |
cur_process, ctypes.byref(mem_struct), ctypes.sizeof(mem_struct)) |
|
77 |
if not ret: |
|
78 |
trace.note(gettext('Failed to GetProcessMemoryInfo()')) |
|
4011.1.1
by John Arbash Meinel
Implement -Dmemory for win32 |
79 |
return
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
80 |
info = {'PageFaultCount': mem_struct.PageFaultCount, |
81 |
'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize, |
|
82 |
'WorkingSetSize': mem_struct.WorkingSetSize, |
|
83 |
'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage, |
|
84 |
'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage, |
|
85 |
'QuotaPeakNonPagedPoolUsage': |
|
86 |
mem_struct.QuotaPeakNonPagedPoolUsage, |
|
87 |
'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage, |
|
88 |
'PagefileUsage': mem_struct.PagefileUsage, |
|
89 |
'PeakPagefileUsage': mem_struct.PeakPagefileUsage, |
|
90 |
'PrivateUsage': mem_struct.PrivateUsage, |
|
91 |
}
|
|
4163.1.1
by John Arbash Meinel
Some small fixes for the win32 'trace.debug_mem()' code. |
92 |
if short: |
4989.1.6
by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used. |
93 |
# using base-2 units (see HACKING.txt).
|
6147.1.1
by Jonathan Riddell
use .format() instead of % for string formatting where there are multiple formats in one string to allow for translations |
94 |
trace.note(gettext('WorkingSize {0:>7}KiB' |
7143.15.2
by Jelmer Vernooij
Run autopep8. |
95 |
'\tPeakWorking {1:>7}KiB\t{2}').format( |
4163.1.1
by John Arbash Meinel
Some small fixes for the win32 'trace.debug_mem()' code. |
96 |
info['WorkingSetSize'] / 1024, |
97 |
info['PeakWorkingSetSize'] / 1024, |
|
6147.1.1
by Jonathan Riddell
use .format() instead of % for string formatting where there are multiple formats in one string to allow for translations |
98 |
message)) |
4163.1.1
by John Arbash Meinel
Some small fixes for the win32 'trace.debug_mem()' code. |
99 |
return
|
100 |
if message: |
|
101 |
trace.note('%s', message) |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
102 |
trace.note(gettext('WorkingSize %8d KiB'), |
103 |
info['WorkingSetSize'] / 1024) |
|
104 |
trace.note(gettext('PeakWorking %8d KiB'), |
|
105 |
info['PeakWorkingSetSize'] / 1024) |
|
106 |
trace.note(gettext('PagefileUsage %8d KiB'), |
|
107 |
info.get('PagefileUsage', 0) / 1024) |
|
6138.3.4
by Jonathan Riddell
add gettext() to uses of trace.note() |
108 |
trace.note(gettext('PeakPagefileUsage %8d KiB'), |
4989.1.6
by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used. |
109 |
info.get('PeakPagefileUsage', 0) / 1024) |
7143.15.2
by Jelmer Vernooij
Run autopep8. |
110 |
trace.note(gettext('PrivateUsage %8d KiB'), |
111 |
info.get('PrivateUsage', 0) / 1024) |
|
6138.3.4
by Jonathan Riddell
add gettext() to uses of trace.note() |
112 |
trace.note(gettext('PageFaultCount %8d'), info.get('PageFaultCount', 0)) |
4011.1.1
by John Arbash Meinel
Implement -Dmemory for win32 |
113 |
|
114 |
||
1185.16.86
by mbp at sourcefrog
- win32 get_console_size from Alexander |
115 |
def get_console_size(defaultx=80, defaulty=25): |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
116 |
"""Return size of current console. |
117 |
||
118 |
This function try to determine actual size of current working
|
|
119 |
console window and return tuple (sizex, sizey) if success,
|
|
120 |
or default size (defaultx, defaulty) otherwise.
|
|
121 |
"""
|
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
122 |
import ctypes |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
123 |
# To avoid problem with redirecting output via pipe
|
4747.3.6
by Vincent Ladeuil
terminal_width can now returns None. |
124 |
# we need to use stderr instead of stdout
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
125 |
h = ctypes.windll.kernel32.GetStdHandle(WIN32_STDERR_HANDLE) |
126 |
csbi = ctypes.create_string_buffer(22) |
|
127 |
res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi) |
|
128 |
||
129 |
if res: |
|
130 |
(bufx, bufy, curx, cury, wattr, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
131 |
left, top, right, bottom, maxx, maxy) = struct.unpack( |
4989.1.6
by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used. |
132 |
"hhhhHhhhhhh", csbi.raw) |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
133 |
sizex = right - left + 1 |
134 |
sizey = bottom - top + 1 |
|
135 |
return (sizex, sizey) |
|
136 |
else: |
|
137 |
return (defaultx, defaulty) |
|
138 |
||
139 |
||
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
140 |
def _get_sh_special_folder_path(csidl): |
141 |
"""Call SHGetSpecialFolderPathW if available, or return None. |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
142 |
|
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
143 |
Result is always unicode (or None).
|
144 |
"""
|
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
145 |
import ctypes |
146 |
try: |
|
147 |
SHGetSpecialFolderPath = \ |
|
148 |
ctypes.windll.shell32.SHGetSpecialFolderPathW |
|
149 |
except AttributeError: |
|
150 |
pass
|
|
151 |
else: |
|
152 |
buf = ctypes.create_unicode_buffer(MAX_PATH) |
|
153 |
if SHGetSpecialFolderPath(None, buf, csidl, 0): |
|
154 |
return buf.value |
|
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
155 |
|
156 |
||
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
157 |
def get_appdata_location(): |
158 |
"""Return Application Data location. |
|
159 |
Return None if we cannot obtain location.
|
|
160 |
||
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
161 |
Windows defines two 'Application Data' folders per user - a 'roaming'
|
162 |
one that moves with the user as they logon to different machines, and
|
|
163 |
a 'local' one that stays local to the machine. This returns the 'roaming'
|
|
164 |
directory, and thus is suitable for storing user-preferences, etc.
|
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
165 |
"""
|
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
166 |
appdata = _get_sh_special_folder_path(CSIDL_APPDATA) |
167 |
if appdata: |
|
168 |
return appdata |
|
6362.2.2
by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths |
169 |
# Use APPDATA if defined, will return None if not
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
170 |
return os.environ.get('APPDATA') |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
171 |
|
172 |
||
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
173 |
def get_local_appdata_location(): |
174 |
"""Return Local Application Data location. |
|
175 |
Return the same as get_appdata_location() if we cannot obtain location.
|
|
176 |
||
177 |
Windows defines two 'Application Data' folders per user - a 'roaming'
|
|
178 |
one that moves with the user as they logon to different machines, and
|
|
179 |
a 'local' one that stays local to the machine. This returns the 'local'
|
|
180 |
directory, and thus is suitable for caches, temp files and other things
|
|
181 |
which don't need to move with the user.
|
|
182 |
"""
|
|
183 |
local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA) |
|
184 |
if local: |
|
185 |
return local |
|
186 |
# Vista supplies LOCALAPPDATA, but XP and earlier do not.
|
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
187 |
local = os.environ.get('LOCALAPPDATA') |
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
188 |
if local: |
189 |
return local |
|
190 |
return get_appdata_location() |
|
191 |
||
192 |
||
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
193 |
def get_home_location(): |
194 |
"""Return user's home location. |
|
195 |
Assume on win32 it's the <My Documents> folder.
|
|
196 |
If location cannot be obtained return system drive root,
|
|
197 |
i.e. C:\
|
|
198 |
"""
|
|
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
199 |
home = _get_sh_special_folder_path(CSIDL_PERSONAL) |
200 |
if home: |
|
201 |
return home |
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
202 |
home = os.environ.get('HOME') |
6362.2.2
by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths |
203 |
if home is not None: |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
204 |
return home |
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
205 |
homepath = os.environ.get('HOMEPATH') |
6362.2.2
by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths |
206 |
if homepath is not None: |
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
207 |
return os.path.join(os.environ.get('HOMEDIR', ''), home) |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
208 |
# at least return windows root directory
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
209 |
windir = os.environ.get('WINDIR') |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
210 |
if windir: |
2610.1.1
by Martin Pool
Fix get_home_location on Win98 (gzlist,r=john,r=alexander) |
211 |
return os.path.splitdrive(windir)[0] + '/' |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
212 |
# otherwise C:\ is good enough for 98% users
|
6973.6.4
by Jelmer Vernooij
Avoid text_type() |
213 |
return u'C:/' |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
214 |
|
215 |
||
216 |
def get_user_name(): |
|
217 |
"""Return user name as login name. |
|
218 |
If name cannot be obtained return None.
|
|
219 |
"""
|
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
220 |
try: |
221 |
advapi32 = ctypes.windll.advapi32 |
|
222 |
GetUserName = getattr(advapi32, 'GetUserNameW') |
|
223 |
except AttributeError: |
|
224 |
pass
|
|
225 |
else: |
|
226 |
buf = ctypes.create_unicode_buffer(UNLEN + 1) |
|
227 |
n = ctypes.c_int(UNLEN + 1) |
|
228 |
if GetUserName(buf, ctypes.byref(n)): |
|
229 |
return buf.value |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
230 |
# otherwise try env variables
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
231 |
return os.environ.get('USERNAME') |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
232 |
|
233 |
||
3626.1.3
by John Arbash Meinel
Use GetComputerNameEx from ctypes when available. |
234 |
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
|
235 |
# computer or the cluster associated with the local computer."
|
|
236 |
_WIN32_ComputerNameDnsHostname = 1 |
|
237 |
||
7143.15.2
by Jelmer Vernooij
Run autopep8. |
238 |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
239 |
def get_host_name(): |
240 |
"""Return host machine name. |
|
241 |
If name cannot be obtained return None.
|
|
242 |
||
6362.2.3
by Martin Packman
Always return username and hostname as unicode in win32utils |
243 |
:return: A unicode string representing the host name.
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
244 |
"""
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
245 |
import ctypes |
246 |
buf = ctypes.create_unicode_buffer(MAX_COMPUTERNAME_LENGTH + 1) |
|
247 |
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH + 1) |
|
3626.1.3
by John Arbash Meinel
Use GetComputerNameEx from ctypes when available. |
248 |
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
249 |
# Try GetComputerNameEx which gives a proper Unicode hostname
|
250 |
GetComputerNameEx = getattr( |
|
251 |
ctypes.windll.kernel32, 'GetComputerNameExW', None) |
|
252 |
if (GetComputerNameEx is not None |
|
253 |
and GetComputerNameEx(_WIN32_ComputerNameDnsHostname, |
|
254 |
buf, ctypes.byref(n))): |
|
255 |
return buf.value |
|
256 |
return os.environ.get('COMPUTERNAME') |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
257 |
|
258 |
||
2617.5.8
by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range |
259 |
def _ensure_with_dir(path): |
4989.1.6
by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used. |
260 |
if (not os.path.split(path)[0] or path.startswith(u'*') |
7143.15.2
by Jelmer Vernooij
Run autopep8. |
261 |
or path.startswith(u'?')): |
2617.5.8
by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range |
262 |
return u'./' + path, True |
263 |
else: |
|
264 |
return path, False |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
265 |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
266 |
|
2617.5.8
by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range |
267 |
def _undo_ensure_with_dir(path, corrected): |
268 |
if corrected: |
|
269 |
return path[2:] |
|
270 |
else: |
|
271 |
return path |
|
272 |
||
273 |
||
4786.1.2
by John Arbash Meinel
Refactor the glob_expand code a bit, making the inner function more reusable. |
274 |
def glob_one(possible_glob): |
275 |
"""Same as glob.glob(). |
|
276 |
||
277 |
work around bugs in glob.glob()
|
|
278 |
- Python bug #1001604 ("glob doesn't return unicode with ...")
|
|
279 |
- failing expansion for */* with non-iso-8859-* chars
|
|
280 |
"""
|
|
281 |
corrected_glob, corrected = _ensure_with_dir(possible_glob) |
|
282 |
glob_files = glob.glob(corrected_glob) |
|
283 |
||
284 |
if not glob_files: |
|
285 |
# special case to let the normal code path handle
|
|
286 |
# files that do not exist, etc.
|
|
287 |
glob_files = [possible_glob] |
|
288 |
elif corrected: |
|
289 |
glob_files = [_undo_ensure_with_dir(elem, corrected) |
|
290 |
for elem in glob_files] |
|
291 |
return [elem.replace(u'\\', u'/') for elem in glob_files] |
|
292 |
||
293 |
||
2598.3.1
by Kuno Meyer
fix method rename glob_expand_for_win32 -> win32utils.glob_expand |
294 |
def glob_expand(file_list): |
2568.2.2
by Robert Collins
* New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class |
295 |
"""Replacement for glob expansion by the shell. |
296 |
||
297 |
Win32's cmd.exe does not do glob expansion (eg ``*.py``), so we do our own
|
|
298 |
here.
|
|
299 |
||
300 |
:param file_list: A list of filenames which may include shell globs.
|
|
301 |
:return: An expanded list of filenames.
|
|
302 |
||
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
303 |
Introduced in breezy 0.18.
|
2568.2.2
by Robert Collins
* New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class |
304 |
"""
|
305 |
if not file_list: |
|
306 |
return [] |
|
307 |
expanded_file_list = [] |
|
308 |
for possible_glob in file_list: |
|
4786.1.2
by John Arbash Meinel
Refactor the glob_expand code a bit, making the inner function more reusable. |
309 |
expanded_file_list.extend(glob_one(possible_glob)) |
310 |
return expanded_file_list |
|
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
311 |
|
312 |
||
313 |
def get_app_path(appname): |
|
5891.1.2
by Andrew Bennetts
Fix a bunch of docstring formatting nits, making pydoctor a bit happier. |
314 |
r"""Look up in Windows registry for full path to application executable. |
4031.3.1
by Frank Aspell
Fixing various typos |
315 |
Typically, applications create subkey with their basename
|
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
316 |
in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
|
317 |
||
318 |
:param appname: name of application (if no filename extension
|
|
319 |
is specified, .exe used)
|
|
320 |
:return: full path to aplication executable from registry,
|
|
321 |
or appname itself if nothing found.
|
|
322 |
"""
|
|
2681.4.3
by Alexander Belchenko
move import _winreg into function get_app_path to avoid ImportError on non-win32 platforms |
323 |
import _winreg |
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
324 |
|
325 |
basename = appname |
|
326 |
if not os.path.splitext(basename)[1]: |
|
327 |
basename = appname + '.exe' |
|
328 |
||
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
329 |
try: |
330 |
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
331 |
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' + |
332 |
basename) |
|
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
333 |
except EnvironmentError: |
334 |
return appname |
|
335 |
||
336 |
try: |
|
337 |
try: |
|
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
338 |
path, type_id = _winreg.QueryValueEx(hkey, '') |
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
339 |
except WindowsError: |
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
340 |
return appname |
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
341 |
finally: |
342 |
_winreg.CloseKey(hkey) |
|
343 |
||
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
344 |
if type_id == REG_SZ: |
345 |
return path |
|
346 |
return appname |
|
3023.1.2
by Alexander Belchenko
Martin's review. |
347 |
|
348 |
||
349 |
def set_file_attr_hidden(path): |
|
350 |
"""Set file attributes to hidden if possible""" |
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
351 |
from ctypes.wintypes import BOOL, DWORD, LPWSTR |
352 |
import ctypes |
|
7341.1.1
by Martin
Remove win32 code using pywin32 library |
353 |
# <https://docs.microsoft.com/windows/desktop/api/fileapi/nf-fileapi-setfileattributesw>
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
354 |
SetFileAttributes = ctypes.windll.kernel32.SetFileAttributesW |
355 |
SetFileAttributes.argtypes = LPWSTR, DWORD |
|
356 |
SetFileAttributes.restype = BOOL |
|
7341.1.1
by Martin
Remove win32 code using pywin32 library |
357 |
FILE_ATTRIBUTE_HIDDEN = 2 |
358 |
if not SetFileAttributes(path, FILE_ATTRIBUTE_HIDDEN): |
|
359 |
e = ctypes.WinError() |
|
360 |
from . import trace |
|
361 |
trace.mutter('Unable to set hidden attribute on %r: %s', path, e) |
|
4355.2.1
by Alexander Belchenko
Using unicode Windows API to obtain command-line arguments. |
362 |
|
363 |
||
5274.4.12
by Martin
Change interface of _command_line_to_argv so old tests can still be used with new stripping logic |
364 |
def _command_line_to_argv(command_line, argv, single_quotes_allowed=False): |
4913.5.2
by Gordon Tyler
Changed shlex_split_unicode to prevent wildcard expansion in the win32 codepath. |
365 |
"""Convert a Unicode command line into a list of argv arguments. |
366 |
||
4913.5.25
by Gordon Tyler
Simplified win32utils.command_line_to_argv and made it private since it's no longer used outside of the module. |
367 |
It performs wildcard expansion to make wildcards act closer to how they
|
368 |
work in posix shells, versus how they work by default on Windows. Quoted
|
|
369 |
arguments are left untouched.
|
|
4913.5.2
by Gordon Tyler
Changed shlex_split_unicode to prevent wildcard expansion in the win32 codepath. |
370 |
|
371 |
:param command_line: The unicode string to split into an arg list.
|
|
4913.5.11
by Gordon Tyler
Added optional single quote support to UnicodeShlex and thus command_line_to_argv (defaults to disabled). |
372 |
:param single_quotes_allowed: Whether single quotes are accepted as quoting
|
373 |
characters like double quotes. False by
|
|
374 |
default.
|
|
4913.5.2
by Gordon Tyler
Changed shlex_split_unicode to prevent wildcard expansion in the win32 codepath. |
375 |
:return: A list of unicode strings.
|
4786.1.1
by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows. |
376 |
"""
|
6437.65.1
by Scanferlato
Fix two typos in docs and comment |
377 |
# First, split the command line
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
378 |
s = cmdline.Splitter( |
379 |
command_line, single_quotes_allowed=single_quotes_allowed) |
|
380 |
||
381 |
# Bug #587868 Now make sure that the length of s agrees with sys.argv
|
|
382 |
# we do this by simply counting the number of arguments in each. The counts should
|
|
383 |
# agree no matter what encoding sys.argv is in (AFAIK)
|
|
384 |
# len(arguments) < len(sys.argv) should be an impossibility since python gets
|
|
5274.4.8
by Jason Spashett
Reverse merge -r 5280..5279 |
385 |
# args from the very same PEB as does GetCommandLineW
|
386 |
arguments = list(s) |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
387 |
|
5274.4.8
by Jason Spashett
Reverse merge -r 5280..5279 |
388 |
# Now shorten the command line we get from GetCommandLineW to match sys.argv
|
5274.4.12
by Martin
Change interface of _command_line_to_argv so old tests can still be used with new stripping logic |
389 |
if len(arguments) < len(argv): |
5274.4.11
by Martin
Minor cleanup, use plain AssertionError and revert needless variable rename |
390 |
raise AssertionError("Split command line can't be shorter than argv") |
5274.4.12
by Martin
Change interface of _command_line_to_argv so old tests can still be used with new stripping logic |
391 |
arguments = arguments[len(arguments) - len(argv):] |
7143.15.2
by Jelmer Vernooij
Run autopep8. |
392 |
|
5274.4.2
by Jason Spashett
Tidied up fix for 587868. Put assert in for impossible case in command line lengths. |
393 |
# Carry on to process globs (metachars) in the command line
|
5274.4.1
by Jason Spashett
Initial Fix for 587868 |
394 |
# expand globs if necessary
|
4786.1.1
by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows. |
395 |
# TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
|
396 |
# '**/' style globs
|
|
5274.4.11
by Martin
Minor cleanup, use plain AssertionError and revert needless variable rename |
397 |
args = [] |
5274.4.8
by Jason Spashett
Reverse merge -r 5280..5279 |
398 |
for is_quoted, arg in arguments: |
4913.5.25
by Gordon Tyler
Simplified win32utils.command_line_to_argv and made it private since it's no longer used outside of the module. |
399 |
if is_quoted or not glob.has_magic(arg): |
5274.4.11
by Martin
Minor cleanup, use plain AssertionError and revert needless variable rename |
400 |
args.append(arg) |
4786.1.1
by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows. |
401 |
else: |
5274.4.11
by Martin
Minor cleanup, use plain AssertionError and revert needless variable rename |
402 |
args.extend(glob_one(arg)) |
403 |
return args |
|
5274.4.3
by Jason Spashett
Merge from lp:bzr. Remove code for fix 588277 (revs 5274.3.1 - 5274.3.2) as this bug also fixes that issue. Make changes as per code review. |
404 |
|
4786.1.1
by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows. |
405 |
|
7502.3.1
by Jelmer Vernooij
Cleanup Windows functions. |
406 |
def _ctypes_is_local_pid_dead(pid): |
407 |
import ctypes |
|
408 |
kernel32 = ctypes.wintypes.windll.kernel32 |
|
409 |
"""True if pid doesn't correspond to live process on this machine""" |
|
410 |
handle = kernel32.OpenProcess(1, False, pid) |
|
411 |
if not handle: |
|
412 |
errorcode = ctypes.GetLastError() |
|
413 |
if errorcode == 5: # ERROR_ACCESS_DENIED |
|
414 |
# Probably something alive we're not allowed to kill
|
|
415 |
return False |
|
416 |
elif errorcode == 87: # ERROR_INVALID_PARAMETER |
|
417 |
return True |
|
418 |
raise ctypes.WinError(errorcode) |
|
419 |
Kernel32.CloseHandle(handle) |
|
420 |
return False |
|
421 |
||
422 |
is_local_pid_dead = _ctypes_is_local_pid_dead |