]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/MkPkg.py
Sync BaseTools Branch (version r2271) to EDKII main trunk.
[mirror_edk2.git] / BaseTools / Source / Python / UPT / MkPkg.py
1 ## @file
2 # Install distribution package.
3 #
4 # Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 #
6 # This program and the accompanying materials are licensed and made available
7 # under the terms and conditions of the BSD License which accompanies this
8 # 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 MkPkg
17 '''
18
19 ##
20 # Import Modules
21 #
22 from os import remove
23 from os import getcwd
24 from os import chdir
25 import os.path
26 from sys import stdin
27 from sys import platform
28 from traceback import format_exc
29 from platform import python_version
30 import md5
31 from time import strftime
32 from time import localtime
33 from uuid import uuid4
34
35 from Logger import StringTable as ST
36 from Logger.ToolError import OPTION_UNKNOWN_ERROR
37 from Logger.ToolError import OPTION_VALUE_INVALID
38 from Logger.ToolError import ABORT_ERROR
39 from Logger.ToolError import UPT_REPKG_ERROR
40 from Logger.ToolError import CODE_ERROR
41 from Logger.ToolError import FatalError
42 from Logger.ToolError import FILE_NOT_FOUND
43 import Logger.Log as Logger
44
45 from Xml.XmlParser import DistributionPackageXml
46 from Xml.IniToXml import IniToXml
47
48 from Library.Misc import CheckEnvVariable
49 from Library import GlobalData
50 from Library.ParserValidate import IsValidPath
51
52 from Core.DistributionPackageClass import DistributionPackageClass
53 from Core.PackageFile import PackageFile
54
55 ## CheckForExistingDp
56 #
57 # Check if there is a same name DP file existing
58 # @param Path: The path to be checked
59 #
60 def CheckForExistingDp(Path):
61 if os.path.exists(Path):
62 Logger.Info(ST.MSG_DISTRIBUTION_PACKAGE_FILE_EXISTS % Path)
63 Input = stdin.readline()
64 Input = Input.replace('\r', '').replace('\n', '')
65 if Input.upper() != "Y":
66 Logger.Error("\nMkPkg", ABORT_ERROR, ST.ERR_USER_ABORT, RaiseError=True)
67
68 ## Tool entrance method
69 #
70 # This method mainly dispatch specific methods per the command line options.
71 # If no error found, return zero value so the caller of this tool can know
72 # if it's executed successfully or not.
73 #
74 #
75 def Main(Options = None):
76 if Options == None:
77 Logger.Error("\nMkPkg", OPTION_UNKNOWN_ERROR, ST.ERR_OPTION_NOT_FOUND)
78 try:
79 DataBase = GlobalData.gDB
80 ContentFileClosed = True
81 CheckEnvVariable()
82 WorkspaceDir = GlobalData.gWORKSPACE
83
84 #
85 # Init PackFileToCreate
86 #
87 if not Options.PackFileToCreate:
88 Logger.Error("\nMkPkg", OPTION_UNKNOWN_ERROR, ST.ERR_OPTION_NOT_FOUND)
89
90 #
91 # Handle if the distribution package file already exists
92 #
93 CheckForExistingDp(Options.PackFileToCreate)
94
95 #
96 # Check package file existing and valid
97 #
98 CheckFileList('.DEC', Options.PackageFileList, ST.ERR_INVALID_PACKAGE_NAME, ST.ERR_INVALID_PACKAGE_PATH)
99 #
100 # Check module file existing and valid
101 #
102 CheckFileList('.INF', Options.ModuleFileList, ST.ERR_INVALID_MODULE_NAME, ST.ERR_INVALID_MODULE_PATH)
103
104 #
105 # Get list of files that installed with RePackage attribute available
106 #
107 RePkgDict = DataBase.GetRePkgDict()
108
109 ContentFile = PackageFile(GlobalData.gCONTENT_FILE, "w")
110 ContentFileClosed = False
111
112 #
113 # Add temp distribution header
114 #
115 if Options.PackageInformationDataFile:
116 XmlFile = IniToXml(Options.PackageInformationDataFile)
117 DistPkg = DistributionPackageXml().FromXml(XmlFile)
118 remove(XmlFile)
119
120 #
121 # add distribution level tool/misc files
122 # before pack, current dir should be workspace dir, else the full
123 # path will be in the pack file
124 #
125 Cwd = getcwd()
126 chdir(WorkspaceDir)
127 ToolObject = DistPkg.Tools
128 MiscObject = DistPkg.MiscellaneousFiles
129 FileList = []
130 if ToolObject:
131 FileList += ToolObject.GetFileList()
132 if MiscObject:
133 FileList += MiscObject.GetFileList()
134 for FileObject in FileList:
135 #
136 # If you have unicode file names, please convert them to byte
137 # strings in your desired encoding before passing them to
138 # write().
139 #
140 FromFile = os.path.normpath(FileObject.GetURI()).encode('utf_8')
141 FileFullPath = os.path.normpath(os.path.join(WorkspaceDir, FromFile))
142 if FileFullPath in RePkgDict:
143 (DpGuid, DpVersion, DpName, Repackage) = RePkgDict[FileFullPath]
144 if not Repackage:
145 Logger.Error("\nMkPkg",
146 UPT_REPKG_ERROR,
147 ST.ERR_UPT_REPKG_ERROR,
148 ExtraData=ST.MSG_REPKG_CONFLICT %\
149 (FileFullPath, DpGuid, DpVersion, DpName)
150 )
151 else:
152 DistPkg.Header.RePackage = True
153 ContentFile.PackFile(FromFile)
154 chdir(Cwd)
155
156 #
157 # Add init dp information
158 #
159 else:
160 DistPkg = DistributionPackageClass()
161 DistPkg.Header.Name = 'Distribution Package'
162 DistPkg.Header.Guid = str(uuid4())
163 DistPkg.Header.Version = '1.0'
164
165 DistPkg.GetDistributionPackage(WorkspaceDir, Options.PackageFileList, \
166 Options.ModuleFileList)
167 FileList, MetaDataFileList = DistPkg.GetDistributionFileList()
168 for File in FileList + MetaDataFileList:
169 FileFullPath = os.path.normpath(os.path.join(WorkspaceDir, File))
170 #
171 # check whether file was included in a distribution that can not
172 # be repackaged
173 #
174 if FileFullPath in RePkgDict:
175 (DpGuid, DpVersion, DpName, Repackage) = RePkgDict[FileFullPath]
176 if not Repackage:
177 Logger.Error("\nMkPkg",
178 UPT_REPKG_ERROR,
179 ST.ERR_UPT_REPKG_ERROR,
180 ExtraData = \
181 ST.MSG_REPKG_CONFLICT %(FileFullPath, DpName, \
182 DpGuid, DpVersion)
183 )
184 else:
185 DistPkg.Header.RePackage = True
186
187 Cwd = getcwd()
188 chdir(WorkspaceDir)
189 ContentFile.PackFiles(FileList)
190 chdir(Cwd)
191
192 Logger.Verbose(ST.MSG_COMPRESS_DISTRIBUTION_PKG)
193
194 ContentFile.Close()
195 ContentFileClosed = True
196
197 #
198 # Add Md5Sigature
199 #
200 DistPkg.Header.Signature = md5.new(open(str(ContentFile), 'rb').read()).hexdigest()
201 #
202 # Add current Date
203 #
204 DistPkg.Header.Date = str(strftime("%Y-%m-%dT%H:%M:%S", localtime()))
205
206 #
207 # Finish final dp file
208 #
209 DistPkgFile = PackageFile(Options.PackFileToCreate, "w")
210 DistPkgFile.PackFile(str(ContentFile))
211 DistPkgXml = DistributionPackageXml()
212 DistPkgFile.PackData(DistPkgXml.ToXml(DistPkg), GlobalData.gDESC_FILE)
213 DistPkgFile.Close()
214 Logger.Quiet(ST.MSG_FINISH)
215 ReturnCode = 0
216
217 except FatalError, XExcept:
218 ReturnCode = XExcept.args[0]
219 if Logger.GetLevel() <= Logger.DEBUG_9:
220 Logger.Quiet(ST.MSG_PYTHON_ON % \
221 (python_version(), platform) + format_exc())
222 except KeyboardInterrupt:
223 ReturnCode = ABORT_ERROR
224 if Logger.GetLevel() <= Logger.DEBUG_9:
225 Logger.Quiet(ST.MSG_PYTHON_ON % \
226 (python_version(), platform) + format_exc())
227 except OSError:
228 pass
229 except:
230 Logger.Error(
231 "\nMkPkg",
232 CODE_ERROR,
233 ST.ERR_UNKNOWN_FATAL_CREATING_ERR % \
234 Options.PackFileToCreate,
235 ExtraData=ST.MSG_SEARCH_FOR_HELP,
236 RaiseError=False
237 )
238 Logger.Quiet(ST.MSG_PYTHON_ON % \
239 (python_version(), platform) + format_exc())
240 ReturnCode = CODE_ERROR
241 finally:
242 if os.path.exists(GlobalData.gCONTENT_FILE):
243 if not ContentFileClosed:
244 ContentFile.Close()
245 os.remove(GlobalData.gCONTENT_FILE)
246
247 return ReturnCode
248
249
250 ## CheckFileList
251 #
252 # @param QualifiedExt: QualifiedExt
253 # @param FileList: FileList
254 # @param ErrorStringExt: ErrorStringExt
255 # @param ErrorStringFullPath: ErrorStringFullPath
256 #
257 def CheckFileList(QualifiedExt, FileList, ErrorStringExt, ErrorStringFullPath):
258 if not FileList:
259 return
260 WorkspaceDir = GlobalData.gWORKSPACE
261 WorkspaceDir = os.path.normpath(WorkspaceDir)
262 for Item in FileList:
263 Ext = os.path.splitext(Item)[1]
264 if Ext.upper() != QualifiedExt.upper():
265 Logger.Error("\nMkPkg", OPTION_VALUE_INVALID, \
266 ErrorStringExt % Item)
267
268 Item = os.path.normpath(Item)
269 Path = os.path.normpath(os.path.join(WorkspaceDir, Item))
270 if not os.path.exists(Path):
271 Logger.Error("\nMkPkg", FILE_NOT_FOUND, ST.ERR_NOT_FOUND % Item)
272 elif Item == Path:
273 Logger.Error("\nMkPkg", OPTION_VALUE_INVALID,
274 ErrorStringFullPath % Item)
275 elif not IsValidPath(Item, WorkspaceDir):
276 Logger.Error("\nMkPkg", OPTION_VALUE_INVALID, \
277 ErrorStringExt % Item)
278
279 if not os.path.split(Item)[0]:
280 Logger.Error("\nMkPkg", OPTION_VALUE_INVALID, \
281 ST.ERR_INVALID_METAFILE_PATH % Item)