]>
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 - 2018, Intel Corporation. All rights reserved.<BR>
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
22 from Logger
.ToolError
import FILE_OPEN_FAILURE
23 from Logger
.ToolError
import FILE_CHECKSUM_FAILURE
24 from Logger
.ToolError
import FILE_NOT_FOUND
25 from Logger
.ToolError
import FILE_DECOMPRESS_FAILURE
26 from Logger
.ToolError
import FILE_UNKNOWN_ERROR
27 from Logger
.ToolError
import FILE_WRITE_FAILURE
28 from Logger
.ToolError
import FILE_COMPRESS_FAILURE
29 import Logger
.Log
as Logger
30 from Logger
import StringTable
as ST
31 from Library
.Misc
import CreateDirectory
32 from Library
.Misc
import RemoveDirectory
33 from Core
.FileHook
import __FileHookOpen__
34 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
38 def __init__(self
, FileName
, Mode
="r"):
39 self
._FileName
= FileName
40 if Mode
not in ["r", "w", "a"]:
43 self
._ZipFile
= zipfile
.ZipFile(FileName
, Mode
, \
46 for Filename
in self
._ZipFile
.namelist():
47 self
._Files
[os
.path
.normpath(Filename
)] = Filename
48 except BaseException
as Xstr
:
49 Logger
.Error("PackagingTool", FILE_OPEN_FAILURE
,
50 ExtraData
="%s (%s)" % (FileName
, str(Xstr
)))
52 BadFile
= self
._ZipFile
.testzip()
53 if BadFile
is not None:
54 Logger
.Error("PackagingTool", FILE_CHECKSUM_FAILURE
,
55 ExtraData
="[%s] in %s" % (BadFile
, FileName
))
67 # @param To: the destination file
69 def Unpack(self
, ToDest
):
70 for FileN
in self
._ZipFile
.namelist():
71 ToFile
= os
.path
.normpath(os
.path
.join(ToDest
, FileN
))
72 Msg
= "%s -> %s" % (FileN
, ToFile
)
74 self
.Extract(FileN
, ToFile
)
78 # @param File: the extracted file
79 # @param ToFile: the destination file
81 def UnpackFile(self
, File
, ToFile
):
82 File
= File
.replace('\\', '/')
83 if File
in self
._ZipFile
.namelist():
84 Msg
= "%s -> %s" % (File
, ToFile
)
86 self
.Extract(File
, ToFile
)
93 # @param Which: the source path
94 # @param ToDest: the destination path
96 def Extract(self
, Which
, ToDest
):
97 Which
= os
.path
.normpath(Which
)
98 if Which
not in self
._Files
:
99 Logger
.Error("PackagingTool", FILE_NOT_FOUND
,
100 ExtraData
="[%s] in %s" % (Which
, self
._FileName
))
102 FileContent
= self
._ZipFile
.read(self
._Files
[Which
])
103 except BaseException
as Xstr
:
104 Logger
.Error("PackagingTool", FILE_DECOMPRESS_FAILURE
,
105 ExtraData
="[%s] in %s (%s)" % (Which
, \
109 CreateDirectory(os
.path
.dirname(ToDest
))
110 if os
.path
.exists(ToDest
) and not os
.access(ToDest
, os
.W_OK
):
111 Logger
.Warn("PackagingTool", \
112 ST
.WRN_FILE_NOT_OVERWRITTEN
% ToDest
)
115 ToFile
= __FileHookOpen__(ToDest
, 'wb')
116 except BaseException
as Xstr
:
117 Logger
.Error("PackagingTool", FILE_OPEN_FAILURE
,
118 ExtraData
="%s (%s)" % (ToDest
, str(Xstr
)))
121 ToFile
.write(FileContent
)
123 except BaseException
as Xstr
:
124 Logger
.Error("PackagingTool", FILE_WRITE_FAILURE
,
125 ExtraData
="%s (%s)" % (ToDest
, str(Xstr
)))
129 # @param Files: the removed files
131 def Remove(self
, Files
):
132 TmpDir
= os
.path
.join(tempfile
.gettempdir(), ".packaging")
133 if os
.path
.exists(TmpDir
):
134 RemoveDirectory(TmpDir
, True)
139 SinF
= os
.path
.normpath(SinF
)
140 if SinF
not in self
._Files
:
141 Logger
.Error("PackagingTool", FILE_NOT_FOUND
,
142 ExtraData
="%s is not in %s!" % \
143 (SinF
, self
._FileName
))
144 self
._Files
.pop(SinF
)
145 self
._ZipFile
.close()
147 self
._ZipFile
= zipfile
.ZipFile(self
._FileName
, "w", \
148 zipfile
.ZIP_DEFLATED
)
151 self
.PackFiles(self
._Files
)
153 RemoveDirectory(TmpDir
, True)
155 ## Pack the files under Top directory, the directory shown in the zipFile start from BaseDir,
156 # BaseDir should be the parent directory of the Top directory, for example,
157 # Pack(Workspace\Dir1, Workspace) will pack files under Dir1, and the path in the zipfile will
158 # start from Workspace
160 # @param Top: the top directory
161 # @param BaseDir: the base directory
163 def Pack(self
, Top
, BaseDir
):
164 if not os
.path
.isdir(Top
):
165 Logger
.Error("PackagingTool", FILE_UNKNOWN_ERROR
, \
166 "%s is not a directory!" %Top
)
171 RelaDir
= Top
[Top
.upper().find(BaseDir
.upper()).\
172 join(len(BaseDir
).join(1)):]
174 for Root
, Dirs
, Files
in os
.walk(RelaDir
):
181 if Dir
.startswith('.'):
184 if File1
.startswith('.'):
186 ExtName
= os
.path
.splitext(File1
)[1]
188 # skip '.dec', '.inf', '.dsc', '.fdf' files
190 if ExtName
.lower() in ['.dec', '.inf', '.dsc', '.fdf']:
192 FilesToPack
.append(os
.path
.join(Root
, File1
))
193 self
.PackFiles(FilesToPack
)
198 # @param Files: the files to pack
200 def PackFiles(self
, Files
):
203 os
.chdir(mws
.getWs(mws
.WORKSPACE
, File
))
209 # @param File: the files to pack
210 # @param ArcName: the Arc Name
212 def PackFile(self
, File
, ArcName
=None):
215 # avoid packing same file multiple times
217 if platform
.system() != 'Windows':
218 File
= File
.replace('\\', '/')
219 ZipedFilesNameList
= self
._ZipFile
.namelist()
220 for ZipedFile
in ZipedFilesNameList
:
221 if File
== os
.path
.normpath(ZipedFile
):
223 Logger
.Info("packing ..." + File
)
224 self
._ZipFile
.write(File
, ArcName
)
225 except BaseException
as Xstr
:
226 Logger
.Error("PackagingTool", FILE_COMPRESS_FAILURE
,
227 ExtraData
="%s (%s)" % (File
, str(Xstr
)))
229 ## Write data to the packed file
231 # @param Data: data to write
232 # @param ArcName: the Arc Name
234 def PackData(self
, Data
, ArcName
):
236 if os
.path
.splitext(ArcName
)[1].lower() == '.pkg':
237 Data
= Data
.encode('utf_8')
238 self
._ZipFile
.writestr(ArcName
, Data
)
239 except BaseException
as Xstr
:
240 Logger
.Error("PackagingTool", FILE_COMPRESS_FAILURE
,
241 ExtraData
="%s (%s)" % (ArcName
, str(Xstr
)))
247 self
._ZipFile
.close()