~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/win32utils.py

  • Committer: Robert Collins
  • Date: 2009-12-16 22:29:31 UTC
  • mto: This revision was merged to the branch mainline in revision 4920.
  • Revision ID: robertc@robertcollins.net-20091216222931-wbbn5ey4mwmpatwd
Review feedback.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
21
21
 
22
22
import glob
23
23
import os
 
24
import re
 
25
import shlex
24
26
import struct
 
27
import StringIO
25
28
import sys
26
29
 
27
 
from bzrlib import cmdline
28
30
 
29
31
# Windows version
30
32
if sys.platform == 'win32':
135
137
                'WorkingSetSize': mem_struct.WorkingSetSize,
136
138
                'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage,
137
139
                'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage,
138
 
                'QuotaPeakNonPagedPoolUsage':
139
 
                    mem_struct.QuotaPeakNonPagedPoolUsage,
 
140
                'QuotaPeakNonPagedPoolUsage': mem_struct.QuotaPeakNonPagedPoolUsage,
140
141
                'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage,
141
142
                'PagefileUsage': mem_struct.PagefileUsage,
142
143
                'PeakPagefileUsage': mem_struct.PeakPagefileUsage,
153
154
                   ' or win32process')
154
155
        return
155
156
    if short:
156
 
        # using base-2 units (see HACKING.txt).
157
 
        trace.note('WorkingSize %7dKiB'
158
 
                   '\tPeakWorking %7dKiB\t%s',
 
157
        trace.note('WorkingSize %7dKB'
 
158
                   '\tPeakWorking %7dKB\t%s',
159
159
                   info['WorkingSetSize'] / 1024,
160
160
                   info['PeakWorkingSetSize'] / 1024,
161
161
                   message)
162
162
        return
163
163
    if message:
164
164
        trace.note('%s', message)
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',
169
 
               info.get('PeakPagefileUsage', 0) / 1024)
170
 
    trace.note('PrivateUsage      %8d KiB', info.get('PrivateUsage', 0) / 1024)
 
165
    trace.note('WorkingSize       %8d KB', info['WorkingSetSize'] / 1024)
 
166
    trace.note('PeakWorking       %8d KB', info['PeakWorkingSetSize'] / 1024)
 
167
    trace.note('PagefileUsage     %8d KB', info.get('PagefileUsage', 0) / 1024)
 
168
    trace.note('PeakPagefileUsage %8d KB', info.get('PeakPagefileUsage', 0) / 1024)
 
169
    trace.note('PrivateUsage      %8d KB', info.get('PrivateUsage', 0) / 1024)
171
170
    trace.note('PageFaultCount    %8d', info.get('PageFaultCount', 0))
172
171
 
173
172
 
190
189
 
191
190
    if res:
192
191
        (bufx, bufy, curx, cury, wattr,
193
 
        left, top, right, bottom, maxx, maxy) = struct.unpack(
194
 
            "hhhhHhhhhhh", csbi.raw)
 
192
        left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
195
193
        sizex = right - left + 1
196
194
        sizey = bottom - top + 1
197
195
        return (sizex, sizey)
394
392
 
395
393
 
396
394
def _ensure_unicode(s):
 
395
    from bzrlib import osutils
397
396
    if s and type(s) != unicode:
398
397
        from bzrlib import osutils
399
398
        s = s.decode(osutils.get_user_encoding())
414
413
 
415
414
 
416
415
def _ensure_with_dir(path):
417
 
    if (not os.path.split(path)[0] or path.startswith(u'*')
418
 
        or path.startswith(u'?')):
 
416
    if not os.path.split(path)[0] or path.startswith(u'*') or path.startswith(u'?'):
419
417
        return u'./' + path, True
420
418
    else:
421
419
        return path, False
468
466
 
469
467
 
470
468
def get_app_path(appname):
471
 
    r"""Look up in Windows registry for full path to application executable.
 
469
    """Look up in Windows registry for full path to application executable.
472
470
    Typically, applications create subkey with their basename
473
471
    in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
474
472
 
522
520
            trace.mutter('Unable to set hidden attribute on %r: %s', path, e)
523
521
 
524
522
 
525
 
def _command_line_to_argv(command_line, argv, single_quotes_allowed=False):
526
 
    """Convert a Unicode command line into a list of argv arguments.
527
 
 
528
 
    It performs wildcard expansion to make wildcards act closer to how they
529
 
    work in posix shells, versus how they work by default on Windows. Quoted
530
 
    arguments are left untouched.
531
 
 
532
 
    :param command_line: The unicode string to split into an arg list.
533
 
    :param single_quotes_allowed: Whether single quotes are accepted as quoting
534
 
                                  characters like double quotes. False by
535
 
                                  default.
536
 
    :return: A list of unicode strings.
537
 
    """
