src/mercurialserver/ruleset.py
author Paul Crowley <paul@lshift.net>
Sat, 19 Dec 2009 16:31:31 +0000
changeset 243 2674d7d1c02f
parent 242 03d8f07230b3
child 297 9875791ab421
permissions -rw-r--r--
Clean up CREDITS
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
242
03d8f07230b3 Tidy up file prologues; move credits to CREDITS
Paul Crowley <paul@lshift.net>
parents: 237
diff changeset
     1
"""
03d8f07230b3 Tidy up file prologues; move credits to CREDITS
Paul Crowley <paul@lshift.net>
parents: 237
diff changeset
     2
Glob-based, order-based rules matcher that can answer "maybe"
03d8f07230b3 Tidy up file prologues; move credits to CREDITS
Paul Crowley <paul@lshift.net>
parents: 237
diff changeset
     3
where the inputs make clear that something is unknown.
03d8f07230b3 Tidy up file prologues; move credits to CREDITS
Paul Crowley <paul@lshift.net>
parents: 237
diff changeset
     4
"""
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     5
32
4059dbe9f26a new break-in system
Paul Crowley <paul@lshift.net>
parents: 23
diff changeset
     6
import sys
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     7
import re
39
f5055ce263c7 New system. No breaking in, just putting files in /etc/mercurial-server
Paul Crowley <paul@lshift.net>
parents: 33
diff changeset
     8
import os
f5055ce263c7 New system. No breaking in, just putting files in /etc/mercurial-server
Paul Crowley <paul@lshift.net>
parents: 33
diff changeset
     9
import os.path
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    10
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    11
def globmatcher(pattern):
106
0519745e7a57 Much less strict about most things
Paul Crowley <paul@lshift.net>
parents: 78
diff changeset
    12
    p = "[^/]*".join(re.escape(c) for c in pattern.split("*"))
0519745e7a57 Much less strict about most things
Paul Crowley <paul@lshift.net>
parents: 78
diff changeset
    13
    # ** means "match recursively" ie "ignore directories"
237
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    14
    return re.compile(p.replace("[^/]*[^/]*", ".*") + "$")
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    15
237
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    16
# Returns True for a definite match
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    17
# False for a definite non-match
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    18
# None where we can't be sure because a key is None
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    19
def rule(pairs):
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    20
    matchers = [(k, globmatcher(v)) for k, v in pairs]
48
f0cb7ad9e4ab Don't use keyword arguments everywhere
Paul Crowley <paul@lshift.net>
parents: 45
diff changeset
    21
    def c(kw):
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    22
        for k, m in matchers:
237
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    23
            if k not in kw:
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    24
                return False
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    25
            kkw = kw[k]
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    26
            if kkw is None:
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    27
                return None
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    28
            if m.match(kkw) is None:
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    29
                return False
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    30
        return True
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    31
    return c
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    32
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    33
class Ruleset(object):
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    34
    '''Class representing the rules in a rule file'''
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    35
    
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    36
    levels = ["init", "write", "read", "deny"]
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    37
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    38
    def __init__(self):
48
f0cb7ad9e4ab Don't use keyword arguments everywhere
Paul Crowley <paul@lshift.net>
parents: 45
diff changeset
    39
        self.rules = []
21
59540181a4bb simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    40
        self.preset = {}
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    41
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    42
    def add(self, action, conditions):
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    43
        self.rules.append((action, conditions))
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    44
21
59540181a4bb simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    45
    def set(self, **kw):
59540181a4bb simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    46
        self.preset.update(kw)
59540181a4bb simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    47
        
78
2a3407a14654 Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    48
    def get(self, k):
2a3407a14654 Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    49
        return self.preset.get(k, None)
2a3407a14654 Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    50
        
237
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    51
    def matchrules(self, kw):
21
59540181a4bb simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    52
        d = self.preset.copy()
48
f0cb7ad9e4ab Don't use keyword arguments everywhere
Paul Crowley <paul@lshift.net>
parents: 45
diff changeset
    53
        d.update(kw)
237
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    54
        res = set()
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    55
        for a, c in self.rules:
237
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    56
            m = c(d)
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    57
            if m is None:
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    58
                # "Maybe match" - add it and carry on
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    59
                res.add(a)
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    60
            elif m:
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    61
                # Definite match - add it and stop
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    62
                res.add(a)
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    63
                break
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    64
        return res
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    65
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    66
    def allow(self, level, **kw):
237
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    67
        for a in self.matchrules(kw):
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    68
            if a in self.levels:
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    69
                if self.levels.index(a) <= self.levels.index(level):
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    70
                    return True
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    71
        return False
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    72
    
39
f5055ce263c7 New system. No breaking in, just putting files in /etc/mercurial-server
Paul Crowley <paul@lshift.net>
parents: 33
diff changeset
    73
    def readfile(self, fn):
237
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    74
        f = open(fn)
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    75
        try:
237
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    76
            for l in f:
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    77
                l = l.strip()
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    78
                if len(l) == 0 or l.startswith("#"):
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    79
                    continue
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    80
                l = l.split()
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    81
                self.add(l[0], rule([c.split("=", 1) for c in l[1:]]))
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    82
        finally:
d30f3f312ece Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents: 109
diff changeset
    83
            f.close()
32
4059dbe9f26a new break-in system
Paul Crowley <paul@lshift.net>
parents: 23
diff changeset
    84
77
8d14aac93b5d Most of the way through abolishing env vars
Paul Crowley <paul@lshift.net>
parents: 67
diff changeset
    85
rules = Ruleset()
8d14aac93b5d Most of the way through abolishing env vars
Paul Crowley <paul@lshift.net>
parents: 67
diff changeset
    86