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