87
97
MAX_COMPUTERNAME_LENGTH = 31
100
def debug_memory_win32api(message='', short=True):
101
"""Use trace.note() to dump the running memory info."""
102
from bzrlib import trace
104
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
105
"""Used by GetProcessMemoryInfo"""
106
_fields_ = [('cb', ctypes.c_ulong),
107
('PageFaultCount', ctypes.c_ulong),
108
('PeakWorkingSetSize', ctypes.c_size_t),
109
('WorkingSetSize', ctypes.c_size_t),
110
('QuotaPeakPagedPoolUsage', ctypes.c_size_t),
111
('QuotaPagedPoolUsage', ctypes.c_size_t),
112
('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t),
113
('QuotaNonPagedPoolUsage', ctypes.c_size_t),
114
('PagefileUsage', ctypes.c_size_t),
115
('PeakPagefileUsage', ctypes.c_size_t),
116
('PrivateUsage', ctypes.c_size_t),
118
cur_process = ctypes.windll.kernel32.GetCurrentProcess()
119
mem_struct = PROCESS_MEMORY_COUNTERS_EX()
120
ret = ctypes.windll.psapi.GetProcessMemoryInfo(cur_process,
121
ctypes.byref(mem_struct),
122
ctypes.sizeof(mem_struct))
124
trace.note('Failed to GetProcessMemoryInfo()')
126
info = {'PageFaultCount': mem_struct.PageFaultCount,
127
'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize,
128
'WorkingSetSize': mem_struct.WorkingSetSize,
129
'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage,
130
'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage,
131
'QuotaPeakNonPagedPoolUsage': mem_struct.QuotaPeakNonPagedPoolUsage,
132
'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage,
133
'PagefileUsage': mem_struct.PagefileUsage,
134
'PeakPagefileUsage': mem_struct.PeakPagefileUsage,
135
'PrivateUsage': mem_struct.PrivateUsage,
139
# win32process does not return PrivateUsage, because it doesn't use
140
# PROCESS_MEMORY_COUNTERS_EX (it uses the one without _EX).
141
proc = win32process.GetCurrentProcess()
142
info = win32process.GetProcessMemoryInfo(proc)
144
trace.note('Cannot debug memory on win32 without ctypes'
148
trace.note('WorkingSize %7dKB'
149
'\tPeakWorking %7dKB\t%s',
150
info['WorkingSetSize'] / 1024,
151
info['PeakWorkingSetSize'] / 1024,
155
trace.note('%s', message)
156
trace.note('WorkingSize %8d KB', info['WorkingSetSize'] / 1024)
157
trace.note('PeakWorking %8d KB', info['PeakWorkingSetSize'] / 1024)
158
trace.note('PagefileUsage %8d KB', info.get('PagefileUsage', 0) / 1024)
159
trace.note('PeakPagefileUsage %8d KB', info.get('PeakPagefileUsage', 0) / 1024)
160
trace.note('PrivateUsage %8d KB', info.get('PrivateUsage', 0) / 1024)
161
trace.note('PageFaultCount %8d', info.get('PageFaultCount', 0))
90
164
def get_console_size(defaultx=80, defaulty=25):
91
165
"""Return size of current console.
114
188
return (defaultx, defaulty)
191
def _get_sh_special_folder_path(csidl):
192
"""Call SHGetSpecialFolderPathW if available, or return None.
194
Result is always unicode (or None).
198
SHGetSpecialFolderPath = \
199
ctypes.windll.shell32.SHGetSpecialFolderPathW
200
except AttributeError:
203
buf = ctypes.create_unicode_buffer(MAX_PATH)
204
if SHGetSpecialFolderPath(None,buf,csidl,0):
207
global has_win32com_shell
208
if has_win32com_shell is None:
210
from win32com.shell import shell
211
has_win32com_shell = True
213
has_win32com_shell = False
214
if has_win32com_shell:
215
# still need to bind the name locally, but this is fast.
216
from win32com.shell import shell
218
return shell.SHGetSpecialFolderPath(0, csidl, 0)
220
# possibly E_NOTIMPL meaning we can't load the function pointer,
221
# or E_FAIL meaning the function failed - regardless, just ignore it
117
226
def get_appdata_location():
118
227
"""Return Application Data location.
119
228
Return None if we cannot obtain location.
121
Returned value can be unicode or plain sring.
230
Windows defines two 'Application Data' folders per user - a 'roaming'
231
one that moves with the user as they logon to different machines, and
232
a 'local' one that stays local to the machine. This returns the 'roaming'
233
directory, and thus is suitable for storing user-preferences, etc.
235
Returned value can be unicode or plain string.
122
236
To convert plain string to unicode use
123
s.decode(bzrlib.user_encoding)
237
s.decode(osutils.get_user_encoding())
238
(XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
127
SHGetSpecialFolderPath = \
128
ctypes.windll.shell32.SHGetSpecialFolderPathW
129
except AttributeError:
132
buf = ctypes.create_unicode_buffer(MAX_PATH)
133
if SHGetSpecialFolderPath(None,buf,CSIDL_APPDATA,0):
240
appdata = _get_sh_special_folder_path(CSIDL_APPDATA)
135
243
# from env variable
136
244
appdata = os.environ.get('APPDATA')
258
def get_local_appdata_location():
259
"""Return Local Application Data location.
260
Return the same as get_appdata_location() if we cannot obtain location.
262
Windows defines two 'Application Data' folders per user - a 'roaming'
263
one that moves with the user as they logon to different machines, and
264
a 'local' one that stays local to the machine. This returns the 'local'
265
directory, and thus is suitable for caches, temp files and other things
266
which don't need to move with the user.
268
Returned value can be unicode or plain string.
269
To convert plain string to unicode use
270
s.decode(osutils.get_user_encoding())
271
(XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
273
local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)
276
# Vista supplies LOCALAPPDATA, but XP and earlier do not.
277
local = os.environ.get('LOCALAPPDATA')
280
return get_appdata_location()
150
283
def get_home_location():
151
284
"""Return user's home location.
152
285
Assume on win32 it's the <My Documents> folder.
153
286
If location cannot be obtained return system drive root,
156
Returned value can be unicode or plain sring.
289
Returned value can be unicode or plain string.
157
290
To convert plain string to unicode use
158
s.decode(bzrlib.user_encoding)
291
s.decode(osutils.get_user_encoding())
162
SHGetSpecialFolderPath = \
163
ctypes.windll.shell32.SHGetSpecialFolderPathW
164
except AttributeError:
167
buf = ctypes.create_unicode_buffer(MAX_PATH)
168
if SHGetSpecialFolderPath(None,buf,CSIDL_PERSONAL,0):
293
home = _get_sh_special_folder_path(CSIDL_PERSONAL)
170
296
# try for HOME env variable
171
297
home = os.path.expanduser('~')
202
328
return os.environ.get('USERNAME', None)
331
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
332
# computer or the cluster associated with the local computer."
333
_WIN32_ComputerNameDnsHostname = 1
205
335
def get_host_name():
206
336
"""Return host machine name.
207
337
If name cannot be obtained return None.
209
Returned value can be unicode or plain sring.
210
To convert plain string to unicode use
211
s.decode(bzrlib.user_encoding)
339
:return: A unicode string representing the host name. On win98, this may be
340
a plain string as win32 api doesn't support unicode.
344
return win32api.GetComputerNameEx(_WIN32_ComputerNameDnsHostname)
345
except (NotImplementedError, win32api.error):
346
# NotImplemented will happen on win9x...
215
350
kernel32 = ctypes.windll.kernel32
216
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix)
217
351
except AttributeError:
352
pass # Missing the module we need
220
354
buf = create_buffer(MAX_COMPUTERNAME_LENGTH+1)
221
355
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
222
if GetComputerName(buf, ctypes.byref(n)):
224
# otherwise try env variables
225
return os.environ.get('COMPUTERNAME', None)
357
# Try GetComputerNameEx which gives a proper Unicode hostname
358
GetComputerNameEx = getattr(kernel32, 'GetComputerNameEx'+suffix,
360
if (GetComputerNameEx is not None
361
and GetComputerNameEx(_WIN32_ComputerNameDnsHostname,
362
buf, ctypes.byref(n))):
365
# Try GetComputerName in case GetComputerNameEx wasn't found
366
# It returns the NETBIOS name, which isn't as good, but still ok.
367
# The first GetComputerNameEx might have changed 'n', so reset it
368
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
369
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix,
371
if (GetComputerName is not None
372
and GetComputerName(buf, ctypes.byref(n))):
374
# otherwise try env variables, which will be 'mbcs' encoded
375
# on Windows (Python doesn't expose the native win32 unicode environment)
377
# http://msdn.microsoft.com/en-us/library/aa246807.aspx
378
# environment variables should always be encoded in 'mbcs'.
380
return os.environ['COMPUTERNAME'].decode("mbcs")
228
385
def _ensure_unicode(s):
386
from bzrlib import osutils
229
387
if s and type(s) != unicode:
231
s = s.decode(bzrlib.user_encoding)
388
from bzrlib import osutils
389
s = s.decode(osutils.get_user_encoding())
235
393
def get_appdata_location_unicode():
236
394
return _ensure_unicode(get_appdata_location())
291
448
glob_files = [_undo_ensure_with_dir(elem, corrected) for elem in glob_files]
292
449
expanded_file_list += glob_files
294
return [elem.replace(u'\\', u'/') for elem in expanded_file_list]
451
return [elem.replace(u'\\', u'/') for elem in expanded_file_list]
297
454
def get_app_path(appname):
298
455
"""Look up in Windows registry for full path to application executable.
299
Typicaly, applications create subkey with their basename
456
Typically, applications create subkey with their basename
300
457
in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
302
459
:param appname: name of application (if no filename extension
330
487
"""Set file attributes to hidden if possible"""
331
488
if has_win32file:
332
489
win32file.SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)
492
if has_ctypes and winver != 'Windows 98':
493
def get_unicode_argv():
494
LPCWSTR = ctypes.c_wchar_p
496
POINTER = ctypes.POINTER
497
prototype = ctypes.WINFUNCTYPE(LPCWSTR)
498
GetCommandLine = prototype(("GetCommandLineW",
499
ctypes.windll.kernel32))
500
prototype = ctypes.WINFUNCTYPE(POINTER(LPCWSTR), LPCWSTR, POINTER(INT))
501
CommandLineToArgv = prototype(("CommandLineToArgvW",
502
ctypes.windll.shell32))
504
pargv = CommandLineToArgv(GetCommandLine(), ctypes.byref(c))
505
# Skip the first argument, since we only care about parameters
506
argv = [pargv[i] for i in range(1, c.value)]
507
if getattr(sys, 'frozen', None) is None:
508
# Invoked via 'python.exe' which takes the form:
509
# python.exe [PYTHON_OPTIONS] C:\Path\bzr [BZR_OPTIONS]
510
# we need to get only BZR_OPTIONS part,
511
# so let's using sys.argv[1:] as reference to get the tail
513
tail_len = len(sys.argv[1:])
514
ix = len(argv) - tail_len
518
get_unicode_argv = None