/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: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import sys
26
26
 
27
27
from bzrlib import cmdline
28
 
from bzrlib.i18n import gettext
29
28
 
30
29
# Windows version
31
30
if sys.platform == 'win32':
68
67
        create_buffer = ctypes.create_unicode_buffer
69
68
        suffix = 'W'
70
69
try:
 
70
    import win32file
71
71
    import pywintypes
72
 
    has_pywintypes = True
73
 
except ImportError:
74
 
    has_pywintypes = has_win32file = has_win32api = False
75
 
else:
76
 
    try:
77
 
        import win32file
78
 
        has_win32file = True
79
 
    except ImportError:
80
 
        has_win32file = False
81
 
    try:
82
 
        import win32api
83
 
        has_win32api = True
84
 
    except ImportError:
85
 
        has_win32api = False
 
72
    has_win32file = True
 
73
except ImportError:
 
74
    has_win32file = False
 
75
try:
 
76
    import win32api
 
77
    has_win32api = True
 
78
except ImportError:
 
79
    has_win32api = False
86
80
 
87
81
# pulling in win32com.shell is a bit of overhead, and normally we don't need
88
82
# it as ctypes is preferred and common.  lazy_imports and "optional"
134
128
            ctypes.byref(mem_struct),
135
129
            ctypes.sizeof(mem_struct))
136
130
        if not ret:
137
 
            trace.note(gettext('Failed to GetProcessMemoryInfo()'))
 
131
            trace.note('Failed to GetProcessMemoryInfo()')
138
132
            return
139
133
        info = {'PageFaultCount': mem_struct.PageFaultCount,
140
134
                'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize,
155
149
        proc = win32process.GetCurrentProcess()
156
150
        info = win32process.GetProcessMemoryInfo(proc)
157
151
    else:
158
 
        trace.note(gettext('Cannot debug memory on win32 without ctypes'
159
 
                   ' or win32process'))
 
152
        trace.note('Cannot debug memory on win32 without ctypes'
 
153
                   ' or win32process')
160
154
        return
161
155
    if short:
162
156
        # using base-2 units (see HACKING.txt).
163
 
        trace.note(gettext('WorkingSize {0:>7}KiB'
164
 
                   '\tPeakWorking {1:>7}KiB\t{2}').format(
 
157
        trace.note('WorkingSize %7dKiB'
 
158
                   '\tPeakWorking %7dKiB\t%s',
165
159
                   info['WorkingSetSize'] / 1024,
166
160
                   info['PeakWorkingSetSize'] / 1024,
167
 
                   message))
 
161
                   message)
168
162
        return
169
163
    if message:
170
164
        trace.note('%s', message)
171
 
    trace.note(gettext('WorkingSize       %8d KiB'), info['WorkingSetSize'] / 1024)
172
 
    trace.note(gettext('PeakWorking       %8d KiB'), info['PeakWorkingSetSize'] / 1024)
173
 
    trace.note(gettext('PagefileUsage     %8d KiB'), info.get('PagefileUsage', 0) / 1024)
174
 
    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',
175
169
               info.get('PeakPagefileUsage', 0) / 1024)
176
 
    trace.note(gettext('PrivateUsage      %8d KiB'), info.get('PrivateUsage', 0) / 1024)
177
 
    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))
178
172
 
179
173
 
180
174
def get_console_size(defaultx=80, defaulty=25):
474
468
 
475
469
 
476
470
def get_app_path(appname):
477
 
    r"""Look up in Windows registry for full path to application executable.
 
471
    """Look up in Windows registry for full path to application executable.
478
472
    Typically, applications create subkey with their basename
479
473
    in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
480
474
 
528
522
            trace.mutter('Unable to set hidden attribute on %r: %s', path, e)
529
523
 
530
524
 
531
 
def _command_line_to_argv(command_line, argv, single_quotes_allowed=False):
 
525
def _command_line_to_argv(command_line, single_quotes_allowed=False):
532
526
    """Convert a Unicode command line into a list of argv arguments.
533
527
 
534
528
    It performs wildcard expansion to make wildcards act closer to how they
541
535
                                  default.
542
536
    :return: A list of unicode strings.
543
537
    """
