2 # Install distribution package.
4 # Copyright (c) 2011 - 2014, 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
56 # Check if there is a same name DP file existing
57 # @param Path: The path to be checked
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)
67 ## Tool entrance method
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.
74 def Main(Options
= None):
76 Logger
.Error("\nMkPkg", OPTION_UNKNOWN_ERROR
, ST
.ERR_OPTION_NOT_FOUND
)
78 DataBase
= GlobalData
.gDB
79 ContentFileClosed
= True
80 WorkspaceDir
= GlobalData
.gWORKSPACE
83 # Init PackFileToCreate
85 if not Options
.PackFileToCreate
:
86 Logger
.Error("\nMkPkg", OPTION_UNKNOWN_ERROR
, ST
.ERR_OPTION_NOT_FOUND
)
89 # Handle if the distribution package file already exists
91 CheckForExistingDp(Options
.PackFileToCreate
)
94 # Check package file existing and valid
96 CheckFileList('.DEC', Options
.PackageFileList
, ST
.ERR_INVALID_PACKAGE_NAME
, ST
.ERR_INVALID_PACKAGE_PATH
)
98 # Check module file existing and valid
100 CheckFileList('.INF', Options
.ModuleFileList
, ST
.ERR_INVALID_MODULE_NAME
, ST
.ERR_INVALID_MODULE_PATH
)
103 # Get list of files that installed with RePackage attribute available
105 RePkgDict
= DataBase
.GetRePkgDict()
107 ContentFile
= PackageFile(GlobalData
.gCONTENT_FILE
, "w")
108 ContentFileClosed
= False
111 # Add temp distribution header
113 if Options
.PackageInformationDataFile
:
114 XmlFile
= IniToXml(Options
.PackageInformationDataFile
)
115 DistPkg
= DistributionPackageXml().FromXml(XmlFile
)
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
125 ToolObject
= DistPkg
.Tools
126 MiscObject
= DistPkg
.MiscellaneousFiles
129 FileList
+= ToolObject
.GetFileList()
131 FileList
+= MiscObject
.GetFileList()
132 for FileObject
in FileList
:
134 # If you have unicode file names, please convert them to byte
135 # strings in your desired encoding before passing them to
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
]
143 Logger
.Error("\nMkPkg",
145 ST
.ERR_UPT_REPKG_ERROR
,
146 ExtraData
=ST
.MSG_REPKG_CONFLICT
%\
147 (FileFullPath
, DpGuid
, DpVersion
, DpName
)
150 DistPkg
.Header
.RePackage
= True
151 ContentFile
.PackFile(FromFile
)
155 # Add init dp information
158 DistPkg
= DistributionPackageClass()
159 DistPkg
.Header
.Name
= 'Distribution Package'
160 DistPkg
.Header
.Guid
= str(uuid4())
161 DistPkg
.Header
.Version
= '1.0'
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
))
169 # check whether file was included in a distribution that can not
172 if FileFullPath
in RePkgDict
:
173 (DpGuid
, DpVersion
, DpName
, Repackage
) = RePkgDict
[FileFullPath
]
175 Logger
.Error("\nMkPkg",
177 ST
.ERR_UPT_REPKG_ERROR
,
179 ST
.MSG_REPKG_CONFLICT
%(FileFullPath
, DpName
, \
183 DistPkg
.Header
.RePackage
= True
187 ContentFile
.PackFiles(FileList
)
190 Logger
.Verbose(ST
.MSG_COMPRESS_DISTRIBUTION_PKG
)
193 ContentFileClosed
= True
198 DistPkg
.Header
.Signature
= md5
.new(open(str(ContentFile
), 'rb').read()).hexdigest()
202 DistPkg
.Header
.Date
= str(strftime("%Y-%m-%dT%H:%M:%S", localtime()))
205 # Finish final dp file
207 DistPkgFile
= PackageFile(Options
.PackFileToCreate
, "w")
208 DistPkgFile
.PackFile(str(ContentFile
))
209 DistPkgXml
= DistributionPackageXml()
210 DistPkgFile
.PackData(DistPkgXml
.ToXml(DistPkg
), GlobalData
.gDESC_FILE
)
212 Logger
.Quiet(ST
.MSG_FINISH
)
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())
231 ST
.ERR_UNKNOWN_FATAL_CREATING_ERR
% \
232 Options
.PackFileToCreate
,
233 ExtraData
=ST
.MSG_SEARCH_FOR_HELP
,
236 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
237 (python_version(), platform
) + format_exc())
238 ReturnCode
= CODE_ERROR
240 if os
.path
.exists(GlobalData
.gCONTENT_FILE
):
241 if not ContentFileClosed
:
243 os
.remove(GlobalData
.gCONTENT_FILE
)
250 # @param QualifiedExt: QualifiedExt
251 # @param FileList: FileList
252 # @param ErrorStringExt: ErrorStringExt
253 # @param ErrorStringFullPath: ErrorStringFullPath
255 def CheckFileList(QualifiedExt
, FileList
, ErrorStringExt
, ErrorStringFullPath
):
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
)
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
)
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
)
277 if not os
.path
.split(Item
)[0]:
278 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
279 ST
.ERR_INVALID_METAFILE_PATH
% Item
)