]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/Misc.py
BaseTools: Fix private includes for FILE_GUID override
[mirror_edk2.git] / BaseTools / Source / Python / Common / Misc.py
index 01bd62a9e2df45dd44ae70e208a6092adb95d0ab..d082c58befda5b46aa3f40113c96f1566ded60fd 100644 (file)
@@ -2,13 +2,7 @@
 # Common routines used by all tools\r
 #\r
 # Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution.  The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-#\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 \r
 ##\r
@@ -28,6 +22,7 @@ from random import sample
 from struct import pack\r
 import uuid\r
 import subprocess\r
+import tempfile\r
 from collections import OrderedDict\r
 \r
 import Common.LongFilePathOs as os\r
@@ -42,6 +37,7 @@ from Common.MultipleWorkspace import MultipleWorkspace as mws
 from CommonDataClass.Exceptions import BadExpression\r
 from Common.caching import cached_property\r
 \r
+ArrayIndex = re.compile("\[\s*[0-9a-fA-FxX]*\s*\]")\r
 ## Regular expression used to find out place holders in string template\r
 gPlaceholderPattern = re.compile("\$\{([^$()\s]+)\}", re.MULTILINE | re.UNICODE)\r
 \r
@@ -285,6 +281,7 @@ def ProcessDuplicatedInf(Path, BaseName, Workspace):
     #\r
     RtPath.Path = TempFullPath\r
     RtPath.BaseName = BaseName\r
+    RtPath.OriginalPath = Path\r
     #\r
     # If file exists, compare contents\r
     #\r
@@ -456,15 +453,22 @@ def RemoveDirectory(Directory, Recursively=False):
 #   @retval     False           If the file content is the same\r
 #\r
 def SaveFileOnChange(File, Content, IsBinaryFile=True):\r
-    if not IsBinaryFile:\r
-        Content = Content.replace("\n", os.linesep)\r
 \r
     if os.path.exists(File):\r
-        try:\r
-            if Content == open(File, "rb").read():\r
-                return False\r
-        except:\r
-            EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File)\r
+        if IsBinaryFile:\r
+            try:\r
+                with open(File, "rb") as f:\r
+                    if Content == f.read():\r
+                        return False\r
+            except:\r
+                EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File)\r
+        else:\r
+            try:\r
+                with open(File, "r") as f:\r
+                    if Content == f.read():\r
+                        return False\r
+            except:\r
+                EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File)\r
 \r
     DirName = os.path.dirname(File)\r
     if not CreateDirectory(DirName):\r
@@ -475,12 +479,26 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
         if not os.access(DirName, os.W_OK):\r
             EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write permission on directory %s" % DirName)\r
 \r
-    try:\r
-        Fd = open(File, "wb")\r
-        Fd.write(Content)\r
-        Fd.close()\r
-    except IOError as X:\r
-        EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
+    OpenMode = "w"\r
+    if IsBinaryFile:\r
+        OpenMode = "wb"\r
+\r
+    if GlobalData.gIsWindows and not os.path.exists(File):\r
+        # write temp file, then rename the temp file to the real file\r
+        # to make sure the file be immediate saved to disk\r
+        with tempfile.NamedTemporaryFile(OpenMode, dir=os.path.dirname(File), delete=False) as tf:\r
+            tf.write(Content)\r
+            tempname = tf.name\r
+        try:\r
+            os.rename(tempname, File)\r
+        except:\r
+            EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
+    else:\r
+        try:\r
+            with open(File, OpenMode) as Fd:\r
+                Fd.write(Content)\r
+        except IOError as X:\r
+            EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
 \r
     return True\r
 \r
@@ -574,13 +592,14 @@ def RealPath(File, Dir='', OverrideDir=''):
 #\r
 def GuidValue(CName, PackageList, Inffile = None):\r
     for P in PackageList:\r
-        GuidKeys = P.Guids.keys()\r
+        GuidKeys = list(P.Guids.keys())\r
         if Inffile and P._PrivateGuids:\r
             if not Inffile.startswith(P.MetaFile.Dir):\r
                 GuidKeys = [x for x in P.Guids if x not in P._PrivateGuids]\r
         if CName in GuidKeys:\r
             return P.Guids[CName]\r
     return None\r
