src/mercurialserver/ruleset.py
branchdebian
changeset 115 731a72b742db
parent 109 72100d3ed1bd
child 237 d30f3f312ece
equal deleted inserted replaced
99:e99262dfa950 115:731a72b742db
    10 import os
    10 import os
    11 import os.path
    11 import os.path
    12 
    12 
    13 allowedchars = "A-Za-z0-9_-"
    13 allowedchars = "A-Za-z0-9_-"
    14 
    14 
    15 goodpathre = re.compile("([%s]+/)*[%s]+$" % (allowedchars, allowedchars))
       
    16 def goodpath(path):
       
    17     return goodpathre.match(path) is not None
       
    18 
       
    19 goodglobre = re.compile("[*/%s]+$" % allowedchars)
       
    20 
       
    21 def goodglob(pattern):
       
    22     return goodglobre.match(pattern) is not None
       
    23 
       
    24 # Don't put anything except *A-Za-z0-9_- in rule globs or   
       
    25 # it will match nothing.  No regexp metachars, not even .
       
    26 # We may fix this later.
       
    27 def globmatcher(pattern):
    15 def globmatcher(pattern):
    28     if not goodglob(pattern):
    16     p = "[^/]*".join(re.escape(c) for c in pattern.split("*"))
    29         #fail("Bad glob pattern in auth config: %s" % pattern)
    17     # ** means "match recursively" ie "ignore directories"
    30         # FIXME: report it somehow
    18     rex = re.compile(p.replace("[^/]*[^/]*", ".*") + "$")
    31         return lambda x: False
       
    32     # Substitution cunning so ** can be different from *
       
    33     pattern = pattern.replace("*", "[]")
       
    34     pattern = pattern.replace("[][]", "[/%s]*" % allowedchars)
       
    35     pattern = pattern.replace("[]", "[%s]*" % allowedchars)
       
    36     rex = re.compile(pattern + "$")
       
    37     # None matches everything
    19     # None matches everything
    38     return lambda x: x is None or rex.match(x) is not None
    20     return lambda x: x is None or rex.match(x) is not None
    39 
    21 
    40 def rule(pairs):
    22 def rule(pairs):
    41     matchers = [(k, globmatcher(v)) for k, v in pairs]
    23     matchers = [(k, globmatcher(v)) for k, v in pairs]