/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/win32utils.py

  • Committer: Aaron Bentley
  • Date: 2009-06-29 14:51:13 UTC
  • mfrom: (4489 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4490.
  • Revision ID: aaron@aaronbentley.com-20090629145113-3w350dxgqppnzo4g
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Win32-specific helper functions
 
18
 
 
19
Only one dependency: ctypes should be installed.
 
20
"""
 
21
 
 
22
import os
 
23
import struct
 
24
import sys
 
25
 
 
26
 
 
27
# Windows version
 
28
if sys.platform == 'win32':
 
29
    _major,_minor,_build,_platform,_text = sys.getwindowsversion()
 
30
    # from MSDN:
 
31
    # dwPlatformId
 
32
    #   The operating system platform.
 
33
    #   This member can be one of the following values.
 
34
    #   ==========================  ======================================
 
35
    #   Value                       Meaning
 
36
    #   --------------------------  --------------------------------------
 
37
    #   VER_PLATFORM_WIN32_NT       The operating system is Windows Vista,
 
38
    #   2                           Windows Server "Longhorn",
 
39
    #                               Windows Server 2003, Windows XP,
 
40
    #                               Windows 2000, or Windows NT.
 
41
    #
 
42
    #   VER_PLATFORM_WIN32_WINDOWS  The operating system is Windows Me,
 
43
    #   1                           Windows 98, or Windows 95.
 
44
    #   ==========================  ======================================
 
45
    if _platform == 2:
 
46
        winver = 'Windows NT'
 
47
    else:
 
48
        # don't care about real Windows name, just to force safe operations
 
49
        winver = 'Windows 98'
 
50
else:
 
51
    winver = None
 
52
 
 
53
 
 
54
# We can cope without it; use a separate variable to help pyflakes
 
55
try:
 
56
    import ctypes
 
57
    has_ctypes = True
 
58
except ImportError:
 
59
    has_ctypes = False
 
60
else:
 
61
    if winver == 'Windows 98':
 
62
        create_buffer = ctypes.create_string_buffer
 
63
        suffix = 'A'
 
64
    else:
 
65
        create_buffer = ctypes.create_unicode_buffer
 
66
        suffix = 'W'
 
67
try:
 
68
    import win32file
 
69
    has_win32file = True
 
70
except ImportError:
 
71
    has_win32file = False
 
72
try:
 
73
    import win32api
 
74
    has_win32api = True
 
75
except ImportError:
 
76
    has_win32api = False
 
77
 
 
78
# pulling in win32com.shell is a bit of overhead, and normally we don't need
 
79
# it as ctypes is preferred and common.  lazy_imports and "optional"
 
80
# modules don't work well, so we do our own lazy thing...
 
81
has_win32com_shell = None # Set to True or False once we know for sure...
 
82
 
 
83
# Special Win32 API constants
 
84
# Handles of std streams
 
85
WIN32_STDIN_HANDLE = -10
 
86
WIN32_STDOUT_HANDLE = -11
 
87
WIN32_STDERR_HANDLE = -12
 
88
 
 
89
# CSIDL constants (from MSDN 2003)
 
90
CSIDL_APPDATA = 0x001A      # Application Data folder
 
91
CSIDL_LOCAL_APPDATA = 0x001c# <user name>\Local Settings\Application Data (non roaming)
 
92
CSIDL_PERSONAL = 0x0005     # My Documents folder
 
93
 
 
94
# from winapi C headers
 
95
MAX_PATH = 260
 
96
UNLEN = 256
 
97
MAX_COMPUTERNAME_LENGTH = 31
 
98
 
 
99
# Registry data type ids
 
100
REG_SZ = 1
 
101
REG_EXPAND_SZ = 2
 
102
 
 
103
 
 
104
def debug_memory_win32api(message='', short=True):
 
105
    """Use trace.note() to dump the running memory info."""
 
106
    from bzrlib import trace
 
107
    if has_ctypes:
 
108
        class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
 
109
            """Used by GetProcessMemoryInfo"""
 
110
            _fields_ = [('cb', ctypes.c_ulong),
 
111
                        ('PageFaultCount', ctypes.c_ulong),
 
112
                        ('PeakWorkingSetSize', ctypes.c_size_t),
 
113
                        ('WorkingSetSize', ctypes.c_size_t),
 
114
                        ('QuotaPeakPagedPoolUsage', ctypes.c_size_t),
 
115
                        ('QuotaPagedPoolUsage', ctypes.c_size_t),
 
116
                        ('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t),
 
117
                        ('QuotaNonPagedPoolUsage', ctypes.c_size_t),
 
118
                        ('PagefileUsage', ctypes.c_size_t),
 
119
                        ('PeakPagefileUsage', ctypes.c_size_t),
 
120
                        ('PrivateUsage', ctypes.c_size_t),
 
121
                       ]
 
122
        cur_process = ctypes.windll.kernel32.GetCurrentProcess()
 
123
        mem_struct = PROCESS_MEMORY_COUNTERS_EX()
 
124
        ret = ctypes.windll.psapi.GetProcessMemoryInfo(cur_process,
 
125
            ctypes.byref(mem_struct),
 
126
            ctypes.sizeof(mem_struct))
 
127
        if not ret:
 
128
            trace.note('Failed to GetProcessMemoryInfo()')
 
129
            return
 
130
        info = {'PageFaultCount': mem_struct.PageFaultCount,
 
131
                'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize,
 
132
                'WorkingSetSize': mem_struct.WorkingSetSize,
 
133
                'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage,
 
134
                'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage,
 
135
                'QuotaPeakNonPagedPoolUsage': mem_struct.QuotaPeakNonPagedPoolUsage,
 
136
                'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage,
 
137
                'PagefileUsage': mem_struct.PagefileUsage,
 
138
                'PeakPagefileUsage': mem_struct.PeakPagefileUsage,
 
139
                'PrivateUsage': mem_struct.PrivateUsage,
 
140
               }
 
141
    elif has_win32api:
 
142
        import win32process
 
143
        # win32process does not return PrivateUsage, because it doesn't use
 
144
        # PROCESS_MEMORY_COUNTERS_EX (it uses the one without _EX).
 
145
        proc = win32process.GetCurrentProcess()
 
146
        info = win32process.GetProcessMemoryInfo(proc)
 
147
    else:
 
148
        trace.note('Cannot debug memory on win32 without ctypes'
 
149
                   ' or win32process')
 
150
        return
 
151
    if short:
 
152
        trace.note('WorkingSize %7dKB'
 
153
                   '\tPeakWorking %7dKB\t%s',
 
154
                   info['WorkingSetSize'] / 1024,
 
155
                   info['PeakWorkingSetSize'] / 1024,
 
156
                   message)
 
157
        return
 
158
    if message:
 
159
        trace.note('%s', message)
 
160
    trace.note('WorkingSize       %8d KB', info['WorkingSetSize'] / 1024)
 
161
    trace.note('PeakWorking       %8d KB', info['PeakWorkingSetSize'] / 1024)
 
162
    trace.note('PagefileUsage     %8d KB', info.get('PagefileUsage', 0) / 1024)
 
163
    trace.note('PeakPagefileUsage %8d KB', info.get('PeakPagefileUsage', 0) / 1024)
 
164
    trace.note('PrivateUsage      %8d KB', info.get('PrivateUsage', 0) / 1024)
 
165
    trace.note('PageFaultCount    %8d', info.get('PageFaultCount', 0))
 
166
 
 
167
 
 
168
def get_console_size(defaultx=80, defaulty=25):
 
169
    """Return size of current console.
 
170
 
 
171
    This function try to determine actual size of current working
 
172
    console window and return tuple (sizex, sizey) if success,
 
173
    or default size (defaultx, defaulty) otherwise.
 
174
    """
 
175
    if not has_ctypes:
 
176
        # no ctypes is found
 
177
        return (defaultx, defaulty)
 
178
 
 
179
    # To avoid problem with redirecting output via pipe
 
180
    # need to use stderr instead of stdout
 
181
    h = ctypes.windll.kernel32.GetStdHandle(WIN32_STDERR_HANDLE)
 
182
    csbi = ctypes.create_string_buffer(22)
 
183
    res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
 
184
 
 
185
    if res:
 
186
        (bufx, bufy, curx, cury, wattr,
 
187
        left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
 
188
        sizex = right - left + 1
 
189
        sizey = bottom - top + 1
 
190
        return (sizex, sizey)
 
191
    else:
 
192
        return (defaultx, defaulty)
 
193
 
 
194
 
 
195
def _get_sh_special_folder_path(csidl):
 
196
    """Call SHGetSpecialFolderPathW if available, or return None.
 
197
 
 
198
    Result is always unicode (or None).
 
199
    """
 
200
    if has_ctypes:
 
201
        try:
 
202
            SHGetSpecialFolderPath = \
 
203
                ctypes.windll.shell32.SHGetSpecialFolderPathW
 
204
        except AttributeError:
 
205
            pass
 
206
        else:
 
207
            buf = ctypes.create_unicode_buffer(MAX_PATH)
 
208
            if SHGetSpecialFolderPath(None,buf,csidl,0):
 
209
                return buf.value
 
210
 
 
211
    global has_win32com_shell
 
212
    if has_win32com_shell is None:
 
213
        try:
 
214
            from win32com.shell import shell
 
215
            has_win32com_shell = True
 
216
        except ImportError:
 
217
            has_win32com_shell = False
 
218
    if has_win32com_shell:
 
219
        # still need to bind the name locally, but this is fast.
 
220
        from win32com.shell import shell
 
221
        try:
 
222
            return shell.SHGetSpecialFolderPath(0, csidl, 0)
 
223
        except shell.error:
 
224
            # possibly E_NOTIMPL meaning we can't load the function pointer,
 
225
            # or E_FAIL meaning the function failed - regardless, just ignore it
 
226
            pass
 
227
    return None
 
228
 
 
229
 
 
230
def get_appdata_location():
 
231
    """Return Application Data location.
 
232
    Return None if we cannot obtain location.
 
233
 
 
234
    Windows defines two 'Application Data' folders per user - a 'roaming'
 
235
    one that moves with the user as they logon to different machines, and
 
236
    a 'local' one that stays local to the machine.  This returns the 'roaming'
 
237
    directory, and thus is suitable for storing user-preferences, etc.
 
238
 
 
239
    Returned value can be unicode or plain string.
 
240
    To convert plain string to unicode use
 
241
    s.decode(osutils.get_user_encoding())
 
242
    (XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
 
243
    """
 
244
    appdata = _get_sh_special_folder_path(CSIDL_APPDATA)
 
245
    if appdata:
 
246
        return appdata
 
247
    # from env variable
 
248
    appdata = os.environ.get('APPDATA')
 
249
    if appdata:
 
250
        return appdata
 
251
    # if we fall to this point we on win98
 
252
    # at least try C:/WINDOWS/Application Data
 
253
    windir = os.environ.get('windir')
 
254
    if windir:
 
255
        appdata = os.path.join(windir, 'Application Data')
 
256
        if os.path.isdir(appdata):
 
257
            return appdata
 
258
    # did not find anything
 
259
    return None
 
260
 
 
261
 
 
262
def get_local_appdata_location():
 
263
    """Return Local Application Data location.
 
264
    Return the same as get_appdata_location() if we cannot obtain location.
 
265
 
 
266
    Windows defines two 'Application Data' folders per user - a 'roaming'
 
267
    one that moves with the user as they logon to different machines, and
 
268
    a 'local' one that stays local to the machine.  This returns the 'local'
 
269
    directory, and thus is suitable for caches, temp files and other things
 
270
    which don't need to move with the user.
 
271
 
 
272
    Returned value can be unicode or plain string.
 
273
    To convert plain string to unicode use
 
274
    s.decode(osutils.get_user_encoding())
 
275
    (XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
 
276
    """
 
277
    local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)
 
278
    if local:
 
279
        return local
 
280
    # Vista supplies LOCALAPPDATA, but XP and earlier do not.
 
281
    local = os.environ.get('LOCALAPPDATA')
 
282
    if local:
 
283
        return local
 
284
    return get_appdata_location()
 
285
 
 
286
 
 
287
def get_home_location():
 
288
    """Return user's home location.
 
289
    Assume on win32 it's the <My Documents> folder.
 
290
    If location cannot be obtained return system drive root,
 
291
    i.e. C:\
 
292
 
 
293
    Returned value can be unicode or plain string.
 
294
    To convert plain string to unicode use
 
295
    s.decode(osutils.get_user_encoding())
 
296
    """
 
297
    home = _get_sh_special_folder_path(CSIDL_PERSONAL)
 
298
    if home:
 
299
        return home
 
300
    # try for HOME env variable
 
301
    home = os.path.expanduser('~')
 
302
    if home != '~':
 
303
        return home
 
304
    # at least return windows root directory
 
305
    windir = os.environ.get('windir')
 
306
    if windir:
 
307
        return os.path.splitdrive(windir)[0] + '/'
 
308
    # otherwise C:\ is good enough for 98% users
 
309
    return 'C:/'
 
310
 
 
311
 
 
312
def get_user_name():
 
313
    """Return user name as login name.
 
314
    If name cannot be obtained return None.
 
315
 
 
316
    Returned value can be unicode or plain string.
 
317
    To convert plain string to unicode use
 
318
    s.decode(osutils.get_user_encoding())
 
319
    """
 
320
    if has_ctypes:
 
321
        try:
 
322
            advapi32 = ctypes.windll.advapi32
 
323
            GetUserName = getattr(advapi32, 'GetUserName'+suffix)
 
324
        except AttributeError:
 
325
            pass
 
326
        else:
 
327
            buf = create_buffer(UNLEN+1)
 
328
            n = ctypes.c_int(UNLEN+1)
 
329
            if GetUserName(buf, ctypes.byref(n)):
 
330
                return buf.value
 
331
    # otherwise try env variables
 
332
    return os.environ.get('USERNAME', None)
 
333
 
 
334
 
 
335
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
 
336
# computer or the cluster associated with the local computer."
 
337
_WIN32_ComputerNameDnsHostname = 1
 
338
 
 
339
def get_host_name():
 
340
    """Return host machine name.
 
341
    If name cannot be obtained return None.
 
342
 
 
343
    :return: A unicode string representing the host name. On win98, this may be
 
344
        a plain string as win32 api doesn't support unicode.
 
345
    """
 
346
    if has_win32api:
 
347
        try:
 
348
            return win32api.GetComputerNameEx(_WIN32_ComputerNameDnsHostname)
 
349
        except (NotImplementedError, win32api.error):
 
350
            # NotImplemented will happen on win9x...
 
351
            pass
 
352
    if has_ctypes:
 
353
        try:
 
354
            kernel32 = ctypes.windll.kernel32
 
355
        except AttributeError:
 
356
            pass # Missing the module we need
 
357
        else:
 
358
            buf = create_buffer(MAX_COMPUTERNAME_LENGTH+1)
 
359
            n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
 
360
 
 
361
            # Try GetComputerNameEx which gives a proper Unicode hostname
 
362
            GetComputerNameEx = getattr(kernel32, 'GetComputerNameEx'+suffix,
 
363
                                        None)
 
364
            if (GetComputerNameEx is not None
 
365
                and GetComputerNameEx(_WIN32_ComputerNameDnsHostname,
 
366
                                      buf, ctypes.byref(n))):
 
367
                return buf.value
 
368
 
 
369
            # Try GetComputerName in case GetComputerNameEx wasn't found
 
370
            # It returns the NETBIOS name, which isn't as good, but still ok.
 
371
            # The first GetComputerNameEx might have changed 'n', so reset it
 
372
            n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
 
373
            GetComputerName = getattr(kernel32, 'GetComputerName'+suffix,
 
374
                                      None)
 
375
            if (GetComputerName is not None
 
376
                and GetComputerName(buf, ctypes.byref(n))):
 
377
                return buf.value
 
378
    # otherwise try env variables, which will be 'mbcs' encoded
 
379
    # on Windows (Python doesn't expose the native win32 unicode environment)
 
380
    # According to this:
 
381
    # http://msdn.microsoft.com/en-us/library/aa246807.aspx
 
382
    # environment variables should always be encoded in 'mbcs'.
 
383
    try:
 
384
        return os.environ['COMPUTERNAME'].decode("mbcs")
 
385
    except KeyError:
 
386
        return None
 
387
 
 
388
 
 
389
def _ensure_unicode(s):
 
390
    from bzrlib import osutils
 
391
    if s and type(s) != unicode:
 
392
        from bzrlib import osutils
 
393
        s = s.decode(osutils.get_user_encoding())
 
394
    return s
 
395
 
 
396
 
 
397
def get_appdata_location_unicode():
 
398
    return _ensure_unicode(get_appdata_location())
 
399
 
 
400
def get_home_location_unicode():
 
401
    return _ensure_unicode(get_home_location())
 
402
 
 
403
def get_user_name_unicode():
 
404
    return _ensure_unicode(get_user_name())
 
405
 
 
406
def get_host_name_unicode():
 
407
    return _ensure_unicode(get_host_name())
 
408
 
 
409
 
 
410
def _ensure_with_dir(path):
 
411
    if not os.path.split(path)[0] or path.startswith(u'*') or path.startswith(u'?'):
 
412
        return u'./' + path, True
 
413
    else:
 
414
        return path, False
 
415
 
 
416
def _undo_ensure_with_dir(path, corrected):
 
417
    if corrected:
 
418
        return path[2:]
 
419
    else:
 
420
        return path
 
421
 
 
422
 
 
423
 
 
424
def glob_expand(file_list):
 
425
    """Replacement for glob expansion by the shell.
 
426
 
 
427
    Win32's cmd.exe does not do glob expansion (eg ``*.py``), so we do our own
 
428
    here.
 
429
 
 
430
    :param file_list: A list of filenames which may include shell globs.
 
431
    :return: An expanded list of filenames.
 
432
 
 
433
    Introduced in bzrlib 0.18.
 
434
    """
 
435
    if not file_list:
 
436
        return []
 
437
    import glob
 
438
    expanded_file_list = []
 
439
    for possible_glob in file_list:
 
440
        # work around bugs in glob.glob()
 
441
        # - Python bug #1001604 ("glob doesn't return unicode with ...")
 
442
        # - failing expansion for */* with non-iso-8859-* chars
 
443
        possible_glob, corrected = _ensure_with_dir(possible_glob)
 
444
        glob_files = glob.glob(possible_glob)
 
445
 
 
446
        if glob_files == []:
 
447
            # special case to let the normal code path handle
 
448
            # files that do not exists
 
449
            expanded_file_list.append(
 
450
                _undo_ensure_with_dir(possible_glob, corrected))
 
451
        else:
 
452
            glob_files = [_undo_ensure_with_dir(elem, corrected) for elem in glob_files]
 
453
            expanded_file_list += glob_files
 
454
 
 
455
    return [elem.replace(u'\\', u'/') for elem in expanded_file_list]
 
456
 
 
457
 
 
458
def get_app_path(appname):
 
459
    """Look up in Windows registry for full path to application executable.
 
460
    Typically, applications create subkey with their basename
 
461
    in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
 
462
 
 
463
    :param  appname:    name of application (if no filename extension
 
464
                        is specified, .exe used)
 
465
    :return:    full path to aplication executable from registry,
 
466
                or appname itself if nothing found.
 
467
    """
 
468
    import _winreg
 
469
 
 
470
    basename = appname
 
471
    if not os.path.splitext(basename)[1]:
 
472
        basename = appname + '.exe'
 
473
 
 
474
    try:
 
475
        hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
 
476
            'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' +
 
477
            basename)
 
478
    except EnvironmentError:
 
479
        return appname
 
480
 
 
481
    try:
 
482
        try:
 
483
            path, type_id = _winreg.QueryValueEx(hkey, '')
 
484
        except WindowsError:
 
485
            return appname
 
486
    finally:
 
487
        _winreg.CloseKey(hkey)
 
488
 
 
489
    if type_id == REG_SZ:
 
490
        return path
 
491
    if type_id == REG_EXPAND_SZ and has_win32api:
 
492
        fullpath = win32api.ExpandEnvironmentStrings(path)
 
493
        if len(fullpath) > 1 and fullpath[0] == '"' and fullpath[-1] == '"':
 
494
            fullpath = fullpath[1:-1]   # remove quotes around value
 
495
        return fullpath
 
496
    return appname
 
497
 
 
498
 
 
499
def set_file_attr_hidden(path):
 
500
    """Set file attributes to hidden if possible"""
 
501
    if has_win32file:
 
502
        win32file.SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)
 
503
 
 
504
 
 
505
if has_ctypes and winver != 'Windows 98':
 
506
    def get_unicode_argv():
 
507
        LPCWSTR = ctypes.c_wchar_p
 
508
        INT = ctypes.c_int
 
509
        POINTER = ctypes.POINTER
 
510
        prototype = ctypes.WINFUNCTYPE(LPCWSTR)
 
511
        GetCommandLine = prototype(("GetCommandLineW",
 
512
                                    ctypes.windll.kernel32))
 
513
        prototype = ctypes.WINFUNCTYPE(POINTER(LPCWSTR), LPCWSTR, POINTER(INT))
 
514
        CommandLineToArgv = prototype(("CommandLineToArgvW",
 
515
                                       ctypes.windll.shell32))
 
516
        c = INT(0)
 
517
        pargv = CommandLineToArgv(GetCommandLine(), ctypes.byref(c))
 
518
        # Skip the first argument, since we only care about parameters
 
519
        argv = [pargv[i] for i in range(1, c.value)]
 
520
        if getattr(sys, 'frozen', None) is None:
 
521
            # Invoked via 'python.exe' which takes the form:
 
522
            #   python.exe [PYTHON_OPTIONS] C:\Path\bzr [BZR_OPTIONS]
 
523
            # we need to get only BZR_OPTIONS part,
 
524
            # so let's using sys.argv[1:] as reference to get the tail
 
525
            # of unicode argv
 
526
            tail_len = len(sys.argv[1:])
 
527
            ix = len(argv) - tail_len
 
528
            argv = argv[ix:]
 
529
        return argv
 
530
else:
 
531
    get_unicode_argv = None