mercurial/util.py
changeset 740 d2422f10c136
parent 705 574869103985
parent 737 8db4d406b3d3
child 742 092937de2ad7
equal deleted inserted replaced
722:e5b39ce2c3c9 740:d2422f10c136
     4 #
     4 #
     5 # This software may be used and distributed according to the terms
     5 # This software may be used and distributed according to the terms
     6 # of the GNU General Public License, incorporated herein by reference.
     6 # of the GNU General Public License, incorporated herein by reference.
     7 
     7 
     8 import os, errno
     8 import os, errno
       
     9 from demandload import *
       
    10 demandload(globals(), "re")
     9 
    11 
    10 def unique(g):
    12 def unique(g):
    11     seen = {}
    13     seen = {}
    12     for f in g:
    14     for f in g:
    13         if f not in seen:
    15         if f not in seen:
    26         return "killed by signal %d" % val, val
    28         return "killed by signal %d" % val, val
    27     elif os.WIFSTOPPED(code):
    29     elif os.WIFSTOPPED(code):
    28         val = os.WSTOPSIG(code)
    30         val = os.WSTOPSIG(code)
    29         return "stopped by signal %d" % val, val
    31         return "stopped by signal %d" % val, val
    30     raise ValueError("invalid exit code")
    32     raise ValueError("invalid exit code")
       
    33 
       
    34 def always(fn): return True
       
    35 def never(fn): return False
       
    36 
       
    37 def globre(pat, head = '^', tail = '$'):
       
    38     "convert a glob pattern into a regexp"
       
    39     i, n = 0, len(pat)
       
    40     res = ''
       
    41     group = False
       
    42     def peek(): return i < n and pat[i]
       
    43     while i < n:
       
    44         c = pat[i]
       
    45         i = i+1
       
    46         if c == '*':
       
    47             if peek() == '*':
       
    48                 i += 1
       
    49                 res += '.*'
       
    50             else:
       
    51                 res += '[^/]*'
       
    52         elif c == '?':
       
    53             res += '.'
       
    54         elif c == '[':
       
    55             j = i
       
    56             if j < n and pat[j] in '!]':
       
    57                 j += 1
       
    58             while j < n and pat[j] != ']':
       
    59                 j += 1
       
    60             if j >= n:
       
    61                 res += '\\['
       
    62             else:
       
    63                 stuff = pat[i:j].replace('\\','\\\\')
       
    64                 i = j + 1
       
    65                 if stuff[0] == '!':
       
    66                     stuff = '^' + stuff[1:]
       
    67                 elif stuff[0] == '^':
       
    68                     stuff = '\\' + stuff
       
    69                 res = '%s[%s]' % (res, stuff)
       
    70         elif c == '{':
       
    71             group = True
       
    72             res += '(?:'
       
    73         elif c == '}' and group:
       
    74             res += ')'
       
    75             group = False
       
    76         elif c == ',' and group:
       
    77             res += '|'
       
    78         else:
       
    79             res += re.escape(c)
       
    80     return head + res + tail
    31 
    81 
    32 def system(cmd, errprefix=None):
    82 def system(cmd, errprefix=None):
    33     """execute a shell command that must succeed"""
    83     """execute a shell command that must succeed"""
    34     rc = os.system(cmd)
    84     rc = os.system(cmd)
    35     if rc:
    85     if rc: