2 # This file is used to create/update/query/erase a meta file table
4 # Copyright (c) 2008 - 2018, 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
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.
17 from __future__
import absolute_import
20 import Common
.EdkLogger
as EdkLogger
21 from Common
.BuildToolError
import FORMAT_INVALID
23 from CommonDataClass
.DataClass
import MODEL_FILE_DSC
, MODEL_FILE_DEC
, MODEL_FILE_INF
, \
25 from Common
.DataType
import *
27 class MetaFileTable():
28 # TRICK: use file ID as the part before '.'
33 def __init__(self
, DB
, MetaFile
, FileType
, Temporary
, FromItem
=None):
34 self
.MetaFile
= MetaFile
39 self
.CurrentContent
= []
40 DB
.TblFile
.append([MetaFile
.Name
,
47 self
.FileId
= len(DB
.TblFile
)
48 self
.ID
= self
.FileId
* 10**8
50 self
.TableName
= "_%s_%s_%s" % (FileType
, len(DB
.TblFile
), uuid
.uuid4().hex)
52 self
.TableName
= "_%s_%s" % (FileType
, len(DB
.TblFile
))
54 def IsIntegrity(self
):
56 TimeStamp
= self
.MetaFile
.TimeStamp
57 if not self
.CurrentContent
:
60 Result
= self
.CurrentContent
[-1][0] < 0
62 # update the timestamp in database
63 self
.DB
.SetFileTimeStamp(self
.FileId
, TimeStamp
)
66 if TimeStamp
!= self
.DB
.GetFileTimeStamp(self
.FileId
):
67 # update the timestamp in database
68 self
.DB
.SetFileTimeStamp(self
.FileId
, TimeStamp
)
70 except Exception as Exc
:
71 EdkLogger
.debug(EdkLogger
.DEBUG_5
, str(Exc
))
76 self
.CurrentContent
.append(self
._DUMMY
_)
79 return [item
for item
in self
.CurrentContent
if item
[0] >= 0 and item
[-1]>=0]
81 ## Python class representation of table storing module data
82 class ModuleTable(MetaFileTable
):
85 Model INTEGER NOT NULL,
91 BelongsToItem REAL NOT NULL,
92 StartLine INTEGER NOT NULL,
93 StartColumn INTEGER NOT NULL,
94 EndLine INTEGER NOT NULL,
95 EndColumn INTEGER NOT NULL,
96 Enabled INTEGER DEFAULT 0
98 # used as table end flag, in case the changes to database is not committed to db file
99 _DUMMY_
= [-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1]
102 def __init__(self
, Db
, MetaFile
, Temporary
):
103 MetaFileTable
.__init
__(self
, Db
, MetaFile
, MODEL_FILE_INF
, Temporary
)
105 ## Insert a record into table Inf
107 # @param Model: Model of a Inf item
108 # @param Value1: Value1 of a Inf item
109 # @param Value2: Value2 of a Inf item
110 # @param Value3: Value3 of a Inf item
111 # @param Scope1: Arch of a Inf item
112 # @param Scope2 Platform os a Inf item
113 # @param BelongsToItem: The item belongs to which another item
114 # @param StartLine: StartLine of a Inf item
115 # @param StartColumn: StartColumn of a Inf item
116 # @param EndLine: EndLine of a Inf item
117 # @param EndColumn: EndColumn of a Inf item
118 # @param Enabled: If this item enabled
120 def Insert(self
, Model
, Value1
, Value2
, Value3
, Scope1
=TAB_ARCH_COMMON
, Scope2
=TAB_COMMON
,
121 BelongsToItem
=-1, StartLine
=-1, StartColumn
=-1, EndLine
=-1, EndColumn
=-1, Enabled
=0):
123 (Value1
, Value2
, Value3
, Scope1
, Scope2
) = (Value1
.strip(), Value2
.strip(), Value3
.strip(), Scope1
.strip(), Scope2
.strip())
124 self
.ID
= self
.ID
+ self
._ID
_STEP
_
125 if self
.ID
>= (MODEL_FILE_INF
+ self
._ID
_MAX
_):
126 self
.ID
= MODEL_FILE_INF
+ self
._ID
_STEP
_
142 self
.CurrentContent
.append(row
)
147 # @param Model: The Model of Record
148 # @param Arch: The Arch attribute of Record
149 # @param Platform The Platform attribute of Record
151 # @retval: A recordSet of all found records
153 def Query(self
, Model
, Arch
=None, Platform
=None, BelongsToItem
=None):
155 QueryTab
= self
.CurrentContent
156 result
= [item
for item
in QueryTab
if item
[1] == Model
and item
[-1]>=0 ]
158 if Arch
is not None and Arch
!= TAB_ARCH_COMMON
:
159 ArchList
= set(['COMMON'])
161 result
= [item
for item
in result
if item
[5] in ArchList
]
163 if Platform
is not None and Platform
!= TAB_COMMON
:
164 Platformlist
= set( ['COMMON','DEFAULT'])
165 Platformlist
.add(Platform
)
166 result
= [item
for item
in result
if item
[6] in Platformlist
]
168 if BelongsToItem
is not None:
169 result
= [item
for item
in result
if item
[7] == BelongsToItem
]
171 result
= [ [r
[2],r
[3],r
[4],r
[5],r
[6],r
[0],r
[9]] for r
in result
]
174 ## Python class representation of table storing package data
175 class PackageTable(MetaFileTable
):
178 Model INTEGER NOT NULL,
179 Value1 TEXT NOT NULL,
184 BelongsToItem REAL NOT NULL,
185 StartLine INTEGER NOT NULL,
186 StartColumn INTEGER NOT NULL,
187 EndLine INTEGER NOT NULL,
188 EndColumn INTEGER NOT NULL,
189 Enabled INTEGER DEFAULT 0
191 # used as table end flag, in case the changes to database is not committed to db file
192 _DUMMY_
= [-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1]
195 def __init__(self
, Cursor
, MetaFile
, Temporary
):
196 MetaFileTable
.__init
__(self
, Cursor
, MetaFile
, MODEL_FILE_DEC
, Temporary
)
200 # Insert a record into table Dec
202 # @param Model: Model of a Dec item
203 # @param Value1: Value1 of a Dec item
204 # @param Value2: Value2 of a Dec item
205 # @param Value3: Value3 of a Dec item
206 # @param Scope1: Arch of a Dec item
207 # @param Scope2: Module type of a Dec item
208 # @param BelongsToItem: The item belongs to which another item
209 # @param StartLine: StartLine of a Dec item
210 # @param StartColumn: StartColumn of a Dec item
211 # @param EndLine: EndLine of a Dec item
212 # @param EndColumn: EndColumn of a Dec item
213 # @param Enabled: If this item enabled
215 def Insert(self
, Model
, Value1
, Value2
, Value3
, Scope1
=TAB_ARCH_COMMON
, Scope2
=TAB_COMMON
,
216 BelongsToItem
=-1, StartLine
=-1, StartColumn
=-1, EndLine
=-1, EndColumn
=-1, Enabled
=0):
217 (Value1
, Value2
, Value3
, Scope1
, Scope2
) = (Value1
.strip(), Value2
.strip(), Value3
.strip(), Scope1
.strip(), Scope2
.strip())
218 self
.ID
= self
.ID
+ self
._ID
_STEP
_
234 self
.CurrentContent
.append(row
)
239 # @param Model: The Model of Record
240 # @param Arch: The Arch attribute of Record
242 # @retval: A recordSet of all found records
244 def Query(self
, Model
, Arch
=None):
246 QueryTab
= self
.CurrentContent
247 result
= [item
for item
in QueryTab
if item
[1] == Model
and item
[-1]>=0 ]
249 if Arch
is not None and Arch
!= TAB_ARCH_COMMON
:
250 ArchList
= set(['COMMON'])
252 result
= [item
for item
in result
if item
[5] in ArchList
]
254 return [[r
[2], r
[3], r
[4], r
[5], r
[6], r
[0], r
[8]] for r
in result
]
256 def GetValidExpression(self
, TokenSpaceGuid
, PcdCName
):
258 QueryTab
= self
.CurrentContent
259 result
= [[item
[2], item
[8]] for item
in QueryTab
if item
[3] == TokenSpaceGuid
and item
[4] == PcdCName
]
268 comment
= comment
.strip("#")
269 comment
= comment
.strip()
271 if comment
.startswith("@ValidRange"):
272 comment
= comment
.replace("@ValidRange", "", 1)
273 validateranges
.append(comment
.split("|")[1].strip())
274 if comment
.startswith("@ValidList"):
275 comment
= comment
.replace("@ValidList", "", 1)
276 validlists
.append(comment
.split("|")[1].strip())
277 if comment
.startswith("@Expression"):
278 comment
= comment
.replace("@Expression", "", 1)
279 expressions
.append(comment
.split("|")[1].strip())
280 except Exception as Exc
:
282 if oricomment
.startswith("@ValidRange"):
283 ValidType
= "@ValidRange"
284 if oricomment
.startswith("@ValidList"):
285 ValidType
= "@ValidList"
286 if oricomment
.startswith("@Expression"):
287 ValidType
= "@Expression"
288 EdkLogger
.error('Parser', FORMAT_INVALID
, "The syntax for %s of PCD %s.%s is incorrect" % (ValidType
, TokenSpaceGuid
, PcdCName
),
289 ExtraData
=oricomment
, File
=self
.MetaFile
, Line
=LineNum
)
290 return set(), set(), set()
291 return set(validateranges
), set(validlists
), set(expressions
)
293 ## Python class representation of table storing platform data
294 class PlatformTable(MetaFileTable
):
297 Model INTEGER NOT NULL,
298 Value1 TEXT NOT NULL,
304 BelongsToItem REAL NOT NULL,
305 FromItem REAL NOT NULL,
306 StartLine INTEGER NOT NULL,
307 StartColumn INTEGER NOT NULL,
308 EndLine INTEGER NOT NULL,
309 EndColumn INTEGER NOT NULL,
310 Enabled INTEGER DEFAULT 0
312 # used as table end flag, in case the changes to database is not committed to db file
313 _DUMMY_
= [-1, -1, '====', '====', '====', '====', '====','====', -1, -1, -1, -1, -1, -1, -1]
316 def __init__(self
, Cursor
, MetaFile
, Temporary
, FromItem
=0):
317 MetaFileTable
.__init
__(self
, Cursor
, MetaFile
, MODEL_FILE_DSC
, Temporary
, FromItem
)
321 # Insert a record into table Dsc
323 # @param Model: Model of a Dsc item
324 # @param Value1: Value1 of a Dsc item
325 # @param Value2: Value2 of a Dsc item
326 # @param Value3: Value3 of a Dsc item
327 # @param Scope1: Arch of a Dsc item
328 # @param Scope2: Module type of a Dsc item
329 # @param BelongsToItem: The item belongs to which another item
330 # @param FromItem: The item belongs to which dsc file
331 # @param StartLine: StartLine of a Dsc item
332 # @param StartColumn: StartColumn of a Dsc item
333 # @param EndLine: EndLine of a Dsc item
334 # @param EndColumn: EndColumn of a Dsc item
335 # @param Enabled: If this item enabled
337 def Insert(self
, Model
, Value1
, Value2
, Value3
, Scope1
=TAB_ARCH_COMMON
, Scope2
=TAB_COMMON
, Scope3
=TAB_DEFAULT_STORES_DEFAULT
,BelongsToItem
=-1,
338 FromItem
=-1, StartLine
=-1, StartColumn
=-1, EndLine
=-1, EndColumn
=-1, Enabled
=1):
339 (Value1
, Value2
, Value3
, Scope1
, Scope2
, Scope3
) = (Value1
.strip(), Value2
.strip(), Value3
.strip(), Scope1
.strip(), Scope2
.strip(), Scope3
.strip())
340 self
.ID
= self
.ID
+ self
._ID
_STEP
_
358 self
.CurrentContent
.append(row
)
364 # @param Model: The Model of Record
365 # @param Scope1: Arch of a Dsc item
366 # @param Scope2: Module type of a Dsc item
367 # @param BelongsToItem: The item belongs to which another item
368 # @param FromItem: The item belongs to which dsc file
370 # @retval: A recordSet of all found records
372 def Query(self
, Model
, Scope1
=None, Scope2
=None, BelongsToItem
=None, FromItem
=None):
374 QueryTab
= self
.CurrentContent
375 result
= [item
for item
in QueryTab
if item
[1] == Model
and item
[-1]>0 ]
376 if Scope1
is not None and Scope1
!= TAB_ARCH_COMMON
:
377 Sc1
= set(['COMMON'])
379 result
= [item
for item
in result
if item
[5] in Sc1
]
380 Sc2
= set( ['COMMON','DEFAULT'])
381 if Scope2
and Scope2
!= TAB_COMMON
:
383 Index
= Scope2
.index('.')
384 NewScope
= TAB_COMMON
+ Scope2
[Index
:]
387 result
= [item
for item
in result
if item
[6] in Sc2
]
389 if BelongsToItem
is not None:
390 result
= [item
for item
in result
if item
[8] == BelongsToItem
]
392 result
= [item
for item
in result
if item
[8] < 0]
393 if FromItem
is not None:
394 result
= [item
for item
in result
if item
[9] == FromItem
]
396 result
= [ [r
[2],r
[3],r
[4],r
[5],r
[6],r
[7],r
[0],r
[9]] for r
in result
]
399 def DisableComponent(self
,comp_id
):
400 for item
in self
.CurrentContent
:
401 if item
[0] == comp_id
or item
[8] == comp_id
:
404 ## Factory class to produce different storage for different type of meta-file
405 class MetaFileStorage(object):
407 MODEL_FILE_INF
: ModuleTable
,
408 MODEL_FILE_DEC
: PackageTable
,
409 MODEL_FILE_DSC
: PlatformTable
,
410 MODEL_FILE_OTHERS
: MetaFileTable
,
414 ".inf" : MODEL_FILE_INF
,
415 ".dec" : MODEL_FILE_DEC
,
416 ".dsc" : MODEL_FILE_DSC
,
420 def __new__(Class
, Cursor
, MetaFile
, FileType
=None, Temporary
=False, FromItem
=None):
421 # no type given, try to find one
422 key
= (MetaFile
.Path
, FileType
,Temporary
,FromItem
)
423 if key
in Class
._ObjectCache
:
424 return Class
._ObjectCache
[key
]
426 if MetaFile
.Type
in self
._FILE
_TYPE
_:
427 FileType
= Class
._FILE
_TYPE
_[MetaFile
.Type
]
429 FileType
= MODEL_FILE_OTHERS
431 # don't pass the type around if it's well known
432 if FileType
== MODEL_FILE_OTHERS
:
433 Args
= (Cursor
, MetaFile
, FileType
, Temporary
)
435 Args
= (Cursor
, MetaFile
, Temporary
)
437 Args
= Args
+ (FromItem
,)
439 # create the storage object and return it to caller
440 reval
= Class
._FILE
_TABLE
_[FileType
](*Args
)
442 Class
._ObjectCache
[key
] = reval