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