src/ruleset.py
changeset 39 f5055ce263c7
parent 33 18e93dbdaf12
child 45 59dee3c04279
equal deleted inserted replaced
38:f1ee930c4ba8 39:f5055ce263c7
     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 sys
     8 import sys
     9 import re
     9 import re
       
    10 import os
       
    11 import os.path
    10 
    12 
    11 allowedchars = "A-Za-z0-9_-"
    13 allowedchars = "A-Za-z0-9_-"
    12 
    14 
    13 goodpathre = re.compile("([%s]+/)*[%s]+$" % (allowedchars, allowedchars))
    15 goodpathre = re.compile("([%s]+/)*[%s]+$" % (allowedchars, allowedchars))
    14 def goodpath(path):
    16 def goodpath(path):
    72 
    74 
    73     def allow(self, level, **kw):
    75     def allow(self, level, **kw):
    74         a = self.matchrule(**kw)
    76         a = self.matchrule(**kw)
    75         return a in self.levels and self.levels.index(a) <= self.levels.index(level)
    77         return a in self.levels and self.levels.index(a) <= self.levels.index(level)
    76     
    78     
    77     @classmethod
    79     def readfile(self, fn):
    78     def readfile(cls, fn):
       
    79         res = cls()
       
    80         try:
    80         try:
    81             f = open(fn)
    81             f = open(fn)
    82             try:
    82             try:
    83                 for l in f:
    83                 for l in f:
    84                     l = l.strip()
    84                     l = l.strip()
    85                     if len(l) == 0 or l.startswith("#"):
    85                     if len(l) == 0 or l.startswith("#"):
    86                         continue
    86                         continue
    87                     l = l.split()
    87                     l = l.split()
    88                     res.add(l[0], rule([c.split("=", 1) for c in l[1:]]))
    88                     self.add(l[0], rule([c.split("=", 1) for c in l[1:]]))
    89             finally:
    89             finally:
    90                 f.close()
    90                 f.close()
    91         except Exception, e:
    91         except Exception, e:
    92             print >> sys.stderr, "Failure reading rules file:", e
    92             print >> sys.stderr, "Failure reading rules file:", e
    93             return cls()
       
    94         return res
       
    95 
    93 
       
    94 def rules_from_env():
       
    95     res = Ruleset()
       
    96     for f in os.environ['HG_ACCESS_RULES_PATH'].split(os.pathsep):
       
    97         if os.path.isfile(f):
       
    98             res.readfile(f)
       
    99     return res
       
   100