]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/Misc.py
BaseTools: Replace StandardError with Expression
[mirror_edk2.git] / BaseTools / Source / Python / Common / Misc.py
index 5ffd8cd0223dbeb12f8f0428153de5509da0ea00..01171adb9b9e49754f0163873ae4d8feefe997c1 100644 (file)
@@ -42,6 +42,13 @@ import subprocess
 ## Regular expression used to find out place holders in string template\r
 gPlaceholderPattern = re.compile("\$\{([^$()\s]+)\}", re.MULTILINE | re.UNICODE)\r
 \r
+## regular expressions for map file processing\r
+startPatternGeneral = re.compile("^Start[' ']+Length[' ']+Name[' ']+Class")\r
+addressPatternGeneral = re.compile("^Address[' ']+Publics by Value[' ']+Rva\+Base")\r
+valuePatternGcc = re.compile('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$')\r
+pcdPatternGcc = re.compile('^([\da-fA-Fx]+) +([\da-fA-Fx]+)')\r
+secReGeneral = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
+\r
 ## Dictionary used to store file time stamp for quick re-access\r
 gFileTimeStampCache = {}    # {file path : file time stamp}\r
 \r
@@ -84,6 +91,7 @@ def _parseForXcode(lines, efifilepath, varnames):
         if status == 1 and len(line) != 0:\r
             for varname in varnames:\r
                 if varname in line:\r
+                    # cannot pregenerate this RegEx since it uses varname from varnames.\r
                     m = re.match('^([\da-fA-FxX]+)([\s\S]*)([_]*%s)$' % varname, line)\r
                     if m is not None:\r
                         ret.append((varname, m.group(1)))\r
@@ -109,7 +117,7 @@ def _parseForGCC(lines, efifilepath, varnames):
 \r
         # status handler\r
         if status == 3:\r
-            m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$', line)\r
+            m = valuePatternGcc.match(line)\r
             if m is not None:\r
                 sections.append(m.groups(0))\r
             for varname in varnames:\r
@@ -122,7 +130,7 @@ def _parseForGCC(lines, efifilepath, varnames):
                     else:\r
                         Str = line[len(".data.%s" % varname):]\r
                     if Str:\r
-                        m = re.match('^([\da-fA-Fx]+) +([\da-fA-Fx]+)', Str.strip())\r
+                        m = pcdPatternGcc.match(Str.strip())\r
                         if m is not None:\r
                             varoffset.append((varname, int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))\r
 \r
@@ -150,22 +158,21 @@ def _parseGeneral(lines, efifilepath, varnames):
     status = 0    #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r
     secs  = []    # key = section name\r
     varoffset = []\r
-    secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
     symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.:\\\\\w\?@\$]+) +([\da-fA-F]+)', re.UNICODE)\r
 \r
     for line in lines:\r
         line = line.strip()\r
-        if re.match("^Start[' ']+Length[' ']+Name[' ']+Class", line):\r
+        if startPatternGeneral.match(line):\r
             status = 1\r
             continue\r
-        if re.match("^Address[' ']+Publics by Value[' ']+Rva\+Base", line):\r
+        if addressPatternGeneral.match(line):\r
             status = 2\r
             continue\r
-        if re.match("^entry point at", line):\r
+        if line.startswith("entry point at"):\r
             status = 3\r
             continue        \r
         if status == 1 and len(line) != 0:\r
-            m =  secRe.match(line)\r
+            m =  secReGeneral.match(line)\r
             assert m is not None, "Fail to parse the section in map file , line is %s" % line\r
             sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0)\r
             secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class])\r
@@ -177,6 +184,7 @@ def _parseGeneral(lines, efifilepath, varnames):
                 sec_no     = int(sec_no,     16)\r
                 sym_offset = int(sym_offset, 16)\r
                 vir_addr   = int(vir_addr,   16)\r
+                # cannot pregenerate this RegEx since it uses varname from varnames.\r
                 m2 = re.match('^[_]*(%s)' % varname, sym_name)\r
                 if m2 is not None:\r
                     # fond a binary pcd entry in map file\r
@@ -470,7 +478,7 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
             Fd = open(File, "wb")\r
             Fd.write(Content)\r
             Fd.close()\r
-    except IOError, X:\r
+    except IOError as X:\r
         EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
 \r
     return True\r
@@ -504,7 +512,7 @@ def DataRestore(File):
     try:\r
         Fd = open(File, 'rb')\r
         Data = cPickle.load(Fd)\r
