162
149
proc = win32process.GetCurrentProcess()
163
150
info = win32process.GetProcessMemoryInfo(proc)
165
trace.note(gettext('Cannot debug memory on win32 without ctypes'
152
trace.note('Cannot debug memory on win32 without ctypes'
169
156
# using base-2 units (see HACKING.txt).
170
trace.note(gettext('WorkingSize {0:>7}KiB'
171
'\tPeakWorking {1:>7}KiB\t{2}').format(
157
trace.note('WorkingSize %7dKiB'
158
'\tPeakWorking %7dKiB\t%s',
172
159
info['WorkingSetSize'] / 1024,
173
160
info['PeakWorkingSetSize'] / 1024,
177
164
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'),
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',
182
169
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))
170
trace.note('PrivateUsage %8d KiB', info.get('PrivateUsage', 0) / 1024)
171
trace.note('PageFaultCount %8d', info.get('PageFaultCount', 0))
187
174
def get_console_size(defaultx=80, defaulty=25):
255
242
one that moves with the user as they logon to different machines, and
256
243
a 'local' one that stays local to the machine. This returns the 'roaming'
257
244
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')
259
251
appdata = _get_sh_special_folder_path(CSIDL_APPDATA)
262
# Use APPDATA if defined, will return None if not
263
return get_environ_unicode('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
266
269
def get_local_appdata_location():
272
275
a 'local' one that stays local to the machine. This returns the 'local'
273
276
directory, and thus is suitable for caches, temp files and other things
274
277
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')
276
284
local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)
279
287
# Vista supplies LOCALAPPDATA, but XP and earlier do not.
280
local = get_environ_unicode('LOCALAPPDATA')
288
local = os.environ.get('LOCALAPPDATA')
283
291
return get_appdata_location()
288
296
Assume on win32 it's the <My Documents> folder.
289
297
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())
292
304
home = _get_sh_special_folder_path(CSIDL_PERSONAL)
295
home = get_environ_unicode('HOME')
307
# try for HOME env variable
308
home = os.path.expanduser('~')
298
homepath = get_environ_unicode('HOMEPATH')
299
if homepath is not None:
300
return os.path.join(get_environ_unicode('HOMEDIR', ''), home)
301
311
# at least return windows root directory
302
windir = get_environ_unicode('WINDIR')
312
windir = os.environ.get('windir')
304
314
return os.path.splitdrive(windir)[0] + '/'
305
315
# otherwise C:\ is good enough for 98% users
306
return unicode('C:/')
309
319
def get_user_name():
310
320
"""Return user name as login name.
311
321
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())
367
382
if (GetComputerName is not None
368
383
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)))
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")
375
396
def _ensure_unicode(s):
376
397
if s and type(s) != unicode:
377
398
from bzrlib import osutils
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)
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())
395
416
def _ensure_with_dir(path):
514
536
:return: A list of unicode strings.
516
# First, spit the command line
517
538
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
539
# Now that we've split the content, expand globs if necessary
533
540
# TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
534
541
# '**/' style globs
536
for is_quoted, arg in arguments:
543
for is_quoted, arg in s:
537
544
if is_quoted or not glob.has_magic(arg):
550
557
if command_line is None:
551
558
raise ctypes.WinError()
552
559
# Skip the first argument, since we only care about parameters
553
argv = _command_line_to_argv(command_line, sys.argv)[1:]
560
argv = _command_line_to_argv(command_line)[1:]
561
if getattr(sys, 'frozen', None) is None:
562
# Invoked via 'python.exe' which takes the form:
563
# python.exe [PYTHON_OPTIONS] C:\Path\bzr [BZR_OPTIONS]
564
# we need to get only BZR_OPTIONS part,
565
# We already removed 'python.exe' so we remove everything up to and
566
# including the first non-option ('-') argument.
567
for idx in xrange(len(argv)):
568
if argv[idx][: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
573
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):