2 # Install distribution package.
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
21 from sys
import platform
22 from traceback
import format_exc
23 from platform
import python_version
24 from hashlib
import md5
25 from time
import strftime
26 from time
import localtime
27 from uuid
import uuid4
29 from Logger
import StringTable
as ST
30 from Logger
.ToolError
import OPTION_UNKNOWN_ERROR
31 from Logger
.ToolError
import OPTION_VALUE_INVALID
32 from Logger
.ToolError
import ABORT_ERROR
33 from Logger
.ToolError
import UPT_REPKG_ERROR
34 from Logger
.ToolError
import CODE_ERROR
35 from Logger
.ToolError
import FatalError
36 from Logger
.ToolError
import FILE_NOT_FOUND
37 import Logger
.Log
as Logger
39 from Xml
.XmlParser
import DistributionPackageXml
40 from Xml
.IniToXml
import IniToXml
42 from Library
import GlobalData
43 from Library
.ParserValidate
import IsValidPath
45 from Core
.DistributionPackageClass
import DistributionPackageClass
46 from Core
.PackageFile
import PackageFile
47 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
51 # Check if there is a same name DP file existing
52 # @param Path: The path to be checked
54 def CheckForExistingDp(Path
):
55 if os
.path
.exists(Path
):
56 Logger
.Info(ST
.MSG_DISTRIBUTION_PACKAGE_FILE_EXISTS
% Path
)
57 Input
= stdin
.readline()
58 Input
= Input
.replace('\r', '').replace('\n', '')
59 if Input
.upper() != "Y":
60 Logger
.Error("\nMkPkg", ABORT_ERROR
, ST
.ERR_USER_ABORT
, RaiseError
=True)
62 ## Tool entrance method
64 # This method mainly dispatch specific methods per the command line options.
65 # If no error found, return zero value so the caller of this tool can know
66 # if it's executed successfully or not.
69 def Main(Options
= None):
71 Logger
.Error("\nMkPkg", OPTION_UNKNOWN_ERROR
, ST
.ERR_OPTION_NOT_FOUND
)
73 DataBase
= GlobalData
.gDB
74 ContentFileClosed
= True
75 WorkspaceDir
= GlobalData
.gWORKSPACE
78 # Init PackFileToCreate
80 if not Options
.PackFileToCreate
:
81 Logger
.Error("\nMkPkg", OPTION_UNKNOWN_ERROR
, ST
.ERR_OPTION_NOT_FOUND
)
84 # Handle if the distribution package file already exists
86 CheckForExistingDp(Options
.PackFileToCreate
)
89 # Check package file existing and valid
91 CheckFileList('.DEC', Options
.PackageFileList
, ST
.ERR_INVALID_PACKAGE_NAME
, ST
.ERR_INVALID_PACKAGE_PATH
)
93 # Check module file existing and valid
95 CheckFileList('.INF', Options
.ModuleFileList
, ST
.ERR_INVALID_MODULE_NAME
, ST
.ERR_INVALID_MODULE_PATH
)
98 # Get list of files that installed with RePackage attribute available
100 RePkgDict
= DataBase
.GetRePkgDict()
102 ContentFile
= PackageFile(GlobalData
.gCONTENT_FILE
, "w")
103 ContentFileClosed
= False
106 # Add temp distribution header
108 if Options
.PackageInformationDataFile
:
109 XmlFile
= IniToXml(Options
.PackageInformationDataFile
)
110 DistPkg
= DistributionPackageXml().FromXml(XmlFile
)
114 # add distribution level tool/misc files
115 # before pack, current dir should be workspace dir, else the full
116 # path will be in the pack file
120 ToolObject
= DistPkg
.Tools
121 MiscObject
= DistPkg
.MiscellaneousFiles
124 FileList
+= ToolObject
.GetFileList()
126 FileList
+= MiscObject
.GetFileList()
127 for FileObject
in FileList
:
129 # If you have unicode file names, please convert them to byte
130 # strings in your desired encoding before passing them to
133 FromFile
= os
.path
.normpath(FileObject
.GetURI()).encode('utf_8')
134 FileFullPath
= mws
.join(WorkspaceDir
, FromFile
)
135 if FileFullPath
in RePkgDict
:
136 (DpGuid
, DpVersion
, DpName
, Repackage
) = RePkgDict
[FileFullPath
]
138 Logger
.Error("\nMkPkg",
140 ST
.ERR_UPT_REPKG_ERROR
,
141 ExtraData
=ST
.MSG_REPKG_CONFLICT
%\
142 (FileFullPath
, DpGuid
, DpVersion
, DpName
)
145 DistPkg
.Header
.RePackage
= True
146 ContentFile
.PackFile(FromFile
)
150 # Add init dp information
153 DistPkg
= DistributionPackageClass()
154 DistPkg
.Header
.Name
= 'Distribution Package'
155 DistPkg
.Header
.Guid
= str(uuid4())
156 DistPkg
.Header
.Version
= '1.0'
158 DistPkg
.GetDistributionPackage(WorkspaceDir
, Options
.PackageFileList
, \
159 Options
.ModuleFileList
)
160 FileList
, MetaDataFileList
= DistPkg
.GetDistributionFileList()
161 for File
in FileList
+ MetaDataFileList
:
162 FileFullPath
= os
.path
.normpath(os
.path
.join(WorkspaceDir
, File
))
164 # check whether file was included in a distribution that can not
167 if FileFullPath
in RePkgDict
:
168 (DpGuid
, DpVersion
, DpName
, Repackage
) = RePkgDict
[FileFullPath
]
170 Logger
.Error("\nMkPkg",
172 ST
.ERR_UPT_REPKG_ERROR
,
174 ST
.MSG_REPKG_CONFLICT
%(FileFullPath
, DpName
, \
178 DistPkg
.Header
.RePackage
= True
182 ContentFile
.PackFiles(FileList
)
185 Logger
.Verbose(ST
.MSG_COMPRESS_DISTRIBUTION_PKG
)
188 ContentFileClosed
= True
193 DistPkg
.Header
.Signature
= md5(open(str(ContentFile
), 'rb').read()).hexdigest()
197 DistPkg
.Header
.Date
= str(strftime("%Y-%m-%dT%H:%M:%S", localtime()))
200 # Finish final dp file
202 DistPkgFile
= PackageFile(Options
.PackFileToCreate
, "w")
203 DistPkgFile
.PackFile(str(ContentFile
))
204 DistPkgXml
= DistributionPackageXml()
205 DistPkgFile
.PackData(DistPkgXml
.ToXml(DistPkg
), GlobalData
.gDESC_FILE
)
207 Logger
.Quiet(ST
.MSG_FINISH
)
210 except FatalError
as XExcept
:
211 ReturnCode
= XExcept
.args
[0]
212 if Logger
.GetLevel() <= Logger
.DEBUG_9
:
213 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
214 (python_version(), platform
) + format_exc())
215 except KeyboardInterrupt:
216 ReturnCode
= ABORT_ERROR
217 if Logger
.GetLevel() <= Logger
.DEBUG_9
:
218 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
219 (python_version(), platform
) + format_exc())
226 ST
.ERR_UNKNOWN_FATAL_CREATING_ERR
% \
227 Options
.PackFileToCreate
,
228 ExtraData
=ST
.MSG_SEARCH_FOR_HELP
,
231 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
232 (python_version(), platform
) + format_exc())
233 ReturnCode
= CODE_ERROR
235 if os
.path
.exists(GlobalData
.gCONTENT_FILE
):
236 if not ContentFileClosed
:
238 os
.remove(GlobalData
.gCONTENT_FILE
)
245 # @param QualifiedExt: QualifiedExt
246 # @param FileList: FileList
247 # @param ErrorStringExt: ErrorStringExt
248 # @param ErrorStringFullPath: ErrorStringFullPath
250 def CheckFileList(QualifiedExt
, FileList
, ErrorStringExt
, ErrorStringFullPath
):
253 WorkspaceDir
= GlobalData
.gWORKSPACE
254 WorkspaceDir
= os
.path
.normpath(WorkspaceDir
)
255 for Item
in FileList
:
256 Ext
= os
.path
.splitext(Item
)[1]
257 if Ext
.upper() != QualifiedExt
.upper():
258 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
259 ErrorStringExt
% Item
)
261 Item
= os
.path
.normpath(Item
)
262 Path
= mws
.join(WorkspaceDir
, Item
)
263 if not os
.path
.exists(Path
):
264 Logger
.Error("\nMkPkg", FILE_NOT_FOUND
, ST
.ERR_NOT_FOUND
% Item
)
266 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
,
267 ErrorStringFullPath
% Item
)
268 elif not IsValidPath(Item
, WorkspaceDir
):
269 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
270 ErrorStringExt
% Item
)
272 if not os
.path
.split(Item
)[0]:
273 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
274 ST
.ERR_INVALID_METAFILE_PATH
% Item
)