149
162
proc = win32process.GetCurrentProcess()
150
163
info = win32process.GetProcessMemoryInfo(proc)
152
trace.note('Cannot debug memory on win32 without ctypes'
165
trace.note(gettext('Cannot debug memory on win32 without ctypes'
156
169
# using base-2 units (see HACKING.txt).
157
trace.note('WorkingSize %7dKiB'
158
'\tPeakWorking %7dKiB\t%s',
170
trace.note(gettext('WorkingSize {0:>7}KiB'
171
'\tPeakWorking {1:>7}KiB\t{2}').format(
159
172
info['WorkingSetSize'] / 1024,
160
173
info['PeakWorkingSetSize'] / 1024,
164
177
trace.note('%s', message)
165
trace.note('WorkingSize %8d KiB', info['WorkingSetSize'] / 1024)
166
trace.note('PeakWorking %8d KiB', info['PeakWorkingSetSize'] / 1024)
167
trace.note('PagefileUsage %8d KiB', info.get('PagefileUsage', 0) / 1024)
168
trace.note('PeakPagefileUsage %8d KiB',
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'),
169
182
info.get('PeakPagefileUsage', 0) / 1024)
170
trace.note('PrivateUsage %8d KiB', info.get('PrivateUsage', 0) / 1024)
171
trace.note('PageFaultCount %8d', info.get('PageFaultCount', 0))
183
trace.note(gettext('PrivateUsage %8d KiB'), info.get('PrivateUsage', 0) / 1024)
184
trace.note(gettext('PageFaultCount %8d'), info.get('PageFaultCount', 0))
174
187
def get_console_size(defaultx=80, defaulty=25):
242
255
one that moves with the user as they logon to different machines, and
243
256
a 'local' one that stays local to the machine. This returns the 'roaming'
244
257
directory, and thus is suitable for storing user-preferences, etc.
246
Returned value can be unicode or plain string.
247
To convert plain string to unicode use
248
s.decode(osutils.get_user_encoding())
249
(XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
251
259
appdata = _get_sh_special_folder_path(CSIDL_APPDATA)
255
appdata = os.environ.get('APPDATA')
258
# if we fall to this point we on win98
259
# at least try C:/WINDOWS/Application Data
260
windir = os.environ.get('windir')
262
appdata = os.path.join(windir, 'Application Data')
263
if os.path.isdir(appdata):
265
# did not find anything
262
# Use APPDATA if defined, will return None if not
263
return get_environ_unicode('APPDATA')
269
266
def get_local_appdata_location():
275
272
a 'local' one that stays local to the machine. This returns the 'local'
276
273
directory, and thus is suitable for caches, temp files and other things
277
274
which don't need to move with the user.
279
Returned value can be unicode or plain string.
280
To convert plain string to unicode use
281
s.decode(osutils.get_user_encoding())
282
(XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
284
276
local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)
287
279
# Vista supplies LOCALAPPDATA, but XP and earlier do not.
288
local = os.environ.get('LOCALAPPDATA')
280
local = get_environ_unicode('LOCALAPPDATA')
291
283
return get_appdata_location()
296
288
Assume on win32 it's the <My Documents> folder.
297
289
If location cannot be obtained return system drive root,
300
Returned value can be unicode or plain string.
301
To convert plain string to unicode use
302
s.decode(osutils.get_user_encoding())
304
292
home = _get_sh_special_folder_path(CSIDL_PERSONAL)
307
# try for HOME env variable
308
home = os.path.expanduser('~')
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)
311
301
# at least return windows root directory
312
windir = os.environ.get('windir')
302
windir = get_environ_unicode('WINDIR')
314
304
return os.path.splitdrive(windir)[0] + '/'
315
305
# otherwise C:\ is good enough for 98% users
306
return unicode('C:/')
319
309
def get_user_name():
320
310
"""Return user name as login name.
321
311
If name cannot be obtained return None.
323
Returned value can be unicode or plain string.
324
To convert plain string to unicode use
325
s.decode(osutils.get_user_encoding())
382
367
if (GetComputerName is not None
383
368
and GetComputerName(buf, ctypes.byref(n))):
385
# otherwise try env variables, which will be 'mbcs' encoded
386
# on Windows (Python doesn't expose the native win32 unicode environment)
388
# http://msdn.microsoft.com/en-us/library/aa246807.aspx
389
# environment variables should always be encoded in 'mbcs'.
391
return os.environ['COMPUTERNAME'].decode("mbcs")
369
return extract_buffer(buf)
370
return get_environ_unicode('COMPUTERNAME')
373
@symbol_versioning.deprecated_method(
374
symbol_versioning.deprecated_in((2, 5, 0)))
396
375
def _ensure_unicode(s):
397
376
if s and type(s) != unicode:
398
377
from bzrlib import osutils
403
def get_appdata_location_unicode():
404
return _ensure_unicode(get_appdata_location())
406
def get_home_location_unicode():
407
return _ensure_unicode(get_home_location())
409
def get_user_name_unicode():
410
return _ensure_unicode(get_user_name())
412
def get_host_name_unicode():
413
return _ensure_unicode(get_host_name())
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)
416
395
def _ensure_with_dir(path):
574
552
# Skip the first argument, since we only care about parameters
575
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]
578
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):