]> 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 c7daf5417cdada9b6113467bfc4f3180dbf5536d..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
@@ -482,15 +479,23 @@ 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
+    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
-            with open(File, "wb") as Fd:\r
-                Fd.write(Content)\r
-        except IOError as X:\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, 'w') as Fd:\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
@@ -1032,7 +1037,7 @@ def ParseFieldValue (Value):
             p.stderr.close()\r
         if err:\r
             raise BadExpression("DevicePath: %s" % str(err))\r
-        out = out.decode()\r
+        out = out.decode(encoding='utf-8', errors='ignore')\r
         Size = len(out.split())\r
         out = ','.join(out.split())\r
         return '{' + out + '}', Size\r
@@ -1401,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