2 # This file is used to define common parser functions for meta-data
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
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.
15 from CommonDataClass
.DataClass
import *
16 from EccToolError
import *
19 ## Get the inlcude path list for a source file
21 # 1. Find the source file belongs to which inf file
22 # 2. Find the inf's package
23 # 3. Return the include path list of the package
25 def GetIncludeListOfFile(WorkSpace
, Filepath
, Db
):
27 Filepath
= os
.path
.normpath(Filepath
)
29 select Value1, FullPath from Inf, File where Inf.Model = %s and Inf.BelongsToFile in(
30 select distinct B.BelongsToFile from File as A left join Inf as B
31 where A.ID = B.BelongsToFile and B.Model = %s and (A.Path || '%s' || B.Value1) = '%s')
32 and Inf.BelongsToFile = File.ID""" \
33 % (MODEL_META_DATA_PACKAGE
, MODEL_EFI_SOURCE_FILE
, '\\', Filepath
)
34 RecordSet
= Db
.TblFile
.Exec(SqlCommand
)
35 for Record
in RecordSet
:
36 DecFullPath
= os
.path
.normpath(os
.path
.join(WorkSpace
, Record
[0]))
37 InfFullPath
= os
.path
.normpath(os
.path
.join(WorkSpace
, Record
[1]))
38 (DecPath
, DecName
) = os
.path
.split(DecFullPath
)
39 (InfPath
, InfName
) = os
.path
.split(InfFullPath
)
40 SqlCommand
= """select Value1 from Dec where BelongsToFile =
41 (select ID from File where FullPath = '%s') and Model = %s""" \
42 % (DecFullPath
, MODEL_EFI_INCLUDE
)
43 NewRecordSet
= Db
.TblDec
.Exec(SqlCommand
)
44 if InfPath
not in IncludeList
:
45 IncludeList
.append(InfPath
)
46 for NewRecord
in NewRecordSet
:
47 IncludePath
= os
.path
.normpath(os
.path
.join(DecPath
, NewRecord
[0]))
48 if IncludePath
not in IncludeList
:
49 IncludeList
.append(IncludePath
)
55 # Search table file and find all specific type files
57 def GetFileList(FileModel
, Db
):
59 SqlCommand
= """select FullPath from File where Model = %s""" % str(FileModel
)
60 RecordSet
= Db
.TblFile
.Exec(SqlCommand
)
61 for Record
in RecordSet
:
62 FileList
.append(Record
[0])
68 # Search table file and find all small tables
70 def GetTableList(FileModelList
, Table
, Db
):
72 SqlCommand
= """select ID from File where Model in %s""" % str(FileModelList
)
73 RecordSet
= Db
.TblFile
.Exec(SqlCommand
)
74 for Record
in RecordSet
:
75 TableName
= Table
+ str(Record
[0])
76 TableList
.append(TableName
)
80 ## ParseHeaderCommentSection
82 # Parse Header comment section lines, extract Abstract, Description, Copyright
85 # @param CommentList: List of (Comment, LineNumber)
86 # @param FileName: FileName of the comment
88 def ParseHeaderCommentSection(CommentList
, FileName
= None):
95 STR_HEADER_COMMENT_START
= "@file"
98 # used to indicate the state of processing header comment section of dec,
101 HEADER_COMMENT_NOT_STARTED
= -1
102 HEADER_COMMENT_STARTED
= 0
103 HEADER_COMMENT_FILE
= 1
104 HEADER_COMMENT_ABSTRACT
= 2
105 HEADER_COMMENT_DESCRIPTION
= 3
106 HEADER_COMMENT_COPYRIGHT
= 4
107 HEADER_COMMENT_LICENSE
= 5
108 HEADER_COMMENT_END
= 6
110 # first find the last copyright line
113 HeaderCommentStage
= HEADER_COMMENT_NOT_STARTED
114 for Index
in xrange(len(CommentList
)-1, 0, -1):
115 Line
= CommentList
[Index
][0]
116 if _IsCopyrightLine(Line
):
120 for Item
in CommentList
:
124 if not Line
.startswith('#') and Line
:
125 SqlStatement
= """ select ID from File where FullPath like '%s'""" % FileName
126 ResultSet
= EccGlobalData
.gDb
.TblFile
.Exec(SqlStatement
)
127 for Result
in ResultSet
:
128 Msg
= 'Comment must start with #'
129 EccGlobalData
.gDb
.TblReport
.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER
, Msg
, "File", Result
[0])
130 Comment
= CleanString2(Line
)[1]
131 Comment
= Comment
.strip()
133 # if there are blank lines between License or Description, keep them as they would be
134 # indication of different block; or in the position that Abstract should be, also keep it
135 # as it indicates that no abstract
137 if not Comment
and HeaderCommentStage
not in [HEADER_COMMENT_LICENSE
, \
138 HEADER_COMMENT_DESCRIPTION
, HEADER_COMMENT_ABSTRACT
]:
141 if HeaderCommentStage
== HEADER_COMMENT_NOT_STARTED
:
142 if Comment
.startswith(STR_HEADER_COMMENT_START
):
143 HeaderCommentStage
= HEADER_COMMENT_ABSTRACT
145 License
+= Comment
+ EndOfLine
147 if HeaderCommentStage
== HEADER_COMMENT_ABSTRACT
:
149 # in case there is no abstract and description
153 HeaderCommentStage
= HEADER_COMMENT_DESCRIPTION
154 elif _IsCopyrightLine(Comment
):
155 Copyright
+= Comment
+ EndOfLine
156 HeaderCommentStage
= HEADER_COMMENT_COPYRIGHT
158 Abstract
+= Comment
+ EndOfLine
159 HeaderCommentStage
= HEADER_COMMENT_DESCRIPTION
160 elif HeaderCommentStage
== HEADER_COMMENT_DESCRIPTION
:
162 # in case there is no description
164 if _IsCopyrightLine(Comment
):
165 Copyright
+= Comment
+ EndOfLine
166 HeaderCommentStage
= HEADER_COMMENT_COPYRIGHT
168 Description
+= Comment
+ EndOfLine
169 elif HeaderCommentStage
== HEADER_COMMENT_COPYRIGHT
:
170 if _IsCopyrightLine(Comment
):
171 Copyright
+= Comment
+ EndOfLine
174 # Contents after copyright line are license, those non-copyright lines in between
175 # copyright line will be discarded
180 License
+= Comment
+ EndOfLine
181 HeaderCommentStage
= HEADER_COMMENT_LICENSE
183 if not Comment
and not License
:
185 License
+= Comment
+ EndOfLine
188 SqlStatement
= """ select ID from File where FullPath like '%s'""" % FileName
189 ResultSet
= EccGlobalData
.gDb
.TblFile
.Exec(SqlStatement
)
190 for Result
in ResultSet
:
191 Msg
= 'Header comment section must have copyright information'
192 EccGlobalData
.gDb
.TblReport
.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER
, Msg
, "File", Result
[0])
195 SqlStatement
= """ select ID from File where FullPath like '%s'""" % FileName
196 ResultSet
= EccGlobalData
.gDb
.TblFile
.Exec(SqlStatement
)
197 for Result
in ResultSet
:
198 Msg
= 'Header comment section must have license information'
199 EccGlobalData
.gDb
.TblReport
.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER
, Msg
, "File", Result
[0])
201 return Abstract
.strip(), Description
.strip(), Copyright
.strip(), License
.strip()
204 # check whether current line is copyright line, the criteria is whether there is case insensitive keyword "Copyright"
205 # followed by zero or more white space characters followed by a "(" character
207 # @param LineContent: the line need to be checked
208 # @return: True if current line is copyright line, False else
210 def _IsCopyrightLine (LineContent
):
211 LineContent
= LineContent
.upper()
214 ReIsCopyrightRe
= re
.compile(r
"""(^|\s)COPYRIGHT *\(""", re
.DOTALL
)
215 if ReIsCopyrightRe
.search(LineContent
):
223 # Split comments in a string
226 # @param Line: The string to be cleaned
227 # @param CommentCharacter: Comment char, used to ignore comment content,
228 # default is DataType.TAB_COMMENT_SPLIT
230 def CleanString2(Line
, CommentCharacter
='#', AllowCppStyleComment
=False):
236 # Replace EDK1's comment character
238 if AllowCppStyleComment
:
239 Line
= Line
.replace('//', CommentCharacter
)
241 # separate comments and statements
243 LineParts
= Line
.split(CommentCharacter
, 1)
245 # remove whitespace again
247 Line
= LineParts
[0].strip()
248 if len(LineParts
) > 1:
249 Comment
= LineParts
[1].strip()
251 # Remove prefixed and trailing comment characters
255 while Start
< End
and Comment
.startswith(CommentCharacter
, Start
, End
):
257 while End
>= 0 and Comment
.endswith(CommentCharacter
, Start
, End
):
259 Comment
= Comment
[Start
:End
]
260 Comment
= Comment
.strip()