From nobody Tue Feb 10 13:56:44 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1529528938743420.089745587022; Wed, 20 Jun 2018 14:08:58 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 3173A211BED5F; Wed, 20 Jun 2018 14:08:32 -0700 (PDT) Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 29CB5211BD61A for ; Wed, 20 Jun 2018 14:08:30 -0700 (PDT) Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Jun 2018 14:08:29 -0700 Received: from jcarsey-desk1.amr.corp.intel.com ([10.7.159.144]) by orsmga004.jf.intel.com with ESMTP; 20 Jun 2018 14:08:29 -0700 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.65; helo=mga03.intel.com; envelope-from=jaben.carsey@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,249,1526367600"; d="scan'208";a="209784387" From: Jaben Carsey To: edk2-devel@lists.01.org Date: Wed, 20 Jun 2018 14:08:13 -0700 Message-Id: <605bc234de703e41e099fb1105678f9fb8411d28.1529528784.git.jaben.carsey@intel.com> X-Mailer: git-send-email 2.16.2.windows.1 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [PATCH v1 07/11] BaseTools: refactor file opening/writing X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" change mode minimal needed permissions change to use with statement Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jaben Carsey --- BaseTools/Source/Python/AutoGen/AutoGen.py | 57 ++++---= -- BaseTools/Source/Python/AutoGen/GenC.py | 5 +- BaseTools/Source/Python/AutoGen/GenMake.py | 5 +- BaseTools/Source/Python/AutoGen/IdfClassObject.py | 21 ++-- BaseTools/Source/Python/AutoGen/StrGather.py | 18 +-- BaseTools/Source/Python/AutoGen/UniClassObject.py | 5 +- BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py | 5 +- BaseTools/Source/Python/BPDG/GenVpd.py | 30 +++-- BaseTools/Source/Python/Common/Misc.py | 39 +++---- BaseTools/Source/Python/Common/TargetTxtClassObject.py | 13 +-- BaseTools/Source/Python/Common/ToolDefClassObject.py | 4 +- BaseTools/Source/Python/Common/VpdInfoFile.py | 4 +- BaseTools/Source/Python/Ecc/Ecc.py | 4 +- BaseTools/Source/Python/Eot/EotGlobalData.py | 14 +-- BaseTools/Source/Python/Eot/FileProfile.py | 8 +- BaseTools/Source/Python/Eot/Report.py | 6 +- BaseTools/Source/Python/GenFds/Capsule.py | 7 +- BaseTools/Source/Python/GenFds/CapsuleData.py | 10 +- BaseTools/Source/Python/GenFds/FdfParser.py | 20 +--- BaseTools/Source/Python/GenFds/FfsFileStatement.py | 5 +- BaseTools/Source/Python/GenFds/Fv.py | 37 +++--- BaseTools/Source/Python/GenFds/FvImageSection.py | 8 +- BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py | 53 ++++---= -- BaseTools/Source/Python/GenFds/GuidSection.py | 45 ++++---- BaseTools/Source/Python/GenFds/Region.py | 15 +-- BaseTools/Source/Python/GenFds/Vtf.py | 122 +++++++= +++---------- BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py | 21 ++-- BaseTools/Source/Python/PatchPcdValue/PatchPcdValue.py | 19 ++- BaseTools/Source/Python/Table/TableReport.py | 47 ++++---- BaseTools/Source/Python/TargetTool/TargetTool.py | 97 +++++++= +-------- BaseTools/Source/Python/Trim/Trim.py | 67 +++++--= ---- BaseTools/Source/Python/Workspace/DscBuildData.py | 17 +-- BaseTools/Source/Python/build/BuildReport.py | 81 +++++++= ------ BaseTools/Source/Python/build/build.py | 16 +-- 34 files changed, 401 insertions(+), 524 deletions(-) diff --git a/BaseTools/Source/Python/AutoGen/AutoGen.py b/BaseTools/Source/= Python/AutoGen/AutoGen.py index b7dd086e28a8..4ce39cf9583a 100644 --- a/BaseTools/Source/Python/AutoGen/AutoGen.py +++ b/BaseTools/Source/Python/AutoGen/AutoGen.py @@ -675,10 +675,8 @@ class WorkspaceAutoGen(AutoGen): for files in AllWorkSpaceMetaFiles: if files.endswith('.dec'): continue - f =3D open(files, 'r') - Content =3D f.read() - f.close() - m.update(Content) + with open(files, 'r') as f: + m.update(f.read()) SaveFileOnChange(os.path.join(self.BuildDir, 'AutoGen.hash'), = m.hexdigest(), True) GlobalData.gPlatformHash =3D m.hexdigest() =20 @@ -686,11 +684,11 @@ class WorkspaceAutoGen(AutoGen): # Write metafile list to build directory # AutoGenFilePath =3D os.path.join(self.BuildDir, 'AutoGen') - if os.path.exists (AutoGenFilePath): - os.remove(AutoGenFilePath) if not os.path.exists(self.BuildDir): os.makedirs(self.BuildDir) - with open(os.path.join(self.BuildDir, 'AutoGen'), 'w+') as file: + elif os.path.exists (AutoGenFilePath): + os.remove(AutoGenFilePath) + with open(AutoGenFilePath, 'w') as file: for f in AllWorkSpaceMetaFiles: print >> file, f return True @@ -704,20 +702,16 @@ class WorkspaceAutoGen(AutoGen): HashFile =3D os.path.join(PkgDir, Pkg.PackageName + '.hash') m =3D hashlib.md5() # Get .dec file's hash value - f =3D open(Pkg.MetaFile.Path, 'r') - Content =3D f.read() - f.close() - m.update(Content) + with open(Pkg.MetaFile.Path, 'r') as f: + m.update(f.read()) # Get include files hash value if Pkg.Includes: for inc in sorted(Pkg.Includes, key=3Dlambda x: str(x)): for Root, Dirs, Files in os.walk(str(inc)): for File in sorted(Files): File_Path =3D os.path.join(Root, File) - f =3D open(File_Path, 'r') - Content =3D f.read() - f.close() - m.update(Content) + with open(File_Path, 'r') as f: + m.update(f.read()) SaveFileOnChange(HashFile, m.hexdigest(), True) GlobalData.gPackageHash[Pkg.Arch][Pkg.PackageName] =3D m.hexdigest= () =20 @@ -3641,9 +3635,8 @@ class ModuleAutoGen(AutoGen): Vfri =3D os.path.join(self.OutputDir, SrcFile.BaseName + '.i') if not os.path.exists(Vfri): continue - VfriFile =3D open(Vfri, 'r') + with open(Vfri, 'r') as VfriFile: Content =3D VfriFile.read() - VfriFile.close() Pos =3D Content.find('efivarstore') while Pos !=3D -1: # @@ -3711,11 +3704,6 @@ class ModuleAutoGen(AutoGen): OutputName =3D '%sOffset.bin' % self.Name UniVfrOffsetFileName =3D os.path.join( self.OutputDir, OutputN= ame) =20 - try: - fInputfile =3D open(UniVfrOffsetFileName, "wb+", 0) - except: - EdkLogger.error("build", FILE_OPEN_FAILURE, "File open failed = for %s" % UniVfrOffsetFileName,None) - # Use a instance of StringIO to cache data fStringIO =3D StringIO('') =20 =20 @@ -3742,17 +3730,19 @@ class ModuleAutoGen(AutoGen): fStringIO.write(''.join(VfrGuid)) =20 VfrValue =3D pack ('Q', int (Item[1], 16)) fStringIO.write (VfrValue) - # + + try: + with open(UniVfrOffsetFileName, "wb", 0) as fInputfile: # write data into file. - # try : =20 fInputfile.write (fStringIO.getvalue()) except: EdkLogger.error("build", FILE_WRITE_FAILURE, "Write data to fi= le %s failed, please check whether the " "file been locked or using by other applicatio= ns." %UniVfrOffsetFileName,None) + except: + EdkLogger.error("build", FILE_OPEN_FAILURE, "File open failed = for %s" % UniVfrOffsetFileName,None) =20 fStringIO.close () - fInputfile.close () return OutputName =20 ## Create AsBuilt INF file the module @@ -4130,9 +4120,8 @@ class ModuleAutoGen(AutoGen): FileDir =3D path.join(GlobalData.gBinCacheSource, self.Arch, self.= SourceDir, self.MetaFile.BaseName) HashFile =3D path.join(FileDir, self.Name + '.hash') if os.path.exists(HashFile): - f =3D open(HashFile, 'r') + with open(HashFile, 'r') as f: CacheHash =3D f.read() - f.close() if GlobalData.gModuleHash[self.Arch][self.Name]: if CacheHash =3D=3D GlobalData.gModuleHash[self.Arch][self= .Name]: for root, dir, files in os.walk(FileDir): @@ -4296,17 +4285,13 @@ class ModuleAutoGen(AutoGen): m.update(GlobalData.gModuleHash[self.Arch][Lib.Name]) =20 # Add Module self - f =3D open(str(self.MetaFile), 'r') - Content =3D f.read() - f.close() - m.update(Content) + with open(str(self.MetaFile), 'r') as f: + m.update(f.read()) # Add Module's source files if self.SourceFileList: for File in sorted(self.SourceFileList, key=3Dlambda x: str(x)= ): - f =3D open(str(File), 'r') - Content =3D f.read() - f.close() - m.update(Content) + with open(str(File), 'r') as f: + m.update(f.read()) =20 ModuleHashFile =3D path.join(self.BuildDir, self.Name + ".hash") if self.Name not in GlobalData.gModuleHash[self.Arch]: @@ -4364,7 +4349,7 @@ class ModuleAutoGen(AutoGen): =20 if os.path.exists (self.GetTimeStampPath()): os.remove (self.GetTimeStampPath()) - with open(self.GetTimeStampPath(), 'w+') as file: + with open(self.GetTimeStampPath(), 'w') as file: for f in FileSet: print >> file, f =20 diff --git a/BaseTools/Source/Python/AutoGen/GenC.py b/BaseTools/Source/Pyt= hon/AutoGen/GenC.py index ae3af085a16b..c6395a1b7da3 100644 --- a/BaseTools/Source/Python/AutoGen/GenC.py +++ b/BaseTools/Source/Python/AutoGen/GenC.py @@ -1814,9 +1814,8 @@ def CreateIdfFileCode(Info, AutoGenC, StringH, IdfGen= CFlag, IdfGenBinBuffer): Index +=3D 1 continue =20 - TmpFile =3D open(File.Path, 'rb') - Buffer =3D TmpFile.read() - TmpFile.close() + with open(File.Path, 'rb') as f: + Buffer =3D f.read() if File.Ext.upper() =3D=3D '.PNG': TempBuffer =3D pack('B', EFI_HII_IIBT_IMAG= E_PNG) TempBuffer +=3D pack('I', len(Buffer)) diff --git a/BaseTools/Source/Python/AutoGen/GenMake.py b/BaseTools/Source/= Python/AutoGen/GenMake.py index f19c1bfdaff1..59e195ddf1ef 100644 --- a/BaseTools/Source/Python/AutoGen/GenMake.py +++ b/BaseTools/Source/Python/AutoGen/GenMake.py @@ -1026,12 +1026,11 @@ cleanlib: CurrentFileDependencyList =3D DepDb[F] else: try: - Fd =3D open(F.Path, 'r') + with open(F.Path, 'r') as f: + FileContent =3D f.read() except BaseException, X: EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData= =3DF.Path + "\n\t" + str(X)) =20 - FileContent =3D Fd.read() - Fd.close() if len(FileContent) =3D=3D 0: continue =20 diff --git a/BaseTools/Source/Python/AutoGen/IdfClassObject.py b/BaseTools/= Source/Python/AutoGen/IdfClassObject.py index e5b933c2036f..dbd9dfe30c20 100644 --- a/BaseTools/Source/Python/AutoGen/IdfClassObject.py +++ b/BaseTools/Source/Python/AutoGen/IdfClassObject.py @@ -1,7 +1,7 @@ ## @file # This file is used to collect all defined strings in Image Definition fil= es # -# Copyright (c) 2016, Intel Corporation. All rights reserved.
+# Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BS= D License # which accompanies this distribution. The full text of the license may b= e found at @@ -69,13 +69,12 @@ class IdfFileClassObject(object): self.ImageFilesDict =3D {} self.ImageIDList =3D [] for File in FileList: - if File is None: + if not File: EdkLogger.error("Image Definition File Parser", PARSER_ERR= OR, 'No Image definition file is given.') =20 try: - IdfFile =3D open(LongFilePath(File.Path), mode=3D'r') - FileIn =3D IdfFile.read() - IdfFile.close() + with open(LongFilePath(File.Path), mode=3D'r') as f: + FileIn =3D f.read() except: EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=3DFi= le) =20 @@ -118,12 +117,12 @@ def SearchImageID(ImageFileObject, FileList): =20 for File in FileList: if os.path.isfile(File): - Lines =3D open(File, 'r') - for Line in Lines: - ImageIdList =3D IMAGE_TOKEN.findall(Line) - for ID in ImageIdList: - EdkLogger.debug(EdkLogger.DEBUG_5, "Found ImageID iden= tifier: " + ID) - ImageFileObject.SetImageIDReferenced(ID) + with open(File, 'r') as f: + for Line in f: + ImageIdList =3D IMAGE_TOKEN.findall(Line) + for ID in ImageIdList: + EdkLogger.debug(EdkLogger.DEBUG_5, "Found ImageID = identifier: " + ID) + ImageFileObject.SetImageIDReferenced(ID) =20 class ImageFileObject(object): def __init__(self, FileName, ImageID, TransParent =3D False): diff --git a/BaseTools/Source/Python/AutoGen/StrGather.py b/BaseTools/Sourc= e/Python/AutoGen/StrGather.py index d41bcb3f7137..082d8cdc5f3c 100644 --- a/BaseTools/Source/Python/AutoGen/StrGather.py +++ b/BaseTools/Source/Python/AutoGen/StrGather.py @@ -529,11 +529,11 @@ def SearchString(UniObjectClass, FileList, IsCompatib= leMode): =20 for File in FileList: if os.path.isfile(File): - Lines =3D open(File, 'r') - for Line in Lines: - for StrName in STRING_TOKEN.findall(Line): - EdkLogger.debug(EdkLogger.DEBUG_5, "Found string ident= ifier: " + StrName) - UniObjectClass.SetStringReferenced(StrName) + with open(File, 'r') as f: + for Line in f: + for StrName in STRING_TOKEN.findall(Line): + EdkLogger.debug(EdkLogger.DEBUG_5, "Found string i= dentifier: " + StrName) + UniObjectClass.SetStringReferenced(StrName) =20 UniObjectClass.ReToken() =20 @@ -603,9 +603,9 @@ if __name__ =3D=3D '__main__': SkipList =3D ['.inf', '.uni'] BaseName =3D 'DriverSample' (h, c) =3D GetStringFiles(UniFileList, SrcFileList, IncludeList, SkipL= ist, BaseName, True) - hfile =3D open('unistring.h', 'w') - cfile =3D open('unistring.c', 'w') - hfile.write(h) - cfile.write(c) + with open('unistring.h', 'w') as f: + f.write(h) + with open('unistring.c', 'w') as f: + f.write(c) =20 EdkLogger.info('end') diff --git a/BaseTools/Source/Python/AutoGen/UniClassObject.py b/BaseTools/= Source/Python/AutoGen/UniClassObject.py index 5a3c2547783b..b1b9c96c39bb 100644 --- a/BaseTools/Source/Python/AutoGen/UniClassObject.py +++ b/BaseTools/Source/Python/AutoGen/UniClassObject.py @@ -303,9 +303,8 @@ class UniFileClassObject(object): # Read file # try: - UniFile =3D open(FileName, mode=3D'rb') - FileIn =3D UniFile.read() - UniFile.close() + with open(FileName, mode=3D'rb') as f: + FileIn =3D f.read() except: EdkLogger.Error("build", FILE_OPEN_FAILURE, ExtraData=3DFile) =20 diff --git a/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py b/B= aseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py index 3b54865000bf..2c6bb8e396a9 100644 --- a/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py +++ b/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py @@ -165,9 +165,8 @@ class VAR_CHECK_PCD_VARIABLE_TAB_CONTAINER(object): =20 DbFile =3D StringIO() if Phase =3D=3D 'DXE' and os.path.exists(BinFilePath): - BinFile =3D open(BinFilePath, "rb") - BinBuffer =3D BinFile.read() - BinFile.close() + with open(BinFilePath, "rb") as f: + BinBuffer =3D f.read() BinBufferSize =3D len(BinBuffer) if (BinBufferSize % 4): for i in range(4 - (BinBufferSize % 4)): diff --git a/BaseTools/Source/Python/BPDG/GenVpd.py b/BaseTools/Source/Pyth= on/BPDG/GenVpd.py index 4fa12b7d59de..dba815415f92 100644 --- a/BaseTools/Source/Python/BPDG/GenVpd.py +++ b/BaseTools/Source/Python/BPDG/GenVpd.py @@ -323,13 +323,11 @@ class GenVPD : self.PcdFixedOffsetSizeList =3D [] self.PcdUnknownOffsetList =3D [] try: - fInputfile =3D open(InputFileName, "r", 0) - try: - self.FileLinesList =3D fInputfile.readlines() - except: - EdkLogger.error("BPDG", BuildToolError.FILE_READ_FAILURE, = "File read failed for %s" % InputFileName, None) - finally: - fInputfile.close() + with open(InputFileName, "r", 0) as f: + try: + self.FileLinesList =3D f.readlines() + except: + EdkLogger.error("BPDG", BuildToolError.FILE_READ_FAILU= RE, "File read failed for %s" % InputFileName, None) except: EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "Fil= e open failed for %s" % InputFileName, None) =20 @@ -661,12 +659,6 @@ class GenVPD : def GenerateVpdFile (self, MapFileName, BinFileName): #Open an VPD file to process =20 - try: - fVpdFile =3D open(BinFileName, "wb", 0) - except: - # Open failed - EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "Fil= e open failed for %s" % self.VpdFileName, None) - try : fMapFile =3D open(MapFileName, "w", 0) except: @@ -697,12 +689,16 @@ class GenVPD : else: fStringIO.write (eachPcd.PcdValue) =20 - try : - fVpdFile.write (fStringIO.getvalue()) + try: + with open(BinFileName, "wb", 0) as fVpdFile: + try : + fVpdFile.write (fStringIO.getvalue()) + except: + EdkLogger.error("BPDG", BuildToolError.FILE_WRITE_FAIL= URE, "Write data to file %s failed, please check whether the file been lock= ed or using by other applications." % self.VpdFileName, None) except: - EdkLogger.error("BPDG", BuildToolError.FILE_WRITE_FAILURE, "Wr= ite data to file %s failed, please check whether the file been locked or us= ing by other applications." % self.VpdFileName, None) + # Open failed + EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "Fil= e open failed for %s" % self.VpdFileName, None) =20 fStringIO.close () - fVpdFile.close () fMapFile.close () =20 diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Pyth= on/Common/Misc.py index 8fba734568bd..148cbe2cf99d 100644 --- a/BaseTools/Source/Python/Common/Misc.py +++ b/BaseTools/Source/Python/Common/Misc.py @@ -63,15 +63,14 @@ def GetVariableOffset(mapfilepath, efifilepath, varname= s): =20 @return List whos elements are tuple with variable name and raw offset """ - lines =3D [] try: - f =3D open(mapfilepath, 'r') - lines =3D f.readlines() - f.close() + with open(mapfilepath, 'r') as f: + lines =3D f.readlines() except: return None =20 - if len(lines) =3D=3D 0: return None + if len(lines) =3D=3D 0:=20 + return None firstline =3D lines[0].strip() if (firstline.startswith("Archive member included ") and firstline.endswith(" file (symbol)")): @@ -471,13 +470,11 @@ def SaveFileOnChange(File, Content, IsBinaryFile=3DTr= ue): if not SaveFileToDisk(File, Content): EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData= =3DFile) except: - Fd =3D open(File, "wb") + with open(File, "wb") as Fd: + Fd.write(Content) + else: + with open(File, "wb") as Fd: Fd.write(Content) - Fd.close() - else: - Fd =3D open(File, "wb") - Fd.write(Content) - Fd.close() except IOError, X: EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData=3D'IOError %s= ' % X) =20 @@ -489,15 +486,11 @@ def SaveFileOnChange(File, Content, IsBinaryFile=3DTr= ue): # @param File The path of file to store the object # def DataDump(Data, File): - Fd =3D None try: - Fd =3D open(File, 'wb') - cPickle.dump(Data, Fd, cPickle.HIGHEST_PROTOCOL) + with open(File, 'wb') as Fd: + cPickle.dump(Data, Fd, cPickle.HIGHEST_PROTOCOL) except: EdkLogger.error("", FILE_OPEN_FAILURE, ExtraData=3DFile, RaiseErro= r=3DFalse) - finally: - if Fd is not None: - Fd.close() =20 ## Restore a Python object from a file # @@ -507,18 +500,12 @@ def DataDump(Data, File): # @retval None If failure in file operation # def DataRestore(File): - Data =3D None - Fd =3D None try: - Fd =3D open(File, 'rb') - Data =3D cPickle.load(Fd) + with open(File, 'rb') as Fd: + return cPickle.load(Fd) except Exception, e: EdkLogger.verbose("Failed to load [%s]\n\t%s" % (File, str(e))) - Data =3D None - finally: - if Fd is not None: - Fd.close() - return Data + return None =20 ## Retrieve and cache the real path name in file system # diff --git a/BaseTools/Source/Python/Common/TargetTxtClassObject.py b/BaseT= ools/Source/Python/Common/TargetTxtClassObject.py index f8459c892e36..6f5e5f0d173d 100644 --- a/BaseTools/Source/Python/Common/TargetTxtClassObject.py +++ b/BaseTools/Source/Python/Common/TargetTxtClassObject.py @@ -77,16 +77,14 @@ class TargetTxtClassObject(object): # @retval 1 Open file failed # def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCh= aracter): - F =3D None + self.ConfDirectoryPath =3D os.path.dirname(FileName) try: - F =3D open(FileName, 'r') - self.ConfDirectoryPath =3D os.path.dirname(FileName) + with open(FileName, 'r') as F: + Lines =3D F.readlines() except: EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=3DFileNa= me) - if F is not None: - F.close() =20 - for Line in F: + for Line in Lines: Line =3D Line.strip() if Line.startswith(CommentCharacter) or Line =3D=3D '': continue @@ -131,10 +129,7 @@ class TargetTxtClassObject(object): EdkLogger.error("build", FORMAT_INVALID, "Invalid numb= er of [%s]: %s." % (Key, Value), File=3DFileName) self.TargetTxtDictionary[Key] =3D Value - #elif Key not in GlobalData.gGlobalDefines: - # GlobalData.gGlobalDefines[Key] =3D Value =20 - F.close() return 0 =20 ## TargetTxtDict diff --git a/BaseTools/Source/Python/Common/ToolDefClassObject.py b/BaseToo= ls/Source/Python/Common/ToolDefClassObject.py index dd985ab30359..feac169df5c8 100644 --- a/BaseTools/Source/Python/Common/ToolDefClassObject.py +++ b/BaseTools/Source/Python/Common/ToolDefClassObject.py @@ -117,8 +117,8 @@ class ToolDefClassObject(object): FileContent =3D [] if os.path.isfile(FileName): try: - F =3D open(FileName, 'r') - FileContent =3D F.readlines() + with open(FileName, 'r') as F: + FileContent =3D F.readlines() except: EdkLogger.error("tools_def.txt parser", FILE_OPEN_FAILURE,= ExtraData=3DFileName) else: diff --git a/BaseTools/Source/Python/Common/VpdInfoFile.py b/BaseTools/Sour= ce/Python/Common/VpdInfoFile.py index 2b447772eafe..117be45cf20c 100644 --- a/BaseTools/Source/Python/Common/VpdInfoFile.py +++ b/BaseTools/Source/Python/Common/VpdInfoFile.py @@ -153,12 +153,12 @@ class VpdInfoFile: # @param FilePath The full path string for existing VPD PCD info file. def Read(self, FilePath): try: - fd =3D open(FilePath, "r") + with open(FilePath, "r") as fd: + Lines =3D fd.readlines() except: EdkLogger.error("VpdInfoFile",=20 BuildToolError.FILE_OPEN_FAILURE,=20 "Fail to open file %s for written." % FilePath) - Lines =3D fd.readlines() for Line in Lines: Line =3D Line.strip() if len(Line) =3D=3D 0 or Line.startswith("#"): diff --git a/BaseTools/Source/Python/Ecc/Ecc.py b/BaseTools/Source/Python/E= cc/Ecc.py index e78d70372e36..c4d9b36ac228 100644 --- a/BaseTools/Source/Python/Ecc/Ecc.py +++ b/BaseTools/Source/Python/Ecc/Ecc.py @@ -1,7 +1,7 @@ ## @file # This file is used to be the main entrance of ECC tool # -# Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BS= D License # which accompanies this distribution. The full text of the license may b= e found at @@ -201,7 +201,7 @@ class Ecc(object): for specificDir in SpecificDirs: =20 ScanFolders.append(os.path.join(EccGlobalData.gTarget, spe= cificDir)) EdkLogger.quiet("Building database for meta data files ...") - Op =3D open(EccGlobalData.gConfig.MetaDataFileCheckPathOfGenerateF= ileList, 'w+') + Op =3D open(EccGlobalData.gConfig.MetaDataFileCheckPathOfGenerateF= ileList, 'w') #SkipDirs =3D Read from config file SkipDirs =3D EccGlobalData.gConfig.SkipDirList SkipDirString =3D string.join(SkipDirs, '|') diff --git a/BaseTools/Source/Python/Eot/EotGlobalData.py b/BaseTools/Sourc= e/Python/Eot/EotGlobalData.py index cb6a940ab8f9..a13c7e8f5851 100644 --- a/BaseTools/Source/Python/Eot/EotGlobalData.py +++ b/BaseTools/Source/Python/Eot/EotGlobalData.py @@ -1,7 +1,7 @@ ## @file # This file is used to save global datas # -# Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.
+# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BS= D License # which accompanies this distribution. The full text of the license may b= e found at @@ -38,27 +38,27 @@ gMACRO['CAPSULE_INF'] =3D '' =20 # Log file for unmatched variables gUN_MATCHED_LOG =3D 'Log_UnMatched.log' -gOP_UN_MATCHED =3D open(gUN_MATCHED_LOG, 'w+') +gOP_UN_MATCHED =3D open(gUN_MATCHED_LOG, 'w') =20 # Log file for all INF files gINF_FILES =3D 'Log_Inf_File.log' -gOP_INF =3D open(gINF_FILES, 'w+') +gOP_INF =3D open(gINF_FILES, 'w') =20 # Log file for not dispatched PEIM/DRIVER gUN_DISPATCHED_LOG =3D 'Log_UnDispatched.log' -gOP_UN_DISPATCHED =3D open(gUN_DISPATCHED_LOG, 'w+') +gOP_UN_DISPATCHED =3D open(gUN_DISPATCHED_LOG, 'w') =20 # Log file for unmatched variables in function calling gUN_MATCHED_IN_LIBRARY_CALLING_LOG =3D 'Log_UnMatchedInLibraryCalling.log' -gOP_UN_MATCHED_IN_LIBRARY_CALLING =3D open(gUN_MATCHED_IN_LIBRARY_CALLING_= LOG, 'w+') +gOP_UN_MATCHED_IN_LIBRARY_CALLING =3D open(gUN_MATCHED_IN_LIBRARY_CALLING_= LOG, 'w') =20 # Log file for order of dispatched PEIM/DRIVER gDISPATCH_ORDER_LOG =3D 'Log_DispatchOrder.log' -gOP_DISPATCH_ORDER =3D open(gDISPATCH_ORDER_LOG, 'w+') +gOP_DISPATCH_ORDER =3D open(gDISPATCH_ORDER_LOG, 'w') =20 # Log file for found source files gSOURCE_FILES =3D 'Log_SourceFiles.log' -gOP_SOURCE_FILES =3D open(gSOURCE_FILES, 'w+') +gOP_SOURCE_FILES =3D open(gSOURCE_FILES, 'w') =20 # Dict for GUID found in DEC files gGuidDict =3D dict() diff --git a/BaseTools/Source/Python/Eot/FileProfile.py b/BaseTools/Source/= Python/Eot/FileProfile.py index 0544c0d55b44..bf6a4c054baa 100644 --- a/BaseTools/Source/Python/Eot/FileProfile.py +++ b/BaseTools/Source/Python/Eot/FileProfile.py @@ -1,7 +1,7 @@ ## @file # fragments of source file # -# Copyright (c) 2007 - 2014, 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 B= SD License @@ -49,11 +49,7 @@ class FileProfile : self.FileLinesList =3D [] self.FileLinesListFromFile =3D [] try: - fsock =3D open(FileName, "rb", 0) - try: + with open(FileName, "rb", 0) as fsock: self.FileLinesListFromFile =3D fsock.readlines() - finally: - fsock.close() - except IOError: raise Warning("Error when opening file %s" % FileName) diff --git a/BaseTools/Source/Python/Eot/Report.py b/BaseTools/Source/Pytho= n/Eot/Report.py index 7435b4d7c930..99b8b152180a 100644 --- a/BaseTools/Source/Python/Eot/Report.py +++ b/BaseTools/Source/Python/Eot/Report.py @@ -1,7 +1,7 @@ ## @file # This file is used to create report for Eot tool # -# Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.
+# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BS= D License # which accompanies this distribution. The full text of the license may b= e found at @@ -33,10 +33,10 @@ class Report(object): # def __init__(self, ReportName =3D 'Report.html', FvObj =3D None, Dispa= tchName=3DNone): self.ReportName =3D ReportName - self.Op =3D open(ReportName, 'w+') + self.Op =3D open(ReportName, 'w') self.DispatchList =3D None if DispatchName: - self.DispatchList =3D open(DispatchName, 'w+') + self.DispatchList =3D open(DispatchName, 'w') self.FvObj =3D FvObj self.FfsIndex =3D 0 self.PpiIndex =3D 0 diff --git a/BaseTools/Source/Python/GenFds/Capsule.py b/BaseTools/Source/P= ython/GenFds/Capsule.py index fbd48f3c6d76..6aae2fcb7d97 100644 --- a/BaseTools/Source/Python/GenFds/Capsule.py +++ b/BaseTools/Source/Python/GenFds/Capsule.py @@ -137,9 +137,8 @@ class Capsule (CapsuleClassObject) : FileName =3D driver.GenCapsuleSubItem() FwMgrHdr.write(pack('=3DQ', PreSize)) PreSize +=3D os.path.getsize(FileName) - File =3D open(FileName, 'rb') - Content.write(File.read()) - File.close() + with open(FileName, 'rb') as File: + Content.write(File.read()) for fmp in self.FmpPayloadList: if fmp.Existed: FwMgrHdr.write(pack('=3DQ', PreSize)) @@ -247,7 +246,7 @@ class Capsule (CapsuleClassObject) : def GenCapInf(self): self.CapInfFileName =3D os.path.join(GenFdsGlobalVariable.FvDir, self.UiCapsuleName + "_Cap" + '.inf') - CapInfFile =3D StringIO.StringIO() #open (self.CapInfFileName , 'w= +') + CapInfFile =3D StringIO.StringIO() =20 CapInfFile.writelines("[options]" + T_CHAR_LF) =20 diff --git a/BaseTools/Source/Python/GenFds/CapsuleData.py b/BaseTools/Sour= ce/Python/GenFds/CapsuleData.py index dd4c27bd15c7..9916bd4d2627 100644 --- a/BaseTools/Source/Python/GenFds/CapsuleData.py +++ b/BaseTools/Source/Python/GenFds/CapsuleData.py @@ -233,12 +233,10 @@ class CapsulePayload(CapsuleData): # # Append file content to the structure # - ImageFile =3D open(self.ImageFile, 'rb') - Buffer +=3D ImageFile.read() - ImageFile.close() + with open(self.ImageFile, 'rb') as ImageFile: + Buffer +=3D ImageFile.read() if self.VendorCodeFile: - VendorFile =3D open(self.VendorCodeFile, 'rb') - Buffer +=3D VendorFile.read() - VendorFile.close() + with open(self.VendorCodeFile, 'rb') as VendorFile: + Buffer +=3D VendorFile.read() self.Existed =3D True return Buffer diff --git a/BaseTools/Source/Python/GenFds/FdfParser.py b/BaseTools/Source= /Python/GenFds/FdfParser.py index ba076c8c1ecd..af1760bf729c 100644 --- a/BaseTools/Source/Python/GenFds/FdfParser.py +++ b/BaseTools/Source/Python/GenFds/FdfParser.py @@ -155,16 +155,11 @@ class IncludeFileProfile : self.FileName =3D FileName self.FileLinesList =3D [] try: - fsock =3D open(FileName, "rb", 0) - try: + with open(FileName, "rb", 0) as fsock: self.FileLinesList =3D fsock.readlines() - for index, line in enumerate(self.FileLinesList): - if not line.endswith('\n'): - self.FileLinesList[index] +=3D '\n' - - finally: - fsock.close() - + for index, line in enumerate(self.FileLinesList): + if not line.endswith('\n'): + self.FileLinesList[index] +=3D '\n' except: EdkLogger.error("FdfParser", FILE_OPEN_FAILURE, ExtraData=3DFi= leName) =20 @@ -216,16 +211,11 @@ class FileProfile : def __init__(self, FileName): self.FileLinesList =3D [] try: - fsock =3D open(FileName, "rb", 0) - try: + with open(FileName, "rb", 0) as fsock: self.FileLinesList =3D fsock.readlines() - finally: - fsock.close() - except: EdkLogger.error("FdfParser", FILE_OPEN_FAILURE, ExtraData=3DFi= leName) =20 - self.PcdDict =3D {} self.InfList =3D [] self.InfDict =3D {'ArchTBD':[]} diff --git a/BaseTools/Source/Python/GenFds/FfsFileStatement.py b/BaseTools= /Source/Python/GenFds/FfsFileStatement.py index 871499d3d2ad..1449d363eac3 100644 --- a/BaseTools/Source/Python/GenFds/FfsFileStatement.py +++ b/BaseTools/Source/Python/GenFds/FfsFileStatement.py @@ -104,11 +104,10 @@ class FileStatement (FileStatementClassObject) : MaxAlignValue =3D 1 for Index, File in enumerate(self.FileName): try: - f =3D open(File, 'rb') + with open(File, 'rb') as f: + Content =3D f.read() except: GenFdsGlobalVariable.ErrorLogger("Error openin= g RAW file %s." % (File)) - Content =3D f.read() - f.close() AlignValue =3D 1 if self.SubAlignment[Index] is not None: AlignValue =3D GenFdsGlobalVariable.GetAlignme= nt(self.SubAlignment[Index]) diff --git a/BaseTools/Source/Python/GenFds/Fv.py b/BaseTools/Source/Python= /GenFds/Fv.py index 29daba5a3a3e..d4b0611fc55a 100644 --- a/BaseTools/Source/Python/GenFds/Fv.py +++ b/BaseTools/Source/Python/GenFds/Fv.py @@ -163,8 +163,8 @@ class FV (FvClassObject): NewFvInfo =3D open(FvInfoFileName, 'r').read() if NewFvInfo is not None and NewFvInfo !=3D OrigFvInfo: FvChildAddr =3D [] - AddFileObj =3D open(FvInfoFileName, 'r') - AddrStrings =3D AddFileObj.readlines() + with open(FvInfoFileName, 'r') as AddFileObj: + AddrStrings =3D AddFileObj.readlines() AddrKeyFound =3D False for AddrString in AddrStrings: if AddrKeyFound: @@ -172,7 +172,6 @@ class FV (FvClassObject): FvChildAddr.append (AddrString) elif AddrString.find ("[FV_BASE_ADDRESS]") !=3D -1: AddrKeyFound =3D True - AddFileObj.close() =20 if FvChildAddr !=3D []: # Update Ffs again @@ -195,14 +194,14 @@ class FV (FvClassObject): # Write the Fv contents to Buffer # if os.path.isfile(FvOutputFile): - FvFileObj =3D open(FvOutputFile, 'rb') + with open(FvOutputFile, 'rb') as FvFileObj: + Buffer.write(FvFileObj.read()) + FvFileObj.seek(0) + # PI FvHeader is 0x48 byte + FvHeaderBuffer =3D FvFileObj.read(0x48) + GenFdsGlobalVariable.VerboseLogger("\nGenerate %s FV Succe= ssfully" % self.UiFvName) GenFdsGlobalVariable.SharpCounter =3D 0 - - Buffer.write(FvFileObj.read()) - FvFileObj.seek(0) - # PI FvHeader is 0x48 byte - FvHeaderBuffer =3D FvFileObj.read(0x48) # FV alignment position. FvAlignmentValue =3D 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1= F) if FvAlignmentValue >=3D 0x400: @@ -217,7 +216,6 @@ class FV (FvClassObject): else: # FvAlignmentValue is less than 1K self.FvAlignment =3D str (FvAlignmentValue) - FvFileObj.close() GenFds.ImageBinDict[self.UiFvName.upper() + 'fv'] =3D FvOu= tputFile GenFdsGlobalVariable.LargeFileInFvFlags.pop() else: @@ -378,16 +376,15 @@ class FV (FvClassObject): # check if the file path exists or not if not os.path.isfile(FileFullPath): GenFdsGlobalVariable.ErrorLogger("Error opening FV= Extension Header Entry file %s." % (self.FvExtEntryData[Index])) - FvExtFile =3D open (FileFullPath,'rb') - FvExtFile.seek(0,2) - Size =3D FvExtFile.tell() - if Size >=3D 0x10000: - GenFdsGlobalVariable.ErrorLogger("The size of FV E= xtension Header Entry file %s exceeds 0x10000." % (self.FvExtEntryData[Inde= x])) - TotalSize +=3D (Size + 4) - FvExtFile.seek(0) - Buffer +=3D pack('HH', (Size + 4), int(self.FvExtEntry= TypeValue[Index], 16)) - Buffer +=3D FvExtFile.read()=20 - FvExtFile.close() + with open (FileFullPath,'rb') as FvExtFile: + FvExtFile.seek(0,2) + Size =3D FvExtFile.tell() + if Size >=3D 0x10000: + GenFdsGlobalVariable.ErrorLogger("The size of = FV Extension Header Entry file %s exceeds 0x10000." % (self.FvExtEntryData[= Index])) + TotalSize +=3D (Size + 4) + FvExtFile.seek(0) + Buffer +=3D pack('HH', (Size + 4), int(self.FvExtE= ntryTypeValue[Index], 16)) + Buffer +=3D FvExtFile.read() if self.FvExtEntryType[Index] =3D=3D 'DATA': ByteList =3D self.FvExtEntryData[Index].split(',') Size =3D len (ByteList) diff --git a/BaseTools/Source/Python/GenFds/FvImageSection.py b/BaseTools/S= ource/Python/GenFds/FvImageSection.py index 0fc7115b677b..5a6e652d2fc5 100644 --- a/BaseTools/Source/Python/GenFds/FvImageSection.py +++ b/BaseTools/Source/Python/GenFds/FvImageSection.py @@ -64,8 +64,8 @@ class FvImageSection(FvImageSectionClassObject): FvAlignmentValue =3D 0 if os.path.isfile(FvFileName): with open (FvFileName,'rb') as FvFileObj: - # PI FvHeader is 0x48 byte - FvHeaderBuffer =3D FvFileObj.read(0x48) + # PI FvHeader is 0x48 byte + FvHeaderBuffer =3D FvFileObj.read(0x48) # FV alignment position. FvAlignmentValue =3D 1 << (ord (FvHeaderBuffer[0x2E]) = & 0x1F) if FvAlignmentValue > MaxFvAlignment: @@ -110,8 +110,8 @@ class FvImageSection(FvImageSectionClassObject): FvFileName =3D GenFdsGlobalVariable.ReplaceWorkspaceMa= cro(self.FvFileName) if os.path.isfile(FvFileName): with open (FvFileName,'rb') as FvFileObj: - # PI FvHeader is 0x48 byte - FvHeaderBuffer =3D FvFileObj.read(0x48) + # PI FvHeader is 0x48 byte + FvHeaderBuffer =3D FvFileObj.read(0x48) # FV alignment position. FvAlignmentValue =3D 1 << (ord (FvHeaderBuffer[0x2= E]) & 0x1F) # FvAlignmentValue is larger than or equal to 1K diff --git a/BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py b/BaseT= ools/Source/Python/GenFds/GenFdsGlobalVariable.py index c2e82de891d3..6876068dbe3e 100644 --- a/BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py +++ b/BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py @@ -300,31 +300,27 @@ class GenFdsGlobalVariable: # Create FV Address inf file # GenFdsGlobalVariable.FvAddressFileName =3D os.path.join(GenFdsGlob= alVariable.FfsDir, 'FvAddress.inf') - FvAddressFile =3D open(GenFdsGlobalVariable.FvAddressFileName, 'w') - # - # Add [Options] - # - FvAddressFile.writelines("[options]" + T_CHAR_LF) BsAddress =3D '0' for Arch in ArchList: if GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVari= able.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVar= iable.ToolChainTag].BsBaseAddress: BsAddress =3D GenFdsGlobalVariable.WorkSpace.BuildObject[G= enFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, = GenFdsGlobalVariable.ToolChainTag].BsBaseAddress break - - FvAddressFile.writelines("EFI_BOOT_DRIVER_BASE_ADDRESS =3D " + \ - BsAddress + \ - T_CHAR_LF) - RtAddress =3D '0' for Arch in ArchList: if GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVari= able.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVar= iable.ToolChainTag].RtBaseAddress: RtAddress =3D GenFdsGlobalVariable.WorkSpace.BuildObject[G= enFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, = GenFdsGlobalVariable.ToolChainTag].RtBaseAddress + with open(GenFdsGlobalVariable.FvAddressFileName, 'w') as FvAddres= sFile: + # + # Add [Options] + # + FvAddressFile.writelines("[options]" + T_CHAR_LF) + FvAddressFile.writelines("EFI_BOOT_DRIVER_BASE_ADDRESS =3D " += \ + BsAddress + \ + T_CHAR_LF) + FvAddressFile.writelines("EFI_RUNTIME_DRIVER_BASE_ADDRESS =3D = " + \ + RtAddress + \ + T_CHAR_LF) =20 - FvAddressFile.writelines("EFI_RUNTIME_DRIVER_BASE_ADDRESS =3D " + \ - RtAddress + \ - T_CHAR_LF) - - FvAddressFile.close() =20 def SetEnv(FdfParser, WorkSpace, ArchList, GlobalData): GenFdsGlobalVariable.ModuleFile =3D WorkSpace.ModuleFile @@ -361,11 +357,6 @@ class GenFdsGlobalVariable: # Create FV Address inf file # GenFdsGlobalVariable.FvAddressFileName =3D os.path.join(GenFdsGlob= alVariable.FfsDir, 'FvAddress.inf') - FvAddressFile =3D open(GenFdsGlobalVariable.FvAddressFileName, 'w') - # - # Add [Options] - # - FvAddressFile.writelines("[options]" + T_CHAR_LF) BsAddress =3D '0' for Arch in ArchList: BsAddress =3D GenFdsGlobalVariable.WorkSpace.BuildObject[GenFd= sGlobalVariable.ActivePlatform, Arch, @@ -373,11 +364,6 @@ class GenFdsGlobalVariable: GlobalD= ata.gGlobalDefines["TOOL_CHAIN_TAG"]].BsBaseAddress if BsAddress: break - - FvAddressFile.writelines("EFI_BOOT_DRIVER_BASE_ADDRESS =3D " + \ - BsAddress + \ - T_CHAR_LF) - RtAddress =3D '0' for Arch in ArchList: if GenFdsGlobalVariable.WorkSpace.BuildObject[ @@ -386,12 +372,17 @@ class GenFdsGlobalVariable: RtAddress =3D GenFdsGlobalVariable.WorkSpace.BuildObject[ GenFdsGlobalVariable.ActivePlatform, Arch, GlobalData.= gGlobalDefines['TARGET'], GlobalData.gGlobalDefines["TOOL_CHAIN_TAG"]].RtBaseAdd= ress - - FvAddressFile.writelines("EFI_RUNTIME_DRIVER_BASE_ADDRESS =3D " + \ - RtAddress + \ - T_CHAR_LF) - - FvAddressFile.close() + with open(GenFdsGlobalVariable.FvAddressFileName, 'w') as FvAddres= sFile: + # + # Add [Options] + # + FvAddressFile.writelines("[options]" + T_CHAR_LF) + FvAddressFile.writelines("EFI_BOOT_DRIVER_BASE_ADDRESS =3D " += \ + BsAddress + \ + T_CHAR_LF) + FvAddressFile.writelines("EFI_RUNTIME_DRIVER_BASE_ADDRESS =3D = " + \ + RtAddress + \ + T_CHAR_LF) =20 ## ReplaceWorkspaceMacro() # diff --git a/BaseTools/Source/Python/GenFds/GuidSection.py b/BaseTools/Sour= ce/Python/GenFds/GuidSection.py index bc95c7cd9d42..28571292f5a6 100644 --- a/BaseTools/Source/Python/GenFds/GuidSection.py +++ b/BaseTools/Source/Python/GenFds/GuidSection.py @@ -202,33 +202,28 @@ class GuidSection(GuidSectionClassObject) : if not os.path.exists(TempFile) : EdkLogger.error("GenFds", COMMAND_FAILURE, 'Fail to ca= ll %s, no output file was generated' % ExternalTool) =20 - FileHandleIn =3D open(DummyFile, 'rb') - FileHandleIn.seek(0, 2) - InputFileSize =3D FileHandleIn.tell() + with open(DummyFile, 'rb') as FileHandleIn, open(TempFile,= 'rb') as FileHandleOut: + FileHandleIn.seek(0, 2) + InputFileSize =3D FileHandleIn.tell() + FileHandleOut.seek(0, 2) + TempFileSize =3D FileHandleOut.tell() =20 - FileHandleOut =3D open(TempFile, 'rb') - FileHandleOut.seek(0, 2) - TempFileSize =3D FileHandleOut.tell() + Attribute =3D [] + HeaderLength =3D None + if self.ExtraHeaderSize !=3D -1: + HeaderLength =3D str(self.ExtraHeaderSize) =20 - Attribute =3D [] - HeaderLength =3D None - if self.ExtraHeaderSize !=3D -1: - HeaderLength =3D str(self.ExtraHeaderSize) - - if self.ProcessRequired =3D=3D "NONE" and HeaderLength is = None: - if TempFileSize > InputFileSize: - FileHandleIn.seek(0) - BufferIn =3D FileHandleIn.read() - FileHandleOut.seek(0) - BufferOut =3D FileHandleOut.read() - if BufferIn =3D=3D BufferOut[TempFileSize - InputF= ileSize:]: - HeaderLength =3D str(TempFileSize - InputFileS= ize) - #auto sec guided attribute with process required - if HeaderLength is None: - Attribute.append('PROCESSING_REQUIRED') - - FileHandleIn.close() - FileHandleOut.close() + if self.ProcessRequired =3D=3D "NONE" and HeaderLength= is None: + if TempFileSize > InputFileSize: + FileHandleIn.seek(0) + BufferIn =3D FileHandleIn.read() + FileHandleOut.seek(0) + BufferOut =3D FileHandleOut.read() + if BufferIn =3D=3D BufferOut[TempFileSize - In= putFileSize:]: + HeaderLength =3D str(TempFileSize - InputF= ileSize) + #auto sec guided attribute with process required + if HeaderLength is None: + Attribute.append('PROCESSING_REQUIRED') =20 if FirstCall and 'PROCESSING_REQUIRED' in Attribute: # Guided data by -z option on first call is the proces= s required data. Call the guided tool with the real option. diff --git a/BaseTools/Source/Python/GenFds/Region.py b/BaseTools/Source/Py= thon/GenFds/Region.py index 9d632b6321e2..e67d056cc178 100644 --- a/BaseTools/Source/Python/GenFds/Region.py +++ b/BaseTools/Source/Python/GenFds/Region.py @@ -159,9 +159,8 @@ class Region(RegionClassObject): EdkLogger.error("GenFds", GENFDS_ERROR, "Size of FV File (%s) is large= r than Region Size 0x%X specified." \ % (RegionData, Size)) - BinFile =3D open(FileName, 'rb') - Buffer.write(BinFile.read()) - BinFile.close() + with open(FileName, 'rb') as BinFile: + Buffer.write(BinFile.read()) Size =3D Size - FileLength # # Pad the left buffer @@ -213,9 +212,8 @@ class Region(RegionClassObject): EdkLogger.error("GenFds", GENFDS_ERROR, "Size 0x%X of Capsule File (%s) is lar= ger than Region Size 0x%X specified." \ % (FileLength, RegionData, Size)) - BinFile =3D open(FileName, 'rb') - Buffer.write(BinFile.read()) - BinFile.close() + with open(FileName, 'rb') as BinFile: + Buffer.write(BinFile.read()) Size =3D Size - FileLength # # Pad the left buffer @@ -245,9 +243,8 @@ class Region(RegionClassObject): "Size of File (%s) is larger than Regi= on Size 0x%X specified." \ % (RegionData, Size)) GenFdsGlobalVariable.InfLogger(' Region File Name =3D %s= ' % RegionData) - BinFile =3D open(RegionData, 'rb') - Buffer.write(BinFile.read()) - BinFile.close() + with open(RegionData, 'rb') as BinFile: + Buffer.write(BinFile.read()) Size =3D Size - FileLength # # Pad the left buffer diff --git a/BaseTools/Source/Python/GenFds/Vtf.py b/BaseTools/Source/Pytho= n/GenFds/Vtf.py index 18ea37b9afdd..291070827b78 100644 --- a/BaseTools/Source/Python/GenFds/Vtf.py +++ b/BaseTools/Source/Python/GenFds/Vtf.py @@ -1,7 +1,7 @@ ## @file # process VTF generation # -# Copyright (c) 2007 - 2014, 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 B= SD License @@ -67,81 +67,79 @@ class Vtf (VtfClassObject): def GenBsfInf (self): FvList =3D self.GetFvList() self.BsfInfName =3D os.path.join(GenFdsGlobalVariable.FvDir, self.= UiName + '.inf') - BsfInf =3D open(self.BsfInfName, 'w+') - if self.ResetBin is not None: - BsfInf.writelines ("[OPTIONS]" + T_CHAR_LF) - BsfInf.writelines ("IA32_RST_BIN" + \ - " =3D " + \ - GenFdsGlobalVariable.MacroExtend(GenFdsGlob= alVariable.ReplaceWorkspaceMacro(self.ResetBin)) + \ - T_CHAR_LF) - BsfInf.writelines (T_CHAR_LF) - - BsfInf.writelines ("[COMPONENTS]" + T_CHAR_LF) - - for ComponentObj in self.ComponentStatementList : - BsfInf.writelines ("COMP_NAME" + \ - " =3D " + \ - ComponentObj.CompName + \ - T_CHAR_LF) - if ComponentObj.CompLoc.upper() =3D=3D 'NONE': - BsfInf.writelines ("COMP_LOC" + \ + with open(self.BsfInfName, 'w') as BsfInf: + if self.ResetBin is not None: + BsfInf.writelines ("[OPTIONS]" + T_CHAR_LF) + BsfInf.writelines ("IA32_RST_BIN" + \ " =3D " + \ - 'N' + \ + GenFdsGlobalVariable.MacroExtend(GenFds= GlobalVariable.ReplaceWorkspaceMacro(self.ResetBin)) + \ T_CHAR_LF) + BsfInf.writelines (T_CHAR_LF) + + BsfInf.writelines ("[COMPONENTS]" + T_CHAR_LF) =20 - elif ComponentObj.FilePos is not None: - BsfInf.writelines ("COMP_LOC" + \ + for ComponentObj in self.ComponentStatementList : + BsfInf.writelines ("COMP_NAME" + \ " =3D " + \ - ComponentObj.FilePos + \ + ComponentObj.CompName + \ T_CHAR_LF) - else: - Index =3D FvList.index(ComponentObj.CompLoc.upper()) - if Index =3D=3D 0: + if ComponentObj.CompLoc.upper() =3D=3D 'NONE': BsfInf.writelines ("COMP_LOC" + \ " =3D " + \ - 'F' + \ + 'N' + \ T_CHAR_LF) - elif Index =3D=3D 1: + + elif ComponentObj.FilePos is not None: BsfInf.writelines ("COMP_LOC" + \ " =3D " + \ - 'S' + \ + ComponentObj.FilePos + \ T_CHAR_LF) + else: + Index =3D FvList.index(ComponentObj.CompLoc.upper()) + if Index =3D=3D 0: + BsfInf.writelines ("COMP_LOC" + \ + " =3D " + \ + 'F' + \ + T_CHAR_LF) + elif Index =3D=3D 1: + BsfInf.writelines ("COMP_LOC" + \ + " =3D " + \ + 'S' + \ + T_CHAR_LF) =20 - BsfInf.writelines ("COMP_TYPE" + \ - " =3D " + \ - ComponentObj.CompType + \ - T_CHAR_LF) - BsfInf.writelines ("COMP_VER" + \ - " =3D " + \ - ComponentObj.CompVer + \ - T_CHAR_LF) - BsfInf.writelines ("COMP_CS" + \ - " =3D " + \ - ComponentObj.CompCs + \ - T_CHAR_LF) - - BinPath =3D ComponentObj.CompBin - if BinPath !=3D '-': - BinPath =3D GenFdsGlobalVariable.MacroExtend(GenFdsGlobalV= ariable.ReplaceWorkspaceMacro(BinPath)) - BsfInf.writelines ("COMP_BIN" + \ - " =3D " + \ - BinPath + \ - T_CHAR_LF) + BsfInf.writelines ("COMP_TYPE" + \ + " =3D " + \ + ComponentObj.CompType + \ + T_CHAR_LF) + BsfInf.writelines ("COMP_VER" + \ + " =3D " + \ + ComponentObj.CompVer + \ + T_CHAR_LF) + BsfInf.writelines ("COMP_CS" + \ + " =3D " + \ + ComponentObj.CompCs + \ + T_CHAR_LF) =20 - SymPath =3D ComponentObj.CompSym - if SymPath !=3D '-': - SymPath =3D GenFdsGlobalVariable.MacroExtend(GenFdsGlobalV= ariable.ReplaceWorkspaceMacro(SymPath)) - BsfInf.writelines ("COMP_SYM" + \ - " =3D " + \ - SymPath + \ - T_CHAR_LF) - BsfInf.writelines ("COMP_SIZE" + \ - " =3D " + \ - ComponentObj.CompSize + \ - T_CHAR_LF) - BsfInf.writelines (T_CHAR_LF) + BinPath =3D ComponentObj.CompBin + if BinPath !=3D '-': + BinPath =3D GenFdsGlobalVariable.MacroExtend(GenFdsGlo= balVariable.ReplaceWorkspaceMacro(BinPath)) + BsfInf.writelines ("COMP_BIN" + \ + " =3D " + \ + BinPath + \ + T_CHAR_LF) =20 - BsfInf.close() + SymPath =3D ComponentObj.CompSym + if SymPath !=3D '-': + SymPath =3D GenFdsGlobalVariable.MacroExtend(GenFdsGlo= balVariable.ReplaceWorkspaceMacro(SymPath)) + BsfInf.writelines ("COMP_SYM" + \ + " =3D " + \ + SymPath + \ + T_CHAR_LF) + BsfInf.writelines ("COMP_SIZE" + \ + " =3D " + \ + ComponentObj.CompSize + \ + T_CHAR_LF) + BsfInf.writelines (T_CHAR_LF) =20 ## GenFvList() method # diff --git a/BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py b= /BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py index f40c8bd01b23..aa61bc00f277 100644 --- a/BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py +++ b/BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py @@ -46,13 +46,13 @@ def parsePcdInfoFromMapFile(mapfilepath, efifilepath): """ lines =3D [] try: - f =3D open(mapfilepath, 'r') - lines =3D f.readlines() - f.close() + with open(mapfilepath, 'r') as f: + lines =3D f.readlines() except: return None =20 - if len(lines) =3D=3D 0: return None + if len(lines) =3D=3D 0:=20 + return None firstline =3D lines[0].strip() if (firstline.startswith("Archive member included ") and firstline.endswith(" file (symbol)")): @@ -190,18 +190,13 @@ def _parseGeneral(lines, efifilepath): =20 def generatePcdTable(list, pcdpath): try: - f =3D open(pcdpath, 'w') + with open(pcdpath, 'w') as f: + f.write('PCD Name Offset Section Name= \r\n') + for pcditem in list: + f.write('%-30s 0x%-08X %-6s\r\n' % (pcditem[0], pcditem[1]= , pcditem[2])) except: pass =20 - f.write('PCD Name Offset Section Name\r\n') - =20 - for pcditem in list: - f.write('%-30s 0x%-08X %-6s\r\n' % (pcditem[0], pcditem[1], pcdite= m[2])) - f.close() - - #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath=20 - if __name__ =3D=3D '__main__': UsageString =3D "%prog -m -e -o " AdditionalNotes =3D "\nPCD table is generated in file name with .Binar= yPcdTable.txt postfix" diff --git a/BaseTools/Source/Python/PatchPcdValue/PatchPcdValue.py b/BaseT= ools/Source/Python/PatchPcdValue/PatchPcdValue.py index cf2fc7c4f70a..76fef41176ac 100644 --- a/BaseTools/Source/Python/PatchPcdValue/PatchPcdValue.py +++ b/BaseTools/Source/Python/PatchPcdValue/PatchPcdValue.py @@ -49,10 +49,9 @@ def PatchBinaryFile(FileName, ValueOffset, TypeName, Val= ueString, MaxSize=3D0): # # Length of Binary File # - FileHandle =3D open(FileName, 'rb') - FileHandle.seek (0, 2) - FileLength =3D FileHandle.tell() - FileHandle.close() + with open(FileName, 'rb') as FileHandle: + FileHandle.seek (0, 2) + FileLength =3D FileHandle.tell() # # Unify string to upper string # @@ -85,10 +84,9 @@ def PatchBinaryFile(FileName, ValueOffset, TypeName, Val= ueString, MaxSize=3D0): # # Read binary file into array # - FileHandle =3D open(FileName, 'rb') - ByteArray =3D array.array('B') - ByteArray.fromfile(FileHandle, FileLength) - FileHandle.close() + with open(FileName, 'rb') as FileHandle: + ByteArray =3D array.array('B') + ByteArray.fromfile(FileHandle, FileLength) OrigByteList =3D ByteArray.tolist() ByteList =3D ByteArray.tolist() # @@ -193,9 +191,8 @@ def PatchBinaryFile(FileName, ValueOffset, TypeName, Va= lueString, MaxSize=3D0): if ByteList !=3D OrigByteList: ByteArray =3D array.array('B') ByteArray.fromlist(ByteList) - FileHandle =3D open(FileName, 'wb') - ByteArray.tofile(FileHandle) - FileHandle.close() + with open(FileName, 'wb') as FileHandle: + ByteArray.tofile(FileHandle) return 0, "Patch Value into File %s successfully." % (FileName) =20 ## Parse command line options diff --git a/BaseTools/Source/Python/Table/TableReport.py b/BaseTools/Sourc= e/Python/Table/TableReport.py index 9ce1d0aa2518..194c733033c7 100644 --- a/BaseTools/Source/Python/Table/TableReport.py +++ b/BaseTools/Source/Python/Table/TableReport.py @@ -1,7 +1,7 @@ ## @file # This file is used to create/update/query/erase table for ECC reports # -# Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.
+# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BS= D License # which accompanies this distribution. The full text of the license may b= e found at @@ -100,31 +100,30 @@ class TableReport(Table): # def ToCSV(self, Filename=3D'Report.csv'): try: - File =3D open(Filename, 'w+') - File.write("""No, Error Code, Error Message, File, LineNo, Oth= er Error Message\n""") - RecordSet =3D self.Query() - Index =3D 0 - for Record in RecordSet: - Index =3D Index + 1 - ErrorID =3D Record[1] - OtherMsg =3D Record[2] - BelongsToTable =3D Record[3] - BelongsToItem =3D Record[4] - IsCorrected =3D Record[5] - SqlCommand =3D '' - if BelongsToTable =3D=3D 'File': - SqlCommand =3D """select 1, FullPath from %s where ID = =3D %s - """ % (BelongsToTable, BelongsToItem) - else: - SqlCommand =3D """select A.StartLine, B.FullPath from = %s as A, File as B - where A.ID =3D %s and B.ID =3D A.Belon= gsToFile + with open(Filename, 'w') as File: + File.write("""No, Error Code, Error Message, File, LineNo,= Other Error Message\n""") + RecordSet =3D self.Query() + Index =3D 0 + for Record in RecordSet: + Index =3D Index + 1 + ErrorID =3D Record[1] + OtherMsg =3D Record[2] + BelongsToTable =3D Record[3] + BelongsToItem =3D Record[4] + IsCorrected =3D Record[5] + SqlCommand =3D '' + if BelongsToTable =3D=3D 'File': + SqlCommand =3D """select 1, FullPath from %s where= ID =3D %s """ % (BelongsToTable, BelongsToItem) - NewRecord =3D self.Exec(SqlCommand) - if NewRecord !=3D []: - File.write("""%s,%s,"%s",%s,%s,"%s"\n""" % (Index, Err= orID, EccToolError.gEccErrorMessage[ErrorID], NewRecord[0][1], NewRecord[0]= [0], OtherMsg)) - EdkLogger.quiet("%s(%s): [%s]%s %s" % (NewRecord[0][1]= , NewRecord[0][0], ErrorID, EccToolError.gEccErrorMessage[ErrorID], OtherMs= g)) + else: + SqlCommand =3D """select A.StartLine, B.FullPath f= rom %s as A, File as B + where A.ID =3D %s and B.ID =3D A.B= elongsToFile + """ % (BelongsToTable, BelongsToItem) + NewRecord =3D self.Exec(SqlCommand) + if NewRecord !=3D []: + File.write("""%s,%s,"%s",%s,%s,"%s"\n""" % (Index,= ErrorID, EccToolError.gEccErrorMessage[ErrorID], NewRecord[0][1], NewRecor= d[0][0], OtherMsg)) + EdkLogger.quiet("%s(%s): [%s]%s %s" % (NewRecord[0= ][1], NewRecord[0][0], ErrorID, EccToolError.gEccErrorMessage[ErrorID], Oth= erMsg)) =20 - File.close() except IOError: NewFilename =3D 'Report_' + time.strftime("%Y%m%d_%H%M%S.csv",= time.localtime()) EdkLogger.warn("ECC", "The report file %s is locked by other p= rogress, use %s instead!" % (Filename, NewFilename)) diff --git a/BaseTools/Source/Python/TargetTool/TargetTool.py b/BaseTools/S= ource/Python/TargetTool/TargetTool.py index ecac316b7a3a..5c463df6bce5 100644 --- a/BaseTools/Source/Python/TargetTool/TargetTool.py +++ b/BaseTools/Source/Python/TargetTool/TargetTool.py @@ -58,22 +58,21 @@ class TargetTool(): def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCh= aracter): """Convert a text file to a dictionary of (name:value) pairs.""" try: - f =3D open(FileName,'r') - for Line in f: - if Line.startswith(CommentCharacter) or Line.strip() =3D= =3D '': - continue - LineList =3D Line.split(KeySplitCharacter,1) - if len(LineList) >=3D 2: - Key =3D LineList[0].strip() - if Key.startswith(CommentCharacter) =3D=3D False and K= ey in self.TargetTxtDictionary: - if Key =3D=3D TAB_TAT_DEFINES_ACTIVE_PLATFORM or K= ey =3D=3D TAB_TAT_DEFINES_TOOL_CHAIN_CONF \ - or Key =3D=3D TAB_TAT_DEFINES_MAX_CONCURRENT_THR= EAD_NUMBER \ - or Key =3D=3D TAB_TAT_DEFINES_ACTIVE_MODULE: - self.TargetTxtDictionary[Key] =3D LineList[1].= replace('\\', '/').strip() - elif Key =3D=3D TAB_TAT_DEFINES_TARGET or Key =3D= =3D TAB_TAT_DEFINES_TARGET_ARCH \ - or Key =3D=3D TAB_TAT_DEFINES_TOOL_CHAIN_TAG or = Key =3D=3D TAB_TAT_DEFINES_BUILD_RULE_CONF: - self.TargetTxtDictionary[Key] =3D LineList[1].= split() - f.close() + with open(FileName,'r') as f: + for Line in f: + if Line.startswith(CommentCharacter) or Line.strip() = =3D=3D '': + continue + LineList =3D Line.split(KeySplitCharacter,1) + if len(LineList) >=3D 2: + Key =3D LineList[0].strip() + if Key.startswith(CommentCharacter) =3D=3D False a= nd Key in self.TargetTxtDictionary: + if Key =3D=3D TAB_TAT_DEFINES_ACTIVE_PLATFORM = or Key =3D=3D TAB_TAT_DEFINES_TOOL_CHAIN_CONF \ + or Key =3D=3D TAB_TAT_DEFINES_MAX_CONCURRENT= _THREAD_NUMBER \ + or Key =3D=3D TAB_TAT_DEFINES_ACTIVE_MODULE: + self.TargetTxtDictionary[Key] =3D LineList= [1].replace('\\', '/').strip() + elif Key =3D=3D TAB_TAT_DEFINES_TARGET or Key = =3D=3D TAB_TAT_DEFINES_TARGET_ARCH \ + or Key =3D=3D TAB_TAT_DEFINES_TOOL_CHAIN_TAG= or Key =3D=3D TAB_TAT_DEFINES_BUILD_RULE_CONF: + self.TargetTxtDictionary[Key] =3D LineList= [1].split() return 0 except: last_type, last_value, last_tb =3D sys.exc_info() @@ -94,42 +93,38 @@ class TargetTool(): =20 def RWFile(self, CommentCharacter, KeySplitCharacter, Num): try: - fr =3D open(self.FileName, 'r') - fw =3D open(os.path.normpath(os.path.join(self.WorkSpace, 'Con= f\\targetnew.txt')), 'w') - - existKeys =3D [] - for Line in fr: - if Line.startswith(CommentCharacter) or Line.strip() =3D= =3D '': - fw.write(Line) - else: - LineList =3D Line.split(KeySplitCharacter,1) - if len(LineList) >=3D 2: - Key =3D LineList[0].strip() - if Key.startswith(CommentCharacter) =3D=3D False a= nd Key in self.TargetTxtDictionary: - if Key not in existKeys: - existKeys.append(Key) - else: - print "Warning: Found duplicate key item i= n original configuration files!" - =20 - if Num =3D=3D 0: - Line =3D "%-30s =3D \n" % Key - else: - ret =3D GetConfigureKeyValue(self, Key) - if ret is not None: - Line =3D ret - fw.write(Line) - for key in self.TargetTxtDictionary: - if key not in existKeys: - print "Warning: %s does not exist in original configur= ation file" % key - Line =3D GetConfigureKeyValue(self, key) - if Line is None: - Line =3D "%-30s =3D " % key - fw.write(Line) + with open(self.FileName, 'r') as fr: + FileRead =3D fr.readlines() + with open(self.FileName, 'w') as fw: + existKeys =3D [] + for Line in FileRead: + if Line.startswith(CommentCharacter) or Line.strip() = =3D=3D '': + fw.write(Line) + else: + LineList =3D Line.split(KeySplitCharacter,1) + if len(LineList) >=3D 2: + Key =3D LineList[0].strip() + if Key.startswith(CommentCharacter) =3D=3D Fal= se and Key in self.TargetTxtDictionary: + if Key not in existKeys: + existKeys.append(Key) + else: + print "Warning: Found duplicate key it= em in original configuration files!" + =20 + if Num =3D=3D 0: + Line =3D "%-30s =3D \n" % Key + else: + ret =3D GetConfigureKeyValue(self, Key) + if ret is not None: + Line =3D ret + fw.write(Line) + for key in self.TargetTxtDictionary: + if key not in existKeys: + print "Warning: %s does not exist in original conf= iguration file" % key + Line =3D GetConfigureKeyValue(self, key) + if Line is None: + Line =3D "%-30s =3D " % key + fw.write(Line) =20 - fr.close() - fw.close() - os.remove(self.FileName) - os.rename(os.path.normpath(os.path.join(self.WorkSpace, 'Conf\= \targetnew.txt')), self.FileName) =20 except: last_type, last_value, last_tb =3D sys.exc_info() diff --git a/BaseTools/Source/Python/Trim/Trim.py b/BaseTools/Source/Python= /Trim/Trim.py index d2e6d317676c..a92df52979c6 100644 --- a/BaseTools/Source/Python/Trim/Trim.py +++ b/BaseTools/Source/Python/Trim/Trim.py @@ -141,14 +141,12 @@ gIncludedAslFile =3D [] def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong): CreateDirectory(os.path.dirname(Target)) try: - f =3D open (Source, 'r') + with open (Source, 'r') as f: + # read whole file + Lines =3D f.readlines() except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=3DSource) =20 - # read whole file - Lines =3D f.readlines() - f.close() - PreprocessedFile =3D "" InjectedFile =3D "" LineIndexOfOriginalFile =3D None @@ -243,11 +241,10 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, = TrimLong): =20 # save to file try: - f =3D open (Target, 'wb') + with open (Target, 'wb') as f: + f.writelines(NewLines) except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=3DTarget) - f.writelines(NewLines) - f.close() =20 ## Trim preprocessed VFR file # @@ -261,12 +258,11 @@ def TrimPreprocessedVfr(Source, Target): CreateDirectory(os.path.dirname(Target)) =20 try: - f =3D open (Source,'r') + with open (Source,'r') as f: + # read whole file + Lines =3D f.readlines() except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=3DSource) - # read whole file - Lines =3D f.readlines() - f.close() =20 FoundTypedef =3D False Brace =3D 0 @@ -310,11 +306,10 @@ def TrimPreprocessedVfr(Source, Target): =20 # save all lines trimmed try: - f =3D open (Target,'w') + with open (Target,'w') as f: + f.writelines(Lines) except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=3DTarget) - f.writelines(Lines) - f.close() =20 ## Read the content ASL file, including ASL included, recursively # @@ -340,7 +335,8 @@ def DoInclude(Source, Indent=3D'', IncludePathList=3D[]= , LocalSearchPath=3DNone): for IncludePath in SearchPathList: IncludeFile =3D os.path.join(IncludePath, Source) if os.path.isfile(IncludeFile): - F =3D open(IncludeFile, "r") + with open(IncludeFile, "r") as OpenFile: + FileLines =3D OpenFile.readlines() break else: EdkLogger.error("Trim", "Failed to find include file %s" % Sou= rce) @@ -356,7 +352,7 @@ def DoInclude(Source, Indent=3D'', IncludePathList=3D[]= , LocalSearchPath=3DNone): return [] gIncludedAslFile.append(IncludeFile) =20 - for Line in F: + for Line in FileLines: LocalSearchPath =3D None Result =3D gAslIncludePattern.findall(Line) if len(Result) =3D=3D 0: @@ -375,7 +371,6 @@ def DoInclude(Source, Indent=3D'', IncludePathList=3D[]= , LocalSearchPath=3DNone): NewFileContent.append("\n") =20 gIncludedAslFile.pop() - F.close() =20 return NewFileContent =20 @@ -425,12 +420,11 @@ def TrimAslFile(Source, Target, IncludePathFile): =20 # save all lines trimmed try: - f =3D open (Target,'w') + with open (Target,'w') as f: + f.writelines(Lines) except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=3DTarget) =20 - f.writelines(Lines) - f.close() =20 def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile): VfrNameList =3D [] @@ -450,11 +444,6 @@ def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile= ): if not VfrUniOffsetList: return =20 - try: - fInputfile =3D open(OutputFile, "wb+", 0) - except: - EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %= s" %OutputFile, None) - # Use a instance of StringIO to cache data fStringIO =3D StringIO.StringIO('') =20 @@ -483,16 +472,16 @@ def GenerateVfrBinSec(ModuleName, DebugDir, OutputFil= e): VfrValue =3D pack ('Q', int (Item[1], 16)) fStringIO.write (VfrValue) =20 - # - # write data into file. - # - try : - fInputfile.write (fStringIO.getvalue()) + try: + with open(OutputFile, "wb+", 0) as fInputfile: + try : + fInputfile.write (fStringIO.getvalue()) + except: + EdkLogger.error("Trim", FILE_WRITE_FAILURE, "Write data to= file %s failed, please check whether the file been locked or using by othe= r applications." %OutputFile, None) except: - EdkLogger.error("Trim", FILE_WRITE_FAILURE, "Write data to file %s= failed, please check whether the file been locked or using by other applic= ations." %OutputFile, None) + EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %= s" %OutputFile, None) =20 fStringIO.close () - fInputfile.close () =20 ## Trim EDK source code file(s) # @@ -560,12 +549,11 @@ def TrimEdkSourceCode(Source, Target): CreateDirectory(os.path.dirname(Target)) =20 try: - f =3D open (Source,'rb') + with open (Source,'rb') as f: + # read whole file + Lines =3D f.read() except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=3DSource) - # read whole file - Lines =3D f.read() - f.close() =20 NewLines =3D None for Re,Repl in gImportCodePatterns: @@ -579,11 +567,10 @@ def TrimEdkSourceCode(Source, Target): return =20 try: - f =3D open (Target,'wb') + with open (Target,'wb') as f: + f.write(NewLines) except: EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=3DTarget) - f.write(NewLines) - f.close() =20 =20 ## Parse command line options diff --git a/BaseTools/Source/Python/Workspace/DscBuildData.py b/BaseTools/= Source/Python/Workspace/DscBuildData.py index edae4e5e7fcf..a472e13da7e0 100644 --- a/BaseTools/Source/Python/Workspace/DscBuildData.py +++ b/BaseTools/Source/Python/Workspace/DscBuildData.py @@ -118,13 +118,10 @@ def GetDependencyList(FileStack,SearchPathList): CurrentFileDependencyList =3D DepDb[F] else: try: - Fd =3D open(F, 'r') - FileContent =3D Fd.read() + with open(F, 'r') as Fd: + FileContent =3D Fd.read() except BaseException, X: EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=3DF = + "\n\t" + str(X)) - finally: - if "Fd" in dir(locals()): - Fd.close() =20 if len(FileContent) =3D=3D 0: continue @@ -2109,9 +2106,8 @@ class DscBuildData(PlatformBuildClassObject): MessageGroup =3D [] if returncode <>0: CAppBaseFileName =3D os.path.join(self.OutputPath, PcdValueIni= tName) - File =3D open (CAppBaseFileName + '.c', 'r') - FileData =3D File.readlines() - File.close() + with open (CAppBaseFileName + '.c', 'r') as File: + FileData =3D File.readlines() for Message in Messages: if " error" in Message or "warning" in Message: FileInfo =3D Message.strip().split('(') @@ -2155,9 +2151,8 @@ class DscBuildData(PlatformBuildClassObject): if returncode <> 0: EdkLogger.warn('Build', COMMAND_FAILURE, 'Can not collect = output from command: %s' % Command) =20 - File =3D open (OutputValueFile, 'r') - FileBuffer =3D File.readlines() - File.close() + with open (OutputValueFile, 'r') as File: + FileBuffer =3D File.readlines() =20 StructurePcdSet =3D [] for Pcd in FileBuffer: diff --git a/BaseTools/Source/Python/build/BuildReport.py b/BaseTools/Sourc= e/Python/build/BuildReport.py index 4e1d0cc18c16..534d73e15c11 100644 --- a/BaseTools/Source/Python/build/BuildReport.py +++ b/BaseTools/Source/Python/build/BuildReport.py @@ -291,18 +291,18 @@ class DepexParser(object): # @param DepexFileName The file name of binary dependency expression= file. # def ParseDepexFile(self, DepexFileName): - DepexFile =3D open(DepexFileName, "rb") DepexStatement =3D [] - OpCode =3D DepexFile.read(1) - while OpCode: - Statement =3D gOpCodeList[struct.unpack("B", OpCode)[0]] - if Statement in ["BEFORE", "AFTER", "PUSH"]: - GuidValue =3D "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02= X%02X" % \ - struct.unpack(PACK_PATTERN_GUID, DepexFile.rea= d(16)) - GuidString =3D self._GuidDb.get(GuidValue, GuidValue) - Statement =3D "%s %s" % (Statement, GuidString) - DepexStatement.append(Statement) + with open(DepexFileName, "rb") as DepexFile: OpCode =3D DepexFile.read(1) + while OpCode: + Statement =3D gOpCodeList[struct.unpack("B", OpCode)[0]] + if Statement in ["BEFORE", "AFTER", "PUSH"]: + GuidValue =3D "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02= X%02X%02X" % \ + struct.unpack(PACK_PATTERN_GUID, DepexFile= .read(16)) + GuidString =3D self._GuidDb.get(GuidValue, GuidValue) + Statement =3D "%s %s" % (Statement, GuidString) + DepexStatement.append(Statement) + OpCode =3D DepexFile.read(1) =20 return DepexStatement =20 @@ -629,14 +629,14 @@ class ModuleReport(object): FwReportFileName =3D os.path.join(self._BuildDir, "DEBUG", self.Mo= duleName + ".txt") if os.path.isfile(FwReportFileName): try: - FileContents =3D open(FwReportFileName).read() - Match =3D gModuleSizePattern.search(FileContents) - if Match: - self.Size =3D int(Match.group(1)) + with open(FwReportFileName).read() as FileContents: + Match =3D gModuleSizePattern.search(FileContents) + if Match: + self.Size =3D int(Match.group(1)) =20 - Match =3D gTimeStampPattern.search(FileContents) - if Match: - self.BuildTimeStamp =3D datetime.fromtimestamp(int(Mat= ch.group(1))) + Match =3D gTimeStampPattern.search(FileContents) + if Match: + self.BuildTimeStamp =3D datetime.fromtimestamp(int= (Match.group(1))) except IOError: EdkLogger.warn(None, "Fail to read report file", FwReportF= ileName) =20 @@ -1527,14 +1527,12 @@ class PredictionReport(object): GuidList =3D os.path.join(self._EotDir, "GuidList.txt") DispatchList =3D os.path.join(self._EotDir, "Dispatch.txt") =20 - TempFile =3D open(SourceList, "w+") - for Item in self._SourceList: - FileWrite(TempFile, Item) - TempFile.close() - TempFile =3D open(GuidList, "w+") - for Key in self._GuidMap: - FileWrite(TempFile, "%s %s" % (Key, self._GuidMap[Key])) - TempFile.close() + with open(SourceList, "w") as TempFile: + for Item in self._SourceList: + FileWrite(TempFile, Item) + with open(GuidList, "w") as TempFile: + for Key in self._GuidMap: + FileWrite(TempFile, "%s %s" % (Key, self._GuidMap[Key])) =20 try: from Eot.Eot import Eot @@ -1925,23 +1923,22 @@ class FdReport(object): break =20 if os.path.isfile(self.VpdFilePath): - fd =3D open(self.VpdFilePath, "r") - Lines =3D fd.readlines() - for Line in Lines: - Line =3D Line.strip() - if len(Line) =3D=3D 0 or Line.startswith("#"): - continue - try: - PcdName, SkuId, Offset, Size, Value =3D Line.split("#"= )[0].split("|") - PcdName, SkuId, Offset, Size, Value =3D PcdName.strip(= ), SkuId.strip(), Offset.strip(), Size.strip(), Value.strip() - if Offset.lower().startswith('0x'): - Offset =3D '0x%08X' % (int(Offset, 16) + self.VPDB= aseAddress) - else: - Offset =3D '0x%08X' % (int(Offset, 10) + self.VPDB= aseAddress) - self.VPDInfoList.append("%s | %s | %s | %s | %s" % (Pc= dName, SkuId, Offset, Size, Value)) - except: - EdkLogger.error("BuildReport", CODE_ERROR, "Fail to pa= rse VPD information file %s" % self.VpdFilePath) - fd.close() + with open(self.VpdFilePath, "r") as fd: + Lines =3D fd.readlines() + for Line in Lines: + Line =3D Line.strip() + if len(Line) =3D=3D 0 or Line.startswith("#"): + continue + try: + PcdName, SkuId, Offset, Size, Value =3D Line.split= ("#")[0].split("|") + PcdName, SkuId, Offset, Size, Value =3D PcdName.st= rip(), SkuId.strip(), Offset.strip(), Size.strip(), Value.strip() + if Offset.lower().startswith('0x'): + Offset =3D '0x%08X' % (int(Offset, 16) + self.= VPDBaseAddress) + else: + Offset =3D '0x%08X' % (int(Offset, 10) + self.= VPDBaseAddress) + self.VPDInfoList.append("%s | %s | %s | %s | %s" %= (PcdName, SkuId, Offset, Size, Value)) + except: + EdkLogger.error("BuildReport", CODE_ERROR, "Fail t= o parse VPD information file %s" % self.VpdFilePath) =20 ## # Generate report for the firmware device. diff --git a/BaseTools/Source/Python/build/build.py b/BaseTools/Source/Pyth= on/build/build.py index 57a5e3525d88..0e1278f5446d 100644 --- a/BaseTools/Source/Python/build/build.py +++ b/BaseTools/Source/Python/build/build.py @@ -320,9 +320,8 @@ def LaunchCommand(Command, WorkingDir): # print out the Response file and its content when make failure RespFile =3D os.path.join(WorkingDir, 'OUTPUT', 'respfilelist.txt') if os.path.isfile(RespFile): - f =3D open(RespFile) + with open(RespFile) as f: RespContent =3D f.read() - f.close() EdkLogger.info(RespContent) =20 EdkLogger.error("build", COMMAND_FAILURE, ExtraData=3D"%s [%s]" % = (Command, WorkingDir)) @@ -1169,9 +1168,8 @@ class Build(): EdkLogger.error("Prebuild", PREBUILD_ERROR, 'Prebuild proc= ess is not success!') =20 if os.path.exists(PrebuildEnvFile): - f =3D open(PrebuildEnvFile) + with open(PrebuildEnvFile) as f: envs =3D f.readlines() - f.close() envs =3D itertools.imap(lambda l: l.split('=3D',1), envs) envs =3D itertools.ifilter(lambda l: len(l) =3D=3D 2, envs) envs =3D itertools.imap(lambda l: [i.strip() for i in l], = envs) @@ -1451,7 +1449,7 @@ class Build(): FunctionList =3D [] if os.path.exists(ImageMapTable): OrigImageBaseAddress =3D 0 - ImageMap =3D open(ImageMapTable, 'r') + with open(ImageMapTable, 'r') as ImageMap: for LinStr in ImageMap: if len (LinStr.strip()) =3D=3D 0: continue @@ -1473,7 +1471,6 @@ class Build(): # Get the real entry point address for IPF= image. # ModuleInfo.Image.EntryPoint =3D RelativeAd= dress - ImageMap.close() # # Add general information. # @@ -1528,7 +1525,7 @@ class Build(): FvMapBuffer =3D os.path.join(Wa.FvDir, FvName + '.Fv.map') if not os.path.exists(FvMapBuffer): continue - FvMap =3D open(FvMapBuffer, 'r') + with open(FvMapBuffer, 'r') as FvMap: #skip FV size information FvMap.readline() FvMap.readline() @@ -1553,8 +1550,6 @@ class Build(): if GuidString.upper() in ModuleList: MapBuffer.write('(IMAGE=3D%s)\n' % (os.path.jo= in(ModuleList[GuidString.upper()].DebugDir, ModuleList[GuidString.upper()].= Name + '.efi'))) =20 - FvMap.close() - ## Collect MAP information of all modules # def _CollectModuleMapBuffer (self, MapBuffer, ModuleList): @@ -2193,10 +2188,9 @@ class Build(): =20 # Write out GuidedSecTools.txt toolsFile =3D os.path.join(FvDir, 'GuidedSectionTools.= txt') - toolsFile =3D open(toolsFile, 'wt') + with open(toolsFile, 'w') as toolsFile: for guidedSectionTool in guidAttribs: print >> toolsFile, ' '.join(guidedSectionTool) - toolsFile.close() =20 ## Returns the full path of the tool. # --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel