diff options
author | Yonghong Zhu <yonghong.zhu@intel.com> | 2018-09-04 10:39:47 +0800 |
---|---|---|
committer | Yonghong Zhu <yonghong.zhu@intel.com> | 2018-10-13 09:57:16 +0800 |
commit | 1b2e077260030290c512f3ac71a044ed1150899f (patch) | |
tree | 4834e04cda1ed358ce20dd24906af28665907e8c /BaseTools/Source/Python/UPT/Library/Misc.py | |
parent | 9d7e1e56a727743a811abfed92e1ede39ecbfa8c (diff) | |
download | edk2-1b2e077260030290c512f3ac71a044ed1150899f.tar.gz edk2-1b2e077260030290c512f3ac71a044ed1150899f.tar.bz2 edk2-1b2e077260030290c512f3ac71a044ed1150899f.zip |
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>
Diffstat (limited to 'BaseTools/Source/Python/UPT/Library/Misc.py')
-rw-r--r-- | BaseTools/Source/Python/UPT/Library/Misc.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/BaseTools/Source/Python/UPT/Library/Misc.py b/BaseTools/Source/Python/UPT/Library/Misc.py index 8c2a6787f0..f9ca8f32e0 100644 --- a/BaseTools/Source/Python/UPT/Library/Misc.py +++ b/BaseTools/Source/Python/UPT/Library/Misc.py @@ -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:
|