]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Ecc/MetaDataParser.py
BaseTools: Various typo
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / MetaDataParser.py
CommitLineData
30fdf114
LG
1## @file\r
2# This file is used to define common parser functions for meta-data\r
3#\r
f7496d71 4# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>\r
40d841f6 5# This program and the accompanying materials\r
30fdf114
LG
6# are licensed and made available under the terms and conditions of the BSD License\r
7# which accompanies this distribution. The full text of the license may be found at\r
8# http://opensource.org/licenses/bsd-license.php\r
9#\r
10# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12#\r
13\r
b6f6b636 14from __future__ import absolute_import\r
1be2ed90 15import Common.LongFilePathOs as os\r
30fdf114 16from CommonDataClass.DataClass import *\r
855698fb 17from Ecc.EccToolError import *\r
c4f52e12 18from Common.MultipleWorkspace import MultipleWorkspace as mws\r
855698fb 19from Ecc import EccGlobalData\r
d0acc87a 20import re\r
fb0b35e0 21## Get the include path list for a source file\r
30fdf114
LG
22#\r
23# 1. Find the source file belongs to which inf file\r
24# 2. Find the inf's package\r
25# 3. Return the include path list of the package\r
26#\r
27def GetIncludeListOfFile(WorkSpace, Filepath, Db):\r
28 IncludeList = []\r
29 Filepath = os.path.normpath(Filepath)\r
30 SqlCommand = """\r
31 select Value1, FullPath from Inf, File where Inf.Model = %s and Inf.BelongsToFile in(\r
e56468c0 32 select distinct B.BelongsToFile from File as A left join Inf as B\r
30fdf114
LG
33 where A.ID = B.BelongsToFile and B.Model = %s and (A.Path || '%s' || B.Value1) = '%s')\r
34 and Inf.BelongsToFile = File.ID""" \\r
35 % (MODEL_META_DATA_PACKAGE, MODEL_EFI_SOURCE_FILE, '\\', Filepath)\r
36 RecordSet = Db.TblFile.Exec(SqlCommand)\r
37 for Record in RecordSet:\r
c4f52e12
LY
38 DecFullPath = os.path.normpath(mws.join(WorkSpace, Record[0]))\r
39 InfFullPath = os.path.normpath(mws.join(WorkSpace, Record[1]))\r
30fdf114
LG
40 (DecPath, DecName) = os.path.split(DecFullPath)\r
41 (InfPath, InfName) = os.path.split(InfFullPath)\r
e56468c0 42 SqlCommand = """select Value1 from Dec where BelongsToFile =\r
30fdf114
LG
43 (select ID from File where FullPath = '%s') and Model = %s""" \\r
44 % (DecFullPath, MODEL_EFI_INCLUDE)\r
45 NewRecordSet = Db.TblDec.Exec(SqlCommand)\r
46 if InfPath not in IncludeList:\r
47 IncludeList.append(InfPath)\r
48 for NewRecord in NewRecordSet:\r
49 IncludePath = os.path.normpath(os.path.join(DecPath, NewRecord[0]))\r
50 if IncludePath not in IncludeList:\r
51 IncludeList.append(IncludePath)\r
e56468c0 52\r
30fdf114
LG
53 return IncludeList\r
54\r
e56468c0 55## Get the file list\r
56#\r
57# Search table file and find all specific type files\r
58#\r
59def GetFileList(FileModel, Db):\r
60 FileList = []\r
61 SqlCommand = """select FullPath from File where Model = %s""" % str(FileModel)\r
62 RecordSet = Db.TblFile.Exec(SqlCommand)\r
63 for Record in RecordSet:\r
64 FileList.append(Record[0])\r
65\r
66 return FileList\r
67\r
30fdf114
LG
68## Get the table list\r
69#\r
70# Search table file and find all small tables\r
71#\r
72def GetTableList(FileModelList, Table, Db):\r
73 TableList = []\r
74 SqlCommand = """select ID from File where Model in %s""" % str(FileModelList)\r
75 RecordSet = Db.TblFile.Exec(SqlCommand)\r
76 for Record in RecordSet:\r
77 TableName = Table + str(Record[0])\r
78 TableList.append(TableName)\r
e56468c0 79\r
30fdf114
LG
80 return TableList\r
81\r
d0acc87a
LG
82## ParseHeaderCommentSection\r
83#\r
84# Parse Header comment section lines, extract Abstract, Description, Copyright\r
85# , License lines\r
86#\r
87# @param CommentList: List of (Comment, LineNumber)\r
88# @param FileName: FileName of the comment\r
89#\r
90def ParseHeaderCommentSection(CommentList, FileName = None):\r
f7496d71 91\r
d0acc87a
LG
92 Abstract = ''\r
93 Description = ''\r
94 Copyright = ''\r
95 License = ''\r
96 EndOfLine = "\n"\r
97 STR_HEADER_COMMENT_START = "@file"\r
f7496d71 98\r
d0acc87a 99 #\r
f7496d71 100 # used to indicate the state of processing header comment section of dec,\r
d0acc87a
LG
101 # inf files\r
102 #\r
103 HEADER_COMMENT_NOT_STARTED = -1\r
104 HEADER_COMMENT_STARTED = 0\r
105 HEADER_COMMENT_FILE = 1\r
106 HEADER_COMMENT_ABSTRACT = 2\r
107 HEADER_COMMENT_DESCRIPTION = 3\r
108 HEADER_COMMENT_COPYRIGHT = 4\r
109 HEADER_COMMENT_LICENSE = 5\r
110 HEADER_COMMENT_END = 6\r
111 #\r
112 # first find the last copyright line\r
113 #\r
114 Last = 0\r
115 HeaderCommentStage = HEADER_COMMENT_NOT_STARTED\r
c60377d7 116 for Index in range(len(CommentList) - 1, 0, -1):\r
d0acc87a
LG
117 Line = CommentList[Index][0]\r
118 if _IsCopyrightLine(Line):\r
119 Last = Index\r
120 break\r
f7496d71 121\r
d0acc87a
LG
122 for Item in CommentList:\r
123 Line = Item[0]\r
124 LineNo = Item[1]\r
f7496d71 125\r
d0acc87a
LG
126 if not Line.startswith('#') and Line:\r
127 SqlStatement = """ select ID from File where FullPath like '%s'""" % FileName\r
128 ResultSet = EccGlobalData.gDb.TblFile.Exec(SqlStatement)\r
129 for Result in ResultSet:\r
130 Msg = 'Comment must start with #'\r
131 EccGlobalData.gDb.TblReport.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER, Msg, "File", Result[0])\r
132 Comment = CleanString2(Line)[1]\r
133 Comment = Comment.strip()\r
134 #\r
f7496d71 135 # if there are blank lines between License or Description, keep them as they would be\r
d0acc87a
LG
136 # indication of different block; or in the position that Abstract should be, also keep it\r
137 # as it indicates that no abstract\r
138 #\r
139 if not Comment and HeaderCommentStage not in [HEADER_COMMENT_LICENSE, \\r
140 HEADER_COMMENT_DESCRIPTION, HEADER_COMMENT_ABSTRACT]:\r
141 continue\r
f7496d71 142\r
d0acc87a
LG
143 if HeaderCommentStage == HEADER_COMMENT_NOT_STARTED:\r
144 if Comment.startswith(STR_HEADER_COMMENT_START):\r
145 HeaderCommentStage = HEADER_COMMENT_ABSTRACT\r
146 else:\r
147 License += Comment + EndOfLine\r
148 else:\r
149 if HeaderCommentStage == HEADER_COMMENT_ABSTRACT:\r
150 #\r
151 # in case there is no abstract and description\r
152 #\r
153 if not Comment:\r
154 Abstract = ''\r
155 HeaderCommentStage = HEADER_COMMENT_DESCRIPTION\r
f7496d71 156 elif _IsCopyrightLine(Comment):\r
d0acc87a
LG
157 Copyright += Comment + EndOfLine\r
158 HeaderCommentStage = HEADER_COMMENT_COPYRIGHT\r
f7496d71 159 else:\r
d0acc87a
LG
160 Abstract += Comment + EndOfLine\r
161 HeaderCommentStage = HEADER_COMMENT_DESCRIPTION\r
162 elif HeaderCommentStage == HEADER_COMMENT_DESCRIPTION:\r
163 #\r
164 # in case there is no description\r
f7496d71
LG
165 #\r
166 if _IsCopyrightLine(Comment):\r
d0acc87a
LG
167 Copyright += Comment + EndOfLine\r
168 HeaderCommentStage = HEADER_COMMENT_COPYRIGHT\r
169 else:\r
f7496d71 170 Description += Comment + EndOfLine\r
d0acc87a 171 elif HeaderCommentStage == HEADER_COMMENT_COPYRIGHT:\r
f7496d71 172 if _IsCopyrightLine(Comment):\r
d0acc87a
LG
173 Copyright += Comment + EndOfLine\r
174 else:\r
175 #\r
176 # Contents after copyright line are license, those non-copyright lines in between\r
f7496d71 177 # copyright line will be discarded\r
d0acc87a
LG
178 #\r
179 if LineNo > Last:\r
180 if License:\r
181 License += EndOfLine\r
182 License += Comment + EndOfLine\r
f7496d71 183 HeaderCommentStage = HEADER_COMMENT_LICENSE\r
d0acc87a
LG
184 else:\r
185 if not Comment and not License:\r
186 continue\r
187 License += Comment + EndOfLine\r
f7496d71 188\r
b3d07ff8 189 if not Copyright.strip():\r
d0acc87a
LG
190 SqlStatement = """ select ID from File where FullPath like '%s'""" % FileName\r
191 ResultSet = EccGlobalData.gDb.TblFile.Exec(SqlStatement)\r
192 for Result in ResultSet:\r
193 Msg = 'Header comment section must have copyright information'\r
194 EccGlobalData.gDb.TblReport.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER, Msg, "File", Result[0])\r
195\r
b3d07ff8 196 if not License.strip():\r
d0acc87a
LG
197 SqlStatement = """ select ID from File where FullPath like '%s'""" % FileName\r
198 ResultSet = EccGlobalData.gDb.TblFile.Exec(SqlStatement)\r
199 for Result in ResultSet:\r
200 Msg = 'Header comment section must have license information'\r
201 EccGlobalData.gDb.TblReport.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER, Msg, "File", Result[0])\r
f7496d71 202\r
b3d07ff8
HC
203 if not Abstract.strip() or Abstract.find('Component description file') > -1:\r
204 SqlStatement = """ select ID from File where FullPath like '%s'""" % FileName\r
205 ResultSet = EccGlobalData.gDb.TblFile.Exec(SqlStatement)\r
206 for Result in ResultSet:\r
207 Msg = 'Header comment section must have Abstract information.'\r
208 EccGlobalData.gDb.TblReport.Insert(ERROR_DOXYGEN_CHECK_FILE_HEADER, Msg, "File", Result[0])\r
f7496d71 209\r
d0acc87a
LG
210 return Abstract.strip(), Description.strip(), Copyright.strip(), License.strip()\r
211\r
212## _IsCopyrightLine\r
f7496d71
LG
213# check whether current line is copyright line, the criteria is whether there is case insensitive keyword "Copyright"\r
214# followed by zero or more white space characters followed by a "(" character\r
d0acc87a
LG
215#\r
216# @param LineContent: the line need to be checked\r
217# @return: True if current line is copyright line, False else\r
218#\r
219def _IsCopyrightLine (LineContent):\r
220 LineContent = LineContent.upper()\r
221 Result = False\r
f7496d71 222\r
d0acc87a
LG
223 ReIsCopyrightRe = re.compile(r"""(^|\s)COPYRIGHT *\(""", re.DOTALL)\r
224 if ReIsCopyrightRe.search(LineContent):\r
225 Result = True\r
f7496d71 226\r
d0acc87a
LG
227 return Result\r
228\r
229\r
230## CleanString2\r
231#\r
232# Split comments in a string\r
233# Remove spaces\r
234#\r
235# @param Line: The string to be cleaned\r
f7496d71 236# @param CommentCharacter: Comment char, used to ignore comment content,\r
d0acc87a
LG
237# default is DataType.TAB_COMMENT_SPLIT\r
238#\r
239def CleanString2(Line, CommentCharacter='#', AllowCppStyleComment=False):\r
240 #\r
241 # remove whitespace\r
242 #\r
243 Line = Line.strip()\r
244 #\r
245 # Replace EDK1's comment character\r
246 #\r
247 if AllowCppStyleComment:\r
248 Line = Line.replace('//', CommentCharacter)\r
249 #\r
250 # separate comments and statements\r
251 #\r
252 LineParts = Line.split(CommentCharacter, 1)\r
253 #\r
254 # remove whitespace again\r
255 #\r
256 Line = LineParts[0].strip()\r
257 if len(LineParts) > 1:\r
258 Comment = LineParts[1].strip()\r
259 #\r
260 # Remove prefixed and trailing comment characters\r
261 #\r
262 Start = 0\r
263 End = len(Comment)\r
264 while Start < End and Comment.startswith(CommentCharacter, Start, End):\r
265 Start += 1\r
266 while End >= 0 and Comment.endswith(CommentCharacter, Start, End):\r
267 End -= 1\r
268 Comment = Comment[Start:End]\r
269 Comment = Comment.strip()\r
270 else:\r
271 Comment = ''\r
272\r
273 return Line, Comment\r