2014-04-07 16:43:39 -04:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2014 IBM Corporation
|
2015-03-12 15:38:50 -04:00
|
|
|
# Copyright 2015 Lenovo
|
2014-04-07 16:43:39 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2013-08-16 16:37:19 -04:00
|
|
|
# this will implement noderange grammar
|
|
|
|
|
|
|
|
# considered ast, but a number of things violate python grammar like [] in
|
|
|
|
# the middle of strings and use of @ for anything is not in their syntax
|
|
|
|
|
|
|
|
|
2015-03-11 17:02:26 -04:00
|
|
|
import itertools
|
|
|
|
import pyparsing as pp
|
2013-08-16 16:37:19 -04:00
|
|
|
|
2015-03-11 17:02:26 -04:00
|
|
|
# construct custom grammar with pyparsing
|
2015-03-24 14:47:38 -04:00
|
|
|
_nodeword = pp.Word(pp.alphanums + '~^$/=-:.*+!')
|
2015-03-11 17:02:26 -04:00
|
|
|
_nodebracket = pp.QuotedString(quoteChar='[', endQuoteChar=']',
|
|
|
|
unquoteResults=False)
|
|
|
|
_nodeatom = pp.Group(pp.OneOrMore(_nodeword | _nodebracket))
|
2015-03-24 17:03:08 -04:00
|
|
|
_paginationstart = pp.Word('<', pp.nums)
|
|
|
|
_paginationend = pp.Word('>', pp.nums)
|
|
|
|
_grammar = _nodeatom | ',-' | ',' | '@' | _paginationstart | _paginationend
|
2015-03-11 17:02:26 -04:00
|
|
|
_parser = pp.nestedExpr(content=_grammar)
|
2015-01-21 16:39:00 -05:00
|
|
|
|
2015-03-11 17:02:26 -04:00
|
|
|
_numextractor = pp.OneOrMore(pp.Word(pp.alphas + '-') | pp.Word(pp.nums))
|
|
|
|
|
2015-03-12 15:38:50 -04:00
|
|
|
|
|
|
|
# TODO: pagination operators <pp.nums and >pp.nums for begin and end respective
|
2013-08-16 16:37:19 -04:00
|
|
|
class NodeRange(object):
|
|
|
|
"""Iterate over a noderange
|
|
|
|
|
|
|
|
:param noderange: string representing a noderange to evaluate
|
2015-03-12 15:38:50 -04:00
|
|
|
:param config: Config manager object to use to vet elements
|
2013-08-16 16:37:19 -04:00
|
|
|
"""
|
2015-01-21 16:39:00 -05:00
|
|
|
|
2015-03-12 15:38:50 -04:00
|
|
|
def __init__(self, noderange, config=None):
|
2015-03-24 17:03:08 -04:00
|
|
|
self.beginpage = None
|
|
|
|
self.endpage = None
|
2015-03-12 15:38:50 -04:00
|
|
|
self.cfm = config
|
2015-03-11 17:02:26 -04:00
|
|
|
elements = _parser.parseString("(" + noderange + ")").asList()[0]
|
2013-08-16 16:37:19 -04:00
|
|
|
self._noderange = self._evaluate(elements)
|
2015-01-21 16:39:00 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def nodes(self):
|
2015-03-24 17:03:08 -04:00
|
|
|
if self.beginpage is None and self.endpage is None:
|
|
|
|
return self._noderange
|
|
|
|
sortedlist = list(self._noderange)
|
|
|
|
sortedlist.sort()
|
|
|
|
if self.beginpage is not None:
|
|
|
|
sortedlist = sortedlist[self.beginpage:]
|
|
|
|
if self.endpage is not None:
|
|
|
|
sortedlist = sortedlist[:self.endpage]
|
|
|
|
return set(sortedlist)
|
2013-08-16 16:37:19 -04:00
|
|
|
|
2015-03-24 14:47:38 -04:00
|
|
|
def _evaluate(self, parsetree, filternodes=None):
|
2015-01-21 16:39:00 -05:00
|
|
|
current_op = 0 # enum, 0 union, 1 subtract, 2 intersect
|
2013-08-16 16:37:19 -04:00
|
|
|
current_range = set([])
|
2015-03-11 17:02:26 -04:00
|
|
|
if not isinstance(parsetree[0], list): # down to a plain text thing
|
2015-03-24 14:47:38 -04:00
|
|
|
return self._expandstring(parsetree, filternodes)
|
2013-08-16 16:37:19 -04:00
|
|
|
for elem in parsetree:
|
|
|
|
if elem == ',-':
|
|
|
|
current_op = 1
|
|
|
|
elif elem == ',':
|
|
|
|
current_op = 0
|
|
|
|
elif elem == '@':
|
|
|
|
current_op = 2
|
|
|
|
elif current_op == 0:
|
|
|
|
current_range |= self._evaluate(elem)
|
|
|
|
elif current_op == 1:
|
2015-03-24 14:47:38 -04:00
|
|
|
current_range -= self._evaluate(elem, current_range)
|
2013-08-16 16:37:19 -04:00
|
|
|
elif current_op == 2:
|
2015-03-24 14:47:38 -04:00
|
|
|
current_range &= self._evaluate(elem, current_range)
|
2013-08-16 16:37:19 -04:00
|
|
|
return current_range
|
|
|
|
|
2015-03-11 17:02:26 -04:00
|
|
|
def failorreturn(self, atom):
|
2015-03-12 15:38:50 -04:00
|
|
|
if self.cfm is not None:
|
2015-03-11 17:02:26 -04:00
|
|
|
raise Exception(atom + " not valid")
|
|
|
|
return set([atom])
|
|
|
|
|
2015-03-12 15:38:50 -04:00
|
|
|
def expandrange(self, seqrange, delimiter):
|
|
|
|
pieces = seqrange.split(delimiter)
|
2015-03-11 17:02:26 -04:00
|
|
|
if len(pieces) % 2 != 0:
|
2015-03-12 15:38:50 -04:00
|
|
|
return self.failorreturn(seqrange)
|
2015-03-11 17:02:26 -04:00
|
|
|
halflen = len(pieces) / 2
|
|
|
|
left = delimiter.join(pieces[:halflen])
|
|
|
|
right = delimiter.join(pieces[halflen:])
|
|
|
|
leftbits = _numextractor.parseString(left).asList()
|
|
|
|
rightbits = _numextractor.parseString(right).asList()
|
|
|
|
if len(leftbits) != len(rightbits):
|
2015-03-12 15:38:50 -04:00
|
|
|
return self.failorreturn(seqrange)
|
2015-03-11 17:02:26 -04:00
|
|
|
finalfmt = ''
|
2015-01-21 16:39:00 -05:00
|
|
|
iterators = []
|
2015-03-11 17:02:26 -04:00
|
|
|
for idx in xrange(len(leftbits)):
|
|
|
|
if leftbits[idx] == rightbits[idx]:
|
|
|
|
finalfmt += leftbits[idx]
|
|
|
|
elif leftbits[idx][0] in pp.alphas:
|
|
|
|
# if string portion unequal, not going to work
|
2015-03-12 15:38:50 -04:00
|
|
|
return self.failorreturn(seqrange)
|
2015-01-21 16:39:00 -05:00
|
|
|
else:
|
2015-03-11 17:02:26 -04:00
|
|
|
curseq = []
|
|
|
|
finalfmt += '{%d}' % len(iterators)
|
|
|
|
iterators.append(curseq)
|
|
|
|
leftnum = int(leftbits[idx])
|
|
|
|
rightnum = int(rightbits[idx])
|
|
|
|
if leftnum > rightnum:
|
|
|
|
width = len(rightbits[idx])
|
|
|
|
minnum = rightnum
|
|
|
|
maxnum = leftnum + 1 # xrange goes to n-1...
|
|
|
|
elif rightnum > leftnum:
|
|
|
|
width = len(leftbits[idx])
|
|
|
|
minnum = leftnum
|
|
|
|
maxnum = rightnum + 1
|
|
|
|
else: # differently padded, but same number...
|
2015-03-12 15:38:50 -04:00
|
|
|
return self.failorreturn(seqrange)
|
2015-03-11 17:02:26 -04:00
|
|
|
numformat = '{0:0%d}' % width
|
|
|
|
for num in xrange(minnum, maxnum):
|
|
|
|
curseq.append(numformat.format(num))
|
|
|
|
results = set([])
|
|
|
|
for combo in itertools.product(*iterators):
|
2015-03-13 14:11:54 -04:00
|
|
|
entname = finalfmt.format(*combo)
|
|
|
|
results |= self.expand_entity(entname)
|
2015-03-11 17:02:26 -04:00
|
|
|
return results
|
2015-01-21 16:39:00 -05:00
|
|
|
|
2015-03-13 14:11:54 -04:00
|
|
|
def expand_entity(self, entname):
|
|
|
|
if self.cfm is None or self.cfm.is_node(entname):
|
|
|
|
return set([entname])
|
|
|
|
if self.cfm.is_node(entname):
|
|
|
|
return set([entname])
|
|
|
|
if self.cfm.is_nodegroup(entname):
|
|
|
|
grpcfg = self.cfm.get_nodegroup_attributes(entname)
|
|
|
|
nodes = grpcfg['nodes']
|
|
|
|
if 'noderange' in grpcfg and grpcfg['noderange']:
|
|
|
|
nodes |= NodeRange(
|
|
|
|
grpcfg['noderange']['value'], self.cfm).nodes
|
|
|
|
return nodes
|
|
|
|
|
2015-03-24 14:47:38 -04:00
|
|
|
def _expandstring(self, element, filternodes=None):
|
2015-03-11 17:02:26 -04:00
|
|
|
prefix = ''
|
|
|
|
for idx in xrange(len(element)):
|
|
|
|
if element[idx][0] == '[':
|
|
|
|
nodes = set([])
|
2015-03-12 15:38:50 -04:00
|
|
|
for numeric in NodeRange(element[idx][1:-1]).nodes:
|
2015-03-11 17:02:26 -04:00
|
|
|
nodes |= self._expandstring(
|
2015-03-12 15:38:50 -04:00
|
|
|
[prefix + numeric] + element[idx + 1:])
|
2015-03-11 17:02:26 -04:00
|
|
|
return nodes
|
|
|
|
else:
|
|
|
|
prefix += element[idx]
|
|
|
|
element = prefix
|
2015-03-12 15:38:50 -04:00
|
|
|
if self.cfm is not None:
|
|
|
|
# this is where we would check for exactly this
|
|
|
|
if self.cfm.is_node(element):
|
|
|
|
return set([element])
|
|
|
|
if self.cfm.is_nodegroup(element):
|
|
|
|
grpcfg = self.cfm.get_nodegroup_attributes(element)
|
|
|
|
nodes = grpcfg['nodes']
|
|
|
|
if 'noderange' in grpcfg and grpcfg['noderange']:
|
2015-03-12 15:59:25 -04:00
|
|
|
nodes |= NodeRange(
|
|
|
|
grpcfg['noderange']['value'], self.cfm).nodes
|
2015-03-12 15:38:50 -04:00
|
|
|
return nodes
|
2015-03-11 17:02:26 -04:00
|
|
|
if '-' in element and ':' not in element:
|
|
|
|
return self.expandrange(element, '-')
|
2013-08-16 16:37:19 -04:00
|
|
|
elif ':' in element: # : range for less ambiguity
|
2015-03-11 17:02:26 -04:00
|
|
|
return self.expandrange(element, ':')
|
2015-03-24 14:47:38 -04:00
|
|
|
elif '=' in element or '!~' in element:
|
|
|
|
if self.cfm is None:
|
|
|
|
raise Exception('Verification configmanager required')
|
|
|
|
return set(self.cfm.filter_node_attributes(element, filternodes))
|
2015-03-11 17:02:26 -04:00
|
|
|
elif element[0] in ('/', '~'):
|
2015-03-24 14:47:38 -04:00
|
|
|
nameexpression = element[1:]
|
|
|
|
if self.cfm is None:
|
|
|
|
raise Exception('Verification configmanager required')
|
|
|
|
return set(self.cfm.filter_nodenames(nameexpression, filternodes))
|
2015-03-11 17:02:26 -04:00
|
|
|
elif '+' in element:
|
2015-03-24 16:12:23 -04:00
|
|
|
element, increment = element.split('+')
|
|
|
|
try:
|
|
|
|
nodename, domain = element.split('.')
|
|
|
|
except ValueError:
|
|
|
|
nodename = element
|
|
|
|
domain = ''
|
|
|
|
increment = int(increment)
|
|
|
|
elembits = _numextractor.parseString(nodename).asList()
|
|
|
|
endnum = str(int(elembits[-1]) + increment)
|
|
|
|
left = ''.join(elembits)
|
|
|
|
if domain:
|
|
|
|
left += '.' + domain
|
|
|
|
right = ''.join(elembits[:-1])
|
|
|
|
right += endnum
|
|
|
|
if domain:
|
|
|
|
right += '.' + domain
|
|
|
|
nrange = left + ':' + right
|
|
|
|
return self.expandrange(nrange, ':')
|
2015-03-24 17:03:08 -04:00
|
|
|
elif '<' in element:
|
|
|
|
self.beginpage = int(element[1:])
|
|
|
|
return set([])
|
|
|
|
elif '>' in element:
|
|
|
|
self.endpage = int(element[1:])
|
|
|
|
return set([])
|
2015-03-12 15:38:50 -04:00
|
|
|
if self.cfm is None:
|
2013-08-16 16:37:19 -04:00
|
|
|
return set([element])
|
2015-03-12 15:38:50 -04:00
|
|
|
raise Exception(element + ' not a recognized node, group, or alias')
|