-    except Exception, e:\r
+    except Exception as e:\r
         EdkLogger.verbose("Failed to load [%s]\n\t%s" % (File, str(e)))\r
         Data = None\r
     finally:\r
@@ -825,7 +833,7 @@ class TemplateString(object):
     def Append(self, AppendString, Dictionary=None):\r
         if Dictionary:\r
             SectionList = self._Parse(AppendString)\r
-            self.String += "".join([S.Instantiate(Dictionary) for S in SectionList])\r
+            self.String += "".join(S.Instantiate(Dictionary) for S in SectionList)\r
         else:\r
             self.String += AppendString\r
 \r
@@ -836,7 +844,7 @@ class TemplateString(object):
     #   @retval     str             The string replaced with placeholder values\r
     #\r
     def Replace(self, Dictionary=None):\r
-        return "".join([S.Instantiate(Dictionary) for S in self._TemplateSectionList])\r
+        return "".join(S.Instantiate(Dictionary) for S in self._TemplateSectionList)\r
 \r
 ## Progress indicator class\r
 #\r
@@ -1211,7 +1219,7 @@ class tdict:
 \r
 def IsFieldValueAnArray (Value):\r
     Value = Value.strip()\r
-    if Value.startswith('GUID') and Value.endswith(')'):\r
+    if Value.startswith(TAB_GUID) and Value.endswith(')'):\r
         return True\r
     if Value.startswith('L"') and Value.endswith('"')  and len(list(Value[2:-1])) > 1:\r
         return True\r
@@ -1270,7 +1278,7 @@ def ParseDevPathValue (Value):
     try:\r
         p = subprocess.Popen(Cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
         out, err = p.communicate()\r
-    except Exception, X:\r
+    except Exception as X:\r
         raise BadExpression("DevicePath: %s" % (str(X)) )\r
     finally:\r
         subprocess._cleanup()\r
@@ -1285,30 +1293,30 @@ def ParseDevPathValue (Value):
 def ParseFieldValue (Value):\r
     if type(Value) == type(0):\r
         return Value, (Value.bit_length() + 7) / 8\r
-    if type(Value) <> type(''):\r
+    if type(Value) != type(''):\r
         raise BadExpression('Type %s is %s' %(Value, type(Value)))\r
     Value = Value.strip()\r
-    if Value.startswith('UINT8') and Value.endswith(')'):\r
+    if Value.startswith(TAB_UINT8) and Value.endswith(')'):\r
         Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
         if Size > 1:\r
             raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))\r
         return Value, 1\r
-    if Value.startswith('UINT16') and Value.endswith(')'):\r
+    if Value.startswith(TAB_UINT16) and Value.endswith(')'):\r
         Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
         if Size > 2:\r
             raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))\r
         return Value, 2\r
-    if Value.startswith('UINT32') and Value.endswith(')'):\r
+    if Value.startswith(TAB_UINT32) and Value.endswith(')'):\r
         Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
         if Size > 4:\r
             raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))\r
         return Value, 4\r
-    if Value.startswith('UINT64') and Value.endswith(')'):\r
+    if Value.startswith(TAB_UINT64) and Value.endswith(')'):\r
         Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
         if Size > 8:\r
             raise BadExpression('Value (%s) Size larger than %d' % (Value, Size))\r
         return Value, 8\r
-    if Value.startswith('GUID') and Value.endswith(')'):\r
+    if Value.startswith(TAB_GUID) and Value.endswith(')'):\r
         Value = Value.split('(', 1)[1][:-1].strip()\r
         if Value[0] == '{' and Value[-1] == '}':\r
             TmpValue = GuidStructureStringToGuidString(Value)\r
@@ -1319,8 +1327,8 @@ def ParseFieldValue (Value):
             Value = Value[1:-1]\r
         try:\r
             Value = "'" + uuid.UUID(Value).get_bytes_le() + "'"\r
-        except ValueError, Message:\r
-            raise BadExpression('%s' % Message)\r
+        except ValueError as Message:\r
+            raise BadExpression(Message)\r
         Value, Size = ParseFieldValue(Value)\r
         return Value, 16\r
     if Value.startswith('L"') and Value.endswith('"'):\r
