I have found a few regexes which are static and thus can be moved to
load time, rather than run time, creation.
---
 This passed a quick QA.

 lib/backend.py        |    6 ++++--
 lib/cmdlib.py         |    5 +++--
 lib/rapi/connector.py |    9 ++++-----
 lib/utils.py          |   14 +++++++++++---
 4 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/lib/backend.py b/lib/backend.py
index 479b0c2..fc24694 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 b4885ce..dc24f1d 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 ead1876..9e9ae7d 100644
--- a/lib/rapi/connector.py
+++ b/lib/rapi/connector.py
@@ -92,18 +92,17 @@ 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 d2020f9..7acb228 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -100,6 +100,14 @@ _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 +1353,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 +1422,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 +2813,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)
-- 
1.7.2.3

Reply via email to