~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/win32utils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-03 09:07:50 UTC
  • mfrom: (5185.1.1 fix-515660)
  • Revision ID: pqm@pqm.ubuntu.com-20100503090750-ojeefmuph3yj8m5z
Update 'bzr bind' help to indicate what happens when no location is
 specified.

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':
129
128
            ctypes.byref(mem_struct),
130
129
            ctypes.sizeof(mem_struct))
131
130
        if not ret:
132
 
            trace.note(gettext('Failed to GetProcessMemoryInfo()'))
 
131
            trace.note('Failed to GetProcessMemoryInfo()')
133
132
            return
134
133
        info = {'PageFaultCount': mem_struct.PageFaultCount,
135
134
                'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize,
150
149
        proc = win32process.GetCurrentProcess()
151
150
        info = win32process.GetProcessMemoryInfo(proc)
152
151
    else:
153
 
        trace.note(gettext('Cannot debug memory on win32 without ctypes'
154
 
                   ' or win32process'))
 
152
        trace.note('Cannot debug memory on win32 without ctypes'
 
153
                   ' or win32process')
155
154
        return
156
155
    if short:
157
156
        # using base-2 units (see HACKING.txt).
158
 
        trace.note(gettext('WorkingSize {0:>7}KiB'
159
 
                   '\tPeakWorking {1:>7}KiB\t{2}').format(
 
157
        trace.note('WorkingSize %7dKiB'
 
158
                   '\tPeakWorking %7dKiB\t%s',
160
159
                   info['WorkingSetSize'] / 1024,
161
160
                   info['PeakWorkingSetSize'] / 1024,
162
 
                   message))
 
161
                   message)
163
162
        return
164
163
    if message:
165
164
        trace.note('%s', message)
166
 
    trace.note(gettext('WorkingSize       %8d KiB'), info['WorkingSetSize'] / 1024)
167
 
    trace.note(gettext('PeakWorking       %8d KiB'), info['PeakWorkingSetSize'] / 1024)
168
 
    trace.note(gettext('PagefileUsage     %8d KiB'), info.get('PagefileUsage', 0) / 1024)
169
 
    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',
170
169
               info.get('PeakPagefileUsage', 0) / 1024)
171
 
    trace.note(gettext('PrivateUsage      %8d KiB'), info.get('PrivateUsage', 0) / 1024)
172
 
    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))
173
172
 
174
173
 
175
174
def get_console_size(defaultx=80, defaulty=25):
469
468
 
470
469
 
471
470
def get_app_path(appname):
472
 
    r"""Look up in Windows registry for full path to application executable.
 
471
    """Look up in Windows registry for full path to application executable.
473
472
    Typically, applications create subkey with their basename
474
473
    in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
475
474
 
523
522
            trace.mutter('Unable to set hidden attribute on %r: %s', path, e)
524
523
 
525
524
 
526
 
def _command_line_to_argv(command_line, argv, single_quotes_allowed=False):
 
525
def _command_line_to_argv(command_line, single_quotes_allowed=False):
527
526
    """Convert a Unicode command line into a list of argv arguments.
528
527
 
529
528
    It performs wildcard expansion to make wildcards act closer to how they
536
535
                                  default.
537
536
    :return: A list of unicode strings.
538
537
    """
539
 
    # First, spit the command line
540
538
    s = cmdline.Splitter(command_line, single_quotes_allowed=single_quotes_allowed)
541
 
    
542
 
    # Bug #587868 Now make sure that the length of s agrees with sys.argv 
543
 
    # we do this by simply counting the number of arguments in each. The counts should 
544
 
    # agree no matter what encoding sys.argv is in (AFAIK) 
545
 
    # len(arguments) < len(sys.argv) should be an impossibility since python gets 
546
 
    # args from the very same PEB as does GetCommandLineW
547
 
    arguments = list(s)
548
 
    
549
 
    # Now shorten the command line we get from GetCommandLineW to match sys.argv
550
 
    if len(arguments) < len(argv):
551
 
        raise AssertionError("Split command line can't be shorter than argv")
552
 
    arguments = arguments[len(arguments) - len(argv):]
553
 
    
554
 
    # Carry on to process globs (metachars) in the command line
555
 
    # expand globs if necessary
 
539
    # Now that we've split the content, expand globs if necessary
556
540
    # TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
557
541
    #       '**/' style globs
558
542
    args = []
559
 
    for is_quoted, arg in arguments:
 
543
    for is_quoted, arg in s:
560
544
        if is_quoted or not glob.has_magic(arg):
561
545
            args.append(arg)
562
546
        else:
573
557
        if command_line is None:
574
558
            raise ctypes.WinError()
575
559
        # Skip the first argument, since we only care about parameters
576
 
        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:]
577
571
        return argv
578
572
else:
579
573
    get_unicode_argv = None
580
 
 
581
 
 
582
 
if has_win32api:
583
 
    def _pywin32_is_local_pid_dead(pid):
584
 
        """True if pid doesn't correspond to live process on this machine"""
585
 
        try:
586
 
            handle = win32api.OpenProcess(1, False, pid) # PROCESS_TERMINATE
587
 
        except pywintypes.error, e:
588
 
            if e[0] == 5: # ERROR_ACCESS_DENIED
589
 
                # Probably something alive we're not allowed to kill
590
 
                return False
591
 
            elif e[0] == 87: # ERROR_INVALID_PARAMETER
592
 
                return True
593
 
            raise
594
 
        handle.close()
595
 
        return False
596
 
    is_local_pid_dead = _pywin32_is_local_pid_dead
597
 
elif has_ctypes and sys.platform == 'win32':
598
 
    from ctypes.wintypes import BOOL, DWORD, HANDLE
599
 
    _kernel32 = ctypes.windll.kernel32
600
 
    _CloseHandle = ctypes.WINFUNCTYPE(BOOL, HANDLE)(
601
 
        ("CloseHandle", _kernel32))
602
 
    _OpenProcess = ctypes.WINFUNCTYPE(HANDLE, DWORD, BOOL, DWORD)(
603
 
        ("OpenProcess", _kernel32))
604
 
    def _ctypes_is_local_pid_dead(pid):
605
 
        """True if pid doesn't correspond to live process on this machine"""
606
 
        handle = _OpenProcess(1, False, pid) # PROCESS_TERMINATE
607
 
        if not handle:
608
 
            errorcode = ctypes.GetLastError()
609
 
            if errorcode == 5: # ERROR_ACCESS_DENIED
610
 
                # Probably something alive we're not allowed to kill
611
 
                return False
612
 
            elif errorcode == 87: # ERROR_INVALID_PARAMETER
613
 
                return True
614
 
            raise ctypes.WinError(errorcode)
615
 
        _CloseHandle(handle)
616
 
        return False
617
 
    is_local_pid_dead = _ctypes_is_local_pid_dead