]>
Commit | Line | Data |
---|---|---|
1 | ## @file\r | |
2 | #\r | |
3 | # PackageFile class represents the zip file of a distribution package.\r | |
4 | #\r | |
5 | # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
6 | #\r | |
7 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
8 | #\r | |
9 | \r | |
10 | '''\r | |
11 | PackageFile\r | |
12 | '''\r | |
13 | \r | |
14 | ##\r | |
15 | # Import Modules\r | |
16 | #\r | |
17 | import os.path\r | |
18 | import zipfile\r | |
19 | import tempfile\r | |
20 | import platform\r | |
21 | \r | |
22 | from Logger.ToolError import FILE_OPEN_FAILURE\r | |
23 | from Logger.ToolError import FILE_CHECKSUM_FAILURE\r | |
24 | from Logger.ToolError import FILE_NOT_FOUND\r | |
25 | from Logger.ToolError import FILE_DECOMPRESS_FAILURE\r | |
26 | from Logger.ToolError import FILE_UNKNOWN_ERROR\r | |
27 | from Logger.ToolError import FILE_WRITE_FAILURE\r | |
28 | from Logger.ToolError import FILE_COMPRESS_FAILURE\r | |
29 | import Logger.Log as Logger\r | |
30 | from Logger import StringTable as ST\r | |
31 | from Library.Misc import CreateDirectory\r | |
32 | from Library.Misc import RemoveDirectory\r | |
33 | from Core.FileHook import __FileHookOpen__\r | |
34 | from Common.MultipleWorkspace import MultipleWorkspace as mws\r | |
35 | \r | |
36 | \r | |
37 | class 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 | |
48 | except BaseException as Xstr:\r | |
49 | Logger.Error("PackagingTool", FILE_OPEN_FAILURE,\r | |
50 | ExtraData="%s (%s)" % (FileName, str(Xstr)))\r | |
51 | \r | |
52 | BadFile = self._ZipFile.testzip()\r | |
53 | if BadFile is not None:\r | |
54 | Logger.Error("PackagingTool", FILE_CHECKSUM_FAILURE,\r | |
55 | ExtraData="[%s] in %s" % (BadFile, FileName))\r | |
56 | \r | |
57 | def GetZipFile(self):\r | |
58 | return self._ZipFile\r | |
59 | \r | |
60 | ## Get file name\r | |
61 | #\r | |
62 | def __str__(self):\r | |
63 | return self._FileName\r | |
64 | \r | |
65 | ## Extract the file\r | |
66 | #\r | |
67 | # @param To: the destination file\r | |
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 | |
75 | \r | |
76 | ## Extract the file\r | |
77 | #\r | |
78 | # @param File: the extracted file\r | |
79 | # @param ToFile: the destination file\r | |
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 | |
88 | \r | |
89 | return ''\r | |
90 | \r | |
91 | ## Extract the file\r | |
92 | #\r | |
93 | # @param Which: the source path\r | |
94 | # @param ToDest: the destination path\r | |
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 | |
103 | except BaseException as Xstr:\r | |
104 | Logger.Error("PackagingTool", FILE_DECOMPRESS_FAILURE,\r | |
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 | |
114 | else:\r | |
115 | ToFile = __FileHookOpen__(ToDest, 'wb')\r | |
116 | except BaseException as Xstr:\r | |
117 | Logger.Error("PackagingTool", FILE_OPEN_FAILURE,\r | |
118 | ExtraData="%s (%s)" % (ToDest, str(Xstr)))\r | |
119 | \r | |
120 | try:\r | |
121 | ToFile.write(FileContent)\r | |
122 | ToFile.close()\r | |
123 | except BaseException as Xstr:\r | |
124 | Logger.Error("PackagingTool", FILE_WRITE_FAILURE,\r | |
125 | ExtraData="%s (%s)" % (ToDest, str(Xstr)))\r | |
126 | \r | |
127 | ## Remove the file\r | |
128 | #\r | |
129 | # @param Files: the removed files\r | |
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 | |
141 | Logger.Error("PackagingTool", FILE_NOT_FOUND,\r | |
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 | |
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 | |
158 | # start from Workspace\r | |
159 | #\r | |
160 | # @param Top: the top directory\r | |
161 | # @param BaseDir: the base directory\r | |
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 | |
172 | join(len(BaseDir).join(1)):]\r | |
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 | |
179 | \r | |
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 | |
197 | #\r | |
198 | # @param Files: the files to pack\r | |
199 | #\r | |
200 | def PackFiles(self, Files):\r | |
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 | |
206 | \r | |
207 | ## Pack the file\r | |
208 | #\r | |
209 | # @param File: the files to pack\r | |
210 | # @param ArcName: the Arc Name\r | |
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 | |
218 | File = File.replace('\\', '/')\r | |
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 | |
225 | except BaseException as Xstr:\r | |
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 | |
230 | #\r | |
231 | # @param Data: data to write\r | |
232 | # @param ArcName: the Arc Name\r | |
233 | #\r | |
234 | def PackData(self, Data, ArcName):\r | |
235 | try:\r | |
236 | if os.path.splitext(ArcName)[1].lower() == '.pkg':\r | |
237 | Data = Data.encode('utf_8')\r | |
238 | self._ZipFile.writestr(ArcName, Data)\r | |
239 | except BaseException as Xstr:\r | |
240 | Logger.Error("PackagingTool", FILE_COMPRESS_FAILURE,\r | |
241 | ExtraData="%s (%s)" % (ArcName, str(Xstr)))\r | |
242 | \r | |
243 | ## Close file\r | |
244 | #\r | |
245 | #\r | |
246 | def Close(self):\r | |
247 | self._ZipFile.close()\r | |
248 | \r | |
249 | \r | |
250 | \r |