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