/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: Gordon Tyler
  • Date: 2010-01-14 04:56:20 UTC
  • mto: (5037.3.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5046.
  • Revision ID: gordon@doxxx.net-20100114045620-lhl4en5s8jityyt8
Added optional single quote support to UnicodeShlex and thus command_line_to_argv (defaults to disabled).

- Fixed unnecessary win32 platform checks in tests.
- Added extra tests for single quote support in UnicodeShlex.
- Changed commands.shlex_split_unicode to allow single quotes on win32 for better cross-platform compatibility.

Show diffs side-by-side

added added

removed removed

Lines of Context:
545
545
                return None
546
546
            else:
547
547
                return self
548
 
        elif next_char == u'"':
 
548
        elif (next_char == u'"'
 
549
              or (context.single_quotes_allowed and next_char == u"'")):
549
550
            context.quoted = True
550
 
            return _Quotes(self)
 
551
            return _Quotes(next_char, self)
551
552
        elif next_char == u'\\':
552
553
            return _Backslash(self)
553
554
        else:
556
557
 
557
558
 
558
559
class _Quotes(object):
559
 
    def __init__(self, exit_state):
 
560
    def __init__(self, quote_char, exit_state):
 
561
        self.quote_char = quote_char
560
562
        self.exit_state = exit_state
561
563
 
562
564
    def process(self, next_char, seq, context):
563
565
        if next_char == u'\\':
564
566
            return _Backslash(self)
565
 
        elif next_char == u'"':
 
567
        elif next_char == self.quote_char:
566
568
            return self.exit_state
567
569
        else:
568
570
            context.token.append(next_char)
607
609
    def process(self, next_char, seq, context):
608
610
        if _whitespace_match(next_char):
609
611
            return None
610
 
        elif next_char == u'"':
611
 
            return _Quotes(self)
 
612
        elif (next_char == u'"'
 
613
              or (context.single_quotes_allowed and next_char == u"'")):
 
614
            return _Quotes(next_char, self)
612
615
        elif next_char == u'\\':
613
616
            return _Backslash(self)
614
617
        else:
617
620
 
618
621
 
619
622
class UnicodeShlex(object):
620
 
    def __init__(self, command_line):
 
623
    def __init__(self, command_line, single_quotes_allowed=False):
621
624
        self._seq = _PushbackSequence(command_line)
 
625
        self.single_quotes_allowed = single_quotes_allowed
622
626
    
623
627
    def __iter__(self):
624
628
        return self
645
649
        return self.quoted, result
646
650
 
647
651
 
648
 
def command_line_to_argv(command_line, wildcard_expansion=True):
 
652
def command_line_to_argv(command_line, wildcard_expansion=True,
 
653
                         single_quotes_allowed=False):
649
654
    """Convert a Unicode command line into a list of argv arguments.
650
655
 
651
656
    This optionally does wildcard expansion, etc. It is intended to make
655
660
    :param command_line: The unicode string to split into an arg list.
656
661
    :param wildcard_expansion: Whether wildcard expansion should be applied to
657
662
                               each argument. True by default.
 
663
    :param single_quotes_allowed: Whether single quotes are accepted as quoting
 
664
                                  characters like double quotes. False by
 
665
                                  default.
658
666
    :return: A list of unicode strings.
659
667
    """
660
 
    s = UnicodeShlex(command_line)
 
668
    s = UnicodeShlex(command_line, single_quotes_allowed=single_quotes_allowed)
661
669
    # Now that we've split the content, expand globs if necessary
662
670
    # TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
663
671
    #       '**/' style globs