]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/Core/PackageFile.py
3 # PackageFile class represents the zip file of a distribution package.
5 # Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
7 # This program and the accompanying materials are licensed and made available
8 # under the terms and conditions of the BSD License which accompanies this
9 # distribution. The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 from Logger
.ToolError
import FILE_OPEN_FAILURE
29 from Logger
.ToolError
import FILE_CHECKSUM_FAILURE
30 from Logger
.ToolError
import FILE_NOT_FOUND
31 from Logger
.ToolError
import FILE_DECOMPRESS_FAILURE
32 from Logger
.ToolError
import FILE_UNKNOWN_ERROR
33 from Logger
.ToolError
import FILE_WRITE_FAILURE
34 from Logger
.ToolError
import FILE_COMPRESS_FAILURE
35 import Logger
.Log
as Logger
36 from Logger
import StringTable
as ST
37 from Library
.Misc
import CreateDirectory
38 from Library
.Misc
import RemoveDirectory
43 def __init__(self
, FileName
, Mode
="r"):
44 self
._FileName
= FileName
45 if Mode
not in ["r", "w", "a"]:
48 self
._ZipFile
= zipfile
.ZipFile(FileName
, Mode
, \
51 for Filename
in self
._ZipFile
.namelist():
52 self
._Files
[os
.path
.normpath(Filename
)] = Filename
53 except BaseException
, Xstr
:
54 Logger
.Error("PackagingTool", FILE_OPEN_FAILURE
,
55 ExtraData
="%s (%s)" % (FileName
, str(Xstr
)))
57 BadFile
= self
._ZipFile
.testzip()
59 Logger
.Error("PackagingTool", FILE_CHECKSUM_FAILURE
,
60 ExtraData
="[%s] in %s" % (BadFile
, FileName
))
72 # @param To: the destination file
74 def Unpack(self
, ToDest
):
75 for FileN
in self
._ZipFile
.namelist():
76 ToFile
= os
.path
.normpath(os
.path
.join(ToDest
, FileN
))
77 Msg
= "%s -> %s" % (FileN
, ToFile
)
79 self
.Extract(FileN
, ToFile
)
83 # @param File: the extracted file
84 # @param ToFile: the destination file
86 def UnpackFile(self
, File
, ToFile
):
87 File
= File
.replace('\\', '/')
88 if File
in self
._ZipFile
.namelist():
89 Msg
= "%s -> %s" % (File
, ToFile
)
91 self
.Extract(File
, ToFile
)
98 # @param Which: the source path
99 # @param To: the destination path
101 def Extract(self
, Which
, ToDest
):
102 Which
= os
.path
.normpath(Which
)
103 if Which
not in self
._Files
:
104 Logger
.Error("PackagingTool", FILE_NOT_FOUND
,
105 ExtraData
="[%s] in %s" % (Which
, self
._FileName
))
107 FileContent
= self
._ZipFile
.read(self
._Files
[Which
])
108 except BaseException
, Xstr
:
109 Logger
.Error("PackagingTool", FILE_DECOMPRESS_FAILURE
,
110 ExtraData
="[%s] in %s (%s)" % (Which
, \
114 CreateDirectory(os
.path
.dirname(ToDest
))
115 if os
.path
.exists(ToDest
) and not os
.access(ToDest
, os
.W_OK
):
116 Logger
.Warn("PackagingTool", \
117 ST
.WRN_FILE_NOT_OVERWRITTEN
% ToDest
)
119 ToFile
= open(ToDest
, "wb")
120 except BaseException
, Xstr
:
121 Logger
.Error("PackagingTool", FILE_OPEN_FAILURE
,
122 ExtraData
="%s (%s)" % (ToDest
, str(Xstr
)))
125 ToFile
.write(FileContent
)
127 except BaseException
, Xstr
:
128 Logger
.Error("PackagingTool", FILE_WRITE_FAILURE
,
129 ExtraData
="%s (%s)" % (ToDest
, str(Xstr
)))
133 # @param Files: the removed files
135 def Remove(self
, Files
):
136 TmpDir
= os
.path
.join(tempfile
.gettempdir(), ".packaging")
137 if os
.path
.exists(TmpDir
):
138 RemoveDirectory(TmpDir
, True)
143 SinF
= os
.path
.normpath(SinF
)
144 if SinF
not in self
._Files
:
145 Logger
.Error("PackagingTool", FILE_NOT_FOUND
,
146 ExtraData
="%s is not in %s!" % \
147 (SinF
, self
._FileName
))
148 self
._Files
.pop(SinF
)
149 self
._ZipFile
.close()
151 self
._ZipFile
= zipfile
.ZipFile(self
._FileName
, "w", \
152 zipfile
.ZIP_DEFLATED
)
155 self
.PackFiles(self
._Files
)
157 RemoveDirectory(TmpDir
, True)
159 ## Pack the files under Top directory, the directory shown in the zipFile start from BaseDir,
160 # BaseDir should be the parent directory of the Top directory, for example,
161 # Pack(Workspace\Dir1, Workspace) will pack files under Dir1, and the path in the zipfile will
162 # start from Workspace
164 # @param Top: the top directory
165 # @param BaseDir: the base directory
167 def Pack(self
, Top
, BaseDir
):
168 if not os
.path
.isdir(Top
):
169 Logger
.Error("PackagingTool", FILE_UNKNOWN_ERROR
, \
170 "%s is not a directory!" %Top
)
175 RelaDir
= Top
[Top
.upper().find(BaseDir
.upper()).\
176 join(len(BaseDir
).join(1)):]
178 for Root
, Dirs
, Files
in os
.walk(RelaDir
):
185 if Dir
.startswith('.'):
188 if File1
.startswith('.'):
190 ExtName
= os
.path
.splitext(File1
)[1]
192 # skip '.dec', '.inf', '.dsc', '.fdf' files
194 if ExtName
.lower() in ['.dec', '.inf', '.dsc', '.fdf']:
196 FilesToPack
.append(os
.path
.join(Root
, File1
))
197 self
.PackFiles(FilesToPack
)
202 # @param Files: the files to pack
204 def PackFiles(self
, Files
):
210 # @param File: the files to pack
211 # @param ArcName: the Arc Name
213 def PackFile(self
, File
, ArcName
=None):
216 # avoid packing same file multiple times
218 if platform
.system() != 'Windows':
219 File
= File
.replace('\\', '/')
220 ZipedFilesNameList
= self
._ZipFile
.namelist()
221 for ZipedFile
in ZipedFilesNameList
:
222 if File
== os
.path
.normpath(ZipedFile
):
224 Logger
.Info("packing ..." + File
)
225 self
._ZipFile
.write(File
, ArcName
)
226 except BaseException
, Xstr
:
227 Logger
.Error("PackagingTool", FILE_COMPRESS_FAILURE
,
228 ExtraData
="%s (%s)" % (File
, str(Xstr
)))
230 ## Write data to the packed file
232 # @param Data: data to write
233 # @param ArcName: the Arc Name
235 def PackData(self
, Data
, ArcName
):
237 self
._ZipFile
.writestr(ArcName
, Data
)
238 except BaseException
, Xstr
:
239 Logger
.Error("PackagingTool", FILE_COMPRESS_FAILURE
,
240 ExtraData
="%s (%s)" % (ArcName
, str(Xstr
)))
246 self
._ZipFile
.close()