+    return None\r
 \r
 ## A string template class\r
 #\r
@@ -761,10 +780,10 @@ class Progressor:
 \r
     ## Constructor\r
     #\r
-    #   @param      OpenMessage     The string printed before progress charaters\r
-    #   @param      CloseMessage    The string printed after progress charaters\r
-    #   @param      ProgressChar    The charater used to indicate the progress\r
-    #   @param      Interval        The interval in seconds between two progress charaters\r
+    #   @param      OpenMessage     The string printed before progress characters\r
+    #   @param      CloseMessage    The string printed after progress characters\r
+    #   @param      ProgressChar    The character used to indicate the progress\r
+    #   @param      Interval        The interval in seconds between two progress characters\r
     #\r
     def __init__(self, OpenMessage="", CloseMessage="", ProgressChar='.', Interval=1.0):\r
         self.PromptMessage = OpenMessage\r
@@ -774,9 +793,9 @@ class Progressor:
         if Progressor._StopFlag is None:\r
             Progressor._StopFlag = threading.Event()\r
 \r
-    ## Start to print progress charater\r
+    ## Start to print progress character\r
     #\r
-    #   @param      OpenMessage     The string printed before progress charaters\r
+    #   @param      OpenMessage     The string printed before progress characters\r
     #\r
     def Start(self, OpenMessage=None):\r
         if OpenMessage is not None:\r
@@ -787,9 +806,9 @@ class Progressor:
             Progressor._ProgressThread.setDaemon(False)\r
             Progressor._ProgressThread.start()\r
 \r
-    ## Stop printing progress charater\r
+    ## Stop printing progress character\r
     #\r
-    #   @param      CloseMessage    The string printed after progress charaters\r
+    #   @param      CloseMessage    The string printed after progress characters\r
     #\r
     def Stop(self, CloseMessage=None):\r
         OriginalCodaMessage = self.CodaMessage\r
@@ -822,127 +841,6 @@ class Progressor:
             Progressor._ProgressThread.join()\r
             Progressor._ProgressThread = None\r
 \r
