1
# Copyright (C) 2005-2010 Canonical Ltd
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.
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.
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Win32-specific helper functions
19
Only one dependency: ctypes should be installed.
22
from __future__ import absolute_import
33
from breezy.i18n import gettext
35
# We can cope without it; use a separate variable to help pyflakes
42
create_buffer = ctypes.create_unicode_buffer
43
extract_buffer = operator.attrgetter("value")
49
has_pywintypes = has_win32file = has_win32api = False
62
# pulling in win32com.shell is a bit of overhead, and normally we don't need
63
# it as ctypes is preferred and common. lazy_imports and "optional"
64
# modules don't work well, so we do our own lazy thing...
65
has_win32com_shell = None # Set to True or False once we know for sure...
67
# Special Win32 API constants
68
# Handles of std streams
69
WIN32_STDIN_HANDLE = -10
70
WIN32_STDOUT_HANDLE = -11
71
WIN32_STDERR_HANDLE = -12
73
# CSIDL constants (from MSDN 2003)
74
CSIDL_APPDATA = 0x001A # Application Data folder
75
CSIDL_LOCAL_APPDATA = 0x001c# <user name>\Local Settings\Application Data (non roaming)
76
CSIDL_PERSONAL = 0x0005 # My Documents folder
78
# from winapi C headers
81
MAX_COMPUTERNAME_LENGTH = 31
83
# Registry data type ids
88
def debug_memory_win32api(message='', short=True):
89
"""Use trace.note() to dump the running memory info."""
90
from breezy import trace
92
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
93
"""Used by GetProcessMemoryInfo"""
94
_fields_ = [('cb', ctypes.c_ulong),
95
('PageFaultCount', ctypes.c_ulong),
96
('PeakWorkingSetSize', ctypes.c_size_t),
97
('WorkingSetSize', ctypes.c_size_t),
98
('QuotaPeakPagedPoolUsage', ctypes.c_size_t),
99
('QuotaPagedPoolUsage', ctypes.c_size_t),
100
('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t),
101
('QuotaNonPagedPoolUsage', ctypes.c_size_t),
102
('PagefileUsage', ctypes.c_size_t),
103
('PeakPagefileUsage', ctypes.c_size_t),
104
('PrivateUsage', ctypes.c_size_t),
106
cur_process = ctypes.windll.kernel32.GetCurrentProcess()
107
mem_struct = PROCESS_MEMORY_COUNTERS_EX()
108
ret = ctypes.windll.psapi.GetProcessMemoryInfo(cur_process,
109
ctypes.byref(mem_struct),
110
ctypes.sizeof(mem_struct))
112
trace.note(gettext('Failed to GetProcessMemoryInfo()'))
114
info = {'PageFaultCount': mem_struct.PageFaultCount,
115
'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize,
116
'WorkingSetSize': mem_struct.WorkingSetSize,
117
'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage,
118
'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage,
119
'QuotaPeakNonPagedPoolUsage':
120
mem_struct.QuotaPeakNonPagedPoolUsage,
121
'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage,
122
'PagefileUsage': mem_struct.PagefileUsage,
123
'PeakPagefileUsage': mem_struct.PeakPagefileUsage,
124
'PrivateUsage': mem_struct.PrivateUsage,
128
# win32process does not return PrivateUsage, because it doesn't use
129
# PROCESS_MEMORY_COUNTERS_EX (it uses the one without _EX).
130
proc = win32process.GetCurrentProcess()
131
info = win32process.GetProcessMemoryInfo(proc)
133
trace.note(gettext('Cannot debug memory on win32 without ctypes'
137
# using base-2 units (see HACKING.txt).
138
trace.note(gettext('WorkingSize {0:>7}KiB'
139
'\tPeakWorking {1:>7}KiB\t{2}').format(
140
info['WorkingSetSize'] / 1024,
141
info['PeakWorkingSetSize'] / 1024,
145
trace.note('%s', message)
146
trace.note(gettext('WorkingSize %8d KiB'), info['WorkingSetSize'] / 1024)
147
trace.note(gettext('PeakWorking %8d KiB'), info['PeakWorkingSetSize'] / 1024)
148
trace.note(gettext('PagefileUsage %8d KiB'), info.get('PagefileUsage', 0) / 1024)
149
trace.note(gettext('PeakPagefileUsage %8d KiB'),
150
info.get('PeakPagefileUsage', 0) / 1024)
151
trace.note(gettext('PrivateUsage %8d KiB'), info.get('PrivateUsage', 0) / 1024)
152
trace.note(gettext('PageFaultCount %8d'), info.get('PageFaultCount', 0))
155
def get_console_size(defaultx=80, defaulty=25):
156
"""Return size of current console.
158
This function try to determine actual size of current working
159
console window and return tuple (sizex, sizey) if success,
160
or default size (defaultx, defaulty) otherwise.
164
return (defaultx, defaulty)
166
# To avoid problem with redirecting output via pipe
167
# we need to use stderr instead of stdout
168
h = ctypes.windll.kernel32.GetStdHandle(WIN32_STDERR_HANDLE)
169
csbi = ctypes.create_string_buffer(22)
170
res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
173
(bufx, bufy, curx, cury, wattr,
174
left, top, right, bottom, maxx, maxy) = struct.unpack(
175
"hhhhHhhhhhh", csbi.raw)
176
sizex = right - left + 1
177
sizey = bottom - top + 1
178
return (sizex, sizey)
180
return (defaultx, defaulty)
183
def _get_sh_special_folder_path(csidl):
184
"""Call SHGetSpecialFolderPathW if available, or return None.
186
Result is always unicode (or None).
190
SHGetSpecialFolderPath = \
191
ctypes.windll.shell32.SHGetSpecialFolderPathW
192
except AttributeError:
195
buf = ctypes.create_unicode_buffer(MAX_PATH)
196
if SHGetSpecialFolderPath(None,buf,csidl,0):
199
global has_win32com_shell
200
if has_win32com_shell is None:
202
from win32com.shell import shell
203
has_win32com_shell = True
205
has_win32com_shell = False
206
if has_win32com_shell:
207
# still need to bind the name locally, but this is fast.
208
from win32com.shell import shell
210
return shell.SHGetSpecialFolderPath(0, csidl, 0)
212
# possibly E_NOTIMPL meaning we can't load the function pointer,
213
# or E_FAIL meaning the function failed - regardless, just ignore it
218
def get_appdata_location():
219
"""Return Application Data location.
220
Return None if we cannot obtain location.
222
Windows defines two 'Application Data' folders per user - a 'roaming'
223
one that moves with the user as they logon to different machines, and
224
a 'local' one that stays local to the machine. This returns the 'roaming'
225
directory, and thus is suitable for storing user-preferences, etc.
227
appdata = _get_sh_special_folder_path(CSIDL_APPDATA)
230
# Use APPDATA if defined, will return None if not
231
return get_environ_unicode('APPDATA')
234
def get_local_appdata_location():
235
"""Return Local Application Data location.
236
Return the same as get_appdata_location() if we cannot obtain location.
238
Windows defines two 'Application Data' folders per user - a 'roaming'
239
one that moves with the user as they logon to different machines, and
240
a 'local' one that stays local to the machine. This returns the 'local'
241
directory, and thus is suitable for caches, temp files and other things
242
which don't need to move with the user.
244
local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)
247
# Vista supplies LOCALAPPDATA, but XP and earlier do not.
248
local = get_environ_unicode('LOCALAPPDATA')
251
return get_appdata_location()
254
def get_home_location():
255
"""Return user's home location.
256
Assume on win32 it's the <My Documents> folder.
257
If location cannot be obtained return system drive root,
260
home = _get_sh_special_folder_path(CSIDL_PERSONAL)
263
home = get_environ_unicode('HOME')
266
homepath = get_environ_unicode('HOMEPATH')
267
if homepath is not None:
268
return os.path.join(get_environ_unicode('HOMEDIR', ''), home)
269
# at least return windows root directory
270
windir = get_environ_unicode('WINDIR')
272
return os.path.splitdrive(windir)[0] + '/'
273
# otherwise C:\ is good enough for 98% users
274
return unicode('C:/')
278
"""Return user name as login name.
279
If name cannot be obtained return None.
283
advapi32 = ctypes.windll.advapi32
284
GetUserName = getattr(advapi32, 'GetUserName'+suffix)
285
except AttributeError:
288
buf = create_buffer(UNLEN+1)
289
n = ctypes.c_int(UNLEN+1)
290
if GetUserName(buf, ctypes.byref(n)):
291
return extract_buffer(buf)
292
# otherwise try env variables
293
return get_environ_unicode('USERNAME')
296
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
297
# computer or the cluster associated with the local computer."
298
_WIN32_ComputerNameDnsHostname = 1
301
"""Return host machine name.
302
If name cannot be obtained return None.
304
:return: A unicode string representing the host name.
308
return win32api.GetComputerNameEx(_WIN32_ComputerNameDnsHostname)
309
except (NotImplementedError, win32api.error):
310
# NotImplemented will happen on win9x...
314
kernel32 = ctypes.windll.kernel32
315
except AttributeError:
316
pass # Missing the module we need
318
buf = create_buffer(MAX_COMPUTERNAME_LENGTH+1)
319
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
321
# Try GetComputerNameEx which gives a proper Unicode hostname
322
GetComputerNameEx = getattr(kernel32, 'GetComputerNameEx'+suffix,
324
if (GetComputerNameEx is not None
325
and GetComputerNameEx(_WIN32_ComputerNameDnsHostname,
326
buf, ctypes.byref(n))):
327
return extract_buffer(buf)
329
# Try GetComputerName in case GetComputerNameEx wasn't found
330
# It returns the NETBIOS name, which isn't as good, but still ok.
331
# The first GetComputerNameEx might have changed 'n', so reset it
332
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
333
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix,
335
if (GetComputerName is not None
336
and GetComputerName(buf, ctypes.byref(n))):
337
return extract_buffer(buf)
338
return get_environ_unicode('COMPUTERNAME')
341
def _ensure_with_dir(path):
342
if (not os.path.split(path)[0] or path.startswith(u'*')
343
or path.startswith(u'?')):
344
return u'./' + path, True
348
def _undo_ensure_with_dir(path, corrected):
355
def glob_one(possible_glob):
356
"""Same as glob.glob().
358
work around bugs in glob.glob()
359
- Python bug #1001604 ("glob doesn't return unicode with ...")
360
- failing expansion for */* with non-iso-8859-* chars
362
corrected_glob, corrected = _ensure_with_dir(possible_glob)
363
glob_files = glob.glob(corrected_glob)
366
# special case to let the normal code path handle
367
# files that do not exist, etc.
368
glob_files = [possible_glob]
370
glob_files = [_undo_ensure_with_dir(elem, corrected)
371
for elem in glob_files]
372
return [elem.replace(u'\\', u'/') for elem in glob_files]
375
def glob_expand(file_list):
376
"""Replacement for glob expansion by the shell.
378
Win32's cmd.exe does not do glob expansion (eg ``*.py``), so we do our own
381
:param file_list: A list of filenames which may include shell globs.
382
:return: An expanded list of filenames.
384
Introduced in breezy 0.18.
388
expanded_file_list = []
389
for possible_glob in file_list:
390
expanded_file_list.extend(glob_one(possible_glob))
391
return expanded_file_list
394
def get_app_path(appname):
395
r"""Look up in Windows registry for full path to application executable.
396
Typically, applications create subkey with their basename
397
in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
399
:param appname: name of application (if no filename extension
400
is specified, .exe used)
401
:return: full path to aplication executable from registry,
402
or appname itself if nothing found.
407
if not os.path.splitext(basename)[1]:
408
basename = appname + '.exe'
411
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
412
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' +
414
except EnvironmentError:
419
path, type_id = _winreg.QueryValueEx(hkey, '')
423
_winreg.CloseKey(hkey)
425
if type_id == REG_SZ:
427
if type_id == REG_EXPAND_SZ and has_win32api:
428
fullpath = win32api.ExpandEnvironmentStrings(path)
429
if len(fullpath) > 1 and fullpath[0] == '"' and fullpath[-1] == '"':
430
fullpath = fullpath[1:-1] # remove quotes around value
435
def set_file_attr_hidden(path):
436
"""Set file attributes to hidden if possible"""
438
SetFileAttributes = win32file.SetFileAttributesW
440
SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)
441
except pywintypes.error as e:
443
trace.mutter('Unable to set hidden attribute on %r: %s', path, e)
446
def _command_line_to_argv(command_line, argv, single_quotes_allowed=False):
447
"""Convert a Unicode command line into a list of argv arguments.
449
It performs wildcard expansion to make wildcards act closer to how they
450
work in posix shells, versus how they work by default on Windows. Quoted
451
arguments are left untouched.
453
:param command_line: The unicode string to split into an arg list.
454
:param single_quotes_allowed: Whether single quotes are accepted as quoting
455
characters like double quotes. False by
457
:return: A list of unicode strings.
459
# First, split the command line
460
s = cmdline.Splitter(command_line, single_quotes_allowed=single_quotes_allowed)
462
# Bug #587868 Now make sure that the length of s agrees with sys.argv
463
# we do this by simply counting the number of arguments in each. The counts should
464
# agree no matter what encoding sys.argv is in (AFAIK)
465
# len(arguments) < len(sys.argv) should be an impossibility since python gets
466
# args from the very same PEB as does GetCommandLineW
469
# Now shorten the command line we get from GetCommandLineW to match sys.argv
470
if len(arguments) < len(argv):
471
raise AssertionError("Split command line can't be shorter than argv")
472
arguments = arguments[len(arguments) - len(argv):]
474
# Carry on to process globs (metachars) in the command line
475
# expand globs if necessary
476
# TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
479
for is_quoted, arg in arguments:
480
if is_quoted or not glob.has_magic(arg):
483
args.extend(glob_one(arg))
488
def get_unicode_argv():
489
prototype = ctypes.WINFUNCTYPE(ctypes.c_wchar_p)
490
GetCommandLineW = prototype(("GetCommandLineW",
491
ctypes.windll.kernel32))
492
command_line = GetCommandLineW()
493
if command_line is None:
494
raise ctypes.WinError()
495
# Skip the first argument, since we only care about parameters
496
argv = _command_line_to_argv(command_line, sys.argv)[1:]
500
def get_environ_unicode(key, default=None):
501
"""Get `key` from environment as unicode or `default` if unset
503
The environment is natively unicode on modern windows versions but
504
Python 2 only accesses it through the legacy bytestring api.
506
Environmental variable names are case insenstive on Windows.
508
A large enough buffer will be allocated to retrieve the value, though
509
it may take two calls to the underlying library function.
511
This needs ctypes because pywin32 does not expose the wide version.
513
cfunc = getattr(get_environ_unicode, "_c_function", None)
515
from ctypes.wintypes import DWORD, LPCWSTR, LPWSTR
516
cfunc = ctypes.WINFUNCTYPE(DWORD, LPCWSTR, LPWSTR, DWORD)(
517
("GetEnvironmentVariableW", ctypes.windll.kernel32))
518
get_environ_unicode._c_function = cfunc
519
buffer_size = 256 # heuristic, 256 characters often enough
521
buffer = ctypes.create_unicode_buffer(buffer_size)
522
length = cfunc(key, buffer, buffer_size)
524
code = ctypes.GetLastError()
525
if code == 203: # ERROR_ENVVAR_NOT_FOUND
527
raise ctypes.WinError(code)
528
if buffer_size > length:
529
return buffer[:length]
534
def _pywin32_is_local_pid_dead(pid):
535
"""True if pid doesn't correspond to live process on this machine"""
537
handle = win32api.OpenProcess(1, False, pid) # PROCESS_TERMINATE
538
except pywintypes.error as e:
539
if e[0] == 5: # ERROR_ACCESS_DENIED
540
# Probably something alive we're not allowed to kill
542
elif e[0] == 87: # ERROR_INVALID_PARAMETER
547
is_local_pid_dead = _pywin32_is_local_pid_dead
548
elif has_ctypes and sys.platform == 'win32':
549
from ctypes.wintypes import BOOL, DWORD, HANDLE
550
_kernel32 = ctypes.windll.kernel32
551
_CloseHandle = ctypes.WINFUNCTYPE(BOOL, HANDLE)(
552
("CloseHandle", _kernel32))
553
_OpenProcess = ctypes.WINFUNCTYPE(HANDLE, DWORD, BOOL, DWORD)(
554
("OpenProcess", _kernel32))
555
def _ctypes_is_local_pid_dead(pid):
556
"""True if pid doesn't correspond to live process on this machine"""
557
handle = _OpenProcess(1, False, pid) # PROCESS_TERMINATE
559
errorcode = ctypes.GetLastError()
560
if errorcode == 5: # ERROR_ACCESS_DENIED
561
# Probably something alive we're not allowed to kill
563
elif errorcode == 87: # ERROR_INVALID_PARAMETER
565
raise ctypes.WinError(errorcode)
568
is_local_pid_dead = _ctypes_is_local_pid_dead
571
def _is_pywintypes_error(evalue):
572
"""True if exception instance is an error from pywin32"""
573
if has_pywintypes and isinstance(evalue, pywintypes.error):