Commit 0b5303da authored by Iustin Pop's avatar Iustin Pop
Browse files

Move compilation of some regexes to init time



I have found a few regexes which are static and thus can be moved to
load time, rather than run time, creation.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent b7d82e40
......@@ -76,6 +76,9 @@ _IES_STATUS_FILE = "status"
_IES_PID_FILE = "pid"
_IES_CA_FILE = "ca"
#: Valid LVS output line regex
_LVSLINE_REGEX = re.compile("^ *([^|]+)\|([0-9.]+)\|([^|]{6})\|?$")
class RPCFail(Exception):
"""Class denoting RPC failure.
......@@ -643,10 +646,9 @@ def GetVolumeList(vg_name):
if result.failed:
_Fail("Failed to list logical volumes, lvs output: %s", result.output)
valid_line_re = re.compile("^ *([^|]+)\|([0-9.]+)\|([^|]{6})\|?$")
for line in result.stdout.splitlines():
line = line.strip()
match = valid_line_re.match(line)
match = _LVSLINE_REGEX.match(line)
if not match:
logging.error("Invalid line returned from lvs output: '%s'", line)
continue
......
......@@ -1233,6 +1233,8 @@ class LUVerifyCluster(LogicalUnit):
ETYPE_ERROR = "ERROR"
ETYPE_WARNING = "WARNING"
_HOOKS_INDENT_RE = re.compile("^", re.M)
class NodeImage(object):
"""A class representing the logical and physical status of a node.
......@@ -2267,7 +2269,6 @@ class LUVerifyCluster(LogicalUnit):
# their results
if phase == constants.HOOKS_PHASE_POST:
# Used to change hooks' output to proper indentation
indent_re = re.compile('^', re.M)
feedback_fn("* Hooks Results")
assert hooks_results, "invalid result from hooks"
......@@ -2288,7 +2289,7 @@ class LUVerifyCluster(LogicalUnit):
self._ErrorIf(test, self.ENODEHOOKS, node_name,
"Script %s failed, output:", script)
if test:
output = indent_re.sub(' ', output)
output = self._HOOKS_INDENT_RE.sub(' ', output)
feedback_fn("%s" % output)
lu_result = 0
......
......@@ -92,18 +92,18 @@ class R_root(baserlib.R_Generic):
"""/ resource.
"""
@staticmethod
def GET():
_ROOT_PATTERN = re.compile("^R_([a-zA-Z0-9]+)$")
@classmethod
def GET(cls):
"""Show the list of mapped resources.
@return: a dictionary with 'name' and 'uri' keys for each of them.
"""
root_pattern = re.compile('^R_([a-zA-Z0-9]+)$')
rootlist = []
for handler in CONNECTOR.values():
m = root_pattern.match(handler.__name__)
m = cls._ROOT_PATTERN.match(handler.__name__)
if m:
name = m.group(1)
if name != 'root':
......
......@@ -100,6 +100,15 @@ _MAC_CHECK = re.compile("^([0-9a-f]{2}:){5}[0-9a-f]{2}$", re.I)
_TIMEOUT_TERM,
_TIMEOUT_KILL) = range(3)
#: Shell param checker regexp
_SHELLPARAM_REGEX = re.compile(r"^[-a-zA-Z0-9._+/:%@]+$")
#: Unit checker regexp
_PARSEUNIT_REGEX = re.compile(r"^([.\d]+)\s*([a-zA-Z]+)?$")
#: ASN1 time regexp
_ANS1_TIME_REGEX = re.compile(r"^(\d+)([-+]\d\d)(\d\d)$")
class RunResult(object):
"""Holds the result of running external programs.
......@@ -1345,7 +1354,7 @@ def IsValidShellParam(word):
@return: True if the word is 'safe'
"""
return bool(re.match("^[-a-zA-Z0-9._+/:%@]+$", word))
return bool(_SHELLPARAM_REGEX.match(word))
def BuildShellCmd(template, *args):
......@@ -1414,7 +1423,7 @@ def ParseUnit(input_string):
is always an int in MiB.
"""
m = re.match('^([.\d]+)\s*([a-zA-Z]+)?$', str(input_string))
m = _PARSEUNIT_REGEX.match(str(input_string))
if not m:
raise errors.UnitParseError("Invalid format")
......@@ -2805,7 +2814,7 @@ def _ParseAsn1Generalizedtime(value):
@param value: ASN1 GENERALIZEDTIME timestamp
"""
m = re.match(r"^(\d+)([-+]\d\d)(\d\d)$", value)
m = _ANS1_TIME_REGEX.match(value)
if m:
# We have an offset
asn1time = m.group(1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment