]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Workspace/MetaFileTable.py
Sync BaseTool trunk (version r2610) into EDKII BaseTools.
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / MetaFileTable.py
CommitLineData
30fdf114
LG
1## @file\r
2# This file is used to create/update/query/erase a meta file table\r
3#\r
40d841f6
LG
4# Copyright (c) 2008, Intel Corporation. All rights reserved.<BR>\r
5# This program and the accompanying materials\r
30fdf114
LG
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
0d2711a6
LG
17import uuid\r
18\r
30fdf114 19import Common.EdkLogger as EdkLogger\r
0d2711a6
LG
20\r
21from MetaDataTable import Table, TableFile\r
30fdf114 22from MetaDataTable import ConvertToSqlString\r
0d2711a6
LG
23from CommonDataClass.DataClass import MODEL_FILE_DSC, MODEL_FILE_DEC, MODEL_FILE_INF, \\r
24 MODEL_FILE_OTHERS\r
30fdf114 25\r
0d2711a6 26class MetaFileTable(Table):\r
30fdf114 27 # TRICK: use file ID as the part before '.'\r
0d2711a6
LG
28 _ID_STEP_ = 0.00000001\r
29 _ID_MAX_ = 0.99999999\r
30\r
31 ## Constructor\r
32 def __init__(self, Cursor, MetaFile, FileType, Temporary):\r
33 self.MetaFile = MetaFile\r
34\r
35 self._FileIndexTable = TableFile(Cursor)\r
36 self._FileIndexTable.Create(False)\r
37\r
38 FileId = self._FileIndexTable.GetFileId(MetaFile)\r
39 if not FileId:\r
40 FileId = self._FileIndexTable.InsertFile(MetaFile, FileType)\r
41\r
42 if Temporary:\r
43 TableName = "_%s_%s_%s" % (FileType, FileId, uuid.uuid4().hex)\r
44 else:\r
45 TableName = "_%s_%s" % (FileType, FileId)\r
46\r
47 #Table.__init__(self, Cursor, TableName, FileId, False)\r
48 Table.__init__(self, Cursor, TableName, FileId, Temporary)\r
49 self.Create(not self.IsIntegrity())\r
50\r
51 def IsIntegrity(self):\r
52 try:\r
64b2609f 53 TimeStamp = self.MetaFile.TimeStamp\r
0d2711a6
LG
54 Result = self.Cur.execute("select ID from %s where ID<0" % (self.Table)).fetchall()\r
55 if not Result:\r
64b2609f
LG
56 # update the timestamp in database\r
57 self._FileIndexTable.SetFileTimeStamp(self.IdBase, TimeStamp) \r
0d2711a6
LG
58 return False\r
59\r
0d2711a6
LG
60 if TimeStamp != self._FileIndexTable.GetFileTimeStamp(self.IdBase):\r
61 # update the timestamp in database\r
62 self._FileIndexTable.SetFileTimeStamp(self.IdBase, TimeStamp)\r
63 return False\r
64 except Exception, Exc:\r
65 EdkLogger.debug(EdkLogger.DEBUG_5, str(Exc))\r
66 return False\r
67 return True\r
68\r
69## Python class representation of table storing module data\r
70class ModuleTable(MetaFileTable):\r
30fdf114
LG
71 _ID_STEP_ = 0.00000001\r
72 _ID_MAX_ = 0.99999999\r
73 _COLUMN_ = '''\r
74 ID REAL PRIMARY KEY,\r
75 Model INTEGER NOT NULL,\r
76 Value1 TEXT NOT NULL,\r
77 Value2 TEXT,\r
78 Value3 TEXT,\r
79 Scope1 TEXT,\r
80 Scope2 TEXT,\r
81 BelongsToItem REAL NOT NULL,\r
82 StartLine INTEGER NOT NULL,\r
83 StartColumn INTEGER NOT NULL,\r
84 EndLine INTEGER NOT NULL,\r
85 EndColumn INTEGER NOT NULL,\r
86 Enabled INTEGER DEFAULT 0\r
87 '''\r
88 # used as table end flag, in case the changes to database is not committed to db file\r
89 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1"\r
90\r
91 ## Constructor\r
0d2711a6
LG
92 def __init__(self, Cursor, MetaFile, Temporary):\r
93 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_INF, Temporary)\r
30fdf114
LG
94\r
95 ## Insert a record into table Inf\r
96 #\r
97 # @param Model: Model of a Inf item\r
98 # @param Value1: Value1 of a Inf item\r
99 # @param Value2: Value2 of a Inf item\r
100 # @param Value3: Value3 of a Inf item\r
101 # @param Scope1: Arch of a Inf item\r
102 # @param Scope2 Platform os a Inf item\r
103 # @param BelongsToItem: The item belongs to which another item\r
104 # @param StartLine: StartLine of a Inf item\r
105 # @param StartColumn: StartColumn of a Inf item\r
106 # @param EndLine: EndLine of a Inf item\r
107 # @param EndColumn: EndColumn of a Inf item\r
108 # @param Enabled: If this item enabled\r
109 #\r
110 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON',\r
111 BelongsToItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=0):\r
112 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))\r
113 return Table.Insert(\r
114 self, \r
115 Model, \r
116 Value1, \r
117 Value2, \r
118 Value3, \r
119 Scope1, \r
120 Scope2,\r
121 BelongsToItem, \r
122 StartLine, \r
123 StartColumn, \r
124 EndLine, \r
125 EndColumn, \r
126 Enabled\r
127 )\r
128\r
129 ## Query table\r
130 #\r
131 # @param Model: The Model of Record \r
132 # @param Arch: The Arch attribute of Record \r
133 # @param Platform The Platform attribute of Record \r
134 #\r
135 # @retval: A recordSet of all found records \r
136 #\r
e8a47801 137 def Query(self, Model, Arch=None, Platform=None, BelongsToItem=None):\r
30fdf114
LG
138 ConditionString = "Model=%s AND Enabled>=0" % Model\r
139 ValueString = "Value1,Value2,Value3,Scope1,Scope2,ID,StartLine"\r
140\r
141 if Arch != None and Arch != 'COMMON':\r
142 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Arch\r
143 if Platform != None and Platform != 'COMMON':\r
144 ConditionString += " AND (Scope2='%s' OR Scope2='COMMON' OR Scope2='DEFAULT')" % Platform\r
e8a47801
LG
145 if BelongsToItem != None:\r
146 ConditionString += " AND BelongsToItem=%s" % BelongsToItem\r
30fdf114
LG
147\r
148 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)\r
149 return self.Exec(SqlCommand)\r
150\r
151## Python class representation of table storing package data\r
0d2711a6 152class PackageTable(MetaFileTable):\r
30fdf114
LG
153 _COLUMN_ = '''\r
154 ID REAL PRIMARY KEY,\r
155 Model INTEGER NOT NULL,\r
156 Value1 TEXT NOT NULL,\r
157 Value2 TEXT,\r
158 Value3 TEXT,\r
159 Scope1 TEXT,\r
160 Scope2 TEXT,\r
161 BelongsToItem REAL NOT NULL,\r
162 StartLine INTEGER NOT NULL,\r
163 StartColumn INTEGER NOT NULL,\r
164 EndLine INTEGER NOT NULL,\r
165 EndColumn INTEGER NOT NULL,\r
166 Enabled INTEGER DEFAULT 0\r
167 '''\r
168 # used as table end flag, in case the changes to database is not committed to db file\r
169 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1"\r
170\r
171 ## Constructor\r
0d2711a6
LG
172 def __init__(self, Cursor, MetaFile, Temporary):\r
173 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_DEC, Temporary)\r
30fdf114
LG
174\r
175 ## Insert table\r
176 #\r
177 # Insert a record into table Dec\r
178 #\r
179 # @param Model: Model of a Dec item\r
180 # @param Value1: Value1 of a Dec item\r
181 # @param Value2: Value2 of a Dec item\r
182 # @param Value3: Value3 of a Dec item\r
183 # @param Scope1: Arch of a Dec item\r
184 # @param Scope2: Module type of a Dec item\r
185 # @param BelongsToItem: The item belongs to which another item\r
186 # @param StartLine: StartLine of a Dec item\r
187 # @param StartColumn: StartColumn of a Dec item\r
188 # @param EndLine: EndLine of a Dec item\r
189 # @param EndColumn: EndColumn of a Dec item\r
190 # @param Enabled: If this item enabled\r
191 #\r
192 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON',\r
193 BelongsToItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=0):\r
194 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))\r
195 return Table.Insert(\r
196 self, \r
197 Model, \r
198 Value1, \r
199 Value2, \r
200 Value3, \r
201 Scope1, \r
202 Scope2,\r
203 BelongsToItem, \r
204 StartLine, \r
205 StartColumn, \r
206 EndLine, \r
207 EndColumn, \r
208 Enabled\r
209 )\r
210\r
211 ## Query table\r
212 #\r
213 # @param Model: The Model of Record \r
214 # @param Arch: The Arch attribute of Record \r
215 #\r
216 # @retval: A recordSet of all found records \r
217 #\r
218 def Query(self, Model, Arch=None):\r
219 ConditionString = "Model=%s AND Enabled>=0" % Model\r
220 ValueString = "Value1,Value2,Value3,Scope1,ID,StartLine"\r
221\r
222 if Arch != None and Arch != 'COMMON':\r
223 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Arch\r
224\r
225 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)\r
226 return self.Exec(SqlCommand)\r
227\r
228## Python class representation of table storing platform data\r
0d2711a6 229class PlatformTable(MetaFileTable):\r
30fdf114
LG
230 _COLUMN_ = '''\r
231 ID REAL PRIMARY KEY,\r
232 Model INTEGER NOT NULL,\r
233 Value1 TEXT NOT NULL,\r
234 Value2 TEXT,\r
235 Value3 TEXT,\r
236 Scope1 TEXT,\r
237 Scope2 TEXT,\r
238 BelongsToItem REAL NOT NULL,\r
239 FromItem REAL NOT NULL,\r
240 StartLine INTEGER NOT NULL,\r
241 StartColumn INTEGER NOT NULL,\r
242 EndLine INTEGER NOT NULL,\r
243 EndColumn INTEGER NOT NULL,\r
244 Enabled INTEGER DEFAULT 0\r
245 '''\r
246 # used as table end flag, in case the changes to database is not committed to db file\r
247 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1, -1"\r
248\r
249 ## Constructor\r
0d2711a6
LG
250 def __init__(self, Cursor, MetaFile, Temporary):\r
251 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_DSC, Temporary)\r
30fdf114
LG
252\r
253 ## Insert table\r
254 #\r
255 # Insert a record into table Dsc\r
256 #\r
257 # @param Model: Model of a Dsc item\r
258 # @param Value1: Value1 of a Dsc item\r
259 # @param Value2: Value2 of a Dsc item\r
260 # @param Value3: Value3 of a Dsc item\r
261 # @param Scope1: Arch of a Dsc item\r
262 # @param Scope2: Module type of a Dsc item\r
263 # @param BelongsToItem: The item belongs to which another item\r
264 # @param FromItem: The item belongs to which dsc file\r
265 # @param StartLine: StartLine of a Dsc item\r
266 # @param StartColumn: StartColumn of a Dsc item\r
267 # @param EndLine: EndLine of a Dsc item\r
268 # @param EndColumn: EndColumn of a Dsc item\r
269 # @param Enabled: If this item enabled\r
270 #\r
271 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON', BelongsToItem=-1, \r
272 FromItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=1):\r
273 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))\r
274 return Table.Insert(\r
275 self, \r
276 Model, \r
277 Value1, \r
278 Value2, \r
279 Value3, \r
280 Scope1, \r
281 Scope2,\r
282 BelongsToItem, \r
283 FromItem,\r
284 StartLine, \r
285 StartColumn, \r
286 EndLine, \r
287 EndColumn, \r
288 Enabled\r
289 )\r
290\r
291 ## Query table\r
292 #\r
293 # @param Model: The Model of Record \r
294 # @param Scope1: Arch of a Dsc item\r
295 # @param Scope2: Module type of a Dsc item\r
296 # @param BelongsToItem: The item belongs to which another item\r
297 # @param FromItem: The item belongs to which dsc file\r
298 #\r
299 # @retval: A recordSet of all found records \r
300 #\r
301 def Query(self, Model, Scope1=None, Scope2=None, BelongsToItem=None, FromItem=None):\r
0d2711a6 302 ConditionString = "Model=%s AND Enabled>0" % Model\r
30fdf114
LG
303 ValueString = "Value1,Value2,Value3,Scope1,Scope2,ID,StartLine"\r
304\r
305 if Scope1 != None and Scope1 != 'COMMON':\r
306 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Scope1\r
307 if Scope2 != None and Scope2 != 'COMMON':\r
308 ConditionString += " AND (Scope2='%s' OR Scope2='COMMON' OR Scope2='DEFAULT')" % Scope2\r
309\r
310 if BelongsToItem != None:\r
311 ConditionString += " AND BelongsToItem=%s" % BelongsToItem\r
312 else:\r
313 ConditionString += " AND BelongsToItem<0"\r
314\r
315 if FromItem != None:\r
316 ConditionString += " AND FromItem=%s" % FromItem\r
317\r
318 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)\r
319 return self.Exec(SqlCommand)\r
320\r
0d2711a6
LG
321## Factory class to produce different storage for different type of meta-file\r
322class MetaFileStorage(object):\r
323 _FILE_TABLE_ = {\r
324 MODEL_FILE_INF : ModuleTable,\r
325 MODEL_FILE_DEC : PackageTable,\r
326 MODEL_FILE_DSC : PlatformTable,\r
327 MODEL_FILE_OTHERS : MetaFileTable,\r
328 }\r
329\r
330 _FILE_TYPE_ = {\r
331 ".inf" : MODEL_FILE_INF,\r
332 ".dec" : MODEL_FILE_DEC,\r
333 ".dsc" : MODEL_FILE_DSC,\r
334 }\r
335\r
336 ## Constructor\r
337 def __new__(Class, Cursor, MetaFile, FileType=None, Temporary=False):\r
338 # no type given, try to find one\r
339 if not FileType:\r
340 if MetaFile.Type in self._FILE_TYPE_:\r
341 FileType = Class._FILE_TYPE_[MetaFile.Type]\r
342 else:\r
343 FileType = MODEL_FILE_OTHERS\r
344\r
345 # don't pass the type around if it's well known\r
346 if FileType == MODEL_FILE_OTHERS:\r
347 Args = (Cursor, MetaFile, FileType, Temporary)\r
348 else:\r
349 Args = (Cursor, MetaFile, Temporary)\r
350\r
351 # create the storage object and return it to caller\r
352 return Class._FILE_TABLE_[FileType](*Args)\r
353\r