dec.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ## @file
  2. #
  3. # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
  4. #
  5. # SPDX-License-Identifier: BSD-2-Clause-Patent
  6. #
  7. from plugins.EdkPlugins.basemodel import ini
  8. import re, os
  9. from plugins.EdkPlugins.basemodel.message import *
  10. class DECFile(ini.BaseINIFile):
  11. def GetSectionInstance(self, parent, name, isCombined=False):
  12. return DECSection(parent, name, isCombined)
  13. def GetComponents(self):
  14. return self.GetSectionByName('Components')
  15. def GetPackageRootPath(self):
  16. return os.path.dirname(self.GetFilename()).strip()
  17. def GetBaseName(self):
  18. return self.GetDefine("PACKAGE_NAME").strip()
  19. def GetVersion(self):
  20. return self.GetDefine("PACKAGE_VERSION").strip()
  21. def GetSectionObjectsByName(self, name, arch=None):
  22. arr = []
  23. sects = self.GetSectionByName(name)
  24. for sect in sects:
  25. # skip unmatched archtecture content
  26. if not sect.IsArchMatch(arch):
  27. continue
  28. for obj in sect.GetObjects():
  29. arr.append(obj)
  30. return arr
  31. class DECSection(ini.BaseINISection):
  32. def GetSectionINIObject(self, parent):
  33. type = self.GetType()
  34. if type.lower().find('defines') != -1:
  35. return DECDefineSectionObject(self)
  36. if type.lower().find('includes') != -1:
  37. return DECIncludeObject(self)
  38. if type.lower().find('pcd') != -1:
  39. return DECPcdObject(self)
  40. if type.lower() == 'libraryclasses':
  41. return DECLibraryClassObject(self)
  42. if type.lower() == 'guids':
  43. return DECGuidObject(self)
  44. if type.lower() == 'ppis':
  45. return DECPpiObject(self)
  46. if type.lower() == 'protocols':
  47. return DECProtocolObject(self)
  48. return DECSectionObject(self)
  49. def GetType(self):
  50. arr = self._name.split('.')
  51. return arr[0].strip()
  52. def GetArch(self):
  53. arr = self._name.split('.')
  54. if len(arr) == 1:
  55. return 'common'
  56. return arr[1]
  57. def IsArchMatch(self, arch):
  58. if arch is None or self.GetArch() == 'common':
  59. return True
  60. if self.GetArch().lower() != arch.lower():
  61. return False
  62. return True
  63. class DECSectionObject(ini.BaseINISectionObject):
  64. def GetArch(self):
  65. return self.GetParent().GetArch()
  66. class DECDefineSectionObject(DECSectionObject):
  67. def __init__(self, parent):
  68. DECSectionObject.__init__(self, parent)
  69. self._key = None
  70. self._value = None
  71. def Parse(self):
  72. assert (self._start == self._end), 'The object in define section must be in single line'
  73. line = self.GetLineByOffset(self._start).strip()
  74. line = line.split('#')[0]
  75. arr = line.split('=')
  76. if len(arr) != 2:
  77. ErrorMsg('Invalid define section object',
  78. self.GetFilename(),
  79. self.GetParent().GetName()
  80. )
  81. return False
  82. self._key = arr[0].strip()
  83. self._value = arr[1].strip()
  84. return True
  85. def GetKey(self):
  86. return self._key
  87. def GetValue(self):
  88. return self._value
  89. class DECGuidObject(DECSectionObject):
  90. _objs = {}
  91. def __init__(self, parent):
  92. DECSectionObject.__init__(self, parent)
  93. self._name = None
  94. def Parse(self):
  95. line = self.GetLineByOffset(self._start).strip().split('#')[0]
  96. self._name = line.split('=')[0].strip()
  97. self._guid = line.split('=')[1].strip()
  98. objdict = DECGuidObject._objs
  99. if self._name not in objdict.keys():
  100. objdict[self._name] = [self]
  101. else:
  102. objdict[self._name].append(self)
  103. return True
  104. def GetName(self):
  105. return self._name
  106. def GetGuid(self):
  107. return self._guid
  108. def Destroy(self):
  109. objdict = DECGuidObject._objs
  110. objdict[self._name].remove(self)
  111. if len(objdict[self._name]) == 0:
  112. del objdict[self._name]
  113. @staticmethod
  114. def GetObjectDict():
  115. return DECGuidObject._objs
  116. class DECPpiObject(DECSectionObject):
  117. _objs = {}
  118. def __init__(self, parent):
  119. DECSectionObject.__init__(self, parent)
  120. self._name = None
  121. def Parse(self):
  122. line = self.GetLineByOffset(self._start).strip().split('#')[0]
  123. self._name = line.split('=')[0].strip()
  124. self._guid = line.split('=')[1].strip()
  125. objdict = DECPpiObject._objs
  126. if self._name not in objdict.keys():
  127. objdict[self._name] = [self]
  128. else:
  129. objdict[self._name].append(self)
  130. return True
  131. def GetName(self):
  132. return self._name
  133. def GetGuid(self):
  134. return self._guid
  135. def Destroy(self):
  136. objdict = DECPpiObject._objs
  137. objdict[self._name].remove(self)
  138. if len(objdict[self._name]) == 0:
  139. del objdict[self._name]
  140. @staticmethod
  141. def GetObjectDict():
  142. return DECPpiObject._objs
  143. class DECProtocolObject(DECSectionObject):
  144. _objs = {}
  145. def __init__(self, parent):
  146. DECSectionObject.__init__(self, parent)
  147. self._name = None
  148. def Parse(self):
  149. line = self.GetLineByOffset(self._start).strip().split('#')[0]
  150. self._name = line.split('=')[0].strip()
  151. self._guid = line.split('=')[1].strip()
  152. objdict = DECProtocolObject._objs
  153. if self._name not in objdict.keys():
  154. objdict[self._name] = [self]
  155. else:
  156. objdict[self._name].append(self)
  157. return True
  158. def GetName(self):
  159. return self._name
  160. def GetGuid(self):
  161. return self._guid
  162. def Destroy(self):
  163. objdict = DECProtocolObject._objs
  164. objdict[self._name].remove(self)
  165. if len(objdict[self._name]) == 0:
  166. del objdict[self._name]
  167. @staticmethod
  168. def GetObjectDict():
  169. return DECProtocolObject._objs
  170. class DECLibraryClassObject(DECSectionObject):
  171. _objs = {}
  172. def __init__(self, parent):
  173. DECSectionObject.__init__(self, parent)
  174. self.mClassName = None
  175. self.mHeaderFile = None
  176. def Parse(self):
  177. line = self.GetLineByOffset(self._start).strip().split('#')[0]
  178. self.mClassName, self.mHeaderFile = line.split('|')
  179. objdict = DECLibraryClassObject._objs
  180. if self.mClassName not in objdict.keys():
  181. objdict[self.mClassName] = [self]
  182. else:
  183. objdict[self.mClassName].append(self)
  184. return True
  185. def GetClassName(self):
  186. return self.mClassName
  187. def GetName(self):
  188. return self.mClassName
  189. def GetHeaderFile(self):
  190. return self.mHeaderFile
  191. def Destroy(self):
  192. objdict = DECLibraryClassObject._objs
  193. objdict[self.mClassName].remove(self)
  194. if len(objdict[self.mClassName]) == 0:
  195. del objdict[self.mClassName]
  196. @staticmethod
  197. def GetObjectDict():
  198. return DECLibraryClassObject._objs
  199. class DECIncludeObject(DECSectionObject):
  200. def __init__(self, parent):
  201. DECSectionObject.__init__(self, parent)
  202. def GetPath(self):
  203. return self.GetLineByOffset(self._start).split('#')[0].strip()
  204. class DECPcdObject(DECSectionObject):
  205. _objs = {}
  206. def __init__(self, parent):
  207. DECSectionObject.__init__(self, parent)
  208. self.mPcdName = None
  209. self.mPcdDefaultValue = None
  210. self.mPcdDataType = None
  211. self.mPcdToken = None
  212. def Parse(self):
  213. line = self.GetLineByOffset(self._start).strip().split('#')[0]
  214. (self.mPcdName, self.mPcdDefaultValue, self.mPcdDataType, self.mPcdToken) = line.split('|')
  215. objdict = DECPcdObject._objs
  216. if self.mPcdName not in objdict.keys():
  217. objdict[self.mPcdName] = [self]
  218. else:
  219. objdict[self.mPcdName].append(self)
  220. return True
  221. def Destroy(self):
  222. objdict = DECPcdObject._objs
  223. objdict[self.mPcdName].remove(self)
  224. if len(objdict[self.mPcdName]) == 0:
  225. del objdict[self.mPcdName]
  226. def GetPcdType(self):
  227. return self.GetParent().GetType()
  228. def GetPcdName(self):
  229. return self.mPcdName
  230. def GetPcdValue(self):
  231. return self.mPcdDefaultValue
  232. def GetPcdDataType(self):
  233. return self.mPcdDataType
  234. def GetPcdToken(self):
  235. return self.mPcdToken
  236. def GetName(self):
  237. return self.GetPcdName().split('.')[1]
  238. @staticmethod
  239. def GetObjectDict():
  240. return DECPcdObject._objs