]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/PackagingTool/PackageFile.py
3 # PackageFile class represents the zip file of a distribution package.
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
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.
23 from Common
import EdkLogger
24 from Common
.Misc
import *
25 from Common
.BuildToolError
import *
28 def __init__(self
, FileName
, Mode
="r"):
29 self
._FileName
= FileName
30 if Mode
not in ["r", "w", "a"]:
33 self
._ZipFile
= zipfile
.ZipFile(FileName
, Mode
, zipfile
.ZIP_DEFLATED
)
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
)))
41 BadFile
= self
._ZipFile
.testzip()
43 EdkLogger
.error("PackagingTool", FILE_CHECKSUM_FAILURE
,
44 ExtraData
="[%s] in %s" % (BadFile
, FileName
))
50 for F
in self
._ZipFile
.namelist():
51 ToFile
= os
.path
.normpath(os
.path
.join(To
, F
))
53 self
.Extract(F
, ToFile
)
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
)
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
))
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
)))
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
)))
83 ToFile
.write(FileContent
)
85 except BaseException
, X
:
86 EdkLogger
.error("PackagingTool", FILE_WRITE_FAILURE
,
87 ExtraData
="%s (%s)" % (To
, str(X
)))
89 def Remove(self
, Files
):
90 TmpDir
= os
.path
.join(tempfile
.gettempdir(), ".packaging")
91 if os
.path
.exists(TmpDir
):
92 RemoveDirectory(TmpDir
, True)
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
103 self
._ZipFile
.close()
105 self
._ZipFile
= zipfile
.ZipFile(self
._FileName
, "w", zipfile
.ZIP_DEFLATED
)
108 self
.PackFiles(self
._Files
)
110 RemoveDirectory(TmpDir
, True)
113 if not os
.path
.isdir(Top
):
114 EdkLogger
.error("PackagingTool", FILE_UNKNOWN_ERROR
, "%s is not a directory!" %Top
)
117 ParentDir
= os
.path
.dirname(Top
)
118 BaseDir
= os
.path
.basename(Top
)
121 for Root
, Dirs
, Files
in os
.walk(BaseDir
):
127 FilesToPack
.append(os
.path
.join(Root
, F
))
128 self
.PackFiles(FilesToPack
)
131 def PackFiles(self
, Files
):
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
)))
140 def PackFile(self
, File
, ArcName
=None):
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
)))
148 def PackData(self
, Data
, ArcName
):
150 self
._ZipFile
.writestr(ArcName
, Data
)
151 except BaseException
, X
:
152 EdkLogger
.error("PackagingTool", FILE_COMPRESS_FAILURE
,
153 ExtraData
="%s (%s)" % (ArcName
, str(X
)))
156 self
._ZipFile
.close()
158 if __name__
== '__main__':