#\r
# PackageFile class represents the zip file of a distribution package.\r
#\r
-# Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
#\r
-# This program and the accompanying materials are licensed and made available \r
-# under the terms and conditions of the BSD License which accompanies this \r
-# distribution. The full text of the license may be found at \r
-# http://opensource.org/licenses/bsd-license.php\r
-#\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
#\r
\r
'''\r
from Library.Misc import CreateDirectory\r
from Library.Misc import RemoveDirectory\r
from Core.FileHook import __FileHookOpen__\r
+from Common.MultipleWorkspace import MultipleWorkspace as mws\r
\r
\r
class PackageFile:\r
self._Files = {}\r
for Filename in self._ZipFile.namelist():\r
self._Files[os.path.normpath(Filename)] = Filename\r
- except BaseException, Xstr:\r
- Logger.Error("PackagingTool", FILE_OPEN_FAILURE, \r
+ except BaseException as Xstr:\r
+ Logger.Error("PackagingTool", FILE_OPEN_FAILURE,\r
ExtraData="%s (%s)" % (FileName, str(Xstr)))\r
\r
BadFile = self._ZipFile.testzip()\r
- if BadFile != None:\r
- Logger.Error("PackagingTool", FILE_CHECKSUM_FAILURE, \r
+ if BadFile is not None:\r
+ Logger.Error("PackagingTool", FILE_CHECKSUM_FAILURE,\r
ExtraData="[%s] in %s" % (BadFile, FileName))\r
- \r
+\r
def GetZipFile(self):\r
return self._ZipFile\r
- \r
- ## Get file name \r
+\r
+ ## Get file name\r
#\r
def __str__(self):\r
return self._FileName\r
- \r
+\r
## Extract the file\r
- # \r
- # @param To: the destination file \r
+ #\r
+ # @param To: the destination file\r
#\r
def Unpack(self, ToDest):\r
for FileN in self._ZipFile.namelist():\r
Msg = "%s -> %s" % (FileN, ToFile)\r
Logger.Info(Msg)\r
self.Extract(FileN, ToFile)\r
- \r
+\r
## Extract the file\r
- # \r
- # @param File: the extracted file \r
- # @param ToFile: the destination file \r
+ #\r
+ # @param File: the extracted file\r
+ # @param ToFile: the destination file\r
#\r
def UnpackFile(self, File, ToFile):\r
File = File.replace('\\', '/')\r
Logger.Info(Msg)\r
self.Extract(File, ToFile)\r
return ToFile\r
- \r
+\r
return ''\r
- \r
+\r
## Extract the file\r
- # \r
- # @param Which: the source path \r
- # @param ToDest: the destination path \r
+ #\r
+ # @param Which: the source path\r
+ # @param ToDest: the destination path\r
#\r
def Extract(self, Which, ToDest):\r
Which = os.path.normpath(Which)\r
ExtraData="[%s] in %s" % (Which, self._FileName))\r
try:\r
FileContent = self._ZipFile.read(self._Files[Which])\r
- except BaseException, Xstr:\r
- Logger.Error("PackagingTool", FILE_DECOMPRESS_FAILURE, \r
+ except BaseException as Xstr:\r
+ Logger.Error("PackagingTool", FILE_DECOMPRESS_FAILURE,\r
ExtraData="[%s] in %s (%s)" % (Which, \\r
self._FileName, \\r
str(Xstr)))\r
return\r
else:\r
ToFile = __FileHookOpen__(ToDest, 'wb')\r
- except BaseException, Xstr:\r
- Logger.Error("PackagingTool", FILE_OPEN_FAILURE, \r
+ except BaseException as Xstr:\r
+ Logger.Error("PackagingTool", FILE_OPEN_FAILURE,\r
ExtraData="%s (%s)" % (ToDest, str(Xstr)))\r
\r
try:\r
ToFile.write(FileContent)\r
ToFile.close()\r
- except BaseException, Xstr:\r
- Logger.Error("PackagingTool", FILE_WRITE_FAILURE, \r
+ except BaseException as Xstr:\r
+ Logger.Error("PackagingTool", FILE_WRITE_FAILURE,\r
ExtraData="%s (%s)" % (ToDest, str(Xstr)))\r
\r
## Remove the file\r
- # \r
- # @param Files: the removed files \r
+ #\r
+ # @param Files: the removed files\r
#\r
def Remove(self, Files):\r
TmpDir = os.path.join(tempfile.gettempdir(), ".packaging")\r
for SinF in Files:\r
SinF = os.path.normpath(SinF)\r
if SinF not in self._Files:\r
- Logger.Error("PackagingTool", FILE_NOT_FOUND, \r
+ Logger.Error("PackagingTool", FILE_NOT_FOUND,\r
ExtraData="%s is not in %s!" % \\r
(SinF, self._FileName))\r
self._Files.pop(SinF)\r
RemoveDirectory(TmpDir, True)\r
\r
## Pack the files under Top directory, the directory shown in the zipFile start from BaseDir,\r
- # BaseDir should be the parent directory of the Top directory, for example, \r
- # Pack(Workspace\Dir1, Workspace) will pack files under Dir1, and the path in the zipfile will \r
+ # BaseDir should be the parent directory of the Top directory, for example,\r
+ # Pack(Workspace\Dir1, Workspace) will pack files under Dir1, and the path in the zipfile will\r
# start from Workspace\r
- # \r
- # @param Top: the top directory \r
- # @param BaseDir: the base directory \r
+ #\r
+ # @param Top: the top directory\r
+ # @param BaseDir: the base directory\r
#\r
def Pack(self, Top, BaseDir):\r
if not os.path.isdir(Top):\r
Cwd = os.getcwd()\r
os.chdir(BaseDir)\r
RelaDir = Top[Top.upper().find(BaseDir.upper()).\\r
- join(len(BaseDir).join(1)):] \r
+ join(len(BaseDir).join(1)):]\r
\r
for Root, Dirs, Files in os.walk(RelaDir):\r
if 'CVS' in Dirs:\r
Dirs.remove('CVS')\r
if '.svn' in Dirs:\r
Dirs.remove('.svn')\r
- \r
+\r
for Dir in Dirs:\r
if Dir.startswith('.'):\r
Dirs.remove(Dir)\r
os.chdir(Cwd)\r
\r
## Pack the file\r
- # \r
- # @param Files: the files to pack \r
+ #\r
+ # @param Files: the files to pack\r
#\r
def PackFiles(self, Files):\r
- for File1 in Files:\r
- self.PackFile(File1)\r
+ for File in Files:\r
+ Cwd = os.getcwd()\r
+ os.chdir(mws.getWs(mws.WORKSPACE, File))\r
+ self.PackFile(File)\r
+ os.chdir(Cwd)\r
\r
## Pack the file\r
- # \r
- # @param File: the files to pack \r
- # @param ArcName: the Arc Name \r
+ #\r
+ # @param File: the files to pack\r
+ # @param ArcName: the Arc Name\r
#\r
def PackFile(self, File, ArcName=None):\r
try:\r
# avoid packing same file multiple times\r
#\r
if platform.system() != 'Windows':\r
- File = File.replace('\\', '/') \r
+ File = File.replace('\\', '/')\r
ZipedFilesNameList = self._ZipFile.namelist()\r
for ZipedFile in ZipedFilesNameList:\r
if File == os.path.normpath(ZipedFile):\r
return\r
Logger.Info("packing ..." + File)\r
self._ZipFile.write(File, ArcName)\r
- except BaseException, Xstr:\r
+ except BaseException as Xstr:\r
Logger.Error("PackagingTool", FILE_COMPRESS_FAILURE,\r
ExtraData="%s (%s)" % (File, str(Xstr)))\r
\r
## Write data to the packed file\r
- # \r
- # @param Data: data to write \r
- # @param ArcName: the Arc Name \r
+ #\r
+ # @param Data: data to write\r
+ # @param ArcName: the Arc Name\r
#\r
def PackData(self, Data, ArcName):\r
try:\r
if os.path.splitext(ArcName)[1].lower() == '.pkg':\r
Data = Data.encode('utf_8')\r
self._ZipFile.writestr(ArcName, Data)\r
- except BaseException, Xstr:\r
+ except BaseException as Xstr:\r
Logger.Error("PackagingTool", FILE_COMPRESS_FAILURE,\r
ExtraData="%s (%s)" % (ArcName, str(Xstr)))\r
\r
## Close file\r
- # \r
+ #\r
#\r
def Close(self):\r
self._ZipFile.close()\r