X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FTrim%2FTrim.py;h=b46d507b4e55e503712ecf0a205cb3d940f206b4;hp=d1e40b025caa0f3f0b86068caea5e626e2f10734;hb=5895956dd955714c0e578a413d0e289153cc9eea;hpb=111dab620362b2f28ce48c18b1e513819e32e37f diff --git a/BaseTools/Source/Python/Trim/Trim.py b/BaseTools/Source/Python/Trim/Trim.py index d1e40b025c..b46d507b4e 100644 --- a/BaseTools/Source/Python/Trim/Trim.py +++ b/BaseTools/Source/Python/Trim/Trim.py @@ -1,7 +1,7 @@ ## @file # Trim files preprocessed by compiler # -# Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.
+# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at @@ -17,12 +17,13 @@ import Common.LongFilePathOs as os import sys import re -import StringIO +from io import BytesIO from optparse import OptionParser from optparse import make_option from Common.BuildToolError import * from Common.Misc import * +from Common.DataType import * from Common.BuildVersion import gBUILD_VERSION import Common.EdkLogger as EdkLogger from Common.LongFilePathSupport import OpenLongFilePath as open @@ -165,6 +166,8 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong): if len(MatchList) == 2: LineNumber = int(MatchList[0], 0) InjectedFile = MatchList[1] + InjectedFile = os.path.normpath(InjectedFile) + InjectedFile = os.path.normcase(InjectedFile) # The first injetcted file must be the preprocessed file itself if PreprocessedFile == "": PreprocessedFile = InjectedFile @@ -173,7 +176,7 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong): elif PreprocessedFile == "" or InjectedFile != PreprocessedFile: continue - if LineIndexOfOriginalFile == None: + if LineIndexOfOriginalFile is None: # # Any non-empty lines must be from original preprocessed file. # And this must be the first one. @@ -193,7 +196,7 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong): # convert Decimal number format Line = gDecNumberPattern.sub(r"\1", Line) - if LineNumber != None: + if LineNumber is not None: EdkLogger.verbose("Got line directive: line=%d" % LineNumber) # in case preprocessor removed some lines, like blank or comment lines if LineNumber <= len(NewLines): @@ -216,10 +219,10 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong): Brace = 0 for Index in range(len(Lines)): Line = Lines[Index] - if MulPatternFlag == False and gTypedef_MulPattern.search(Line) == None: - if SinglePatternFlag == False and gTypedef_SinglePattern.search(Line) == None: + if MulPatternFlag == False and gTypedef_MulPattern.search(Line) is None: + if SinglePatternFlag == False and gTypedef_SinglePattern.search(Line) is None: # remove "#pragram pack" directive - if gPragmaPattern.search(Line) == None: + if gPragmaPattern.search(Line) is None: NewLines.append(Line) continue elif SinglePatternFlag == False: @@ -260,7 +263,7 @@ def TrimPreprocessedVfr(Source, Target): CreateDirectory(os.path.dirname(Target)) try: - f = open (Source,'r') + f = open (Source, 'r') except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source) # read whole file @@ -282,9 +285,9 @@ def TrimPreprocessedVfr(Source, Target): Lines[Index] = "\n" continue - if FoundTypedef == False and gTypedefPattern.search(Line) == None: + if FoundTypedef == False and gTypedefPattern.search(Line) is None: # keep "#pragram pack" directive - if gPragmaPattern.search(Line) == None: + if gPragmaPattern.search(Line) is None: Lines[Index] = "\n" continue elif FoundTypedef == False: @@ -303,13 +306,13 @@ def TrimPreprocessedVfr(Source, Target): FoundTypedef = False TypedefEnd = Index # keep all "typedef struct" except to GUID, EFI_PLABEL and PAL_CALL_RETURN - if Line.strip("} ;\r\n") in ["GUID", "EFI_PLABEL", "PAL_CALL_RETURN"]: + if Line.strip("} ;\r\n") in [TAB_GUID, "EFI_PLABEL", "PAL_CALL_RETURN"]: for i in range(TypedefStart, TypedefEnd+1): Lines[i] = "\n" # save all lines trimmed try: - f = open (Target,'w') + f = open (Target, 'w') except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target) f.writelines(Lines) @@ -406,7 +409,7 @@ def TrimAslFile(Source, Target, IncludePathFile): if IncludePathFile: try: LineNum = 0 - for Line in open(IncludePathFile,'r'): + for Line in open(IncludePathFile, 'r'): LineNum += 1 if Line.startswith("/I") or Line.startswith ("-I"): IncludePathList.append(Line[2:].strip()) @@ -424,7 +427,7 @@ def TrimAslFile(Source, Target, IncludePathFile): # save all lines trimmed try: - f = open (Target,'w') + f = open (Target, 'w') except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target) @@ -437,7 +440,7 @@ def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile): for CurrentDir, Dirs, Files in os.walk(DebugDir): for FileName in Files: Name, Ext = os.path.splitext(FileName) - if Ext == '.lst': + if Ext == '.c' and Name != 'AutoGen': VfrNameList.append (Name + 'Bin') VfrNameList.append (ModuleName + 'Strings') @@ -454,8 +457,8 @@ def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile): except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None) - # Use a instance of StringIO to cache data - fStringIO = StringIO.StringIO('') + # Use a instance of BytesIO to cache data + fStringIO = BytesIO('') for Item in VfrUniOffsetList: if (Item[0].find("Strings") != -1): @@ -510,7 +513,7 @@ def TrimEdkSources(Source, Target): for FileName in Files: Dummy, Ext = os.path.splitext(FileName) if Ext.upper() not in ['.C', '.H']: continue - if Target == None or Target == '': + if Target is None or Target == '': TrimEdkSourceCode( os.path.join(CurrentDir, FileName), os.path.join(CurrentDir, FileName) @@ -559,7 +562,7 @@ def TrimEdkSourceCode(Source, Target): CreateDirectory(os.path.dirname(Target)) try: - f = open (Source,'rb') + f = open (Source, 'rb') except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source) # read whole file @@ -567,8 +570,8 @@ def TrimEdkSourceCode(Source, Target): f.close() NewLines = None - for Re,Repl in gImportCodePatterns: - if NewLines == None: + for Re, Repl in gImportCodePatterns: + if NewLines is None: NewLines = Re.sub(Repl, Lines) else: NewLines = Re.sub(Repl, NewLines) @@ -578,7 +581,7 @@ def TrimEdkSourceCode(Source, Target): return try: - f = open (Target,'wb') + f = open (Target, 'wb') except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target) f.write(NewLines) @@ -667,16 +670,16 @@ def Main(): EdkLogger.SetLevel(CommandOptions.LogLevel + 1) else: EdkLogger.SetLevel(CommandOptions.LogLevel) - except FatalError, X: + except FatalError as X: return 1 try: if CommandOptions.FileType == "Vfr": - if CommandOptions.OutputFile == None: + if CommandOptions.OutputFile is None: CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii' TrimPreprocessedVfr(InputFile, CommandOptions.OutputFile) elif CommandOptions.FileType == "Asl": - if CommandOptions.OutputFile == None: + if CommandOptions.OutputFile is None: CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii' TrimAslFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile) elif CommandOptions.FileType == "EdkSourceCode": @@ -684,13 +687,13 @@ def Main(): elif CommandOptions.FileType == "VfrOffsetBin": GenerateVfrBinSec(CommandOptions.ModuleName, CommandOptions.DebugDir, CommandOptions.OutputFile) else : - if CommandOptions.OutputFile == None: + if CommandOptions.OutputFile is None: CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii' TrimPreprocessedFile(InputFile, CommandOptions.OutputFile, CommandOptions.ConvertHex, CommandOptions.TrimLong) - except FatalError, X: + except FatalError as X: import platform import traceback - if CommandOptions != None and CommandOptions.LogLevel <= EdkLogger.DEBUG_9: + if CommandOptions is not None and CommandOptions.LogLevel <= EdkLogger.DEBUG_9: EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc()) return 1 except: