BaseTools/UPT: Porting UPT Tool from Python2 to Python3

Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yonghong Zhu <yonghong.zhu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
Yonghong Zhu
2018-09-04 10:39:47 +08:00
parent 9d7e1e56a7
commit 1b2e077260
25 changed files with 89 additions and 103 deletions

View File

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