comparison mercurial/match.py @ 34131:0fa781320203

doctest: bulk-replace string literals with b'' for Python 3 Our code transformer can't rewrite string literals in docstrings, and I don't want to make the transformer more complex.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 03 Sep 2017 14:32:11 +0900
parents 2be0bf186950
children a8994d08e4a2
comparison
equal deleted inserted replaced
34130:ada8a19672ab 34131:0fa781320203
578 class subdirmatcher(basematcher): 578 class subdirmatcher(basematcher):
579 """Adapt a matcher to work on a subdirectory only. 579 """Adapt a matcher to work on a subdirectory only.
580 580
581 The paths are remapped to remove/insert the path as needed: 581 The paths are remapped to remove/insert the path as needed:
582 582
583 >>> m1 = match('root', '', ['a.txt', 'sub/b.txt']) 583 >>> m1 = match(b'root', b'', [b'a.txt', b'sub/b.txt'])
584 >>> m2 = subdirmatcher('sub', m1) 584 >>> m2 = subdirmatcher(b'sub', m1)
585 >>> bool(m2('a.txt')) 585 >>> bool(m2(b'a.txt'))
586 False 586 False
587 >>> bool(m2('b.txt')) 587 >>> bool(m2(b'b.txt'))
588 True 588 True
589 >>> bool(m2.matchfn('a.txt')) 589 >>> bool(m2.matchfn(b'a.txt'))
590 False 590 False
591 >>> bool(m2.matchfn('b.txt')) 591 >>> bool(m2.matchfn(b'b.txt'))
592 True 592 True
593 >>> m2.files() 593 >>> m2.files()
594 ['b.txt'] 594 ['b.txt']
595 >>> m2.exact('b.txt') 595 >>> m2.exact(b'b.txt')
596 True 596 True
597 >>> util.pconvert(m2.rel('b.txt')) 597 >>> util.pconvert(m2.rel(b'b.txt'))
598 'sub/b.txt' 598 'sub/b.txt'
599 >>> def bad(f, msg): 599 >>> def bad(f, msg):
600 ... print "%s: %s" % (f, msg) 600 ... print b"%s: %s" % (f, msg)
601 >>> m1.bad = bad 601 >>> m1.bad = bad
602 >>> m2.bad('x.txt', 'No such file') 602 >>> m2.bad(b'x.txt', b'No such file')
603 sub/x.txt: No such file 603 sub/x.txt: No such file
604 >>> m2.abs('c.txt') 604 >>> m2.abs(b'c.txt')
605 'sub/c.txt' 605 'sub/c.txt'
606 """ 606 """
607 607
608 def __init__(self, path, matcher): 608 def __init__(self, path, matcher):
609 super(subdirmatcher, self).__init__(matcher._root, matcher._cwd) 609 super(subdirmatcher, self).__init__(matcher._root, matcher._cwd)
701 return default, pattern 701 return default, pattern
702 702
703 def _globre(pat): 703 def _globre(pat):
704 r'''Convert an extended glob string to a regexp string. 704 r'''Convert an extended glob string to a regexp string.
705 705
706 >>> print _globre(r'?') 706 >>> print _globre(br'?')
707 . 707 .
708 >>> print _globre(r'*') 708 >>> print _globre(br'*')
709 [^/]* 709 [^/]*
710 >>> print _globre(r'**') 710 >>> print _globre(br'**')
711 .* 711 .*
712 >>> print _globre(r'**/a') 712 >>> print _globre(br'**/a')
713 (?:.*/)?a 713 (?:.*/)?a
714 >>> print _globre(r'a/**/b') 714 >>> print _globre(br'a/**/b')
715 a\/(?:.*/)?b 715 a\/(?:.*/)?b
716 >>> print _globre(r'[a*?!^][^b][!c]') 716 >>> print _globre(br'[a*?!^][^b][!c]')
717 [a*?!^][\^b][^c] 717 [a*?!^][\^b][^c]
718 >>> print _globre(r'{a,b}') 718 >>> print _globre(br'{a,b}')
719 (?:a|b) 719 (?:a|b)
720 >>> print _globre(r'.\*\?') 720 >>> print _globre(br'.\*\?')
721 \.\*\? 721 \.\*\?
722 ''' 722 '''
723 i, n = 0, len(pat) 723 i, n = 0, len(pat)
724 res = '' 724 res = ''
725 group = 0 725 group = 0
908 roots are directories to match recursively, whereas exact directories should 908 roots are directories to match recursively, whereas exact directories should
909 be matched non-recursively. The returned (roots, dirs) tuple will also 909 be matched non-recursively. The returned (roots, dirs) tuple will also
910 include directories that need to be implicitly considered as either, such as 910 include directories that need to be implicitly considered as either, such as
911 parent directories. 911 parent directories.
912 912
913 >>> _rootsanddirs(\ 913 >>> _rootsanddirs(
914 [('glob', 'g/h/*', ''), ('glob', 'g/h', ''), ('glob', 'g*', '')]) 914 ... [(b'glob', b'g/h/*', b''), (b'glob', b'g/h', b''),
915 ... (b'glob', b'g*', b'')])
915 (['g/h', 'g/h', '.'], ['g', '.']) 916 (['g/h', 'g/h', '.'], ['g', '.'])
916 >>> _rootsanddirs(\ 917 >>> _rootsanddirs(
917 [('rootfilesin', 'g/h', ''), ('rootfilesin', '', '')]) 918 ... [(b'rootfilesin', b'g/h', b''), (b'rootfilesin', b'', b'')])
918 ([], ['g/h', '.', 'g', '.']) 919 ([], ['g/h', '.', 'g', '.'])
919 >>> _rootsanddirs(\ 920 >>> _rootsanddirs(
920 [('relpath', 'r', ''), ('path', 'p/p', ''), ('path', '', '')]) 921 ... [(b'relpath', b'r', b''), (b'path', b'p/p', b''),
922 ... (b'path', b'', b'')])
921 (['r', 'p/p', '.'], ['p', '.']) 923 (['r', 'p/p', '.'], ['p', '.'])
922 >>> _rootsanddirs(\ 924 >>> _rootsanddirs(
923 [('relglob', 'rg*', ''), ('re', 're/', ''), ('relre', 'rr', '')]) 925 ... [(b'relglob', b'rg*', b''), (b're', b're/', b''),
926 ... (b'relre', b'rr', b'')])
924 (['.', '.', '.'], ['.']) 927 (['.', '.', '.'], ['.'])
925 ''' 928 '''
926 r, d = _patternrootsanddirs(kindpats) 929 r, d = _patternrootsanddirs(kindpats)
927 930
928 # Append the parents as non-recursive/exact directories, since they must be 931 # Append the parents as non-recursive/exact directories, since they must be
935 return r, d 938 return r, d
936 939
937 def _explicitfiles(kindpats): 940 def _explicitfiles(kindpats):
938 '''Returns the potential explicit filenames from the patterns. 941 '''Returns the potential explicit filenames from the patterns.
939 942
940 >>> _explicitfiles([('path', 'foo/bar', '')]) 943 >>> _explicitfiles([(b'path', b'foo/bar', b'')])
941 ['foo/bar'] 944 ['foo/bar']
942 >>> _explicitfiles([('rootfilesin', 'foo/bar', '')]) 945 >>> _explicitfiles([(b'rootfilesin', b'foo/bar', b'')])
943 [] 946 []
944 ''' 947 '''
945 # Keep only the pattern kinds where one can specify filenames (vs only 948 # Keep only the pattern kinds where one can specify filenames (vs only
946 # directory names). 949 # directory names).
947 filable = [kp for kp in kindpats if kp[0] not in ('rootfilesin',)] 950 filable = [kp for kp in kindpats if kp[0] not in ('rootfilesin',)]