]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/UPT/Library/Misc.py
BaseTools/UPT: Porting UPT Tool from Python2 to Python3
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Library / Misc.py
index 8c2a6787f0c16ef25cc13235e36e172c1b7b5cb7..f9ca8f32e068cdebce05f2c0e2b88daa07e61713 100644 (file)
@@ -32,7 +32,7 @@ from os import linesep
 from os import walk\r
 from os import environ\r
 import re\r
-from UserDict import IterableUserDict\r
+from collections import UserDict as IterableUserDict\r
 \r
 import Logger.Log as Logger\r
 from Logger import StringTable as ST\r
@@ -160,19 +160,23 @@ def RemoveDirectory(Directory, Recursively=False):
 #                              or not\r
 #\r
 def SaveFileOnChange(File, Content, IsBinaryFile=True):\r
-    if not IsBinaryFile:\r
-        Content = Content.replace("\n", linesep)\r
-\r
     if os.path.exists(File):\r
         try:\r
-            if Content == __FileHookOpen__(File, "rb").read():\r
-                return False\r
+            if isinstance(Content, bytes):\r
+                if Content == __FileHookOpen__(File, "rb").read():\r
+                    return False\r
+            else:\r
+                if Content == __FileHookOpen__(File, "r").read():\r
+                    return False\r
         except BaseException:\r
             Logger.Error(None, ToolError.FILE_OPEN_FAILURE, ExtraData=File)\r
 \r
     CreateDirectory(os.path.dirname(File))\r
     try:\r
-        FileFd = __FileHookOpen__(File, "wb")\r
+        if isinstance(Content, bytes):\r
+            FileFd = __FileHookOpen__(File, "wb")\r
+        else:\r
+            FileFd = __FileHookOpen__(File, "w")\r
         FileFd.write(Content)\r
         FileFd.close()\r
     except BaseException:\r
@@ -437,7 +441,7 @@ class Sdict(IterableUserDict):
 def CommonPath(PathList):\r
     Path1 = min(PathList).split(os.path.sep)\r
     Path2 = max(PathList).split(os.path.sep)\r
-    for Index in xrange(min(len(Path1), len(Path2))):\r
+    for Index in range(min(len(Path1), len(Path2))):\r
         if Path1[Index] != Path2[Index]:\r
             return os.path.sep.join(Path1[:Index])\r
     return os.path.sep.join(Path1)\r
@@ -890,7 +894,7 @@ def ProcessEdkComment(LineList):
             if FindEdkBlockComment:\r
                 if FirstPos == -1:\r
                     FirstPos = StartPos\r
-                for Index in xrange(StartPos, EndPos+1):\r
+                for Index in range(StartPos, EndPos+1):\r
                     LineList[Index] = ''\r
                 FindEdkBlockComment = False\r
         elif Line.find("//") != -1 and not Line.startswith("#"):\r
@@ -957,7 +961,7 @@ def GetLibInstanceInfo(String, WorkSpace, LineNo):
         FileLinesList = []\r
 \r
         try:\r
-            FInputfile = open(FullFileName, "rb", 0)\r
+            FInputfile = open(FullFileName, "r")\r
             try:\r
                 FileLinesList = FInputfile.readlines()\r
             except BaseException:\r