author | Paul Crowley <paul@lshift.net> |
Mon, 18 Apr 2011 11:13:23 +0100 | |
changeset 299 | f41666a08b87 |
parent 297 | 9875791ab421 |
child 300 | 31c2c6b383fd |
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 |
|
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): |
297
9875791ab421
Fix error in rule matching: False overrides None even if later
Paul Crowley <paul@lshift.net>
parents:
242
diff
changeset
|
22 |
best = True |
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
23 |
for k, m in matchers: |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
24 |
if k not in kw: |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
25 |
return False |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
26 |
kkw = kw[k] |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
27 |
if kkw is None: |
297
9875791ab421
Fix error in rule matching: False overrides None even if later
Paul Crowley <paul@lshift.net>
parents:
242
diff
changeset
|
28 |
best = None |
9875791ab421
Fix error in rule matching: False overrides None even if later
Paul Crowley <paul@lshift.net>
parents:
242
diff
changeset
|
29 |
elif 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
|
30 |
return False |
297
9875791ab421
Fix error in rule matching: False overrides None even if later
Paul Crowley <paul@lshift.net>
parents:
242
diff
changeset
|
31 |
return best |
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
32 |
return c |
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
33 |
|
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
34 |
class Ruleset(object): |
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
35 |
'''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
|
36 |
|
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
37 |
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
|
38 |
|
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
39 |
def __init__(self): |
48
f0cb7ad9e4ab
Don't use keyword arguments everywhere
Paul Crowley <paul@lshift.net>
parents:
45
diff
changeset
|
40 |
self.rules = [] |
21
59540181a4bb
simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents:
18
diff
changeset
|
41 |
self.preset = {} |
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
42 |
|
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
|
21
59540181a4bb
simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents:
18
diff
changeset
|
46 |
def set(self, **kw): |
59540181a4bb
simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents:
18
diff
changeset
|
47 |
self.preset.update(kw) |
59540181a4bb
simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents:
18
diff
changeset
|
48 |
|
78
2a3407a14654
Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents:
77
diff
changeset
|
49 |
def get(self, k): |
2a3407a14654
Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents:
77
diff
changeset
|
50 |
return self.preset.get(k, None) |
2a3407a14654
Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents:
77
diff
changeset
|
51 |
|
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
52 |
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
|
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) |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
55 |
res = set() |
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
56 |
for a, c in self.rules: |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
57 |
m = c(d) |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
58 |
if m is None: |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
59 |
# "Maybe match" - add it and carry on |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
60 |
res.add(a) |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
61 |
elif m: |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
62 |
# Definite match - add it and stop |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
63 |
res.add(a) |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
64 |
break |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
65 |
return res |
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
66 |
|
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
67 |
def allow(self, level, **kw): |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
68 |
for a in self.matchrules(kw): |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
69 |
if a in self.levels: |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
70 |
if self.levels.index(a) <= self.levels.index(level): |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
71 |
return True |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
72 |
return False |
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
73 |
|
39
f5055ce263c7
New system. No breaking in, just putting files in /etc/mercurial-server
Paul Crowley <paul@lshift.net>
parents:
33
diff
changeset
|
74 |
def readfile(self, fn): |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
75 |
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
|
76 |
try: |
237
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
77 |
for l in f: |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
78 |
l = l.strip() |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
79 |
if len(l) == 0 or l.startswith("#"): |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
80 |
continue |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
81 |
l = l.split() |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
82 |
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
|
83 |
finally: |
d30f3f312ece
Handle maybe matches properly
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
84 |
f.close() |
32 | 85 |
|
77
8d14aac93b5d
Most of the way through abolishing env vars
Paul Crowley <paul@lshift.net>
parents:
67
diff
changeset
|
86 |
rules = Ruleset() |
8d14aac93b5d
Most of the way through abolishing env vars
Paul Crowley <paul@lshift.net>
parents:
67
diff
changeset
|
87 |