summaryrefslogtreecommitdiffstats
path: root/BaseTools
diff options
context:
space:
mode:
authorYunhua Feng <yunhuax.feng@intel.com>2018-07-27 16:02:05 +0800
committerYonghong Zhu <yonghong.zhu@intel.com>2018-10-13 09:50:45 +0800
commita09f4c91f785e36f0987aa3a6d7656ba51e6aeda (patch)
tree0a644e4b34f4fceb65d10465dbb6688fcddddfa5 /BaseTools
parentfe3991d63552f2eb25ff728a81a6976a3b1f652b (diff)
downloadedk2-a09f4c91f785e36f0987aa3a6d7656ba51e6aeda.tar.gz
edk2-a09f4c91f785e36f0987aa3a6d7656ba51e6aeda.tar.bz2
edk2-a09f4c91f785e36f0987aa3a6d7656ba51e6aeda.zip
BaseTools: fix the open file's read and write bugs
Cc: Liming Gao <liming.gao@intel.com> Cc: Yonghong Zhu <yonghong.zhu@intel.com> Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com> Reviewed-by: Liming Gao <liming.gao@intel.com>
Diffstat (limited to 'BaseTools')
-rw-r--r--BaseTools/Source/Python/AutoGen/GenMake.py12
-rw-r--r--BaseTools/Source/Python/AutoGen/InfSectionParser.py2
-rw-r--r--BaseTools/Source/Python/BPDG/GenVpd.py4
-rw-r--r--BaseTools/Source/Python/Common/Misc.py28
-rw-r--r--BaseTools/Source/Python/GenFds/FdfParser.py4
-rw-r--r--BaseTools/Source/Python/Trim/Trim.py6
-rw-r--r--BaseTools/Source/Python/build/BuildReport.py2
7 files changed, 38 insertions, 20 deletions
diff --git a/BaseTools/Source/Python/AutoGen/GenMake.py b/BaseTools/Source/Python/AutoGen/GenMake.py
index 75b4e2247a..b168a6663d 100644
--- a/BaseTools/Source/Python/AutoGen/GenMake.py
+++ b/BaseTools/Source/Python/AutoGen/GenMake.py
@@ -1031,7 +1031,7 @@ cleanlib:
CurrentFileDependencyList = DepDb[F]
else:
try:
- Fd = open(F.Path, 'r')
+ Fd = open(F.Path, 'rb')
except BaseException as X:
EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=F.Path + "\n\t" + str(X))
@@ -1041,8 +1041,14 @@ cleanlib:
continue
if FileContent[0] == 0xff or FileContent[0] == 0xfe:
- FileContent = unicode(FileContent, "utf-16")
- IncludedFileList = gIncludePattern.findall(FileContent)
+ FileContent = str(FileContent, encoding="utf-16")
+ IncludedFileList = gIncludePattern.findall(FileContent)
+ else:
+ try:
+ FileContent = str(FileContent, encoding="utf-8")
+ IncludedFileList = gIncludePattern.findall(FileContent)
+ except:
+ continue
for Inc in IncludedFileList:
Inc = Inc.strip()
diff --git a/BaseTools/Source/Python/AutoGen/InfSectionParser.py b/BaseTools/Source/Python/AutoGen/InfSectionParser.py
index d985089738..09e9af3fb4 100644
--- a/BaseTools/Source/Python/AutoGen/InfSectionParser.py
+++ b/BaseTools/Source/Python/AutoGen/InfSectionParser.py
@@ -34,7 +34,7 @@ class InfSectionParser():
SectionData = []
try:
- FileLinesList = open(self._FilePath, "r", 0).readlines()
+ FileLinesList = open(self._FilePath, "r").readlines()
except BaseException:
EdkLogger.error("build", AUTOGEN_ERROR, 'File %s is opened failed.' % self._FilePath)
diff --git a/BaseTools/Source/Python/BPDG/GenVpd.py b/BaseTools/Source/Python/BPDG/GenVpd.py
index 366fac095b..bd2c05d782 100644
--- a/BaseTools/Source/Python/BPDG/GenVpd.py
+++ b/BaseTools/Source/Python/BPDG/GenVpd.py
@@ -305,7 +305,7 @@ class GenVPD :
self.PcdFixedOffsetSizeList = []
self.PcdUnknownOffsetList = []
try:
- fInputfile = open(InputFileName, "r", 0)
+ fInputfile = open(InputFileName, "r")
try:
self.FileLinesList = fInputfile.readlines()
except:
@@ -650,7 +650,7 @@ class GenVPD :
EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.VpdFileName, None)
try :
- fMapFile = open(MapFileName, "w", 0)
+ fMapFile = open(MapFileName, "w")
except:
# Open failed
EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.MapFileName, None)
diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Python/Common/Misc.py
index b91f06b19e..1d62a8b56b 100644
--- a/BaseTools/Source/Python/Common/Misc.py
+++ b/BaseTools/Source/Python/Common/Misc.py
@@ -459,8 +459,14 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
if os.path.exists(File):
try:
- if Content == open(File, "rb").read():
- return False
+ if isinstance(Content, bytes):
+ with open(File, "rb") as f:
+ if Content == f.read():
+ return False
+ else:
+ with open(File, "r") as f:
+ if Content == f.read():
+ return False
except:
EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File)
@@ -480,13 +486,19 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
if not SaveFileToDisk(File, Content):
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData=File)
except:
- Fd = open(File, "wb")
- Fd.write(Content)
- Fd.close()
+ if isinstance(Content, bytes):
+ with open(File, "wb") as Fd:
+ Fd.write(Content)
+ else:
+ with open(File, "w") as Fd:
+ Fd.write(Content)
else:
- Fd = open(File, "wb")
- Fd.write(Content)
- Fd.close()
+ if isinstance(Content, bytes):
+ with open(File, "wb") as Fd:
+ Fd.write(Content)
+ else:
+ with open(File, "w") as Fd:
+ Fd.write(Content)
except IOError as X:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)
diff --git a/BaseTools/Source/Python/GenFds/FdfParser.py b/BaseTools/Source/Python/GenFds/FdfParser.py
index cbdc893cdf..ff8ccf21bd 100644
--- a/BaseTools/Source/Python/GenFds/FdfParser.py
+++ b/BaseTools/Source/Python/GenFds/FdfParser.py
@@ -155,7 +155,7 @@ class IncludeFileProfile :
self.FileName = FileName
self.FileLinesList = []
try:
- fsock = open(FileName, "rb", 0)
+ fsock = open(FileName, "r")
try:
self.FileLinesList = fsock.readlines()
for index, line in enumerate(self.FileLinesList):
@@ -216,7 +216,7 @@ class FileProfile :
def __init__(self, FileName):
self.FileLinesList = []
try:
- fsock = open(FileName, "rb", 0)
+ fsock = open(FileName, "r")
try:
self.FileLinesList = fsock.readlines()
finally:
diff --git a/BaseTools/Source/Python/Trim/Trim.py b/BaseTools/Source/Python/Trim/Trim.py
index 4b3091bec3..b9ca83d6d8 100644
--- a/BaseTools/Source/Python/Trim/Trim.py
+++ b/BaseTools/Source/Python/Trim/Trim.py
@@ -245,7 +245,7 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):
# save to file
try:
- f = open (Target, 'wb')
+ f = open (Target, 'w')
except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
f.writelines(NewLines)
@@ -562,7 +562,7 @@ def TrimEdkSourceCode(Source, Target):
CreateDirectory(os.path.dirname(Target))
try:
- f = open (Source, 'rb')
+ f = open (Source, 'r')
except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
# read whole file
@@ -581,7 +581,7 @@ def TrimEdkSourceCode(Source, Target):
return
try:
- f = open (Target, 'wb')
+ f = open (Target, 'w')
except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
f.write(NewLines)
diff --git a/BaseTools/Source/Python/build/BuildReport.py b/BaseTools/Source/Python/build/BuildReport.py
index 4fe29f124d..33cc9db735 100644
--- a/BaseTools/Source/Python/build/BuildReport.py
+++ b/BaseTools/Source/Python/build/BuildReport.py
@@ -634,7 +634,7 @@ class ModuleReport(object):
FwReportFileName = os.path.join(self._BuildDir, "DEBUG", self.ModuleName + ".txt")
if os.path.isfile(FwReportFileName):
try:
- FileContents = open(FwReportFileName).read()
+ FileContents = open(FwReportFileName, 'r').read()
Match = gModuleSizePattern.search(FileContents)
if Match:
self.Size = int(Match.group(1))