]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | ## @file\r |
2 | #\r | |
3 | # PackageFile class represents the zip file of a distribution package.\r | |
4 | #\r | |
40d841f6 LG |
5 | # Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>\r |
6 | # This program and the accompanying materials\r | |
30fdf114 LG |
7 | # are licensed and made available under the terms and conditions of the BSD License\r |
8 | # which accompanies this distribution. The full text of the license may be found at\r | |
9 | # http://opensource.org/licenses/bsd-license.php\r | |
10 | #\r | |
11 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | #\r | |
14 | \r | |
15 | ##\r | |
16 | # Import Modules\r | |
17 | #\r | |
18 | import os\r | |
19 | import sys\r | |
20 | import zipfile\r | |
21 | import tempfile\r | |
22 | \r | |
23 | from Common import EdkLogger\r | |
24 | from Common.Misc import *\r | |
25 | from Common.BuildToolError import *\r | |
26 | \r | |
27 | class PackageFile:\r | |
28 | def __init__(self, FileName, Mode="r"):\r | |
29 | self._FileName = FileName\r | |
30 | if Mode not in ["r", "w", "a"]:\r | |
31 | Mode = "r"\r | |
32 | try:\r | |
33 | self._ZipFile = zipfile.ZipFile(FileName, Mode, zipfile.ZIP_DEFLATED)\r | |
34 | self._Files = {}\r | |
35 | for F in self._ZipFile.namelist():\r | |
36 | self._Files[os.path.normpath(F)] = F\r | |
37 | except BaseException, X:\r | |
38 | EdkLogger.error("PackagingTool", FILE_OPEN_FAILURE, \r | |
39 | ExtraData="%s (%s)" % (FileName, str(X)))\r | |
40 | \r | |
41 | BadFile = self._ZipFile.testzip()\r | |
42 | if BadFile != None:\r | |
43 | EdkLogger.error("PackagingTool", FILE_CHECKSUM_FAILURE, \r | |
44 | ExtraData="[%s] in %s" % (BadFile, FileName))\r | |
45 | \r | |
46 | def __str__(self):\r | |
47 | return self._FileName\r | |
48 | \r | |
49 | def Unpack(self, To):\r | |
50 | for F in self._ZipFile.namelist():\r | |
51 | ToFile = os.path.normpath(os.path.join(To, F))\r | |
52 | print F, "->", ToFile\r | |
53 | self.Extract(F, ToFile)\r | |
54 | \r | |
55 | def UnpackFile(self, File, ToFile):\r | |
56 | File = File.replace('\\', '/')\r | |
57 | if File in self._ZipFile.namelist():\r | |
58 | print File, "->", ToFile\r | |
59 | self.Extract(File, ToFile)\r | |
60 | \r | |
61 | return ToFile\r | |
62 | \r | |
63 | return ''\r | |
64 | \r | |
65 | def Extract(self, Which, To):\r | |
66 | Which = os.path.normpath(Which)\r | |
67 | if Which not in self._Files:\r | |
68 | EdkLogger.error("PackagingTool", FILE_NOT_FOUND,\r | |
69 | ExtraData="[%s] in %s" % (Which, self._FileName))\r | |
70 | try:\r | |
71 | FileContent = self._ZipFile.read(self._Files[Which])\r | |
72 | except BaseException, X:\r | |
73 | EdkLogger.error("PackagingTool", FILE_DECOMPRESS_FAILURE, \r | |
74 | ExtraData="[%s] in %s (%s)" % (Which, self._FileName, str(X)))\r | |
75 | try:\r | |
76 | CreateDirectory(os.path.dirname(To))\r | |
77 | ToFile = open(To, "wb")\r | |
78 | except BaseException, X:\r | |
79 | EdkLogger.error("PackagingTool", FILE_OPEN_FAILURE, \r | |
80 | ExtraData="%s (%s)" % (To, str(X)))\r | |
81 | \r | |
82 | try:\r | |
83 | ToFile.write(FileContent)\r | |
84 | ToFile.close()\r | |
85 | except BaseException, X:\r | |
86 | EdkLogger.error("PackagingTool", FILE_WRITE_FAILURE, \r | |
87 | ExtraData="%s (%s)" % (To, str(X)))\r | |
88 | \r | |
89 | def Remove(self, Files):\r | |
90 | TmpDir = os.path.join(tempfile.gettempdir(), ".packaging")\r | |
91 | if os.path.exists(TmpDir):\r | |
92 | RemoveDirectory(TmpDir, True)\r | |
93 | \r | |
94 | os.mkdir(TmpDir)\r | |
95 | self.Unpack(TmpDir)\r | |
96 | for F in Files:\r | |
97 | F = os.path.normpath(F)\r | |
98 | if F not in self._Files:\r | |
99 | EdkLogger.error("PackagingTool", FILE_NOT_FOUND, \r | |
100 | ExtraData="%s is not in %s!" % (F, self._FileName))\r | |
101 | #os.remove(os.path.join(TmpDir, F)) # no need to really remove file\r | |
102 | self._Files.pop(F)\r | |
103 | self._ZipFile.close()\r | |
104 | \r | |
105 | self._ZipFile = zipfile.ZipFile(self._FileName, "w", zipfile.ZIP_DEFLATED)\r | |
106 | Cwd = os.getcwd()\r | |
107 | os.chdir(TmpDir)\r | |
108 | self.PackFiles(self._Files)\r | |
109 | os.chdir(Cwd)\r | |
110 | RemoveDirectory(TmpDir, True)\r | |
111 | \r | |
112 | def Pack(self, Top):\r | |
113 | if not os.path.isdir(Top):\r | |
114 | EdkLogger.error("PackagingTool", FILE_UNKNOWN_ERROR, "%s is not a directory!" %Top)\r | |
115 | \r | |
116 | FilesToPack = []\r | |
117 | ParentDir = os.path.dirname(Top)\r | |
118 | BaseDir = os.path.basename(Top)\r | |
119 | Cwd = os.getcwd()\r | |
120 | os.chdir(ParentDir)\r | |
121 | for Root, Dirs, Files in os.walk(BaseDir):\r | |
122 | if 'CVS' in Dirs:\r | |
123 | Dirs.remove('CVS')\r | |
124 | if '.svn' in Dirs:\r | |
125 | Dirs.remove('.svn')\r | |
126 | for F in Files:\r | |
127 | FilesToPack.append(os.path.join(Root, F))\r | |
128 | self.PackFiles(FilesToPack)\r | |
129 | os.chdir(Cwd)\r | |
130 | \r | |
131 | def PackFiles(self, Files):\r | |
132 | for F in Files:\r | |
133 | try:\r | |
134 | print "packing ...", F\r | |
135 | self._ZipFile.write(F)\r | |
136 | except BaseException, X:\r | |
137 | EdkLogger.error("PackagingTool", FILE_COMPRESS_FAILURE, \r | |
138 | ExtraData="%s (%s)" % (F, str(X)))\r | |
139 | \r | |
140 | def PackFile(self, File, ArcName=None):\r | |
141 | try:\r | |
142 | print "packing ...", File\r | |
143 | self._ZipFile.write(File, ArcName)\r | |
144 | except BaseException, X:\r | |
145 | EdkLogger.error("PackagingTool", FILE_COMPRESS_FAILURE,\r | |
146 | ExtraData="%s (%s)" % (File, str(X)))\r | |
147 | \r | |
148 | def PackData(self, Data, ArcName):\r | |
149 | try:\r | |
150 | self._ZipFile.writestr(ArcName, Data)\r | |
151 | except BaseException, X:\r | |
152 | EdkLogger.error("PackagingTool", FILE_COMPRESS_FAILURE,\r | |
153 | ExtraData="%s (%s)" % (ArcName, str(X)))\r | |
154 | \r | |
155 | def Close(self):\r | |
156 | self._ZipFile.close()\r | |
157 | \r | |
158 | if __name__ == '__main__':\r | |
159 | pass\r | |
160 | \r |