From 0b5303daf52cc97dda2e8cb7ac6e54fd42c7ab91 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Sat, 27 Nov 2010 18:07:19 +0000
Subject: [PATCH] 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: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/backend.py        |  6 ++++--
 lib/cmdlib.py         |  5 +++--
 lib/rapi/connector.py | 10 +++++-----
 lib/utils.py          | 15 ++++++++++++---
 4 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/lib/backend.py b/lib/backend.py
index 479b0c289..fc24694c1 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 b4885ced8..2ebf5fa94 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 ead1876e0..7650b2136 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 d2020f967..50b9f6513 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)
-- 
GitLab