~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/win32utils.py

  • Committer: Vincent Ladeuil
  • Date: 2009-05-05 15:31:34 UTC
  • mto: (4343.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4344.
  • Revision ID: v.ladeuil+lp@free.fr-20090505153134-q4bp4is9gywsmzrv
Clean up test for log formats.

* bzrlib/tests/blackbox/test_logformats.py:
Update tests to actual style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
        suffix = 'W'
67
67
try:
68
68
    import win32file
69
 
    import pywintypes
70
69
    has_win32file = True
71
70
except ImportError:
72
71
    has_win32file = False
97
96
UNLEN = 256
98
97
MAX_COMPUTERNAME_LENGTH = 31
99
98
 
100
 
# Registry data type ids
101
 
REG_SZ = 1
102
 
REG_EXPAND_SZ = 2
103
 
 
104
99
 
105
100
def debug_memory_win32api(message='', short=True):
106
101
    """Use trace.note() to dump the running memory info."""
272
267
 
273
268
    Returned value can be unicode or plain string.
274
269
    To convert plain string to unicode use
275
 
    s.decode(osutils.get_user_encoding())
 
270
    s.decode(bzrlib.user_encoding)
276
271
    (XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
277
272
    """
278
273
    local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)
438
433
    import glob
439
434
    expanded_file_list = []
440
435
    for possible_glob in file_list:
 
436
 
441
437
        # work around bugs in glob.glob()
442
438
        # - Python bug #1001604 ("glob doesn't return unicode with ...")
443
439
        # - failing expansion for */* with non-iso-8859-* chars
467
463
                or appname itself if nothing found.
468
464
    """
469
465
    import _winreg
 
466
    try:
 
467
        hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
 
468
                               r'SOFTWARE\Microsoft\Windows'
 
469
                               r'\CurrentVersion\App Paths')
 
470
    except EnvironmentError:
 
471
        return appname
470
472
 
471
473
    basename = appname
472
474
    if not os.path.splitext(basename)[1]:
473
475
        basename = appname + '.exe'
474
 
 
475
 
    try:
476
 
        hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
477
 
            'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' +
478
 
            basename)
479
 
    except EnvironmentError:
480
 
        return appname
481
 
 
482
476
    try:
483
477
        try:
484
 
            path, type_id = _winreg.QueryValueEx(hkey, '')
 
478
            fullpath = _winreg.QueryValue(hkey, basename)
485
479
        except WindowsError:
486
 
            return appname
 
480
            fullpath = appname
487
481
    finally:
488
482
        _winreg.CloseKey(hkey)
489
483
 
490
 
    if type_id == REG_SZ:
491
 
        return path
492
 
    if type_id == REG_EXPAND_SZ and has_win32api:
493
 
        fullpath = win32api.ExpandEnvironmentStrings(path)
494
 
        if len(fullpath) > 1 and fullpath[0] == '"' and fullpath[-1] == '"':
495
 
            fullpath = fullpath[1:-1]   # remove quotes around value
496
 
        return fullpath
497
 
    return appname
 
484
    return fullpath
498
485
 
499
486
 
500
487
def set_file_attr_hidden(path):
501
488
    """Set file attributes to hidden if possible"""
502
489
    if has_win32file:
503
 
        if winver != 'Windows 98':
504
 
            SetFileAttributes = win32file.SetFileAttributesW
505
 
        else:
506
 
            SetFileAttributes = win32file.SetFileAttributes
507
 
        try:
508
 
            SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)
509
 
        except pywintypes.error, e:
510
 
            from bzrlib import trace
511
 
            trace.mutter('Unable to set hidden attribute on %r: %s', path, e)
512
 
 
513
 
 
514
 
if has_ctypes and winver != 'Windows 98':
515
 
    def get_unicode_argv():
516
 
        LPCWSTR = ctypes.c_wchar_p
517
 
        INT = ctypes.c_int
518
 
        POINTER = ctypes.POINTER
519
 
        prototype = ctypes.WINFUNCTYPE(LPCWSTR)
520
 
        GetCommandLine = prototype(("GetCommandLineW",
521
 
                                    ctypes.windll.kernel32))
522
 
        prototype = ctypes.WINFUNCTYPE(POINTER(LPCWSTR), LPCWSTR, POINTER(INT))
523
 
        CommandLineToArgv = prototype(("CommandLineToArgvW",
524
 
                                       ctypes.windll.shell32))
525
 
        c = INT(0)
526
 
        pargv = CommandLineToArgv(GetCommandLine(), ctypes.byref(c))
527
 
        # Skip the first argument, since we only care about parameters
528
 
        argv = [pargv[i] for i in range(1, c.value)]
529
 
        if getattr(sys, 'frozen', None) is None:
530
 
            # Invoked via 'python.exe' which takes the form:
531
 
            #   python.exe [PYTHON_OPTIONS] C:\Path\bzr [BZR_OPTIONS]
532
 
            # we need to get only BZR_OPTIONS part,
533
 
            # so let's using sys.argv[1:] as reference to get the tail
534
 
            # of unicode argv
535
 
            tail_len = len(sys.argv[1:])
536
 
            ix = len(argv) - tail_len
537
 
            argv = argv[ix:]
538
 
        return argv
539
 
else:
540
 
    get_unicode_argv = None
 
490
        win32file.SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)