]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/UPT/Core/PackageFile.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Core / PackageFile.py
CommitLineData
4234283c
LG
1## @file\r
2#\r
3# PackageFile class represents the zip file of a distribution package.\r
4#\r
f7496d71 5# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c 6#\r
2e351cbe 7# SPDX-License-Identifier: BSD-2-Clause-Patent\r
4234283c
LG
8#\r
9\r
10'''\r
11PackageFile\r
12'''\r
13\r
14##\r
15# Import Modules\r
16#\r
17import os.path\r
18import zipfile\r
19import tempfile\r
20import platform\r
21\r
22from Logger.ToolError import FILE_OPEN_FAILURE\r
23from Logger.ToolError import FILE_CHECKSUM_FAILURE\r
24from Logger.ToolError import FILE_NOT_FOUND\r
25from Logger.ToolError import FILE_DECOMPRESS_FAILURE\r
26from Logger.ToolError import FILE_UNKNOWN_ERROR\r
27from Logger.ToolError import FILE_WRITE_FAILURE\r
28from Logger.ToolError import FILE_COMPRESS_FAILURE\r
29import Logger.Log as Logger\r
30from Logger import StringTable as ST\r
31from Library.Misc import CreateDirectory\r
32from Library.Misc import RemoveDirectory\r
421ccda3 33from Core.FileHook import __FileHookOpen__\r
fb0f8067 34from Common.MultipleWorkspace import MultipleWorkspace as mws\r
4234283c
LG
35\r
36\r
37class PackageFile:\r
38 def __init__(self, FileName, Mode="r"):\r
39 self._FileName = FileName\r
40 if Mode not in ["r", "w", "a"]:\r
41 Mode = "r"\r
42 try:\r
43 self._ZipFile = zipfile.ZipFile(FileName, Mode, \\r
44 zipfile.ZIP_DEFLATED)\r
45 self._Files = {}\r
46 for Filename in self._ZipFile.namelist():\r
47 self._Files[os.path.normpath(Filename)] = Filename\r
5b0671c1 48 except BaseException as Xstr:\r
f7496d71 49 Logger.Error("PackagingTool", FILE_OPEN_FAILURE,\r
4234283c
LG
50 ExtraData="%s (%s)" % (FileName, str(Xstr)))\r
51\r
52 BadFile = self._ZipFile.testzip()\r
4231a819 53 if BadFile is not None:\r
f7496d71 54 Logger.Error("PackagingTool", FILE_CHECKSUM_FAILURE,\r
4234283c 55 ExtraData="[%s] in %s" % (BadFile, FileName))\r
f7496d71 56\r
4234283c
LG
57 def GetZipFile(self):\r
58 return self._ZipFile\r
f7496d71
LG
59\r
60 ## Get file name\r
4234283c
LG
61 #\r
62 def __str__(self):\r
63 return self._FileName\r
f7496d71 64\r
4234283c 65 ## Extract the file\r
f7496d71
LG
66 #\r
67 # @param To: the destination file\r
4234283c
LG
68 #\r
69 def Unpack(self, ToDest):\r
70 for FileN in self._ZipFile.namelist():\r
71 ToFile = os.path.normpath(os.path.join(ToDest, FileN))\r
72 Msg = "%s -> %s" % (FileN, ToFile)\r
73 Logger.Info(Msg)\r
74 self.Extract(FileN, ToFile)\r
f7496d71 75\r
4234283c 76 ## Extract the file\r
f7496d71
LG
77 #\r
78 # @param File: the extracted file\r
79 # @param ToFile: the destination file\r
4234283c
LG
80 #\r
81 def UnpackFile(self, File, ToFile):\r
82 File = File.replace('\\', '/')\r
83 if File in self._ZipFile.namelist():\r
84 Msg = "%s -> %s" % (File, ToFile)\r
85 Logger.Info(Msg)\r
86 self.Extract(File, ToFile)\r
87 return ToFile\r
f7496d71 88\r
4234283c 89 return ''\r
f7496d71 90\r
4234283c 91 ## Extract the file\r
f7496d71
LG
92 #\r
93 # @param Which: the source path\r
94 # @param ToDest: the destination path\r
4234283c
LG
95 #\r
96 def Extract(self, Which, ToDest):\r
97 Which = os.path.normpath(Which)\r
98 if Which not in self._Files:\r
99 Logger.Error("PackagingTool", FILE_NOT_FOUND,\r
100 ExtraData="[%s] in %s" % (Which, self._FileName))\r
101 try:\r
102 FileContent = self._ZipFile.read(self._Files[Which])\r
5b0671c1 103 except BaseException as Xstr:\r
f7496d71 104 Logger.Error("PackagingTool", FILE_DECOMPRESS_FAILURE,\r
4234283c
LG
105 ExtraData="[%s] in %s (%s)" % (Which, \\r
106 self._FileName, \\r
107 str(Xstr)))\r
108 try:\r
109 CreateDirectory(os.path.dirname(ToDest))\r
110 if os.path.exists(ToDest) and not os.access(ToDest, os.W_OK):\r
111 Logger.Warn("PackagingTool", \\r
112 ST.WRN_FILE_NOT_OVERWRITTEN % ToDest)\r
113 return\r
421ccda3
HC
114 else:\r
115 ToFile = __FileHookOpen__(ToDest, 'wb')\r
5b0671c1 116 except BaseException as Xstr:\r
f7496d71 117 Logger.Error("PackagingTool", FILE_OPEN_FAILURE,\r
4234283c
LG
118 ExtraData="%s (%s)" % (ToDest, str(Xstr)))\r
119\r
120 try:\r
121 ToFile.write(FileContent)\r
122 ToFile.close()\r
5b0671c1 123 except BaseException as Xstr:\r
f7496d71 124 Logger.Error("PackagingTool", FILE_WRITE_FAILURE,\r
4234283c
LG
125 ExtraData="%s (%s)" % (ToDest, str(Xstr)))\r
126\r
127 ## Remove the file\r
f7496d71
LG
128 #\r
129 # @param Files: the removed files\r
4234283c
LG
130 #\r
131 def Remove(self, Files):\r
132 TmpDir = os.path.join(tempfile.gettempdir(), ".packaging")\r
133 if os.path.exists(TmpDir):\r
134 RemoveDirectory(TmpDir, True)\r
135\r
136 os.mkdir(TmpDir)\r
137 self.Unpack(TmpDir)\r
138 for SinF in Files:\r
139 SinF = os.path.normpath(SinF)\r
140 if SinF not in self._Files:\r
f7496d71 141 Logger.Error("PackagingTool", FILE_NOT_FOUND,\r
4234283c
LG
142 ExtraData="%s is not in %s!" % \\r
143 (SinF, self._FileName))\r
144 self._Files.pop(SinF)\r
145 self._ZipFile.close()\r
146\r
147 self._ZipFile = zipfile.ZipFile(self._FileName, "w", \\r
148 zipfile.ZIP_DEFLATED)\r
149 Cwd = os.getcwd()\r
150 os.chdir(TmpDir)\r
151 self.PackFiles(self._Files)\r
152 os.chdir(Cwd)\r
153 RemoveDirectory(TmpDir, True)\r
154\r
155 ## Pack the files under Top directory, the directory shown in the zipFile start from BaseDir,\r
f7496d71
LG
156 # BaseDir should be the parent directory of the Top directory, for example,\r
157 # Pack(Workspace\Dir1, Workspace) will pack files under Dir1, and the path in the zipfile will\r
4234283c 158 # start from Workspace\r
f7496d71
LG
159 #\r
160 # @param Top: the top directory\r
161 # @param BaseDir: the base directory\r
4234283c
LG
162 #\r
163 def Pack(self, Top, BaseDir):\r
164 if not os.path.isdir(Top):\r
165 Logger.Error("PackagingTool", FILE_UNKNOWN_ERROR, \\r
166 "%s is not a directory!" %Top)\r
167\r
168 FilesToPack = []\r
169 Cwd = os.getcwd()\r
170 os.chdir(BaseDir)\r
171 RelaDir = Top[Top.upper().find(BaseDir.upper()).\\r
f7496d71 172 join(len(BaseDir).join(1)):]\r
4234283c
LG
173\r
174 for Root, Dirs, Files in os.walk(RelaDir):\r
175 if 'CVS' in Dirs:\r
176 Dirs.remove('CVS')\r
177 if '.svn' in Dirs:\r
178 Dirs.remove('.svn')\r
f7496d71 179\r
4234283c
LG
180 for Dir in Dirs:\r
181 if Dir.startswith('.'):\r
182 Dirs.remove(Dir)\r
183 for File1 in Files:\r
184 if File1.startswith('.'):\r
185 continue\r
186 ExtName = os.path.splitext(File1)[1]\r
187 #\r
188 # skip '.dec', '.inf', '.dsc', '.fdf' files\r
189 #\r
190 if ExtName.lower() in ['.dec', '.inf', '.dsc', '.fdf']:\r
191 continue\r
192 FilesToPack.append(os.path.join(Root, File1))\r
193 self.PackFiles(FilesToPack)\r
194 os.chdir(Cwd)\r
195\r
196 ## Pack the file\r
f7496d71
LG
197 #\r
198 # @param Files: the files to pack\r
4234283c
LG
199 #\r
200 def PackFiles(self, Files):\r
fb0f8067
HC
201 for File in Files:\r
202 Cwd = os.getcwd()\r
203 os.chdir(mws.getWs(mws.WORKSPACE, File))\r
204 self.PackFile(File)\r
205 os.chdir(Cwd)\r
4234283c
LG
206\r
207 ## Pack the file\r
f7496d71
LG
208 #\r
209 # @param File: the files to pack\r
210 # @param ArcName: the Arc Name\r
4234283c
LG
211 #\r
212 def PackFile(self, File, ArcName=None):\r
213 try:\r
214 #\r
215 # avoid packing same file multiple times\r
216 #\r
217 if platform.system() != 'Windows':\r
f7496d71 218 File = File.replace('\\', '/')\r
4234283c
LG
219 ZipedFilesNameList = self._ZipFile.namelist()\r
220 for ZipedFile in ZipedFilesNameList:\r
221 if File == os.path.normpath(ZipedFile):\r
222 return\r
223 Logger.Info("packing ..." + File)\r
224 self._ZipFile.write(File, ArcName)\r
5b0671c1 225 except BaseException as Xstr:\r
4234283c
LG
226 Logger.Error("PackagingTool", FILE_COMPRESS_FAILURE,\r
227 ExtraData="%s (%s)" % (File, str(Xstr)))\r
228\r
229 ## Write data to the packed file\r
f7496d71
LG
230 #\r
231 # @param Data: data to write\r
232 # @param ArcName: the Arc Name\r
4234283c
LG
233 #\r
234 def PackData(self, Data, ArcName):\r
235 try:\r
421ccda3
HC
236 if os.path.splitext(ArcName)[1].lower() == '.pkg':\r
237 Data = Data.encode('utf_8')\r
4234283c 238 self._ZipFile.writestr(ArcName, Data)\r
5b0671c1 239 except BaseException as Xstr:\r
4234283c
LG
240 Logger.Error("PackagingTool", FILE_COMPRESS_FAILURE,\r
241 ExtraData="%s (%s)" % (ArcName, str(Xstr)))\r
242\r
243 ## Close file\r
f7496d71 244 #\r
4234283c
LG
245 #\r
246 def Close(self):\r
247 self._ZipFile.close()\r
248\r
249\r
250\r