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