-## A dict which can access its keys and/or values orderly\r
-#\r
-#  The class implements a new kind of dict which its keys or values can be\r
-#  accessed in the order they are added into the dict. It guarantees the order\r
-#  by making use of an internal list to keep a copy of keys.\r
-#\r
-class sdict(dict):\r
-    ## Constructor\r
-    def __init__(self):\r
-        dict.__init__(self)\r
-        self._key_list = []\r
-\r
-    ## [] operator\r
-    def __setitem__(self, key, value):\r
-        if key not in self._key_list:\r
-            self._key_list.append(key)\r
-        dict.__setitem__(self, key, value)\r
-\r
-    ## del operator\r
-    def __delitem__(self, key):\r
-        self._key_list.remove(key)\r
-        dict.__delitem__(self, key)\r
-\r
-    ## used in "for k in dict" loop to ensure the correct order\r
-    def __iter__(self):\r
-        return self.iterkeys()\r
-\r
-    ## len() support\r
-    def __len__(self):\r
-        return len(self._key_list)\r
-\r
-    ## "in" test support\r
-    def __contains__(self, key):\r
-        return key in self._key_list\r
-\r
-    ## indexof support\r
-    def index(self, key):\r
-        return self._key_list.index(key)\r
-\r
-    ## insert support\r
-    def insert(self, key, newkey, newvalue, order):\r
-        index = self._key_list.index(key)\r
-        if order == 'BEFORE':\r
-            self._key_list.insert(index, newkey)\r
-            dict.__setitem__(self, newkey, newvalue)\r
-        elif order == 'AFTER':\r
-            self._key_list.insert(index + 1, newkey)\r
-            dict.__setitem__(self, newkey, newvalue)\r
-\r
-    ## append support\r
-    def append(self, sdict):\r
-        for key in sdict:\r
-            if key not in self._key_list:\r
-                self._key_list.append(key)\r
-            dict.__setitem__(self, key, sdict[key])\r
-\r
-    def has_key(self, key):\r
-        return key in self._key_list\r
-\r
-    ## Empty the dict\r
-    def clear(self):\r
-        self._key_list = []\r
-        dict.clear(self)\r
-\r
-    ## Return a copy of keys\r
-    def keys(self):\r
-        keys = []\r
-        for key in self._key_list:\r
-            keys.append(key)\r
-        return keys\r
-\r
-    ## Return a copy of values\r
-    def values(self):\r
-        values = []\r
-        for key in self._key_list:\r
-            values.append(self[key])\r
-        return values\r
-\r
-    ## Return a copy of (key, value) list\r
-    def items(self):\r
-        items = []\r
-        for key in self._key_list:\r
-            items.append((key, self[key]))\r
-        return items\r
-\r
-    ## Iteration support\r
-    def iteritems(self):\r
-        return iter(self.items())\r
-\r
-    ## Keys interation support\r
-    def iterkeys(self):\r
-        return iter(self.keys())\r
-\r
-    ## Values interation support\r
-    def itervalues(self):\r
-        return iter(self.values())\r
-\r
-    ## Return value related to a key, and remove the (key, value) from the dict\r
-    def pop(self, key, *dv):\r
-        value = None\r
-        if key in self._key_list:\r
-            value = self[key]\r
-            self.__delitem__(key)\r
-        elif len(dv) != 0 :\r
-            value = kv[0]\r
-        return value\r
-\r
-    ## Return (key, value) pair, and remove the (key, value) from the dict\r
-    def popitem(self):\r
-        key = self._key_list[-1]\r
-        value = self[key]\r
-        self.__delitem__(key)\r
-        return key, value\r
-\r
-    def update(self, dict=None, **kwargs):\r
-        if dict is not None:\r
-            for k, v in dict.items():\r
-                self[k] = v\r
-        if len(kwargs):\r
-            for k, v in kwargs.items():\r
-                self[k] = v\r
 \r
 ## Dictionary using prioritized list as key\r
 #\r
@@ -1139,6 +1037,7 @@ def ParseFieldValue (Value):
             p.stderr.close()\r
         if err:\r
             raise BadExpression("DevicePath: %s" % str(err))\r
+        out = out.decode(encoding='utf-8', errors='ignore')\r
         Size = len(out.split())\r
         out = ','.join(out.split())\r
         return '{' + out + '}', Size\r
@@ -1146,7 +1045,7 @@ def ParseFieldValue (Value):
     if "{CODE(" in Value:\r
         return Value, len(Value.split(","))\r
     if isinstance(Value, type(0)):\r
-        return Value, (Value.bit_length() + 7) / 8\r
+        return Value, (Value.bit_length() + 7) // 8\r
     if not isinstance(Value, type('')):\r
         raise BadExpression('Type %s is %s' %(Value, type(Value)))\r
     Value = Value.strip()\r
@@ -1180,7 +1079,10 @@ def ParseFieldValue (Value):
         if Value[0] == '"' and Value[-1] == '"':\r
             Value = Value[1:-1]\r
         try:\r
-            Value = "'" + uuid.UUID(Value).get_bytes_le() + "'"\r
+            Value = str(uuid.UUID(Value).bytes_le)\r
+            if Value.startswith("b'"):\r
+                Value = Value[2:-1]\r
+            Value = "'" + Value + "'"\r
         except ValueError as Message:\r
             raise BadExpression(Message)\r
         Value, Size = ParseFieldValue(Value)\r
@@ -1267,12 +1169,12 @@ def ParseFieldValue (Value):
             raise BadExpression("invalid hex value: %s" % Value)\r
         if Value == 0:\r
             return 0, 1\r
-        return Value, (Value.bit_length() + 7) / 8\r
+        return Value, (Value.bit_length() + 7) // 8\r
     if Value[0].isdigit():\r
         Value = int(Value, 10)\r
         if Value == 0:\r
             return 0, 1\r
