PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/edk2/BaseTools/Source/Python/UPT/MkPkg.py

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