544
 
    # First, spit the command line
545
538
    s = cmdline.Splitter(command_line, single_quotes_allowed=single_quotes_allowed)
546
 
    
547
 
    # Bug #587868 Now make sure that the length of s agrees with sys.argv 
548
 
    # we do this by simply counting the number of arguments in each. The counts should 
549
 
    # agree no matter what encoding sys.argv is in (AFAIK) 
550
 
    # len(arguments) < len(sys.argv) should be an impossibility since python gets 
551
 
    # args from the very same PEB as does GetCommandLineW
552
 
    arguments = list(s)
553
 
    
554
 
    # Now shorten the command line we get from GetCommandLineW to match sys.argv
555
 
    if len(arguments) < len(argv):
556
 
        raise AssertionError("Split command line can't be shorter than argv")
557
 
    arguments = arguments[len(arguments) - len(argv):]
558
 
    
559
 
    # Carry on to process globs (metachars) in the command line
560
 
    # expand globs if necessary
 
539
    # Now that we've split the content, expand globs if necessary
561
540
    # TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
562
541
    #       '**/' style globs
563
542
    args = []
564
 
    for is_quoted, arg in arguments:
 
543
    for is_quoted, arg in s:
565
544
        if is_quoted or not glob.has_magic(arg):
566
545
            args.append(arg)
567
546
        else:
578
557
        if command_line is None:
579
558
            raise ctypes.WinError()
580
559
        # Skip the first argument, since we only care about parameters
581
 
        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] != '-':
 
569
                    break
 
570
            argv = argv[idx+1:]
582
571
        return argv
583
572
else:
584
573
    get_unicode_argv = None
585
 
 
586
 
 
587
 
if has_win32api:
588
 
    def _pywin32_is_local_pid_dead(pid):
589
 
        """True if pid doesn't correspond to live process on this machine"""
590
 
        try:
591
 
            handle = win32api.OpenProcess(1, False, pid) # PROCESS_TERMINATE
592
 
        except pywintypes.error, e:
593
 
            if e[0] == 5: # ERROR_ACCESS_DENIED
594
 
                # Probably something alive we're not allowed to kill
595
 
                return False
596
 
            elif e[0] == 87: # ERROR_INVALID_PARAMETER
597
 
                return True
598
 
            raise
599
 
        handle.close()
600
 
        return False
601
 
    is_local_pid_dead = _pywin32_is_local_pid_dead
602
 
elif has_ctypes and sys.platform == 'win32':
603
 
    from ctypes.wintypes import BOOL, DWORD, HANDLE
604
 
    _kernel32 = ctypes.windll.kernel32
605
 
    _CloseHandle = ctypes.WINFUNCTYPE(BOOL, HANDLE)(
606
 
        ("CloseHandle", _kernel32))
607
 
    _OpenProcess = ctypes.WINFUNCTYPE(HANDLE, DWORD, BOOL, DWORD)(
608
 
        ("OpenProcess", _kernel32))
609
 
    def _ctypes_is_local_pid_dead(pid):
610
 
        """True if pid doesn't correspond to live process on this machine"""
611
 
        handle = _OpenProcess(1, False, pid) # PROCESS_TERMINATE
612
 
        if not handle:
613
 
            errorcode = ctypes.GetLastError()
614
 
            if errorcode == 5: # ERROR_ACCESS_DENIED
615
 
                # Probably something alive we're not allowed to kill
616
 
                return False
617
 
            elif errorcode == 87: # ERROR_INVALID_PARAMETER
618
 
                return True
619
 
            raise ctypes.WinError(errorcode)
620
 
        _CloseHandle(handle)
621
 
        return False
622
 
    is_local_pid_dead = _ctypes_is_local_pid_dead
623
 
 
624
 
 
625
 
def _is_pywintypes_error(evalue):
626
 
    """True if exception instance is an error from pywin32"""
627
 
    if has_pywintypes and isinstance(evalue, pywintypes.error):
628
 
        return True
629
 
    return False