author | Paul Crowley <paul@lshift.net> |
Mon, 12 Sep 2011 21:27:30 +0100 | |
changeset 342 | 34ae6564ec0f |
parent 311 | 3cbde66305e4 |
child 371 | e9ce904b62a9 |
permissions | -rw-r--r-- |
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 | 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 |
|
300
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
16 |
# Returns 1 for a definite match |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
17 |
# -1 for a definite non-match |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
18 |
# 0 where we can't be sure because a key is None |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
19 |
def rmatch(k, m, kw): |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
20 |
if k not in kw: |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
21 |
return -1 |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
22 |
kkw = kw[k] |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
23 |
if kkw is None: |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
24 |
return 0 |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
25 |
elif m.match(kkw) is None: |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
26 |
return -1 |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
27 |
else: |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
28 |
return 1 |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
29 |
|
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
30 |
def rule(pairs): |
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
31 |
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
|
32 |
def c(kw): |
300
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
33 |
return min(rmatch(k, m, kw) for k, m in matchers) |
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
34 |
return c |
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 |
class Ruleset(object): |
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
37 |
'''Class representing the rules in a rule file''' |
311 | 38 |
|
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
39 |
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
|
40 |
|
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
41 |
def __init__(self): |
48
f0cb7ad9e4ab
Don't use keyword arguments everywhere
Paul Crowley <paul@lshift.net>
parents:
45
diff
changeset
|
42 |
self.rules = [] |
21
59540181a4bb
simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents:
18
diff
changeset
|
43 |
self.preset = {} |
18
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) |
311 | 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) |
300
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
50 |
|
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
51 |
def allow(self, level, **kw): |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
52 |
levelindex = self.levels.index(level) |
21
59540181a4bb
simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents:
18
diff
changeset
|
53 |
d = self.preset.copy() |
48
f0cb7ad9e4ab
Don't use keyword arguments everywhere
Paul Crowley <paul@lshift.net>
parents:
45
diff
changeset
|
54 |
d.update(kw) |
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) |
300
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
57 |
if m == 1: |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
58 |
# Definite match - what it says goes |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
59 |
return a <= levelindex |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
60 |
elif m == 0: |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
61 |
# "Maybe match" - allow if it says yes, ignore if no |
31c2c6b383fd
Refactor ruleset.py so I can be more confident it's correct
Paul Crowley <paul@lshift.net>
parents:
297
diff
changeset
|
62 |
if a <= levelindex: |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
63 |
return True |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
64 |
return False |
311 | 65 |
|
39
f5055ce263c7
New system. No breaking in, just putting files in /etc/mercurial-server
Paul Crowley <paul@lshift.net>
parents:
33
diff
changeset
|
66 |
def readfile(self, fn): |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
67 |
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
|
68 |
try: |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
69 |
for l in f: |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
70 |
l = l.strip() |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
71 |
if len(l) == 0 or l.startswith("#"): |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
72 |
continue |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
73 |
l = l.split() |
301
f88549f44c8e
Further refactor, dispense with unnecessary method
Paul Crowley <paul@lshift.net>
parents:
300
diff
changeset
|
74 |
# Unrecognized actions are off the high end |
f88549f44c8e
Further refactor, dispense with unnecessary method
Paul Crowley <paul@lshift.net>
parents:
300
diff
changeset
|
75 |
if l[0] in self.levels: |
f88549f44c8e
Further refactor, dispense with unnecessary method
Paul Crowley <paul@lshift.net>
parents:
300
diff
changeset
|
76 |
ix = self.levels.index(l[0]) |
f88549f44c8e
Further refactor, dispense with unnecessary method
Paul Crowley <paul@lshift.net>
parents:
300
diff
changeset
|
77 |
else: |
f88549f44c8e
Further refactor, dispense with unnecessary method
Paul Crowley <paul@lshift.net>
parents:
300
diff
changeset
|
78 |
ix = len(self.levels) |
311 | 79 |
self.rules.append((ix, |
301
f88549f44c8e
Further refactor, dispense with unnecessary method
Paul Crowley <paul@lshift.net>
parents:
300
diff
changeset
|
80 |
rule([c.split("=", 1) for c in l[1:]]))) |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
81 |
finally: |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
82 |
f.close() |
32 | 83 |
|
77
8d14aac93b5d
Most of the way through abolishing env vars
Paul Crowley <paul@lshift.net>
parents:
67
diff
changeset
|
84 |
rules = Ruleset() |