# 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
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
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
\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
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
\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
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
NewFileContent.append("\n")\r
\r
gIncludedAslFile.pop()\r
- F.close()\r
\r
return NewFileContent\r
\r
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
\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
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
"\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