]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - BaseTools/Source/Python/Ecc/Database.py
BaseTools/Ecc: Fix grammar in Ecc error message
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / Database.py
... / ...
CommitLineData
1## @file\r
2# This file is used to create a database used by ECC tool\r
3#\r
4# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
5# SPDX-License-Identifier: BSD-2-Clause-Patent\r
6#\r
7\r
8##\r
9# Import Modules\r
10#\r
11from __future__ import absolute_import\r
12import sqlite3\r
13import Common.LongFilePathOs as os, time\r
14\r
15import Common.EdkLogger as EdkLogger\r
16import CommonDataClass.DataClass as DataClass\r
17\r
18from Table.TableDataModel import TableDataModel\r
19from Table.TableFile import TableFile\r
20from Table.TableFunction import TableFunction\r
21from Table.TablePcd import TablePcd\r
22from Table.TableIdentifier import TableIdentifier\r
23from Table.TableReport import TableReport\r
24from Ecc.MetaFileWorkspace.MetaFileTable import ModuleTable\r
25from Ecc.MetaFileWorkspace.MetaFileTable import PackageTable\r
26from Ecc.MetaFileWorkspace.MetaFileTable import PlatformTable\r
27from Table.TableFdf import TableFdf\r
28\r
29##\r
30# Static definitions\r
31#\r
32DATABASE_PATH = "Ecc.db"\r
33\r
34## Database\r
35#\r
36# This class defined the ECC database\r
37# During the phase of initialization, the database will create all tables and\r
38# insert all records of table DataModel\r
39#\r
40# @param object: Inherited from object class\r
41# @param DbPath: A string for the path of the ECC database\r
42#\r
43# @var Conn: Connection of the ECC database\r
44# @var Cur: Cursor of the connection\r
45# @var TblDataModel: Local instance for TableDataModel\r
46#\r
47class Database(object):\r
48 def __init__(self, DbPath):\r
49 self.DbPath = DbPath\r
50 self.Conn = None\r
51 self.Cur = None\r
52 self.TblDataModel = None\r
53 self.TblFile = None\r
54 self.TblFunction = None\r
55 self.TblIdentifier = None\r
56 self.TblPcd = None\r
57 self.TblReport = None\r
58 self.TblInf = None\r
59 self.TblDec = None\r
60 self.TblDsc = None\r
61 self.TblFdf = None\r
62\r
63 ## Initialize ECC database\r
64 #\r
65 # 1. Delete all old existing tables\r
66 # 2. Create new tables\r
67 # 3. Initialize table DataModel\r
68 #\r
69 def InitDatabase(self, NewDatabase = True):\r
70 EdkLogger.verbose("\nInitialize ECC database started ...")\r
71 #\r
72 # Drop all old existing tables\r
73 #\r
74 if NewDatabase:\r
75 if os.path.exists(self.DbPath):\r
76 os.remove(self.DbPath)\r
77 self.Conn = sqlite3.connect(self.DbPath, isolation_level = 'DEFERRED')\r
78 self.Conn.execute("PRAGMA page_size=4096")\r
79 self.Conn.execute("PRAGMA synchronous=OFF")\r
80 # to avoid non-ascii character conversion error\r
81 self.Conn.text_factory = str\r
82 self.Cur = self.Conn.cursor()\r
83\r
84 self.TblDataModel = TableDataModel(self.Cur)\r
85 self.TblFile = TableFile(self.Cur)\r
86 self.TblFunction = TableFunction(self.Cur)\r
87 self.TblIdentifier = TableIdentifier(self.Cur)\r
88 self.TblPcd = TablePcd(self.Cur)\r
89 self.TblReport = TableReport(self.Cur)\r
90 self.TblInf = ModuleTable(self.Cur)\r
91 self.TblDec = PackageTable(self.Cur)\r
92 self.TblDsc = PlatformTable(self.Cur)\r
93 self.TblFdf = TableFdf(self.Cur)\r
94\r
95 #\r
96 # Create new tables\r
97 #\r
98 if NewDatabase:\r
99 self.TblDataModel.Create()\r
100 self.TblFile.Create()\r
101 self.TblFunction.Create()\r
102 self.TblPcd.Create()\r
103 self.TblReport.Create()\r
104 self.TblInf.Create()\r
105 self.TblDec.Create()\r
106 self.TblDsc.Create()\r
107 self.TblFdf.Create()\r
108\r
109 #\r
110 # Init each table's ID\r
111 #\r
112 self.TblDataModel.InitID()\r
113 self.TblFile.InitID()\r
114 self.TblFunction.InitID()\r
115 self.TblPcd.InitID()\r
116 self.TblReport.InitID()\r
117 self.TblInf.InitID()\r
118 self.TblDec.InitID()\r
119 self.TblDsc.InitID()\r
120 self.TblFdf.InitID()\r
121\r
122 #\r
123 # Initialize table DataModel\r
124 #\r
125 if NewDatabase:\r
126 self.TblDataModel.InitTable()\r
127\r
128 EdkLogger.verbose("Initialize ECC database ... DONE!")\r
129\r
130 ## Query a table\r
131 #\r
132 # @param Table: The instance of the table to be queried\r
133 #\r
134 def QueryTable(self, Table):\r
135 Table.Query()\r
136\r
137 ## Close entire database\r
138 #\r
139 # Commit all first\r
140 # Close the connection and cursor\r
141 #\r
142 def Close(self):\r
143 #\r
144 # Commit to file\r
145 #\r
146 self.Conn.commit()\r
147\r
148 #\r
149 # Close connection and cursor\r
150 #\r
151 self.Cur.close()\r
152 self.Conn.close()\r
153\r
154 ## Insert one file information\r
155 #\r
156 # Insert one file's information to the database\r
157 # 1. Create a record in TableFile\r
158 # 2. Create functions one by one\r
159 # 2.1 Create variables of function one by one\r
160 # 2.2 Create pcds of function one by one\r
161 # 3. Create variables one by one\r
162 # 4. Create pcds one by one\r
163 #\r
164 def InsertOneFile(self, File):\r
165 #\r
166 # Insert a record for file\r
167 #\r
168 FileID = self.TblFile.Insert(File.Name, File.ExtName, File.Path, File.FullPath, Model = File.Model, TimeStamp = File.TimeStamp)\r
169\r
170 if File.Model == DataClass.MODEL_FILE_C or File.Model == DataClass.MODEL_FILE_H:\r
171 IdTable = TableIdentifier(self.Cur)\r
172 IdTable.Table = "Identifier%s" % FileID\r
173 IdTable.Create()\r
174 #\r
175 # Insert function of file\r
176 #\r
177 for Function in File.FunctionList:\r
178 FunctionID = self.TblFunction.Insert(Function.Header, Function.Modifier, Function.Name, Function.ReturnStatement, \\r
179 Function.StartLine, Function.StartColumn, Function.EndLine, Function.EndColumn, \\r
180 Function.BodyStartLine, Function.BodyStartColumn, FileID, \\r
181 Function.FunNameStartLine, Function.FunNameStartColumn)\r
182 #\r
183 # Insert Identifier of function\r
184 #\r
185 for Identifier in Function.IdentifierList:\r
186 IdentifierID = IdTable.Insert(Identifier.Modifier, Identifier.Type, Identifier.Name, Identifier.Value, Identifier.Model, \\r
187 FileID, FunctionID, Identifier.StartLine, Identifier.StartColumn, Identifier.EndLine, Identifier.EndColumn)\r
188 #\r
189 # Insert Pcd of function\r
190 #\r
191 for Pcd in Function.PcdList:\r
192 PcdID = self.TblPcd.Insert(Pcd.CName, Pcd.TokenSpaceGuidCName, Pcd.Token, Pcd.DatumType, Pcd.Model, \\r
193 FileID, FunctionID, Pcd.StartLine, Pcd.StartColumn, Pcd.EndLine, Pcd.EndColumn)\r
194 #\r
195 # Insert Identifier of file\r
196 #\r
197 for Identifier in File.IdentifierList:\r
198 IdentifierID = IdTable.Insert(Identifier.Modifier, Identifier.Type, Identifier.Name, Identifier.Value, Identifier.Model, \\r
199 FileID, -1, Identifier.StartLine, Identifier.StartColumn, Identifier.EndLine, Identifier.EndColumn)\r
200 #\r
201 # Insert Pcd of file\r
202 #\r
203 for Pcd in File.PcdList:\r
204 PcdID = self.TblPcd.Insert(Pcd.CName, Pcd.TokenSpaceGuidCName, Pcd.Token, Pcd.DatumType, Pcd.Model, \\r
205 FileID, -1, Pcd.StartLine, Pcd.StartColumn, Pcd.EndLine, Pcd.EndColumn)\r
206\r
207 EdkLogger.verbose("Insert information from file %s ... DONE!" % File.FullPath)\r
208\r
209 ## UpdateIdentifierBelongsToFunction\r
210 #\r
211 # Update the field "BelongsToFunction" for each Identifier\r
212 #\r
213 #\r
214 def UpdateIdentifierBelongsToFunction_disabled(self):\r
215 EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers started ...")\r
216\r
217 SqlCommand = """select ID, BelongsToFile, StartLine, EndLine, Model from Identifier"""\r
218 EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r
219 self.Cur.execute(SqlCommand)\r
220 Records = self.Cur.fetchall()\r
221 for Record in Records:\r
222 IdentifierID = Record[0]\r
223 BelongsToFile = Record[1]\r
224 StartLine = Record[2]\r
225 EndLine = Record[3]\r
226 Model = Record[4]\r
227\r
228 #\r
229 # Check whether an identifier belongs to a function\r
230 #\r
231 EdkLogger.debug(4, "For common identifiers ... ")\r
232 SqlCommand = """select ID from Function\r
233 where StartLine < %s and EndLine > %s\r
234 and BelongsToFile = %s""" % (StartLine, EndLine, BelongsToFile)\r
235 EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r
236 self.Cur.execute(SqlCommand)\r
237 IDs = self.Cur.fetchall()\r
238 for ID in IDs:\r
239 SqlCommand = """Update Identifier set BelongsToFunction = %s where ID = %s""" % (ID[0], IdentifierID)\r
240 EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r
241 self.Cur.execute(SqlCommand)\r
242\r
243 #\r
244 # Check whether the identifier is a function header\r
245 #\r
246 EdkLogger.debug(4, "For function headers ... ")\r
247 if Model == DataClass.MODEL_IDENTIFIER_COMMENT:\r
248 SqlCommand = """select ID from Function\r
249 where StartLine = %s + 1\r
250 and BelongsToFile = %s""" % (EndLine, BelongsToFile)\r
251 EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r
252 self.Cur.execute(SqlCommand)\r
253 IDs = self.Cur.fetchall()\r
254 for ID in IDs:\r
255 SqlCommand = """Update Identifier set BelongsToFunction = %s, Model = %s where ID = %s""" % (ID[0], DataClass.MODEL_IDENTIFIER_FUNCTION_HEADER, IdentifierID)\r
256 EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r
257 self.Cur.execute(SqlCommand)\r
258\r
259 EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers ... DONE")\r
260\r
261\r
262 ## UpdateIdentifierBelongsToFunction\r
263 #\r
264 # Update the field "BelongsToFunction" for each Identifier\r
265 #\r
266 #\r
267 def UpdateIdentifierBelongsToFunction(self):\r
268 EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers started ...")\r
269\r
270 SqlCommand = """select ID, BelongsToFile, StartLine, EndLine from Function"""\r
271 Records = self.TblFunction.Exec(SqlCommand)\r
272 Data1 = []\r
273 Data2 = []\r
274 for Record in Records:\r
275 FunctionID = Record[0]\r
276 BelongsToFile = Record[1]\r
277 StartLine = Record[2]\r
278 EndLine = Record[3]\r
279 #Data1.append(("'file%s'" % BelongsToFile, FunctionID, BelongsToFile, StartLine, EndLine))\r
280 #Data2.append(("'file%s'" % BelongsToFile, FunctionID, DataClass.MODEL_IDENTIFIER_FUNCTION_HEADER, BelongsToFile, DataClass.MODEL_IDENTIFIER_COMMENT, StartLine - 1))\r
281\r
282 SqlCommand = """Update Identifier%s set BelongsToFunction = %s where BelongsToFile = %s and StartLine > %s and EndLine < %s""" % \\r
283 (BelongsToFile, FunctionID, BelongsToFile, StartLine, EndLine)\r
284 self.TblIdentifier.Exec(SqlCommand)\r
285\r
286 SqlCommand = """Update Identifier%s set BelongsToFunction = %s, Model = %s where BelongsToFile = %s and Model = %s and EndLine = %s""" % \\r
287 (BelongsToFile, FunctionID, DataClass.MODEL_IDENTIFIER_FUNCTION_HEADER, BelongsToFile, DataClass.MODEL_IDENTIFIER_COMMENT, StartLine - 1)\r
288 self.TblIdentifier.Exec(SqlCommand)\r
289# #\r
290# # Check whether an identifier belongs to a function\r
291# #\r
292# print Data1\r
293# SqlCommand = """Update ? set BelongsToFunction = ? where BelongsToFile = ? and StartLine > ? and EndLine < ?"""\r
294# print SqlCommand\r
295# EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r
296# self.Cur.executemany(SqlCommand, Data1)\r
297#\r
298# #\r
299# # Check whether the identifier is a function header\r
300# #\r
301# EdkLogger.debug(4, "For function headers ... ")\r
302# SqlCommand = """Update ? set BelongsToFunction = ?, Model = ? where BelongsToFile = ? and Model = ? and EndLine = ?"""\r
303# EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r
304# self.Cur.executemany(SqlCommand, Data2)\r
305#\r
306# EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers ... DONE")\r
307\r
308\r
309##\r
310#\r
311# This acts like the main() function for the script, unless it is 'import'ed into another\r
312# script.\r
313#\r
314if __name__ == '__main__':\r
315 EdkLogger.Initialize()\r
316 #EdkLogger.SetLevel(EdkLogger.VERBOSE)\r
317 EdkLogger.SetLevel(EdkLogger.DEBUG_0)\r
318 EdkLogger.verbose("Start at " + time.strftime('%H:%M:%S', time.localtime()))\r
319\r
320 Db = Database(DATABASE_PATH)\r
321 Db.InitDatabase()\r
322 Db.QueryTable(Db.TblDataModel)\r
323\r
324 identifier1 = DataClass.IdentifierClass(-1, '', '', "i''1", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 32, 43, 54, 43)\r
325 identifier2 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 15, 43, 20, 43)\r
326 identifier3 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 55, 43, 58, 43)\r
327 identifier4 = DataClass.IdentifierClass(-1, '', '', "i1'", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 77, 43, 88, 43)\r
328 fun1 = DataClass.FunctionClass(-1, '', '', 'fun1', '', 21, 2, 60, 45, 1, 23, 0, [], [])\r
329 file = DataClass.FileClass(-1, 'F1', 'c', 'C:\\', 'C:\\F1.exe', DataClass.MODEL_FILE_C, '2007-12-28', [fun1], [identifier1, identifier2, identifier3, identifier4], [])\r
330 Db.InsertOneFile(file)\r
331 Db.UpdateIdentifierBelongsToFunction()\r
332\r
333 Db.QueryTable(Db.TblFile)\r
334 Db.QueryTable(Db.TblFunction)\r
335 Db.QueryTable(Db.TblPcd)\r
336 Db.QueryTable(Db.TblIdentifier)\r
337\r
338 Db.Close()\r
339 EdkLogger.verbose("End at " + time.strftime('%H:%M:%S', time.localtime()))\r
340\r