538
 
    # First, spit the command line
539
 
    s = cmdline.Splitter(command_line, single_quotes_allowed=single_quotes_allowed)
540
 
    
541
 
    # Bug #587868 Now make sure that the length of s agrees with sys.argv 
542
 
    # we do this by simply counting the number of arguments in each. The counts should 
543
 
    # agree no matter what encoding sys.argv is in (AFAIK) 
544
 
    # len(arguments) < len(sys.argv) should be an impossibility since python gets 
545
 
    # args from the very same PEB as does GetCommandLineW
546
 
    arguments = list(s)
547
 
    
548
 
    # Now shorten the command line we get from GetCommandLineW to match sys.argv
549
 
    if len(arguments) < len(argv):
550
 
        raise AssertionError("Split command line can't be shorter than argv")
551
 
    arguments = arguments[len(arguments) - len(argv):]
552
 
    
553
 
    # Carry on to process globs (metachars) in the command line
554
 
    # expand globs if necessary
 
523
 
 
524
class UnicodeShlex(object):
 
525
    """This is a very simplified version of shlex.shlex.
 
526
 
 
527
    The main change is that it supports non-ascii input streams. The internal
 
528
    structure is quite simplified relative to shlex.shlex, since we aren't
 
529
    trying to handle multiple input streams, etc. In fact, we don't use a
 
530
    file-like api either.
 
531
    """
 
532
 
 
533
    def __init__(self, uni_string):
 
534
        self._input = uni_string
 
535
        self._input_iter = iter(self._input)
 
536
        self._whitespace_match = re.compile(u'\s').match
 
537
        self._word_match = re.compile(u'\S').match
 
538
        self._quote_chars = u'"'
 
539
        # self._quote_match = re.compile(u'[\'"]').match
 
540
        self._escape_match = lambda x: None # Never matches
 
541
        self._escape = '\\'
 
542
        # State can be
 
543
        #   ' ' - after whitespace, starting a new token
 
544
        #   'a' - after text, currently working on a token
 
545
        #   '"' - after ", currently in a "-delimited quoted section
 
546
        #   "\" - after '\', checking the next char
 
547
        self._state = ' '
 
548
        self._token = [] # Current token being parsed
 
549
 
 
550
    def _get_token(self):
 
551
        # Were there quote chars as part of this token?
 
552
        quoted = False
 
553
        quoted_state = None
 
554
        for nextchar in self._input_iter:
 
555
            if self._state == ' ':
 
556
                if self._whitespace_match(nextchar):
 
557
                    # if self._token: return token
 
558
                    continue
 
559
                elif nextchar in self._quote_chars:
 
560
                    self._state = nextchar # quoted state
 
561
                elif self._word_match(nextchar):
 
562
                    self._token.append(nextchar)
 
563
                    self._state = 'a'
 
564
                else:
 
565
                    raise AssertionError('wtttf?')
 
566
            elif self._state in self._quote_chars:
 
567
                quoted = True
 
568
                if nextchar == self._state: # End of quote
 
569
                    self._state = 'a' # posix allows 'foo'bar to translate to
 
570
                                      # foobar
 
571
                elif self._state == '"' and nextchar == self._escape:
 
572
                    quoted_state = self._state
 
573
                    self._state = nextchar
 
574
                else:
 
575
                    self._token.append(nextchar)
 
576
            elif self._state == self._escape:
 
577
                if nextchar == '\\':
 
578
                    self._token.append('\\')
 
579
                elif nextchar == '"':
 
580
                    self._token.append(nextchar)
 
581
                else:
 
582
                    self._token.append('\\' + nextchar)
 
583
                self._state = quoted_state
 
584
            elif self._state == 'a':
 
585
                if self._whitespace_match(nextchar):
 
586
                    if self._token:
 
587
                        break # emit this token
 
588
                    else:
 
589
                        continue # no token to emit
 
590
                elif nextchar in self._quote_chars:
 
591
                    # Start a new quoted section
 
592
                    self._state = nextchar
 
593
                # escape?
 
594
                elif (self._word_match(nextchar)
 
595
                      or nextchar in self._quote_chars
 
596
                      # or whitespace_split?
 
597
                      ):
 
598
                    self._token.append(nextchar)
 
599
                else:
 
600
                    raise AssertionError('state == "a", char: %r'
 
601
                                         % (nextchar,))
 
602
            else:
 
603
                raise AssertionError('unknown state: %r' % (self._state,))
 
604
        result = ''.join(self._token)
 
605
        self._token = []
 
606
        if not quoted and result == '':
 
607
            result = None
 
608
        return quoted, result
 
609
 
 
610
    def __iter__(self):
 
611
        return self
 
612
 
 
613
    def next(self):
 
614
        quoted, token = self._get_token()
 
615
        if token is None:
 