@@ -1490,7 +1498,7 @@ def AnalyzeDscPcd(Setting, PcdType, DataType=''):
     elif PcdType in (MODEL_PCD_DYNAMIC_VPD, MODEL_PCD_DYNAMIC_EX_VPD):\r
         VpdOffset = FieldList[0]\r
         Value = Size = ''\r
-        if not DataType == 'VOID*':\r
+        if not DataType == TAB_VOID:\r
             if len(FieldList) > 1:\r
                 Value = FieldList[1]\r
         else:\r
@@ -1558,7 +1566,7 @@ def AnalyzePcdData(Setting):
 # For PCD value setting\r
 #\r
 def CheckPcdDatum(Type, Value):\r
-    if Type == "VOID*":\r
+    if Type == TAB_VOID:\r
         ValueRe = re.compile(r'\s*L?\".*\"\s*$')\r
         if not (((Value.startswith('L"') or Value.startswith('"')) and Value.endswith('"'))\r
                 or (Value.startswith('{') and Value.endswith('}')) or (Value.startswith("L'") or Value.startswith("'") and Value.endswith("'"))\r
@@ -1918,7 +1926,7 @@ class DefaultStore():
         if not self.DefaultStores or "0" in self.DefaultStores:\r
             return "0",TAB_DEFAULT_STORES_DEFAULT\r
         else:\r
-            minvalue = min([int(value_str) for value_str in self.DefaultStores.keys()])\r
+            minvalue = min(int(value_str) for value_str in self.DefaultStores)\r
             return (str(minvalue), self.DefaultStores[str(minvalue)])\r
     def GetMin(self,DefaultSIdList):\r
         if not DefaultSIdList:\r
@@ -2015,7 +2023,7 @@ class SkuClass():
             skuorderset.append(self.GetSkuChain(skuname))\r
         \r
         skuorder = []\r
-        for index in range(max([len(item) for item in skuorderset])):\r
+        for index in range(max(len(item) for item in skuorderset)):\r
             for subset in skuorderset:\r
                 if index > len(subset)-1:\r
                     continue\r
@@ -2079,20 +2087,7 @@ class SkuClass():
 # Pack a registry format GUID\r
 #\r
 def PackRegistryFormatGuid(Guid):\r
-    Guid = Guid.split('-')\r
-    return pack('=LHHBBBBBBBB',\r
-                int(Guid[0], 16),\r
-                int(Guid[1], 16),\r
-                int(Guid[2], 16),\r
-                int(Guid[3][-4:-2], 16),\r
-                int(Guid[3][-2:], 16),\r
-                int(Guid[4][-12:-10], 16),\r
-                int(Guid[4][-10:-8], 16),\r
-                int(Guid[4][-8:-6], 16),\r
-                int(Guid[4][-6:-4], 16),\r
-                int(Guid[4][-4:-2], 16),\r
-                int(Guid[4][-2:], 16)\r
-                )\r
+    return PackGUID(Guid.split('-'))\r
 \r
 ##  Get the integer value from string like "14U" or integer like 2\r
 #\r
@@ -2118,6 +2113,42 @@ def GetIntegerValue(Input):
     else:\r
         return int(String)\r
 \r
+#\r
+# Pack a GUID (registry format) list into a buffer and return it\r
+#\r
+def PackGUID(Guid):\r
+    return pack(PACK_PATTERN_GUID,\r
+                int(Guid[0], 16),\r
+                int(Guid[1], 16),\r
+                int(Guid[2], 16),\r
+                int(Guid[3][-4:-2], 16),\r
+                int(Guid[3][-2:], 16),\r
+                int(Guid[4][-12:-10], 16),\r
+                int(Guid[4][-10:-8], 16),\r
+                int(Guid[4][-8:-6], 16),\r
+                int(Guid[4][-6:-4], 16),\r
+                int(Guid[4][-4:-2], 16),\r
+                int(Guid[4][-2:], 16)\r
+                )\r
+\r
+#\r
+# Pack a GUID (byte) list into a buffer and return it\r
+#\r
+def PackByteFormatGUID(Guid):\r
+    return pack(PACK_PATTERN_GUID,\r
+                Guid[0],\r
+                Guid[1],\r
+                Guid[2],\r
+                Guid[3],\r
+                Guid[4],\r
+                Guid[5],\r
+                Guid[6],\r
+                Guid[7],\r
+                Guid[8],\r
+                Guid[9],\r
+                Guid[10],\r
+                )\r
+\r
 ##\r
 #\r
 # This acts like the main() function for the script, unless it is 'import'ed into another\r