]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Trim/Trim.py
BaseTools:Trim will trig exception when input asl UTF8 format file
[mirror_edk2.git] / BaseTools / Source / Python / Trim / Trim.py
index 228779b5a9a16f22cbad12e4c8770f0dc6569d29..6f93538a22ea094ad00cfca6e190186f840e549b 100644 (file)
@@ -18,7 +18,7 @@ import Common.LongFilePathOs as os
 import sys\r
 import re\r
 from io import BytesIO\r
-\r
+import codecs\r
 from optparse import OptionParser\r
 from optparse import make_option\r
 from Common.BuildToolError import *\r
@@ -77,14 +77,11 @@ gIncludedAslFile = []
 def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):\r
     CreateDirectory(os.path.dirname(Target))\r
     try:\r
-        f = open (Source, 'r')\r
+        with open(Source, "r") as File:\r
+            Lines = File.readlines()\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
 \r
-    # read whole file\r
-    Lines = f.readlines()\r
-    f.close()\r
-\r
     PreprocessedFile = ""\r
     InjectedFile = ""\r
     LineIndexOfOriginalFile = None\r
@@ -181,11 +178,10 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):
 \r
     # save to file\r
     try:\r
-        f = open (Target, 'w')\r
+        with open(Target, 'w') as File:\r
+            File.writelines(NewLines)\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
-    f.writelines(NewLines)\r
-    f.close()\r
 \r
 ## Trim preprocessed VFR file\r
 #\r
@@ -199,12 +195,11 @@ def TrimPreprocessedVfr(Source, Target):
     CreateDirectory(os.path.dirname(Target))\r
 \r
     try:\r
-        f = open (Source, 'r')\r
+        with open(Source, "r") as File:\r
+            Lines = File.readlines()\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
     # read whole file\r
-    Lines = f.readlines()\r
-    f.close()\r
 \r
     FoundTypedef = False\r
     Brace = 0\r
@@ -248,11 +243,10 @@ def TrimPreprocessedVfr(Source, Target):
 \r
     # save all lines trimmed\r
     try:\r
-        f = open (Target, 'w')\r
+        with open(Target, 'w') as File:\r
+            File.writelines(Lines)\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
-    f.writelines(Lines)\r
-    f.close()\r
 \r
 ## Read the content  ASL file, including ASL included, recursively\r
 #\r
@@ -278,7 +272,12 @@ def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):
         for IncludePath in SearchPathList:\r
             IncludeFile = os.path.join(IncludePath, Source)\r
             if os.path.isfile(IncludeFile):\r
-                F = open(IncludeFile, "r")\r
+                try:\r
+                    with open(IncludeFile, "r") as File:\r
+                        F = File.readlines()\r
+                except:\r
+                    with codecs.open(IncludeFile, "r", encoding='utf-8') as File:\r
+                        F = File.readlines()\r
                 break\r
         else:\r
             EdkLogger.error("Trim", "Failed to find include file %s" % Source)\r
@@ -313,7 +312,6 @@ def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):
         NewFileContent.append("\n")\r
 \r
     gIncludedAslFile.pop()\r
-    F.close()\r
 \r
     return NewFileContent\r
 \r
@@ -345,7 +343,9 @@ def TrimAslFile(Source, Target, IncludePathFile):
     if IncludePathFile:\r
         try:\r
             LineNum = 0\r
-            for Line in open(IncludePathFile, 'r'):\r
+            with open(IncludePathFile, 'r') as File:\r
+                FileLines = File.readlines()\r
+            for Line in FileLines:\r
                 LineNum += 1\r
                 if Line.startswith("/I") or Line.startswith ("-I"):\r
                     IncludePathList.append(Line[2:].strip())\r
@@ -363,13 +363,11 @@ def TrimAslFile(Source, Target, IncludePathFile):
 \r
     # save all lines trimmed\r
     try:\r
-        f = open (Target, 'w')\r
+        with open(Target, 'w') as File:\r
+            File.writelines(Lines)\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
 \r
-    f.writelines(Lines)\r
-    f.close()\r
-\r
 def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):\r
     VfrNameList = []\r
     if os.path.isdir(DebugDir):\r
@@ -389,7 +387,7 @@ def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):
         return\r
 \r
     try:\r
-        fInputfile = open(OutputFile, "wb+", 0)\r
+        fInputfile = open(OutputFile, "wb+")\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None)\r
 \r