#\r
# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
#\r
-# This program and the accompanying materials are licensed and made available \r
-# under the terms and conditions of the BSD License which accompanies this \r
-# distribution. The full text of the license may be found at \r
-# http://opensource.org/licenses/bsd-license.php\r
-#\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
#\r
\r
'''\r
else:\r
STR_HEADER_COMMENT_START = "@file"\r
HeaderCommentStage = HEADER_COMMENT_NOT_STARTED\r
- \r
+\r
#\r
# first find the last copyright line\r
#\r
Last = 0\r
- for Index in xrange(len(CommentList)-1, 0, -1):\r
+ for Index in range(len(CommentList)-1, 0, -1):\r
Line = CommentList[Index][0]\r
if _IsCopyrightLine(Line):\r
Last = Index\r
break\r
- \r
+\r
for Item in CommentList:\r
Line = Item[0]\r
LineNo = Item[1]\r
- \r
+\r
if not Line.startswith(TAB_COMMENT_SPLIT) and Line:\r
Logger.Error("\nUPT", FORMAT_INVALID, ST.ERR_INVALID_COMMENT_FORMAT, FileName, Item[1])\r
Comment = CleanString2(Line)[1]\r
Comment = Comment.strip()\r
#\r
- # if there are blank lines between License or Description, keep them as they would be \r
+ # if there are blank lines between License or Description, keep them as they would be\r
# indication of different block; or in the position that Abstract should be, also keep it\r
# as it indicates that no abstract\r
#\r
if not Comment and HeaderCommentStage not in [HEADER_COMMENT_LICENSE, \\r
HEADER_COMMENT_DESCRIPTION, HEADER_COMMENT_ABSTRACT]:\r
continue\r
- \r
+\r
if HeaderCommentStage == HEADER_COMMENT_NOT_STARTED:\r
if Comment.startswith(STR_HEADER_COMMENT_START):\r
HeaderCommentStage = HEADER_COMMENT_ABSTRACT\r
ValidateCopyright(Result, ST.WRN_INVALID_COPYRIGHT, FileName, LineNo, ErrMsg)\r
Copyright += Comment + EndOfLine\r
HeaderCommentStage = HEADER_COMMENT_COPYRIGHT\r
- else: \r
+ else:\r
Abstract += Comment + EndOfLine\r
HeaderCommentStage = HEADER_COMMENT_DESCRIPTION\r
elif HeaderCommentStage == HEADER_COMMENT_DESCRIPTION:\r
#\r
# in case there is no description\r
- # \r
+ #\r
if _IsCopyrightLine(Comment):\r
Result, ErrMsg = _ValidateCopyright(Comment)\r
ValidateCopyright(Result, ST.WRN_INVALID_COPYRIGHT, FileName, LineNo, ErrMsg)\r
Copyright += Comment + EndOfLine\r
HeaderCommentStage = HEADER_COMMENT_COPYRIGHT\r
else:\r
- Description += Comment + EndOfLine \r
+ Description += Comment + EndOfLine\r
elif HeaderCommentStage == HEADER_COMMENT_COPYRIGHT:\r
if _IsCopyrightLine(Comment):\r
Result, ErrMsg = _ValidateCopyright(Comment)\r
else:\r
#\r
# Contents after copyright line are license, those non-copyright lines in between\r
- # copyright line will be discarded \r
+ # copyright line will be discarded\r
#\r
if LineNo > Last:\r
if License:\r
License += EndOfLine\r
License += Comment + EndOfLine\r
- HeaderCommentStage = HEADER_COMMENT_LICENSE \r
+ HeaderCommentStage = HEADER_COMMENT_LICENSE\r
else:\r
if not Comment and not License:\r
continue\r
License += Comment + EndOfLine\r
- \r
+\r
return Abstract.strip(), Description.strip(), Copyright.strip(), License.strip()\r
\r
## _IsCopyrightLine\r
-# check whether current line is copyright line, the criteria is whether there is case insensitive keyword "Copyright" \r
-# followed by zero or more white space characters followed by a "(" character \r
+# check whether current line is copyright line, the criteria is whether there is case insensitive keyword "Copyright"\r
+# followed by zero or more white space characters followed by a "(" character\r
#\r
# @param LineContent: the line need to be checked\r
# @return: True if current line is copyright line, False else\r
def _IsCopyrightLine (LineContent):\r
LineContent = LineContent.upper()\r
Result = False\r
- \r
+\r
ReIsCopyrightRe = re.compile(r"""(^|\s)COPYRIGHT *\(""", re.DOTALL)\r
if ReIsCopyrightRe.search(LineContent):\r
Result = True\r
\r
## ParseGenericComment\r
#\r
-# @param GenericComment: Generic comment list, element of \r
+# @param GenericComment: Generic comment list, element of\r
# (CommentLine, LineNum)\r
# @param ContainerFile: Input value for filename of Dec file\r
-# \r
+#\r
def ParseGenericComment (GenericComment, ContainerFile=None, SkipTag=None):\r
if ContainerFile:\r
pass\r
- HelpTxt = None \r
- HelpStr = '' \r
- \r
+ HelpTxt = None\r
+ HelpStr = ''\r
+\r
for Item in GenericComment:\r
CommentLine = Item[0]\r
Comment = CleanString2(CommentLine)[1]\r
if SkipTag is not None and Comment.startswith(SkipTag):\r
Comment = Comment.replace(SkipTag, '', 1)\r
HelpStr += Comment + '\n'\r
- \r
+\r
if HelpStr:\r
HelpTxt = TextObject()\r
if HelpStr.endswith('\n') and not HelpStr.endswith('\n\n') and HelpStr != '\n':\r
\r
## ParsePcdErrorCode\r
#\r
-# @param Value: original ErrorCode value \r
+# @param Value: original ErrorCode value\r
# @param ContainerFile: Input value for filename of Dec file\r
-# @param LineNum: Line Num \r
-# \r
-def ParsePcdErrorCode (Value = None, ContainerFile = None, LineNum = None): \r
- try: \r
+# @param LineNum: Line Num\r
+#\r
+def ParsePcdErrorCode (Value = None, ContainerFile = None, LineNum = None):\r
+ try:\r
if Value.strip().startswith((TAB_HEX_START, TAB_CAPHEX_START)):\r
Base = 16\r
else:\r
Base = 10\r
- ErrorCode = long(Value, Base)\r
+ ErrorCode = int(Value, Base)\r
if ErrorCode > PCD_ERR_CODE_MAX_SIZE or ErrorCode < 0:\r
- Logger.Error('Parser', \r
+ Logger.Error('Parser',\r
FORMAT_NOT_SUPPORTED,\r
"The format %s of ErrorCode is not valid, should be UNIT32 type or long type" % Value,\r
- File = ContainerFile, \r
+ File = ContainerFile,\r
Line = LineNum)\r
- #\r
- # To delete the tailing 'L'\r
- #\r
- return hex(ErrorCode)[:-1]\r
- except ValueError, XStr:\r
+ ErrorCode = '0x%x' % ErrorCode\r
+ return ErrorCode\r
+ except ValueError as XStr:\r
if XStr:\r
pass\r
- Logger.Error('Parser', \r
+ Logger.Error('Parser',\r
FORMAT_NOT_SUPPORTED,\r
"The format %s of ErrorCode is not valid, should be UNIT32 type or long type" % Value,\r
- File = ContainerFile, \r
+ File = ContainerFile,\r
Line = LineNum)\r
- \r
+\r
## ParseDecPcdGenericComment\r
#\r
-# @param GenericComment: Generic comment list, element of (CommentLine, \r
+# @param GenericComment: Generic comment list, element of (CommentLine,\r
# LineNum)\r
# @param ContainerFile: Input value for filename of Dec file\r
-# \r
-def ParseDecPcdGenericComment (GenericComment, ContainerFile, TokenSpaceGuidCName, CName, MacroReplaceDict): \r
- HelpStr = '' \r
+#\r
+def ParseDecPcdGenericComment (GenericComment, ContainerFile, TokenSpaceGuidCName, CName, MacroReplaceDict):\r
+ HelpStr = ''\r
PromptStr = ''\r
PcdErr = None\r
PcdErrList = []\r
ValidValueNum = 0\r
ValidRangeNum = 0\r
ExpressionNum = 0\r
- \r
+\r
for (CommentLine, LineNum) in GenericComment:\r
Comment = CleanString2(CommentLine)[1]\r
#\r
if MatchedStr:\r
Macro = MatchedStr.strip().lstrip('$(').rstrip(')').strip()\r
if Macro in MacroReplaceDict:\r
- Comment = Comment.replace(MatchedStr, MacroReplaceDict[Macro]) \r
+ Comment = Comment.replace(MatchedStr, MacroReplaceDict[Macro])\r
if Comment.startswith(TAB_PCD_VALIDRANGE):\r
if ValidValueNum > 0 or ExpressionNum > 0:\r
- Logger.Error('Parser', \r
+ Logger.Error('Parser',\r
FORMAT_NOT_SUPPORTED,\r
ST.WRN_MULTI_PCD_RANGES,\r
- File = ContainerFile, \r
+ File = ContainerFile,\r
Line = LineNum)\r
else:\r
PcdErr = PcdErrorObject()\r
else:\r
Logger.Error("Parser",\r
FORMAT_NOT_SUPPORTED,\r
- Cause, \r
- ContainerFile, \r
+ Cause,\r
+ ContainerFile,\r
LineNum)\r
elif Comment.startswith(TAB_PCD_VALIDLIST):\r
if ValidRangeNum > 0 or ExpressionNum > 0:\r
- Logger.Error('Parser', \r
+ Logger.Error('Parser',\r
FORMAT_NOT_SUPPORTED,\r
ST.WRN_MULTI_PCD_RANGES,\r
- File = ContainerFile, \r
+ File = ContainerFile,\r
Line = LineNum)\r
elif ValidValueNum > 0:\r
- Logger.Error('Parser', \r
+ Logger.Error('Parser',\r
FORMAT_NOT_SUPPORTED,\r
ST.WRN_MULTI_PCD_VALIDVALUE,\r
- File = ContainerFile, \r
+ File = ContainerFile,\r
Line = LineNum)\r
else:\r
PcdErr = PcdErrorObject()\r
else:\r
Logger.Error("Parser",\r
FORMAT_NOT_SUPPORTED,\r
- Cause, \r
- ContainerFile, \r
+ Cause,\r
+ ContainerFile,\r
LineNum)\r
elif Comment.startswith(TAB_PCD_EXPRESSION):\r
if ValidRangeNum > 0 or ValidValueNum > 0:\r
- Logger.Error('Parser', \r
+ Logger.Error('Parser',\r
FORMAT_NOT_SUPPORTED,\r
ST.WRN_MULTI_PCD_RANGES,\r
- File = ContainerFile, \r
+ File = ContainerFile,\r
Line = LineNum)\r
else:\r
PcdErr = PcdErrorObject()\r
else:\r
PcdErr.SetExpression(Expression)\r
PcdErrList.append(PcdErr)\r
- else: \r
+ else:\r
Logger.Error("Parser",\r
FORMAT_NOT_SUPPORTED,\r
- Cause, \r
- ContainerFile, \r
- LineNum) \r
+ Cause,\r
+ ContainerFile,\r
+ LineNum)\r
elif Comment.startswith(TAB_PCD_PROMPT):\r
if PromptStr:\r
- Logger.Error('Parser', \r
+ Logger.Error('Parser',\r
FORMAT_NOT_SUPPORTED,\r
ST.WRN_MULTI_PCD_PROMPT,\r
- File = ContainerFile, \r
+ File = ContainerFile,\r
Line = LineNum)\r
PromptStr = Comment.replace(TAB_PCD_PROMPT, "", 1).strip()\r
else:\r
if Comment:\r
HelpStr += Comment + '\n'\r
- \r
+\r
#\r
# remove the last EOL if the comment is of format 'FOO\n'\r
#\r
LineNum = TailCommentList[0][1]\r
\r
Comment = TailComment.lstrip(" #")\r
- \r
+\r
ReFindFirstWordRe = re.compile(r"""^([^ #]*)""", re.DOTALL)\r
- \r
+\r
#\r
# get first word and compare with SUP_MODULE_LIST\r
#\r
# parse line, it must have supported module type specified\r
#\r
if Comment.find(TAB_COMMENT_SPLIT) == -1:\r
- Comment += TAB_COMMENT_SPLIT \r
+ Comment += TAB_COMMENT_SPLIT\r
SupMode, HelpStr = GetSplitValueList(Comment, TAB_COMMENT_SPLIT, 1)\r
SupModuleList = []\r
for Mod in GetSplitValueList(SupMode, TAB_SPACE_SPLIT):\r
elif Mod not in SUP_MODULE_LIST:\r
Logger.Error("UPT",\r
FORMAT_INVALID,\r
- ST.WRN_INVALID_MODULE_TYPE%Mod, \r
- ContainerFile, \r
+ ST.WRN_INVALID_MODULE_TYPE%Mod,\r
+ ContainerFile,\r
LineNum)\r
else:\r
SupModuleList.append(Mod)\r
\r
## _CheckListExpression\r
#\r
-# @param Expression: Pcd value list expression \r
+# @param Expression: Pcd value list expression\r
#\r
def _CheckListExpression(Expression):\r
ListExpr = ''\r
if TAB_VALUE_SPLIT in Expression:\r
- ListExpr = Expression[Expression.find(TAB_VALUE_SPLIT)+1:] \r
+ ListExpr = Expression[Expression.find(TAB_VALUE_SPLIT)+1:]\r
else:\r
ListExpr = Expression\r
- \r
+\r
return IsValidListExpr(ListExpr)\r
\r
-## _CheckExpreesion\r
+## _CheckExpression\r
#\r
# @param Expression: Pcd value expression\r
#\r
## _CheckRangeExpression\r
#\r
# @param Expression: Pcd range expression\r
-# \r
+#\r
def _CheckRangeExpression(Expression):\r
RangeExpr = ''\r
if TAB_VALUE_SPLIT in Expression:\r
RangeExpr = Expression[Expression.find(TAB_VALUE_SPLIT)+1:]\r
else:\r
RangeExpr = Expression\r
- \r
+\r
return IsValidRangeExpr(RangeExpr)\r
\r
## ValidateCopyright\r
#\r
def ValidateCopyright(Result, ErrType, FileName, LineNo, ErrMsg):\r
if not Result:\r
- Logger.Warn("\nUPT", ErrType, FileName, LineNo, ErrMsg) \r
+ Logger.Warn("\nUPT", ErrType, FileName, LineNo, ErrMsg)\r
\r
## _ValidateCopyright\r
#\r
# @param Line: Line that contains copyright information, # stripped\r
-# \r
+#\r
# @retval Result: True if line is conformed to Spec format, False else\r
# @retval ErrMsg: the detailed error description\r
-# \r
+#\r
def _ValidateCopyright(Line):\r
if Line:\r
pass\r
Result = True\r
ErrMsg = ''\r
- \r
+\r
return Result, ErrMsg\r
\r
def GenerateTokenList (Comment):\r
#\r
- # Tokenize Comment using '#' and ' ' as token seperators\r
+ # Tokenize Comment using '#' and ' ' as token separators\r
#\r
- RelplacedComment = None \r
- while Comment != RelplacedComment:\r
- RelplacedComment = Comment\r
+ ReplacedComment = None\r
+ while Comment != ReplacedComment:\r
+ ReplacedComment = Comment\r
Comment = Comment.replace('##', '#').replace(' ', ' ').replace(' ', '#').strip('# ')\r
return Comment.split('#')\r
\r
Usage = None\r
Type = None\r
String = None\r
- \r
+\r
Comment = Comment[0]\r
- \r
- NumTokens = 2 \r
+\r
+ NumTokens = 2\r
if ParseVariable:\r
- # \r
- # Remove white space around first instance of ':' from Comment if 'Variable' \r
+ #\r
+ # Remove white space around first instance of ':' from Comment if 'Variable'\r
# is in front of ':' and Variable is the 1st or 2nd token in Comment.\r
#\r
- List = Comment.split(':', 1) \r
+ List = Comment.split(':', 1)\r
if len(List) > 1:\r
SubList = GenerateTokenList (List[0].strip())\r
if len(SubList) in [1, 2] and SubList[-1] == 'Variable':\r
- if List[1].strip().find('L"') == 0: \r
+ if List[1].strip().find('L"') == 0:\r
Comment = List[0].strip() + ':' + List[1].strip()\r
- \r
- # \r
+\r
+ #\r
# Remove first instance of L"<VariableName> from Comment and put into String\r
- # if and only if L"<VariableName>" is the 1st token, the 2nd token. Or \r
+ # if and only if L"<VariableName>" is the 1st token, the 2nd token. Or\r
# L"<VariableName>" is the third token immediately following 'Variable:'.\r
#\r
End = -1\r
End = String[2:].find('"')\r
if End >= 0:\r
SubList = GenerateTokenList (Comment[:Start])\r
- if len(SubList) < 2: \r
+ if len(SubList) < 2:\r
Comment = Comment[:Start] + String[End + 3:]\r
String = String[:End + 3]\r
Type = 'Variable'\r
- NumTokens = 1 \r
- \r
+ NumTokens = 1\r
+\r
#\r
- # Initialze HelpText to Comment. \r
+ # Initialize HelpText to Comment.\r
# Content will be remove from HelpText as matching tokens are found\r
- # \r
+ #\r
HelpText = Comment\r
- \r
+\r
#\r
- # Tokenize Comment using '#' and ' ' as token seperators\r
+ # Tokenize Comment using '#' and ' ' as token separators\r
#\r
List = GenerateTokenList (Comment)\r
- \r
+\r
#\r
- # Search first two tokens for Usage and Type and remove any matching tokens \r
+ # Search first two tokens for Usage and Type and remove any matching tokens\r
# from HelpText\r
#\r
for Token in List[0:NumTokens]:\r
if Type is None and Token in TypeTokens:\r
Type = TypeTokens[Token]\r
HelpText = HelpText.replace(Token, '')\r
- if Usage is not None: \r
+ if Usage is not None:\r
for Token in List[0:NumTokens]:\r
if Token in RemoveTokens:\r
HelpText = HelpText.replace(Token, '')\r
- \r
+\r
#\r
# If no Usage token is present and set Usage to UNDEFINED\r
- # \r
+ #\r
if Usage is None:\r
Usage = 'UNDEFINED'\r
- \r
+\r
#\r
# If no Type token is present and set Type to UNDEFINED\r
- # \r
+ #\r
if Type is None:\r
Type = 'UNDEFINED'\r
- \r
+\r
#\r
# If Type is not 'Variable:', then set String to None\r
- # \r
+ #\r
if Type != 'Variable':\r
- String = None \r
- \r
+ String = None\r
+\r
#\r
# Strip ' ' and '#' from the beginning of HelpText\r
- # If HelpText is an empty string after all parsing is \r
+ # If HelpText is an empty string after all parsing is\r
# complete then set HelpText to None\r
- # \r
+ #\r
HelpText = HelpText.lstrip('# ')\r
if HelpText == '':\r
HelpText = None\r
- \r
+\r
#\r
# Return parsing results\r
- # \r
- return Usage, Type, String, HelpText \r
+ #\r
+ return Usage, Type, String, HelpText\r