]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/MetaFileTable.py
Sync BaseTools Trunk (version r2518) 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 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):
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
146 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)
147 return self.Exec(SqlCommand)
148
149 ## Python class representation of table storing package data
150 class PackageTable(MetaFileTable):
151 _COLUMN_ = '''
152 ID REAL PRIMARY KEY,
153 Model INTEGER NOT NULL,
154 Value1 TEXT NOT NULL,
155 Value2 TEXT,
156 Value3 TEXT,
157 Scope1 TEXT,
158 Scope2 TEXT,
159 BelongsToItem REAL NOT NULL,
160 StartLine INTEGER NOT NULL,
161 StartColumn INTEGER NOT NULL,
162 EndLine INTEGER NOT NULL,
163 EndColumn INTEGER NOT NULL,
164 Enabled INTEGER DEFAULT 0
165 '''
166 # used as table end flag, in case the changes to database is not committed to db file
167 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1"
168
169 ## Constructor
170 def __init__(self, Cursor, MetaFile, Temporary):
171 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_DEC, Temporary)
172
173 ## Insert table
174 #
175 # Insert a record into table Dec
176 #
177 # @param Model: Model of a Dec item
178 # @param Value1: Value1 of a Dec item
179 # @param Value2: Value2 of a Dec item
180 # @param Value3: Value3 of a Dec item
181 # @param Scope1: Arch of a Dec item
182 # @param Scope2: Module type of a Dec item
183 # @param BelongsToItem: The item belongs to which another item
184 # @param StartLine: StartLine of a Dec item
185 # @param StartColumn: StartColumn of a Dec item
186 # @param EndLine: EndLine of a Dec item
187 # @param EndColumn: EndColumn of a Dec item
188 # @param Enabled: If this item enabled
189 #
190 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON',
191 BelongsToItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=0):
192 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))
193 return Table.Insert(
194 self,
195 Model,
196 Value1,
197 Value2,
198 Value3,
199 Scope1,
200 Scope2,
201 BelongsToItem,
202 StartLine,
203 StartColumn,
204 EndLine,
205 EndColumn,
206 Enabled
207 )
208
209 ## Query table
210 #
211 # @param Model: The Model of Record
212 # @param Arch: The Arch attribute of Record
213 #
214 # @retval: A recordSet of all found records
215 #
216 def Query(self, Model, Arch=None):
217 ConditionString = "Model=%s AND Enabled>=0" % Model
218 ValueString = "Value1,Value2,Value3,Scope1,ID,StartLine"
219
220 if Arch != None and Arch != 'COMMON':
221 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Arch
222
223 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)
224 return self.Exec(SqlCommand)
225
226 ## Python class representation of table storing platform data
227 class PlatformTable(MetaFileTable):
228 _COLUMN_ = '''
229 ID REAL PRIMARY KEY,
230 Model INTEGER NOT NULL,
231 Value1 TEXT NOT NULL,
232 Value2 TEXT,
233 Value3 TEXT,
234 Scope1 TEXT,
235 Scope2 TEXT,
236 BelongsToItem REAL NOT NULL,
237 FromItem REAL NOT NULL,
238 StartLine INTEGER NOT NULL,
239 StartColumn INTEGER NOT NULL,
240 EndLine INTEGER NOT NULL,
241 EndColumn INTEGER NOT NULL,
242 Enabled INTEGER DEFAULT 0
243 '''
244 # used as table end flag, in case the changes to database is not committed to db file
245 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1, -1"
246
247 ## Constructor
248 def __init__(self, Cursor, MetaFile, Temporary):
249 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_DSC, Temporary)
250
251 ## Insert table
252 #
253 # Insert a record into table Dsc
254 #
255 # @param Model: Model of a Dsc item
256 # @param Value1: Value1 of a Dsc item
257 # @param Value2: Value2 of a Dsc item
258 # @param Value3: Value3 of a Dsc item
259 # @param Scope1: Arch of a Dsc item
260 # @param Scope2: Module type of a Dsc item
261 # @param BelongsToItem: The item belongs to which another item
262 # @param FromItem: The item belongs to which dsc file
263 # @param StartLine: StartLine of a Dsc item
264 # @param StartColumn: StartColumn of a Dsc item
265 # @param EndLine: EndLine of a Dsc item
266 # @param EndColumn: EndColumn of a Dsc item
267 # @param Enabled: If this item enabled
268 #
269 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON', BelongsToItem=-1,
270 FromItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=1):
271 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))
272 return Table.Insert(
273 self,
274 Model,
275 Value1,
276 Value2,
277 Value3,
278 Scope1,
279 Scope2,
280 BelongsToItem,
281 FromItem,
282 StartLine,
283 StartColumn,
284 EndLine,
285 EndColumn,
286 Enabled
287 )
288
289 ## Query table
290 #
291 # @param Model: The Model of Record
292 # @param Scope1: Arch of a Dsc item
293 # @param Scope2: Module type of a Dsc item
294 # @param BelongsToItem: The item belongs to which another item
295 # @param FromItem: The item belongs to which dsc file
296 #
297 # @retval: A recordSet of all found records
298 #
299 def Query(self, Model, Scope1=None, Scope2=None, BelongsToItem=None, FromItem=None):
300 ConditionString = "Model=%s AND Enabled>0" % Model
301 ValueString = "Value1,Value2,Value3,Scope1,Scope2,ID,StartLine"
302
303 if Scope1 != None and Scope1 != 'COMMON':
304 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Scope1
305 if Scope2 != None and Scope2 != 'COMMON':
306 ConditionString += " AND (Scope2='%s' OR Scope2='COMMON' OR Scope2='DEFAULT')" % Scope2
307
308 if BelongsToItem != None:
309 ConditionString += " AND BelongsToItem=%s" % BelongsToItem
310 else:
311 ConditionString += " AND BelongsToItem<0"
312
313 if FromItem != None:
314 ConditionString += " AND FromItem=%s" % FromItem
315
316 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)
317 return self.Exec(SqlCommand)
318
319 ## Factory class to produce different storage for different type of meta-file
320 class MetaFileStorage(object):
321 _FILE_TABLE_ = {
322 MODEL_FILE_INF : ModuleTable,
323 MODEL_FILE_DEC : PackageTable,
324 MODEL_FILE_DSC : PlatformTable,
325 MODEL_FILE_OTHERS : MetaFileTable,
326 }
327
328 _FILE_TYPE_ = {
329 ".inf" : MODEL_FILE_INF,
330 ".dec" : MODEL_FILE_DEC,
331 ".dsc" : MODEL_FILE_DSC,
332 }
333
334 ## Constructor
335 def __new__(Class, Cursor, MetaFile, FileType=None, Temporary=False):
336 # no type given, try to find one
337 if not FileType:
338 if MetaFile.Type in self._FILE_TYPE_:
339 FileType = Class._FILE_TYPE_[MetaFile.Type]
340 else:
341 FileType = MODEL_FILE_OTHERS
342
343 # don't pass the type around if it's well known
344 if FileType == MODEL_FILE_OTHERS:
345 Args = (Cursor, MetaFile, FileType, Temporary)
346 else:
347 Args = (Cursor, MetaFile, Temporary)
348
349 # create the storage object and return it to caller
350 return Class._FILE_TABLE_[FileType](*Args)
351