summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorNarayan Desai <desai@mcs.anl.gov>2009-07-15 22:03:34 +0000
committerNarayan Desai <desai@mcs.anl.gov>2009-07-15 22:03:34 +0000
commit94f606a9b4c9b2922c867cf53955c4f87254ed96 (patch)
tree8c2aeda11a62ccb74f5f9dd98eb648c4546d2e25 /src/lib
parentf9591ddf3830627802c04676254546708ee80b00 (diff)
downloadbcfg2-94f606a9b4c9b2922c867cf53955c4f87254ed96.tar.gz
bcfg2-94f606a9b4c9b2922c867cf53955c4f87254ed96.tar.bz2
bcfg2-94f606a9b4c9b2922c867cf53955c4f87254ed96.zip
GroupPatterns: implement NodeRange (node[[001-096,099]]) support
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@5330 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Server/Plugins/GroupPatterns.py51
1 files changed, 46 insertions, 5 deletions
diff --git a/src/lib/Server/Plugins/GroupPatterns.py b/src/lib/Server/Plugins/GroupPatterns.py
index 1a5d590c1..56195602b 100644
--- a/src/lib/Server/Plugins/GroupPatterns.py
+++ b/src/lib/Server/Plugins/GroupPatterns.py
@@ -2,13 +2,49 @@
import re, lxml.etree
import Bcfg2.Server.Plugin
+class PackedDigitRange(object):
+ def __init__(self, digit_range):
+ self.sparse = list()
+ self.ranges = list()
+ for item in digit_range.split(','):
+ if '-' in item:
+ self.ranges.append(tuple([int(x) for x in item.split('-')]))
+ else:
+ self.sparse.append(int(item))
+
+ def includes(self, other):
+ iother = int(other)
+ return iother in self.sparse or True in \
+ [iother in range(rng[0], rng[1]+1) for rng in self.ranges]
+
class PatternMap(object):
- def __init__(self, pattern, groupname):
+ range_finder = '\\[\\[[\d\-,]+\\]\\]'
+ def __init__(self, pattern, rangestr, groupname):
self.pattern = pattern
- self.re = re.compile(pattern)
+ self.rangestr = rangestr
self.groupname = groupname
+ if pattern != None:
+ self.re = re.compile(pattern)
+ self.process = self.process_re
+ elif rangestr != None:
+ self.process = self.process_range
+ self.re = re.compile('^' + re.subn(self.range_finder, '(\d+)', rangestr)[0])
+ dmatcher = re.compile(re.subn(self.range_finder, '\\[\\[([\d\-,]+)\\]\\]', rangestr)[0])
+ self.dranges = [PackedDigitRange(x) for x in dmatcher.match(rangestr).groups()]
+ else:
+ raise Exception
+
+ def process_range(self, name):
+ match = self.re.match(name)
+ if not match:
+ return None
+ digits = match.groups()
+ for i in range(len(digits)):
+ if not self.dranges[i].includes(digits[i]):
+ return None
+ return self.groupname
- def process(self, name):
+ def process_re(self, name):
match = self.re.match(name)
if not match:
return None
@@ -32,9 +68,14 @@ class PatternFile(Bcfg2.Server.Plugin.SingleXMLFileBacked):
return
for entry in parsed.findall('GroupPattern'):
try:
- pat = entry.find('NamePattern').text
+ pat = None
+ rng = None
+ if entry.find('NamePattern') is not None:
+ pat = entry.find('NamePattern').text
+ if entry.find('NameRange') is not None:
+ rng = entry.find('NameRange').text
grp = entry.find('Group').text
- self.patterns.append(PatternMap(pat, grp))
+ self.patterns.append(PatternMap(pat, rng, grp))
except:
Bcfg2.Server.Plugin.logger.error(\
"GroupPatterns: Failed to initialize pattern %s" % \