616
            raise StopIteration
 
617
        return quoted, token
 
618
 
 
619
 
 
620
def _command_line_to_argv(command_line):
 
621
    """Convert a Unicode command line into a set of argv arguments.
 
622
 
 
623
    This does wildcard expansion, etc. It is intended to make wildcards act
 
624
    closer to how they work in posix shells, versus how they work by default on
 
625
    Windows.
 
626
    """
 
627
    s = UnicodeShlex(command_line)
 
628
    # Now that we've split the content, expand globs
555
629
    # TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
556
630
    #       '**/' style globs
557
631
    args = []
558
 
    for is_quoted, arg in arguments:
 
632
    for is_quoted, arg in s:
559
633
        if is_quoted or not glob.has_magic(arg):
560
634
            args.append(arg)
561
635
        else:
565
639
 
566
640
if has_ctypes and winver != 'Windows 98':
567
641
    def get_unicode_argv():
568
 
        prototype = ctypes.WINFUNCTYPE(ctypes.c_wchar_p)
569
 
        GetCommandLineW = prototype(("GetCommandLineW",
570
 
                                     ctypes.windll.kernel32))
571
 
        command_line = GetCommandLineW()
572
 
        if command_line is None:
573
 
            raise ctypes.WinError()
 
642
        LPCWSTR = ctypes.c_wchar_p
 
643
        INT = ctypes.c_int
 
644
        POINTER = ctypes.POINTER
 
645
        prototype = ctypes.WINFUNCTYPE(LPCWSTR)
 
646
        GetCommandLine = prototype(("GetCommandLineW",
 
647
                                    ctypes.windll.kernel32))
 
648
        prototype = ctypes.WINFUNCTYPE(POINTER(LPCWSTR), LPCWSTR, POINTER(INT))
 
649
        command_line = GetCommandLine()
574
650
        # Skip the first argument, since we only care about parameters
575
 
        argv = _command_line_to_argv(command_line, sys.argv)[1:]
 
651
        argv = _command_line_to_argv(GetCommandLine())[1:]
 
652
        if getattr(sys, 'frozen', None) is None:
 
653
            # Invoked via 'python.exe' which takes the form:
 
654
            #   python.exe [PYTHON_OPTIONS] C:\Path\bzr [BZR_OPTIONS]
 
655
            # we need to get only BZR_OPTIONS part,
 
656
            # We already removed 'python.exe' so we remove everything up to and
 
657
            # including the first non-option ('-') argument.
 
658
            for idx in xrange(len(argv)):
 
659
                if argv[idx][:1] != '-':
 
660
                    break
 
661
            argv = argv[idx+1:]
576
662
        return argv
577
663
else:
578
664
    get_unicode_argv = None
579
 
 
580
 
 
581
 
if has_win32api:
582
 
    def _pywin32_is_local_pid_dead(pid):
583
 
        """True if pid doesn't correspond to live process on this machine"""
584
 
        try:
585
 
            handle = win32api.OpenProcess(1, False, pid) # PROCESS_TERMINATE
586
 
        except pywintypes.error, e:
587
 
            if e[0] == 5: # ERROR_ACCESS_DENIED
588
 
                # Probably something alive we're not allowed to kill
589
 
                return False
590
 
            elif e[0] == 87: # ERROR_INVALID_PARAMETER
591
 
                return True
592
 
            raise
593
 
        handle.close()
594
 
        return False
595
 
    is_local_pid_dead = _pywin32_is_local_pid_dead
596
 
elif has_ctypes and sys.platform == 'win32':
597
 
    from ctypes.wintypes import BOOL, DWORD, HANDLE
598
 
    _kernel32 = ctypes.windll.kernel32
599
 
    _CloseHandle = ctypes.WINFUNCTYPE(BOOL, HANDLE)(
600
 
        ("CloseHandle", _kernel32))
601
 
    _OpenProcess = ctypes.WINFUNCTYPE(HANDLE, DWORD, BOOL, DWORD)(
602
 
        ("OpenProcess", _kernel32))
603
 
    def _ctypes_is_local_pid_dead(pid):
604
 
        """True if pid doesn't correspond to live process on this machine"""
605
 
        handle = _OpenProcess(1, False, pid) # PROCESS_TERMINATE
606
 
        if not handle:
607
 
            errorcode = ctypes.GetLastError()
608
 
            if errorcode == 5: # ERROR_ACCESS_DENIED
609
 
                # Probably something alive we're not allowed to kill
610
 
                return False
611
 
            elif errorcode == 87: # ERROR_INVALID_PARAMETER
612
 
                return True
613
 
            raise ctypes.WinError(errorcode)
614
 
        _CloseHandle(handle)
615
 
        return False
616
 
    is_local_pid_dead = _ctypes_is_local_pid_dead