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