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