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.
32
from bzrlib.i18n import gettext
35
if sys.platform == 'win32':
36
_major,_minor,_build,_platform,_text = sys.getwindowsversion()
39
# The operating system platform.
40
# This member can be one of the following values.
41
# ========================== ======================================
43
# -------------------------- --------------------------------------
44
# VER_PLATFORM_WIN32_NT The operating system is Windows Vista,
45
# 2 Windows Server "Longhorn",
46
# Windows Server 2003, Windows XP,
47
# Windows 2000, or Windows NT.
49
# VER_PLATFORM_WIN32_WINDOWS The operating system is Windows Me,
50
# 1 Windows 98, or Windows 95.
51
# ========================== ======================================
55
# don't care about real Windows name, just to force safe operations
61
# We can cope without it; use a separate variable to help pyflakes
68
if winver == 'Windows 98':
69
create_buffer = ctypes.create_string_buffer
70
def extract_buffer(buf):
71
return buf.value.decode("mbcs")
74
create_buffer = ctypes.create_unicode_buffer
75
extract_buffer = operator.attrgetter("value")
81
has_pywintypes = has_win32file = has_win32api = False
94
# pulling in win32com.shell is a bit of overhead, and normally we don't need
95
# it as ctypes is preferred and common. lazy_imports and "optional"
96
# modules don't work well, so we do our own lazy thing...
97
has_win32com_shell = None # Set to True or False once we know for sure...
99
# Special Win32 API constants
100
# Handles of std streams
101
WIN32_STDIN_HANDLE = -10
102
WIN32_STDOUT_HANDLE = -11
103
WIN32_STDERR_HANDLE = -12
105
# CSIDL constants (from MSDN 2003)
106
CSIDL_APPDATA = 0x001A # Application Data folder
107
CSIDL_LOCAL_APPDATA = 0x001c# <user name>\Local Settings\Application Data (non roaming)
108
CSIDL_PERSONAL = 0x0005 # My Documents folder
110
# from winapi C headers
113
MAX_COMPUTERNAME_LENGTH = 31
115
# Registry data type ids
120
def debug_memory_win32api(message='', short=True):
121
"""Use trace.note() to dump the running memory info."""
122
from bzrlib import trace
124
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
125
"""Used by GetProcessMemoryInfo"""
126
_fields_ = [('cb', ctypes.c_ulong),
127
('PageFaultCount', ctypes.c_ulong),
128
('PeakWorkingSetSize', ctypes.c_size_t),
129
('WorkingSetSize', ctypes.c_size_t),
130
('QuotaPeakPagedPoolUsage', ctypes.c_size_t),
131
('QuotaPagedPoolUsage', ctypes.c_size_t),
132
('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t),
133
('QuotaNonPagedPoolUsage', ctypes.c_size_t),
134
('PagefileUsage', ctypes.c_size_t),
135
('PeakPagefileUsage', ctypes.c_size_t),
136
('PrivateUsage', ctypes.c_size_t),
138
cur_process = ctypes.windll.kernel32.GetCurrentProcess()
139
mem_struct = PROCESS_MEMORY_COUNTERS_EX()
140
ret = ctypes.windll.psapi.GetProcessMemoryInfo(cur_process,
141
ctypes.byref(mem_struct),
142
ctypes.sizeof(mem_struct))
144
trace.note(gettext('Failed to GetProcessMemoryInfo()'))
146
info = {'PageFaultCount': mem_struct.PageFaultCount,
147
'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize,
148
'WorkingSetSize': mem_struct.WorkingSetSize,
149
'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage,
150
'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage,
151
'QuotaPeakNonPagedPoolUsage':
152
mem_struct.QuotaPeakNonPagedPoolUsage,
153
'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage,
154
'PagefileUsage': mem_struct.PagefileUsage,
155
'PeakPagefileUsage': mem_struct.PeakPagefileUsage,
156
'PrivateUsage': mem_struct.PrivateUsage,
160
# win32process does not return PrivateUsage, because it doesn't use
161
# PROCESS_MEMORY_COUNTERS_EX (it uses the one without _EX).
162
proc = win32process.GetCurrentProcess()
163
info = win32process.GetProcessMemoryInfo(proc)
165
trace.note(gettext('Cannot debug memory on win32 without ctypes'
169
# using base-2 units (see HACKING.txt).
170
trace.note(gettext('WorkingSize {0:>7}KiB'
171
'\tPeakWorking {1:>7}KiB\t{2}').format(
172
info['WorkingSetSize'] / 1024,
173
info['PeakWorkingSetSize'] / 1024,
177
trace.note('%s', message)
178
trace.note(gettext('WorkingSize %8d KiB'), info['WorkingSetSize'] / 1024)
179
trace.note(gettext('PeakWorking %8d KiB'), info['PeakWorkingSetSize'] / 1024)
180
trace.note(gettext('PagefileUsage %8d KiB'), info.get('PagefileUsage', 0) / 1024)
181
trace.note(gettext('PeakPagefileUsage %8d KiB'),
182
info.get('PeakPagefileUsage', 0) / 1024)
183
trace.note(gettext('PrivateUsage %8d KiB'), info.get('PrivateUsage', 0) / 1024)
184
trace.note(gettext('PageFaultCount %8d'), info.get('PageFaultCount', 0))
187
def get_console_size(defaultx=80, defaulty=25):
188
"""Return size of current console.
190
This function try to determine actual size of current working
191
console window and return tuple (sizex, sizey) if success,
192
or default size (defaultx, defaulty) otherwise.
196
return (defaultx, defaulty)
198
# To avoid problem with redirecting output via pipe
199
# we need to use stderr instead of stdout
200
h = ctypes.windll.kernel32.GetStdHandle(WIN32_STDERR_HANDLE)
201
csbi = ctypes.create_string_buffer(22)
202
res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
205
(bufx, bufy, curx, cury, wattr,
206
left, top, right, bottom, maxx, maxy) = struct.unpack(
207
"hhhhHhhhhhh", csbi.raw)
208
sizex = right - left + 1
209
sizey = bottom - top + 1
210
return (sizex, sizey)
212
return (defaultx, defaulty)
215
def _get_sh_special_folder_path(csidl):
216
"""Call SHGetSpecialFolderPathW if available, or return None.
218
Result is always unicode (or None).
222
SHGetSpecialFolderPath = \
223
ctypes.windll.shell32.SHGetSpecialFolderPathW
224
except AttributeError:
227
buf = ctypes.create_unicode_buffer(MAX_PATH)
228
if SHGetSpecialFolderPath(None,buf,csidl,0):
231
global has_win32com_shell
232
if has_win32com_shell is None:
234
from win32com.shell import shell
235
has_win32com_shell = True
237
has_win32com_shell = False
238
if has_win32com_shell:
239
# still need to bind the name locally, but this is fast.
240
from win32com.shell import shell
242
return shell.SHGetSpecialFolderPath(0, csidl, 0)
244
# possibly E_NOTIMPL meaning we can't load the function pointer,
245
# or E_FAIL meaning the function failed - regardless, just ignore it
250
def get_appdata_location():
251
"""Return Application Data location.
252
Return None if we cannot obtain location.
254
Windows defines two 'Application Data' folders per user - a 'roaming'
255
one that moves with the user as they logon to different machines, and
256
a 'local' one that stays local to the machine. This returns the 'roaming'
257
directory, and thus is suitable for storing user-preferences, etc.
259
appdata = _get_sh_special_folder_path(CSIDL_APPDATA)
262
# Use APPDATA if defined, will return None if not
263
return get_environ_unicode('APPDATA')
266
def get_local_appdata_location():
267
"""Return Local Application Data location.
268
Return the same as get_appdata_location() if we cannot obtain location.
270
Windows defines two 'Application Data' folders per user - a 'roaming'
271
one that moves with the user as they logon to different machines, and
272
a 'local' one that stays local to the machine. This returns the 'local'
273
directory, and thus is suitable for caches, temp files and other things
274
which don't need to move with the user.
276
local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)
279
# Vista supplies LOCALAPPDATA, but XP and earlier do not.
280
local = get_environ_unicode('LOCALAPPDATA')
283
return get_appdata_location()
286
def get_home_location():
287
"""Return user's home location.
288
Assume on win32 it's the <My Documents> folder.
289
If location cannot be obtained return system drive root,
292
home = _get_sh_special_folder_path(CSIDL_PERSONAL)
295
home = get_environ_unicode('HOME')
298
homepath = get_environ_unicode('HOMEPATH')
299
if homepath is not None:
300
return os.path.join(get_environ_unicode('HOMEDIR', ''), home)
301
# at least return windows root directory
302
windir = get_environ_unicode('WINDIR')
304
return os.path.splitdrive(windir)[0] + '/'
305
# otherwise C:\ is good enough for 98% users
306
return unicode('C:/')
310
"""Return user name as login name.
311
If name cannot be obtained return None.
315
advapi32 = ctypes.windll.advapi32
316
GetUserName = getattr(advapi32, 'GetUserName'+suffix)
317
except AttributeError:
320
buf = create_buffer(UNLEN+1)
321
n = ctypes.c_int(UNLEN+1)
322
if GetUserName(buf, ctypes.byref(n)):
323
return extract_buffer(buf)
324
# otherwise try env variables
325
return get_environ_unicode('USERNAME')
328
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
329
# computer or the cluster associated with the local computer."
330
_WIN32_ComputerNameDnsHostname = 1
333
"""Return host machine name.
334
If name cannot be obtained return None.
336
:return: A unicode string representing the host name.
340
return win32api.GetComputerNameEx(_WIN32_ComputerNameDnsHostname)
341
except (NotImplementedError, win32api.error):
342
# NotImplemented will happen on win9x...
346
kernel32 = ctypes.windll.kernel32
347
except AttributeError:
348
pass # Missing the module we need
350
buf = create_buffer(MAX_COMPUTERNAME_LENGTH+1)
351
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
353
# Try GetComputerNameEx which gives a proper Unicode hostname
354
GetComputerNameEx = getattr(kernel32, 'GetComputerNameEx'+suffix,
356
if (GetComputerNameEx is not None
357
and GetComputerNameEx(_WIN32_ComputerNameDnsHostname,
358
buf, ctypes.byref(n))):
359
return extract_buffer(buf)
361
# Try GetComputerName in case GetComputerNameEx wasn't found
362
# It returns the NETBIOS name, which isn't as good, but still ok.
363
# The first GetComputerNameEx might have changed 'n', so reset it
364
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
365
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix,
367
if (GetComputerName is not None
368
and GetComputerName(buf, ctypes.byref(n))):
369
return extract_buffer(buf)
370
return get_environ_unicode('COMPUTERNAME')
373
@symbol_versioning.deprecated_method(
374
symbol_versioning.deprecated_in((2, 5, 0)))
375
def _ensure_unicode(s):
376
if s and type(s) != unicode:
377
from bzrlib import osutils
378
s = s.decode(osutils.get_user_encoding())
382
get_appdata_location_unicode = symbol_versioning.deprecated_method(
383
symbol_versioning.deprecated_in((2, 5, 0)))(get_appdata_location)
385
get_home_location_unicode = symbol_versioning.deprecated_method(
386
symbol_versioning.deprecated_in((2, 5, 0)))(get_home_location)
388
get_user_name_unicode = symbol_versioning.deprecated_method(
389
symbol_versioning.deprecated_in((2, 5, 0)))(get_user_name)
391
get_host_name_unicode = symbol_versioning.deprecated_method(
392
symbol_versioning.deprecated_in((2, 5, 0)))(get_host_name)
395
def _ensure_with_dir(path):
396
if (not os.path.split(path)[0] or path.startswith(u'*')
397
or path.startswith(u'?')):
398
return u'./' + path, True
402
def _undo_ensure_with_dir(path, corrected):
409
def glob_one(possible_glob):
410
"""Same as glob.glob().
412
work around bugs in glob.glob()
413
- Python bug #1001604 ("glob doesn't return unicode with ...")
414
- failing expansion for */* with non-iso-8859-* chars
416
corrected_glob, corrected = _ensure_with_dir(possible_glob)
417
glob_files = glob.glob(corrected_glob)
420
# special case to let the normal code path handle
421
# files that do not exist, etc.
422
glob_files = [possible_glob]
424
glob_files = [_undo_ensure_with_dir(elem, corrected)
425
for elem in glob_files]
426
return [elem.replace(u'\\', u'/') for elem in glob_files]
429
def glob_expand(file_list):
430
"""Replacement for glob expansion by the shell.
432
Win32's cmd.exe does not do glob expansion (eg ``*.py``), so we do our own
435
:param file_list: A list of filenames which may include shell globs.
436
:return: An expanded list of filenames.
438
Introduced in bzrlib 0.18.
442
expanded_file_list = []
443
for possible_glob in file_list:
444
expanded_file_list.extend(glob_one(possible_glob))
445
return expanded_file_list
448
def get_app_path(appname):
449
r"""Look up in Windows registry for full path to application executable.
450
Typically, applications create subkey with their basename
451
in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
453
:param appname: name of application (if no filename extension
454
is specified, .exe used)
455
:return: full path to aplication executable from registry,
456
or appname itself if nothing found.
461
if not os.path.splitext(basename)[1]:
462
basename = appname + '.exe'
465
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
466
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' +
468
except EnvironmentError:
473
path, type_id = _winreg.QueryValueEx(hkey, '')
477
_winreg.CloseKey(hkey)
479
if type_id == REG_SZ:
481
if type_id == REG_EXPAND_SZ and has_win32api:
482
fullpath = win32api.ExpandEnvironmentStrings(path)
483
if len(fullpath) > 1 and fullpath[0] == '"' and fullpath[-1] == '"':
484
fullpath = fullpath[1:-1] # remove quotes around value
489
def set_file_attr_hidden(path):
490
"""Set file attributes to hidden if possible"""
492
if winver != 'Windows 98':
493
SetFileAttributes = win32file.SetFileAttributesW
495
SetFileAttributes = win32file.SetFileAttributes
497
SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)
498
except pywintypes.error, e:
499
from bzrlib import trace
500
trace.mutter('Unable to set hidden attribute on %r: %s', path, e)
503
def _command_line_to_argv(command_line, argv, single_quotes_allowed=False):
504
"""Convert a Unicode command line into a list of argv arguments.
506
It performs wildcard expansion to make wildcards act closer to how they
507
work in posix shells, versus how they work by default on Windows. Quoted
508
arguments are left untouched.
510
:param command_line: The unicode string to split into an arg list.
511
:param single_quotes_allowed: Whether single quotes are accepted as quoting
512
characters like double quotes. False by
514
:return: A list of unicode strings.
516
# First, spit the command line
517
s = cmdline.Splitter(command_line, single_quotes_allowed=single_quotes_allowed)
519
# Bug #587868 Now make sure that the length of s agrees with sys.argv
520
# we do this by simply counting the number of arguments in each. The counts should
521
# agree no matter what encoding sys.argv is in (AFAIK)
522
# len(arguments) < len(sys.argv) should be an impossibility since python gets
523
# args from the very same PEB as does GetCommandLineW
526
# Now shorten the command line we get from GetCommandLineW to match sys.argv
527
if len(arguments) < len(argv):
528
raise AssertionError("Split command line can't be shorter than argv")
529
arguments = arguments[len(arguments) - len(argv):]
531
# Carry on to process globs (metachars) in the command line
532
# expand globs if necessary
533
# TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
536
for is_quoted, arg in arguments:
537
if is_quoted or not glob.has_magic(arg):
540
args.extend(glob_one(arg))
544
if has_ctypes and winver == 'Windows NT':
545
def get_unicode_argv():
546
prototype = ctypes.WINFUNCTYPE(ctypes.c_wchar_p)
547
GetCommandLineW = prototype(("GetCommandLineW",
548
ctypes.windll.kernel32))
549
command_line = GetCommandLineW()
550
if command_line is None:
551
raise ctypes.WinError()
552
# Skip the first argument, since we only care about parameters
553
argv = _command_line_to_argv(command_line, sys.argv)[1:]
557
def get_environ_unicode(key, default=None):
558
"""Get `key` from environment as unicode or `default` if unset
560
The environment is natively unicode on modern windows versions but
561
Python 2 only accesses it through the legacy bytestring api.
563
Environmental variable names are case insenstive on Windows.
565
A large enough buffer will be allocated to retrieve the value, though
566
it may take two calls to the underlying library function.
568
This needs ctypes because pywin32 does not expose the wide version.
570
cfunc = getattr(get_environ_unicode, "_c_function", None)
572
from ctypes.wintypes import DWORD, LPCWSTR, LPWSTR
573
cfunc = ctypes.WINFUNCTYPE(DWORD, LPCWSTR, LPWSTR, DWORD)(
574
("GetEnvironmentVariableW", ctypes.windll.kernel32))
575
get_environ_unicode._c_function = cfunc
576
buffer_size = 256 # heuristic, 256 characters often enough
578
buffer = ctypes.create_unicode_buffer(buffer_size)
579
length = cfunc(key, buffer, buffer_size)
581
code = ctypes.GetLastError()
582
if code == 203: # ERROR_ENVVAR_NOT_FOUND
584
raise ctypes.WinError(code)
585
if buffer_size > length:
586
return buffer[:length]
589
get_unicode_argv = None
590
def get_environ_unicode(key, default=None):
591
"""Get `key` from environment as unicode or `default` if unset
593
Fallback version that should basically never be needed.
596
return os.environ[key].decode("mbcs")
602
def _pywin32_is_local_pid_dead(pid):
603
"""True if pid doesn't correspond to live process on this machine"""
605
handle = win32api.OpenProcess(1, False, pid) # PROCESS_TERMINATE
606
except pywintypes.error, e:
607
if e[0] == 5: # ERROR_ACCESS_DENIED
608
# Probably something alive we're not allowed to kill
610
elif e[0] == 87: # ERROR_INVALID_PARAMETER
615
is_local_pid_dead = _pywin32_is_local_pid_dead
616
elif has_ctypes and sys.platform == 'win32':
617
from ctypes.wintypes import BOOL, DWORD, HANDLE
618
_kernel32 = ctypes.windll.kernel32
619
_CloseHandle = ctypes.WINFUNCTYPE(BOOL, HANDLE)(
620
("CloseHandle", _kernel32))
621
_OpenProcess = ctypes.WINFUNCTYPE(HANDLE, DWORD, BOOL, DWORD)(
622
("OpenProcess", _kernel32))
623
def _ctypes_is_local_pid_dead(pid):
624
"""True if pid doesn't correspond to live process on this machine"""
625
handle = _OpenProcess(1, False, pid) # PROCESS_TERMINATE
627
errorcode = ctypes.GetLastError()
628
if errorcode == 5: # ERROR_ACCESS_DENIED
629
# Probably something alive we're not allowed to kill
631
elif errorcode == 87: # ERROR_INVALID_PARAMETER
633
raise ctypes.WinError(errorcode)
636
is_local_pid_dead = _ctypes_is_local_pid_dead
639
def _is_pywintypes_error(evalue):
640
"""True if exception instance is an error from pywin32"""
641
if has_pywintypes and isinstance(evalue, pywintypes.error):