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