import Common.LongFilePathOs as os\r
import sys\r
import re\r
-import StringIO\r
+from io import BytesIO\r
\r
from optparse import OptionParser\r
from optparse import make_option\r
# Version and Copyright\r
__version_number__ = ("0.10" + " " + gBUILD_VERSION)\r
__version__ = "%prog Version " + __version_number__\r
-__copyright__ = "Copyright (c) 2007-2017, Intel Corporation. All rights reserved."\r
+__copyright__ = "Copyright (c) 2007-2018, Intel Corporation. All rights reserved."\r
\r
## Regular expression for matching Line Control directive like "#line xxx"\r
gLineControlDirective = re.compile('^\s*#(?:line)?\s+([0-9]+)\s+"*([^"]*)"')\r
if len(MatchList) == 2:\r
LineNumber = int(MatchList[0], 0)\r
InjectedFile = MatchList[1]\r
- # The first injetcted file must be the preprocessed file itself\r
+ InjectedFile = os.path.normpath(InjectedFile)\r
+ InjectedFile = os.path.normcase(InjectedFile)\r
+ # The first injected file must be the preprocessed file itself\r
if PreprocessedFile == "":\r
PreprocessedFile = InjectedFile\r
LineControlDirectiveFound = True\r
else:\r
if LineNumber > (len(NewLines) + 1):\r
for LineIndex in range(len(NewLines), LineNumber-1):\r
- NewLines.append(os.linesep)\r
+ NewLines.append(TAB_LINE_BREAK)\r
NewLines.append(Line)\r
LineNumber = None\r
EdkLogger.verbose("Now we have lines: %d" % len(NewLines))\r
\r
# save to file\r
try:\r
- f = open (Target, 'wb')\r
+ f = open (Target, 'w')\r
except:\r
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
f.writelines(NewLines)\r
#\r
def TrimPreprocessedVfr(Source, Target):\r
CreateDirectory(os.path.dirname(Target))\r
- \r
+\r
try:\r
- f = open (Source,'r')\r
+ f = open (Source, 'r')\r
except:\r
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
# read whole file\r
\r
# save all lines trimmed\r
try:\r
- f = open (Target,'w')\r
+ f = open (Target, 'w')\r
except:\r
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
f.writelines(Lines)\r
SearchPathList = [LocalSearchPath] + IncludePathList\r
else:\r
SearchPathList = IncludePathList\r
- \r
+\r
for IncludePath in SearchPathList:\r
IncludeFile = os.path.join(IncludePath, Source)\r
if os.path.isfile(IncludeFile):\r
except:\r
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
\r
- \r
+\r
# avoid A "include" B and B "include" A\r
IncludeFile = os.path.abspath(os.path.normpath(IncludeFile))\r
if IncludeFile in gIncludedAslFile:\r
ExtraData= "%s -> %s" % (" -> ".join(gIncludedAslFile), IncludeFile))\r
return []\r
gIncludedAslFile.append(IncludeFile)\r
- \r
+\r
for Line in F:\r
LocalSearchPath = None\r
Result = gAslIncludePattern.findall(Line)\r
NewFileContent.append("%s%s" % (Indent, Line))\r
continue\r
#\r
- # We should first search the local directory if current file are using pattern #include "XXX" \r
+ # We should first search the local directory if current file are using pattern #include "XXX"\r
#\r
if Result[0][2] == '"':\r
LocalSearchPath = os.path.dirname(IncludeFile)\r
#\r
# @param Source File to be trimmed\r
# @param Target File to store the trimmed content\r
-# @param IncludePathFile The file to log the external include path \r
+# @param IncludePathFile The file to log the external include path\r
#\r
def TrimAslFile(Source, Target, IncludePathFile):\r
CreateDirectory(os.path.dirname(Target))\r
- \r
+\r
SourceDir = os.path.dirname(Source)\r
if SourceDir == '':\r
SourceDir = '.'\r
- \r
+\r
#\r
# Add source directory as the first search directory\r
#\r
IncludePathList = [SourceDir]\r
- \r
+\r
#\r
# If additional include path file is specified, append them all\r
# to the search directory list.\r
if IncludePathFile:\r
try:\r
LineNum = 0\r
- for Line in open(IncludePathFile,'r'):\r
+ for Line in open(IncludePathFile, 'r'):\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
+ f = open (Target, 'w')\r
except:\r
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
\r
except:\r
EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None)\r
\r
- # Use a instance of StringIO to cache data\r
- fStringIO = StringIO.StringIO('')\r
+ # Use a instance of BytesIO to cache data\r
+ fStringIO = BytesIO()\r
\r
for Item in VfrUniOffsetList:\r
if (Item[0].find("Strings") != -1):\r
# GUID + Offset\r
# { 0x8913c5e0, 0x33f6, 0x4d86, { 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66 } }\r
#\r
- UniGuid = [0xe0, 0xc5, 0x13, 0x89, 0xf6, 0x33, 0x86, 0x4d, 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66]\r
- UniGuid = [chr(ItemGuid) for ItemGuid in UniGuid]\r
- fStringIO.write(''.join(UniGuid))\r
+ UniGuid = b'\xe0\xc5\x13\x89\xf63\x86M\x9b\xf1C\xef\x89\xfc\x06f'\r
+ fStringIO.write(UniGuid)\r
UniValue = pack ('Q', int (Item[1], 16))\r
fStringIO.write (UniValue)\r
else:\r
# GUID + Offset\r
# { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } };\r
#\r
- VfrGuid = [0xb4, 0x7c, 0xbc, 0xd0, 0x47, 0x6a, 0x5f, 0x49, 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2]\r
- VfrGuid = [chr(ItemGuid) for ItemGuid in VfrGuid]\r
- fStringIO.write(''.join(VfrGuid))\r
+ VfrGuid = b'\xb4|\xbc\xd0Gj_I\xaa\x11q\x07F\xda\x06\xa2'\r
+ fStringIO.write(VfrGuid)\r
type (Item[1])\r
VfrValue = pack ('Q', int (Item[1], 16))\r
fStringIO.write (VfrValue)\r
CreateDirectory(os.path.dirname(Target))\r
\r
try:\r
- f = open (Source,'rb')\r
+ f = open (Source, 'r')\r
except:\r
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
# read whole file\r
f.close()\r
\r
NewLines = None\r
- for Re,Repl in gImportCodePatterns:\r
+ for Re, Repl in gImportCodePatterns:\r
if NewLines is None:\r
NewLines = Re.sub(Repl, Lines)\r
else:\r
return\r
\r
try:\r
- f = open (Target,'wb')\r
+ f = open (Target, 'w')\r
except:\r
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
f.write(NewLines)\r
EdkLogger.SetLevel(CommandOptions.LogLevel + 1)\r
else:\r
EdkLogger.SetLevel(CommandOptions.LogLevel)\r
- except FatalError, X:\r
+ except FatalError as X:\r
return 1\r
- \r
+\r
try:\r
if CommandOptions.FileType == "Vfr":\r
if CommandOptions.OutputFile is None:\r
if CommandOptions.OutputFile is None:\r
CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
TrimPreprocessedFile(InputFile, CommandOptions.OutputFile, CommandOptions.ConvertHex, CommandOptions.TrimLong)\r
- except FatalError, X:\r
+ except FatalError as X:\r
import platform\r
import traceback\r
if CommandOptions is not None and CommandOptions.LogLevel <= EdkLogger.DEBUG_9:\r