2 # Install distribution package.
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
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
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.
27 from sys
import platform
28 from traceback
import format_exc
29 from platform
import python_version
31 from time
import strftime
32 from time
import localtime
33 from uuid
import uuid4
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
45 from Xml
.XmlParser
import DistributionPackageXml
46 from Xml
.IniToXml
import IniToXml
48 from Library
import GlobalData
49 from Library
.ParserValidate
import IsValidPath
51 from Core
.DistributionPackageClass
import DistributionPackageClass
52 from Core
.PackageFile
import PackageFile
53 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
57 # Check if there is a same name DP file existing
58 # @param Path: The path to be checked
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)
68 ## Tool entrance method
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.
75 def Main(Options
= None):
77 Logger
.Error("\nMkPkg", OPTION_UNKNOWN_ERROR
, ST
.ERR_OPTION_NOT_FOUND
)
79 DataBase
= GlobalData
.gDB
80 ContentFileClosed
= True
81 WorkspaceDir
= GlobalData
.gWORKSPACE
84 # Init PackFileToCreate
86 if not Options
.PackFileToCreate
:
87 Logger
.Error("\nMkPkg", OPTION_UNKNOWN_ERROR
, ST
.ERR_OPTION_NOT_FOUND
)
90 # Handle if the distribution package file already exists
92 CheckForExistingDp(Options
.PackFileToCreate
)
95 # Check package file existing and valid
97 CheckFileList('.DEC', Options
.PackageFileList
, ST
.ERR_INVALID_PACKAGE_NAME
, ST
.ERR_INVALID_PACKAGE_PATH
)
99 # Check module file existing and valid
101 CheckFileList('.INF', Options
.ModuleFileList
, ST
.ERR_INVALID_MODULE_NAME
, ST
.ERR_INVALID_MODULE_PATH
)
104 # Get list of files that installed with RePackage attribute available
106 RePkgDict
= DataBase
.GetRePkgDict()
108 ContentFile
= PackageFile(GlobalData
.gCONTENT_FILE
, "w")
109 ContentFileClosed
= False
112 # Add temp distribution header
114 if Options
.PackageInformationDataFile
:
115 XmlFile
= IniToXml(Options
.PackageInformationDataFile
)
116 DistPkg
= DistributionPackageXml().FromXml(XmlFile
)
120 # add distribution level tool/misc files
121 # before pack, current dir should be workspace dir, else the full
122 # path will be in the pack file
126 ToolObject
= DistPkg
.Tools
127 MiscObject
= DistPkg
.MiscellaneousFiles
130 FileList
+= ToolObject
.GetFileList()
132 FileList
+= MiscObject
.GetFileList()
133 for FileObject
in FileList
:
135 # If you have unicode file names, please convert them to byte
136 # strings in your desired encoding before passing them to
139 FromFile
= os
.path
.normpath(FileObject
.GetURI()).encode('utf_8')
140 FileFullPath
= mws
.join(WorkspaceDir
, FromFile
)
141 if FileFullPath
in RePkgDict
:
142 (DpGuid
, DpVersion
, DpName
, Repackage
) = RePkgDict
[FileFullPath
]
144 Logger
.Error("\nMkPkg",
146 ST
.ERR_UPT_REPKG_ERROR
,
147 ExtraData
=ST
.MSG_REPKG_CONFLICT
%\
148 (FileFullPath
, DpGuid
, DpVersion
, DpName
)
151 DistPkg
.Header
.RePackage
= True
152 ContentFile
.PackFile(FromFile
)
156 # Add init dp information
159 DistPkg
= DistributionPackageClass()
160 DistPkg
.Header
.Name
= 'Distribution Package'
161 DistPkg
.Header
.Guid
= str(uuid4())
162 DistPkg
.Header
.Version
= '1.0'
164 DistPkg
.GetDistributionPackage(WorkspaceDir
, Options
.PackageFileList
, \
165 Options
.ModuleFileList
)
166 FileList
, MetaDataFileList
= DistPkg
.GetDistributionFileList()
167 for File
in FileList
+ MetaDataFileList
:
168 FileFullPath
= os
.path
.normpath(os
.path
.join(WorkspaceDir
, File
))
170 # check whether file was included in a distribution that can not
173 if FileFullPath
in RePkgDict
:
174 (DpGuid
, DpVersion
, DpName
, Repackage
) = RePkgDict
[FileFullPath
]
176 Logger
.Error("\nMkPkg",
178 ST
.ERR_UPT_REPKG_ERROR
,
180 ST
.MSG_REPKG_CONFLICT
%(FileFullPath
, DpName
, \
184 DistPkg
.Header
.RePackage
= True
188 ContentFile
.PackFiles(FileList
)
191 Logger
.Verbose(ST
.MSG_COMPRESS_DISTRIBUTION_PKG
)
194 ContentFileClosed
= True
199 DistPkg
.Header
.Signature
= md5
.new(open(str(ContentFile
), 'rb').read()).hexdigest()
203 DistPkg
.Header
.Date
= str(strftime("%Y-%m-%dT%H:%M:%S", localtime()))
206 # Finish final dp file
208 DistPkgFile
= PackageFile(Options
.PackFileToCreate
, "w")
209 DistPkgFile
.PackFile(str(ContentFile
))
210 DistPkgXml
= DistributionPackageXml()
211 DistPkgFile
.PackData(DistPkgXml
.ToXml(DistPkg
), GlobalData
.gDESC_FILE
)
213 Logger
.Quiet(ST
.MSG_FINISH
)
216 except FatalError
as XExcept
:
217 ReturnCode
= XExcept
.args
[0]
218 if Logger
.GetLevel() <= Logger
.DEBUG_9
:
219 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
220 (python_version(), platform
) + format_exc())
221 except KeyboardInterrupt:
222 ReturnCode
= ABORT_ERROR
223 if Logger
.GetLevel() <= Logger
.DEBUG_9
:
224 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
225 (python_version(), platform
) + format_exc())
232 ST
.ERR_UNKNOWN_FATAL_CREATING_ERR
% \
233 Options
.PackFileToCreate
,
234 ExtraData
=ST
.MSG_SEARCH_FOR_HELP
,
237 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
238 (python_version(), platform
) + format_exc())
239 ReturnCode
= CODE_ERROR
241 if os
.path
.exists(GlobalData
.gCONTENT_FILE
):
242 if not ContentFileClosed
:
244 os
.remove(GlobalData
.gCONTENT_FILE
)
251 # @param QualifiedExt: QualifiedExt
252 # @param FileList: FileList
253 # @param ErrorStringExt: ErrorStringExt
254 # @param ErrorStringFullPath: ErrorStringFullPath
256 def CheckFileList(QualifiedExt
, FileList
, ErrorStringExt
, ErrorStringFullPath
):
259 WorkspaceDir
= GlobalData
.gWORKSPACE
260 WorkspaceDir
= os
.path
.normpath(WorkspaceDir
)
261 for Item
in FileList
:
262 Ext
= os
.path
.splitext(Item
)[1]
263 if Ext
.upper() != QualifiedExt
.upper():
264 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
265 ErrorStringExt
% Item
)
267 Item
= os
.path
.normpath(Item
)
268 Path
= mws
.join(WorkspaceDir
, Item
)
269 if not os
.path
.exists(Path
):
270 Logger
.Error("\nMkPkg", FILE_NOT_FOUND
, ST
.ERR_NOT_FOUND
% Item
)
272 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
,
273 ErrorStringFullPath
% Item
)
274 elif not IsValidPath(Item
, WorkspaceDir
):
275 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
276 ErrorStringExt
% Item
)
278 if not os
.path
.split(Item
)[0]:
279 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
280 ST
.ERR_INVALID_METAFILE_PATH
% Item
)