2 # Install distribution package.
4 # Copyright (c) 2011, 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
.Misc
import CheckEnvVariable
49 from Library
import GlobalData
50 from Library
.ParserValidate
import IsValidPath
52 from Core
.DistributionPackageClass
import DistributionPackageClass
53 from Core
.PackageFile
import PackageFile
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
82 WorkspaceDir
= GlobalData
.gWORKSPACE
85 # Init PackFileToCreate
87 if not Options
.PackFileToCreate
:
88 Logger
.Error("\nMkPkg", OPTION_UNKNOWN_ERROR
, ST
.ERR_OPTION_NOT_FOUND
)
91 # Handle if the distribution package file already exists
93 CheckForExistingDp(Options
.PackFileToCreate
)
96 # Check package file existing and valid
98 CheckFileList('.DEC', Options
.PackageFileList
, ST
.ERR_INVALID_PACKAGE_NAME
, ST
.ERR_INVALID_PACKAGE_PATH
)
100 # Check module file existing and valid
102 CheckFileList('.INF', Options
.ModuleFileList
, ST
.ERR_INVALID_MODULE_NAME
, ST
.ERR_INVALID_MODULE_PATH
)
105 # Get list of files that installed with RePackage attribute available
107 RePkgDict
= DataBase
.GetRePkgDict()
109 ContentFile
= PackageFile(GlobalData
.gCONTENT_FILE
, "w")
110 ContentFileClosed
= False
113 # Add temp distribution header
115 if Options
.PackageInformationDataFile
:
116 XmlFile
= IniToXml(Options
.PackageInformationDataFile
)
117 DistPkg
= DistributionPackageXml().FromXml(XmlFile
)
121 # add distribution level tool/misc files
122 # before pack, current dir should be workspace dir, else the full
123 # path will be in the pack file
127 ToolObject
= DistPkg
.Tools
128 MiscObject
= DistPkg
.MiscellaneousFiles
131 FileList
+= ToolObject
.GetFileList()
133 FileList
+= MiscObject
.GetFileList()
134 for FileObject
in FileList
:
136 # If you have unicode file names, please convert them to byte
137 # strings in your desired encoding before passing them to
140 FromFile
= os
.path
.normpath(FileObject
.GetURI()).encode('utf_8')
141 FileFullPath
= os
.path
.normpath(os
.path
.join(WorkspaceDir
, FromFile
))
142 if FileFullPath
in RePkgDict
:
143 (DpGuid
, DpVersion
, DpName
, Repackage
) = RePkgDict
[FileFullPath
]
145 Logger
.Error("\nMkPkg",
147 ST
.ERR_UPT_REPKG_ERROR
,
148 ExtraData
=ST
.MSG_REPKG_CONFLICT
%\
149 (FileFullPath
, DpGuid
, DpVersion
, DpName
)
152 DistPkg
.Header
.RePackage
= True
153 ContentFile
.PackFile(FromFile
)
157 # Add init dp information
160 DistPkg
= DistributionPackageClass()
161 DistPkg
.Header
.Name
= 'Distribution Package'
162 DistPkg
.Header
.Guid
= str(uuid4())
163 DistPkg
.Header
.Version
= '1.0'
165 DistPkg
.GetDistributionPackage(WorkspaceDir
, Options
.PackageFileList
, \
166 Options
.ModuleFileList
)
167 FileList
, MetaDataFileList
= DistPkg
.GetDistributionFileList()
168 for File
in FileList
+ MetaDataFileList
:
169 FileFullPath
= os
.path
.normpath(os
.path
.join(WorkspaceDir
, File
))
171 # check whether file was included in a distribution that can not
174 if FileFullPath
in RePkgDict
:
175 (DpGuid
, DpVersion
, DpName
, Repackage
) = RePkgDict
[FileFullPath
]
177 Logger
.Error("\nMkPkg",
179 ST
.ERR_UPT_REPKG_ERROR
,
181 ST
.MSG_REPKG_CONFLICT
%(FileFullPath
, DpName
, \
185 DistPkg
.Header
.RePackage
= True
189 ContentFile
.PackFiles(FileList
)
192 Logger
.Verbose(ST
.MSG_COMPRESS_DISTRIBUTION_PKG
)
195 ContentFileClosed
= True
200 DistPkg
.Header
.Signature
= md5
.new(open(str(ContentFile
), 'rb').read()).hexdigest()
204 DistPkg
.Header
.Date
= str(strftime("%Y-%m-%dT%H:%M:%S", localtime()))
207 # Finish final dp file
209 DistPkgFile
= PackageFile(Options
.PackFileToCreate
, "w")
210 DistPkgFile
.PackFile(str(ContentFile
))
211 DistPkgXml
= DistributionPackageXml()
212 DistPkgFile
.PackData(DistPkgXml
.ToXml(DistPkg
), GlobalData
.gDESC_FILE
)
214 Logger
.Quiet(ST
.MSG_FINISH
)
217 except FatalError
, XExcept
:
218 ReturnCode
= XExcept
.args
[0]
219 if Logger
.GetLevel() <= Logger
.DEBUG_9
:
220 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
221 (python_version(), platform
) + format_exc())
222 except KeyboardInterrupt:
223 ReturnCode
= ABORT_ERROR
224 if Logger
.GetLevel() <= Logger
.DEBUG_9
:
225 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
226 (python_version(), platform
) + format_exc())
233 ST
.ERR_UNKNOWN_FATAL_CREATING_ERR
% \
234 Options
.PackFileToCreate
,
235 ExtraData
=ST
.MSG_SEARCH_FOR_HELP
,
238 Logger
.Quiet(ST
.MSG_PYTHON_ON
% \
239 (python_version(), platform
) + format_exc())
240 ReturnCode
= CODE_ERROR
242 if os
.path
.exists(GlobalData
.gCONTENT_FILE
):
243 if not ContentFileClosed
:
245 os
.remove(GlobalData
.gCONTENT_FILE
)
252 # @param QualifiedExt: QualifiedExt
253 # @param FileList: FileList
254 # @param ErrorStringExt: ErrorStringExt
255 # @param ErrorStringFullPath: ErrorStringFullPath
257 def CheckFileList(QualifiedExt
, FileList
, ErrorStringExt
, ErrorStringFullPath
):
260 WorkspaceDir
= GlobalData
.gWORKSPACE
261 WorkspaceDir
= os
.path
.normpath(WorkspaceDir
)
262 for Item
in FileList
:
263 Ext
= os
.path
.splitext(Item
)[1]
264 if Ext
.upper() != QualifiedExt
.upper():
265 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
266 ErrorStringExt
% Item
)
268 Item
= os
.path
.normpath(Item
)
269 Path
= os
.path
.normpath(os
.path
.join(WorkspaceDir
, Item
))
270 if not os
.path
.exists(Path
):
271 Logger
.Error("\nMkPkg", FILE_NOT_FOUND
, ST
.ERR_NOT_FOUND
% Item
)
273 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
,
274 ErrorStringFullPath
% Item
)
275 elif not IsValidPath(Item
, WorkspaceDir
):
276 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
277 ErrorStringExt
% Item
)
279 if not os
.path
.split(Item
)[0]:
280 Logger
.Error("\nMkPkg", OPTION_VALUE_INVALID
, \
281 ST
.ERR_INVALID_METAFILE_PATH
% Item
)