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" ...@@ -76,6 +76,9 @@ _IES_STATUS_FILE = "status"
_IES_PID_FILE = "pid" _IES_PID_FILE = "pid"
_IES_CA_FILE = "ca" _IES_CA_FILE = "ca"
#: Valid LVS output line regex
_LVSLINE_REGEX = re.compile("^ *([^|]+)\|([0-9.]+)\|([^|]{6})\|?$")
class RPCFail(Exception): class RPCFail(Exception):
"""Class denoting RPC failure. """Class denoting RPC failure.
...@@ -643,10 +646,9 @@ def GetVolumeList(vg_name): ...@@ -643,10 +646,9 @@ def GetVolumeList(vg_name):
if result.failed: if result.failed:
_Fail("Failed to list logical volumes, lvs output: %s", result.output) _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(): for line in result.stdout.splitlines():
line = line.strip() line = line.strip()
match = valid_line_re.match(line) match = _LVSLINE_REGEX.match(line)
if not match: if not match:
logging.error("Invalid line returned from lvs output: '%s'", line) logging.error("Invalid line returned from lvs output: '%s'", line)
continue continue
......
...@@ -1233,6 +1233,8 @@ class LUVerifyCluster(LogicalUnit): ...@@ -1233,6 +1233,8 @@ class LUVerifyCluster(LogicalUnit):
ETYPE_ERROR = "ERROR" ETYPE_ERROR = "ERROR"
ETYPE_WARNING = "WARNING" ETYPE_WARNING = "WARNING"
_HOOKS_INDENT_RE = re.compile("^", re.M)
class NodeImage(object): class NodeImage(object):
"""A class representing the logical and physical status of a node. """A class representing the logical and physical status of a node.
...@@ -2267,7 +2269,6 @@ class LUVerifyCluster(LogicalUnit): ...@@ -2267,7 +2269,6 @@ class LUVerifyCluster(LogicalUnit):
# their results # their results
if phase == constants.HOOKS_PHASE_POST: if phase == constants.HOOKS_PHASE_POST:
# Used to change hooks' output to proper indentation # Used to change hooks' output to proper indentation
indent_re = re.compile('^', re.M)
feedback_fn("* Hooks Results") feedback_fn("* Hooks Results")
assert hooks_results, "invalid result from hooks" assert hooks_results, "invalid result from hooks"
...@@ -2288,7 +2289,7 @@ class LUVerifyCluster(LogicalUnit): ...@@ -2288,7 +2289,7 @@ class LUVerifyCluster(LogicalUnit):
self._ErrorIf(test, self.ENODEHOOKS, node_name, self._ErrorIf(test, self.ENODEHOOKS, node_name,
"Script %s failed, output:", script) "Script %s failed, output:", script)
if test: if test:
output = indent_re.sub(' ', output) output = self._HOOKS_INDENT_RE.sub(' ', output)
feedback_fn("%s" % output) feedback_fn("%s" % output)
lu_result = 0 lu_result = 0
......
...@@ -92,18 +92,18 @@ class R_root(baserlib.R_Generic): ...@@ -92,18 +92,18 @@ class R_root(baserlib.R_Generic):
"""/ resource. """/ resource.
""" """
@staticmethod _ROOT_PATTERN = re.compile("^R_([a-zA-Z0-9]+)$")
def GET():
@classmethod
def GET(cls):
"""Show the list of mapped resources. """Show the list of mapped resources.
@return: a dictionary with 'name' and 'uri' keys for each of them. @return: a dictionary with 'name' and 'uri' keys for each of them.
""" """
root_pattern = re.compile('^R_([a-zA-Z0-9]+)$')
rootlist = [] rootlist = []
for handler in CONNECTOR.values(): for handler in CONNECTOR.values():
m = root_pattern.match(handler.__name__) m = cls._ROOT_PATTERN.match(handler.__name__)
if m: if m:
name = m.group(1) name = m.group(1)
if name != 'root': if name != 'root':
......
...@@ -100,6 +100,15 @@ _MAC_CHECK = re.compile("^([0-9a-f]{2}:){5}[0-9a-f]{2}$", re.I) ...@@ -100,6 +100,15 @@ _MAC_CHECK = re.compile("^([0-9a-f]{2}:){5}[0-9a-f]{2}$", re.I)
_TIMEOUT_TERM, _TIMEOUT_TERM,
_TIMEOUT_KILL) = range(3) _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): class RunResult(object):
"""Holds the result of running external programs. """Holds the result of running external programs.
...@@ -1345,7 +1354,7 @@ def IsValidShellParam(word): ...@@ -1345,7 +1354,7 @@ def IsValidShellParam(word):
@return: True if the word is 'safe' @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): def BuildShellCmd(template, *args):
...@@ -1414,7 +1423,7 @@ def ParseUnit(input_string): ...@@ -1414,7 +1423,7 @@ def ParseUnit(input_string):
is always an int in MiB. 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: if not m:
raise errors.UnitParseError("Invalid format") raise errors.UnitParseError("Invalid format")
...@@ -2805,7 +2814,7 @@ def _ParseAsn1Generalizedtime(value): ...@@ -2805,7 +2814,7 @@ def _ParseAsn1Generalizedtime(value):
@param value: ASN1 GENERALIZEDTIME timestamp @param value: ASN1 GENERALIZEDTIME timestamp
""" """
m = re.match(r"^(\d+)([-+]\d\d)(\d\d)$", value) m = _ANS1_TIME_REGEX.match(value)
if m: if m:
# We have an offset # We have an offset
asn1time = m.group(1) 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