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