diff --git a/lib/backend.py b/lib/backend.py index 479b0c289a547f5143ebd17caa7da527129e0e5d..fc24694c190251777fe4eccaf98c5fec3baddd25 100644 --- a/lib/backend.py +++ b/lib/backend.py @@ -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 diff --git a/lib/cmdlib.py b/lib/cmdlib.py index b4885ced894fdef16d3783f60b53b872dddcbc69..2ebf5fa940401fe4d59be5750e976bf62f69b1bf 100644 --- a/lib/cmdlib.py +++ b/lib/cmdlib.py @@ -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 diff --git a/lib/rapi/connector.py b/lib/rapi/connector.py index ead1876e0a0db5b6a58b435440d4178c8e617422..7650b2136047377fcc56c792ee99e1e654f8665a 100644 --- a/lib/rapi/connector.py +++ b/lib/rapi/connector.py @@ -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': diff --git a/lib/utils.py b/lib/utils.py index d2020f96758fcd1ffcbec9f1e105ffa6e103e48f..50b9f6513828f27a34c925b9fa961a0791b92b14 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -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)