BaseTools: Handle the bytes and str difference

Deal with bytes and str is different, remove the unicode(),
correct open file parameter.
Using utcfromtimestamp instead of fromtimestamp.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Zhiju.Fan <zhijux.fan@intel.com>
Tested-by: Laszlo Ersek <lersek@redhat.com>
Tested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
This commit is contained in:
Feng, Bob C
2019-01-23 10:16:00 +08:00
parent f8d11e5a4a
commit d943b0c339
37 changed files with 247 additions and 244 deletions

View File

@@ -15,7 +15,6 @@ from __future__ import absolute_import
import os
from . import LongFilePathOsPath
from Common.LongFilePathSupport import LongFilePath
from Common.LongFilePathSupport import UniToStr
import time
path = LongFilePathOsPath
@@ -64,7 +63,7 @@ def listdir(path):
List = []
uList = os.listdir(u"%s" % LongFilePath(path))
for Item in uList:
List.append(UniToStr(Item))
List.append(Item)
return List
environ = os.environ

View File

@@ -49,15 +49,3 @@ def CopyLongFilePath(src, dst):
with open(LongFilePath(src), 'rb') as fsrc:
with open(LongFilePath(dst), 'wb') as fdst:
shutil.copyfileobj(fsrc, fdst)
## Convert a python unicode string to a normal string
#
# Convert a python unicode string to a normal string
# UniToStr(u'I am a string') is 'I am a string'
#
# @param Uni: The python unicode string
#
# @retval: The formatted normal string
#
def UniToStr(Uni):
return repr(Uni)[2:-1]

View File

@@ -456,15 +456,22 @@ def RemoveDirectory(Directory, Recursively=False):
# @retval False If the file content is the same
#
def SaveFileOnChange(File, Content, IsBinaryFile=True):
if not IsBinaryFile:
Content = Content.replace("\n", os.linesep)
if os.path.exists(File):
try:
if Content == open(File, "rb").read():
return False
except:
EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File)
if IsBinaryFile:
try:
with open(File, "rb") as f:
if Content == f.read():
return False
except:
EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File)
else:
try:
with open(File, "r") as f:
if Content == f.read():
return False
except:
EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File)
DirName = os.path.dirname(File)
if not CreateDirectory(DirName):
@@ -475,12 +482,18 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
if not os.access(DirName, os.W_OK):
EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write permission on directory %s" % DirName)
try:
Fd = open(File, "wb")
Fd.write(Content)
Fd.close()
except IOError as X:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)
if IsBinaryFile:
try:
with open(File, "wb") as Fd:
Fd.write(Content)
except IOError as X:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)
else:
try:
with open(File, 'w') as Fd:
Fd.write(Content)
except IOError as X:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)
return True
@@ -1060,7 +1073,10 @@ def ParseFieldValue (Value):
if Value[0] == '"' and Value[-1] == '"':
Value = Value[1:-1]
try:
Value = "'" + uuid.UUID(Value).bytes_le + "'"
Value = str(uuid.UUID(Value).bytes_le)
if Value.startswith("b'"):
Value = Value[2:-1]
Value = "'" + Value + "'"
except ValueError as Message:
raise BadExpression(Message)
Value, Size = ParseFieldValue(Value)
@@ -1536,7 +1552,7 @@ class PeImageClass():
ByteArray = array.array('B')
ByteArray.fromfile(PeObject, 4)
# PE signature should be 'PE\0\0'
if ByteArray.tostring() != 'PE\0\0':
if ByteArray.tostring() != b'PE\0\0':
self.ErrorInfo = self.FileName + ' has no valid PE signature PE00'
return
@@ -1752,7 +1768,7 @@ class SkuClass():
# @retval Value The integer value that the input represents
#
def GetIntegerValue(Input):
if type(Input) in (int, long):
if not isinstance(Input, str):
return Input
String = Input
if String.endswith("U"):

View File

@@ -816,11 +816,7 @@ def GetHelpTextList(HelpTextClassList):
return List
def StringToArray(String):
if isinstance(String, unicode):
if len(unicode) == 0:
return "{0x00,0x00}"
return "{%s,0x00,0x00}" % ",".join("0x%02x,0x00" % ord(C) for C in String)
elif String.startswith('L"'):
if String.startswith('L"'):
if String == "L\"\"":
return "{0x00,0x00}"
else:
@@ -843,9 +839,7 @@ def StringToArray(String):
return '{%s,0,0}' % ','.join(String.split())
def StringArrayLength(String):
if isinstance(String, unicode):
return (len(String) + 1) * 2 + 1;
elif String.startswith('L"'):
if String.startswith('L"'):
return (len(String) - 3 + 1) * 2
elif String.startswith('"'):
return (len(String) - 2 + 1)

View File

@@ -92,18 +92,18 @@ class VpdInfoFile:
if (Vpd is None):
EdkLogger.error("VpdInfoFile", BuildToolError.ATTRIBUTE_UNKNOWN_ERROR, "Invalid VPD PCD entry.")
if not (Offset >= 0 or Offset == TAB_STAR):
if not (Offset >= "0" or Offset == TAB_STAR):
EdkLogger.error("VpdInfoFile", BuildToolError.PARAMETER_INVALID, "Invalid offset parameter: %s." % Offset)
if Vpd.DatumType == TAB_VOID:
if Vpd.MaxDatumSize <= 0:
if Vpd.MaxDatumSize <= "0":
EdkLogger.error("VpdInfoFile", BuildToolError.PARAMETER_INVALID,
"Invalid max datum size for VPD PCD %s.%s" % (Vpd.TokenSpaceGuidCName, Vpd.TokenCName))
elif Vpd.DatumType in TAB_PCD_NUMERIC_TYPES:
if not Vpd.MaxDatumSize:
Vpd.MaxDatumSize = MAX_SIZE_TYPE[Vpd.DatumType]
else:
if Vpd.MaxDatumSize <= 0:
if Vpd.MaxDatumSize <= "0":
EdkLogger.error("VpdInfoFile", BuildToolError.PARAMETER_INVALID,
"Invalid max datum size for VPD PCD %s.%s" % (Vpd.TokenSpaceGuidCName, Vpd.TokenCName))
@@ -127,7 +127,7 @@ class VpdInfoFile:
"Invalid parameter FilePath: %s." % FilePath)
Content = FILE_COMMENT_TEMPLATE
Pcds = sorted(self._VpdArray.keys())
Pcds = sorted(self._VpdArray.keys(), key=lambda x: x.TokenCName)
for Pcd in Pcds:
i = 0
PcdTokenCName = Pcd.TokenCName
@@ -249,7 +249,7 @@ def CallExtenalBPDGTool(ToolPath, VpdFileName):
except Exception as X:
EdkLogger.error("BPDG", BuildToolError.COMMAND_FAILURE, ExtraData=str(X))
(out, error) = PopenObject.communicate()
print(out)
print(out.decode(encoding='utf-8', errors='ignore'))
while PopenObject.returncode is None :
PopenObject.wait()