-        return Value, (Value.bit_length() + 7) / 8\r
+        return Value, (Value.bit_length() + 7) // 8\r
     if Value.lower() == 'true':\r
         return 1, 1\r
     if Value.lower() == 'false':\r
@@ -1431,10 +1333,12 @@ def CheckPcdDatum(Type, Value):
             return False, "Invalid value [%s] of type [%s]; must be one of TRUE, True, true, 0x1, 0x01, 1"\\r
                           ", FALSE, False, false, 0x0, 0x00, 0" % (Value, Type)\r
     elif Type in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64]:\r
-        if Value and int(Value, 0) < 0:\r
-            return False, "PCD can't be set to negative value[%s] for datum type [%s]" % (Value, Type)\r
+        if Value.startswith('0') and not Value.lower().startswith('0x') and len(Value) > 1 and Value.lstrip('0'):\r
+            Value = Value.lstrip('0')\r
         try:\r
-            Value = long(Value, 0)\r
+            if Value and int(Value, 0) < 0:\r
+                return False, "PCD can't be set to negative value[%s] for datum type [%s]" % (Value, Type)\r
+            Value = int(Value, 0)\r
             if Value > MAX_VAL_TYPE[Type]:\r
                 return False, "Too large PCD value[%s] for datum type [%s]" % (Value, Type)\r
         except:\r
@@ -1502,6 +1406,7 @@ class PathClass(object):
         self.TagName = TagName\r
         self.ToolCode = ToolCode\r
         self.ToolChainFamily = ToolChainFamily\r
+        self.OriginalPath = self\r
 \r
     ## Convert the object of this class to a string\r
     #\r
@@ -1524,7 +1429,7 @@ class PathClass(object):
 \r
     ## Override __cmp__ function\r
     #\r
-    # Customize the comparsion operation of two PathClass\r
+    # Customize the comparison operation of two PathClass\r
     #\r
     # @retval 0     The two PathClass are different\r
     # @retval -1    The first PathClass is less than the second PathClass\r
@@ -1619,7 +1524,7 @@ class PathClass(object):
             self.Path = os.path.join(RealRoot, RealFile)\r
         return ErrorCode, ErrorInfo\r
 \r
-## Parse PE image to get the required PE informaion.\r
+## Parse PE image to get the required PE information.\r
 #\r
 class PeImageClass():\r
     ## Constructor\r
@@ -1654,7 +1559,7 @@ class PeImageClass():
         ByteArray = array.array('B')\r
         ByteArray.fromfile(PeObject, 4)\r
         # PE signature should be 'PE\0\0'\r
-        if ByteArray.tostring() != 'PE\0\0':\r
+        if ByteArray.tostring() != b'PE\0\0':\r
             self.ErrorInfo = self.FileName + ' has no valid PE signature PE00'\r
             return\r
 \r
@@ -1746,7 +1651,7 @@ class SkuClass():
                             ExtraData = "SKU-ID [%s] value %s exceeds the max value of UINT64"\r
                                       % (SkuName, SkuId))\r
 \r
-        self.AvailableSkuIds = sdict()\r
+        self.AvailableSkuIds = OrderedDict()\r
         self.SkuIdSet = []\r
         self.SkuIdNumberSet = []\r
         self.SkuData = SkuIds\r
@@ -1756,7 +1661,7 @@ class SkuClass():
             self.SkuIdSet = ['DEFAULT']\r
             self.SkuIdNumberSet = ['0U']\r
         elif SkuIdentifier == 'ALL':\r
-            self.SkuIdSet = SkuIds.keys()\r
+            self.SkuIdSet = list(SkuIds.keys())\r
             self.SkuIdNumberSet = [num[0].strip() + 'U' for num in SkuIds.values()]\r
         else:\r
             r = SkuIdentifier.split('|')\r
@@ -1870,7 +1775,7 @@ class SkuClass():
 #   @retval     Value    The integer value that the input represents\r
 #\r
 def GetIntegerValue(Input):\r
-    if type(Input) in (int, long):\r
+    if not isinstance(Input, str):\r
         return Input\r
     String = Input\r
     if String.endswith("U"):\r