]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Trim/Trim.py
BaseTools:Update mailing list address in BaseTools error messages
[mirror_edk2.git] / BaseTools / Source / Python / Trim / Trim.py
index 228779b5a9a16f22cbad12e4c8770f0dc6569d29..43119bd7ff8cf4d210f03623ba9ed42efbfcc000 100644 (file)
@@ -2,13 +2,7 @@
 # Trim files preprocessed by compiler\r
 #\r
 # Copyright (c) 2007 - 2018, 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
@@ -18,7 +12,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 +71,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 +172,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 +189,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 +237,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 +266,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 +306,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 +337,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 +357,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 +381,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
@@ -541,7 +533,7 @@ def Main():
                     "\nTrim",\r
                     CODE_ERROR,\r
                     "Unknown fatal error when trimming [%s]" % InputFile,\r
-                    ExtraData="\n(Please send email to edk2-devel@lists.01.org for help, attaching following call stack trace!)\n",\r
+                    ExtraData="\n(Please send email to %s for help, attaching following call stack trace!)\n" % MSG_EDKII_MAIL_ADDR,\r
                     RaiseError=False\r
                     )\r
         EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r