PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/edk2/BaseTools/Source/Python/Eot/Database.py

https://gitlab.com/envieidoc/Clover
Python | 255 lines | 154 code | 20 blank | 81 comment | 7 complexity | 8098ef9b1931d9a9947f0bd2cd3f2dd9 MD5 | raw file
  1. ## @file
  2. # This file is used to create a database used by EOT tool
  3. #
  4. # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
  5. # This program and the accompanying materials
  6. # are licensed and made available under the terms and conditions of the BSD License
  7. # which accompanies this distribution. The full text of the license may be found at
  8. # http://opensource.org/licenses/bsd-license.php
  9. #
  10. # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  12. #
  13. ##
  14. # Import Modules
  15. #
  16. import sqlite3
  17. import Common.LongFilePathOs as os, time
  18. import Common.EdkLogger as EdkLogger
  19. import CommonDataClass.DataClass as DataClass
  20. from Table.TableDataModel import TableDataModel
  21. from Table.TableFile import TableFile
  22. from Table.TableFunction import TableFunction
  23. from Table.TableIdentifier import TableIdentifier
  24. from Table.TableEotReport import TableEotReport
  25. from Table.TableInf import TableInf
  26. from Table.TableDec import TableDec
  27. from Table.TableDsc import TableDsc
  28. from Table.TableFdf import TableFdf
  29. from Table.TableQuery import TableQuery
  30. ##
  31. # Static definitions
  32. #
  33. DATABASE_PATH = "Eot.db"
  34. ## Database class
  35. #
  36. # This class defined the EOT databse
  37. # During the phase of initialization, the database will create all tables and
  38. # insert all records of table DataModel
  39. #
  40. class Database(object):
  41. ## The constructor
  42. #
  43. # @param self: The object pointer
  44. # @param DbPath: The file path of the database
  45. #
  46. def __init__(self, DbPath):
  47. self.DbPath = DbPath
  48. self.Conn = None
  49. self.Cur = None
  50. self.TblDataModel = None
  51. self.TblFile = None
  52. self.TblFunction = None
  53. self.TblIdentifier = None
  54. self.TblReport = None
  55. self.TblInf = None
  56. self.TblDec = None
  57. self.TblDsc = None
  58. self.TblFdf = None
  59. self.TblQuery = None
  60. self.TblQuery2 = None
  61. ## InitDatabase() method
  62. # 1. Delete all old existing tables
  63. # 2. Create new tables
  64. # 3. Initialize table DataModel
  65. #
  66. # @param self: The object pointer
  67. # @param NewDatabase: Check if it needs to create a new database
  68. #
  69. def InitDatabase(self, NewDatabase = True):
  70. EdkLogger.verbose("\nInitialize EOT database started ...")
  71. #
  72. # Drop all old existing tables
  73. #
  74. if NewDatabase:
  75. if os.path.exists(self.DbPath):
  76. os.remove(self.DbPath)
  77. self.Conn = sqlite3.connect(self.DbPath, isolation_level = 'DEFERRED')
  78. self.Conn.execute("PRAGMA page_size=8192")
  79. self.Conn.execute("PRAGMA synchronous=OFF")
  80. # to avoid non-ascii charater conversion error
  81. self.Conn.text_factory = str
  82. self.Cur = self.Conn.cursor()
  83. self.TblDataModel = TableDataModel(self.Cur)
  84. self.TblFile = TableFile(self.Cur)
  85. self.TblFunction = TableFunction(self.Cur)
  86. self.TblIdentifier = TableIdentifier(self.Cur)
  87. self.TblReport = TableEotReport(self.Cur)
  88. self.TblInf = TableInf(self.Cur)
  89. self.TblDec = TableDec(self.Cur)
  90. self.TblDsc = TableDsc(self.Cur)
  91. self.TblFdf = TableFdf(self.Cur)
  92. self.TblQuery = TableQuery(self.Cur)
  93. self.TblQuery2 = TableQuery(self.Cur)
  94. self.TblQuery2.Table = 'Query2'
  95. # Create new tables
  96. if NewDatabase:
  97. self.TblDataModel.Create()
  98. self.TblFile.Create()
  99. self.TblFunction.Create()
  100. self.TblReport.Create()
  101. self.TblInf.Create()
  102. self.TblDec.Create()
  103. self.TblDsc.Create()
  104. self.TblFdf.Create()
  105. self.TblQuery.Create()
  106. self.TblQuery2.Create()
  107. # Init each table's ID
  108. self.TblDataModel.InitID()
  109. self.TblFile.InitID()
  110. self.TblFunction.InitID()
  111. self.TblReport.InitID()
  112. self.TblInf.InitID()
  113. self.TblDec.InitID()
  114. self.TblDsc.InitID()
  115. self.TblFdf.InitID()
  116. self.TblQuery.Drop()
  117. self.TblQuery.Create()
  118. self.TblQuery.InitID()
  119. self.TblQuery2.Drop()
  120. self.TblQuery2.Create()
  121. self.TblQuery2.InitID()
  122. # Initialize table DataModel
  123. if NewDatabase:
  124. self.TblDataModel.InitTable()
  125. EdkLogger.verbose("Initialize EOT database ... DONE!")
  126. ## QueryTable() method
  127. #
  128. # Query a table
  129. #
  130. # @param self: The object pointer
  131. # @param Table: The instance of the table to be queried
  132. #
  133. def QueryTable(self, Table):
  134. Table.Query()
  135. ## Close() method
  136. #
  137. # Commit all first
  138. # Close the connection and cursor
  139. #
  140. def Close(self):
  141. # Commit to file
  142. self.Conn.commit()
  143. # Close connection and cursor
  144. self.Cur.close()
  145. self.Conn.close()
  146. ## InsertOneFile() method
  147. #
  148. # Insert one file's information to the database
  149. # 1. Create a record in TableFile
  150. # 2. Create functions one by one
  151. # 2.1 Create variables of function one by one
  152. # 2.2 Create pcds of function one by one
  153. # 3. Create variables one by one
  154. # 4. Create pcds one by one
  155. #
  156. # @param self: The object pointer
  157. # @param File: The object of the file to be inserted
  158. #
  159. def InsertOneFile(self, File):
  160. # Insert a record for file
  161. FileID = self.TblFile.Insert(File.Name, File.ExtName, File.Path, File.FullPath, Model = File.Model, TimeStamp = File.TimeStamp)
  162. IdTable = TableIdentifier(self.Cur)
  163. IdTable.Table = "Identifier%s" % FileID
  164. IdTable.Create()
  165. # Insert function of file
  166. for Function in File.FunctionList:
  167. FunctionID = self.TblFunction.Insert(Function.Header, Function.Modifier, Function.Name, Function.ReturnStatement, \
  168. Function.StartLine, Function.StartColumn, Function.EndLine, Function.EndColumn, \
  169. Function.BodyStartLine, Function.BodyStartColumn, FileID, \
  170. Function.FunNameStartLine, Function.FunNameStartColumn)
  171. # Insert Identifier of function
  172. for Identifier in Function.IdentifierList:
  173. IdentifierID = IdTable.Insert(Identifier.Modifier, Identifier.Type, Identifier.Name, Identifier.Value, Identifier.Model, \
  174. FileID, FunctionID, Identifier.StartLine, Identifier.StartColumn, Identifier.EndLine, Identifier.EndColumn)
  175. # Insert Identifier of file
  176. for Identifier in File.IdentifierList:
  177. IdentifierID = IdTable.Insert(Identifier.Modifier, Identifier.Type, Identifier.Name, Identifier.Value, Identifier.Model, \
  178. FileID, -1, Identifier.StartLine, Identifier.StartColumn, Identifier.EndLine, Identifier.EndColumn)
  179. EdkLogger.verbose("Insert information from file %s ... DONE!" % File.FullPath)
  180. ## UpdateIdentifierBelongsToFunction() method
  181. #
  182. # Update the field "BelongsToFunction" for each Indentifier
  183. #
  184. # @param self: The object pointer
  185. #
  186. def UpdateIdentifierBelongsToFunction(self):
  187. EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers started ...")
  188. SqlCommand = """select ID, BelongsToFile, StartLine, EndLine from Function"""
  189. Records = self.TblFunction.Exec(SqlCommand)
  190. Data1 = []
  191. Data2 = []
  192. for Record in Records:
  193. FunctionID = Record[0]
  194. BelongsToFile = Record[1]
  195. StartLine = Record[2]
  196. EndLine = Record[3]
  197. SqlCommand = """Update Identifier%s set BelongsToFunction = %s where BelongsToFile = %s and StartLine > %s and EndLine < %s""" % \
  198. (BelongsToFile, FunctionID, BelongsToFile, StartLine, EndLine)
  199. self.TblIdentifier.Exec(SqlCommand)
  200. SqlCommand = """Update Identifier%s set BelongsToFunction = %s, Model = %s where BelongsToFile = %s and Model = %s and EndLine = %s""" % \
  201. (BelongsToFile, FunctionID, DataClass.MODEL_IDENTIFIER_FUNCTION_HEADER, BelongsToFile, DataClass.MODEL_IDENTIFIER_COMMENT, StartLine - 1)
  202. self.TblIdentifier.Exec(SqlCommand)
  203. ##
  204. #
  205. # This acts like the main() function for the script, unless it is 'import'ed into another
  206. # script.
  207. #
  208. if __name__ == '__main__':
  209. EdkLogger.Initialize()
  210. EdkLogger.SetLevel(EdkLogger.DEBUG_0)
  211. EdkLogger.verbose("Start at " + time.strftime('%H:%M:%S', time.localtime()))
  212. Db = Database(DATABASE_PATH)
  213. Db.InitDatabase()
  214. Db.QueryTable(Db.TblDataModel)
  215. identifier1 = DataClass.IdentifierClass(-1, '', '', "i''1", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 32, 43, 54, 43)
  216. identifier2 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 15, 43, 20, 43)
  217. identifier3 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 55, 43, 58, 43)
  218. identifier4 = DataClass.IdentifierClass(-1, '', '', "i1'", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 77, 43, 88, 43)
  219. fun1 = DataClass.FunctionClass(-1, '', '', 'fun1', '', 21, 2, 60, 45, 1, 23, 0, [], [])
  220. file = DataClass.FileClass(-1, 'F1', 'c', 'C:\\', 'C:\\F1.exe', DataClass.MODEL_FILE_C, '2007-12-28', [fun1], [identifier1, identifier2, identifier3, identifier4], [])
  221. Db.InsertOneFile(file)
  222. Db.QueryTable(Db.TblFile)
  223. Db.QueryTable(Db.TblFunction)
  224. Db.QueryTable(Db.TblIdentifier)
  225. Db.Close()
  226. EdkLogger.verbose("End at " + time.strftime('%H:%M:%S', time.localtime()))