From 401507c786b4b8bc208c0c34ebdad047173e37ea Mon Sep 17 00:00:00 2001 From: "Fan, ZhijuX" Date: Mon, 24 Jun 2019 18:26:27 +0800 Subject: BaseTools:Add DetectNotUsedItem.py to Edk2\BaseTools\Scripts BZ:https://bugzilla.tianocore.org/show_bug.cgi?id=1850 This script is used to Detect unreferenced PCD and GUID/Protocols/PPIs. The input parameters are Dec file and package directory. This script can be run in both Py2 and Py3. Cc: Bob Feng Cc: Liming Gao Signed-off-by: Zhiju.Fan Reviewed-by: Bob Feng Reviewed-by: Liming Gao --- BaseTools/Scripts/DetectNotUsedItem.py | 198 +++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 BaseTools/Scripts/DetectNotUsedItem.py (limited to 'BaseTools/Scripts') diff --git a/BaseTools/Scripts/DetectNotUsedItem.py b/BaseTools/Scripts/DetectNotUsedItem.py new file mode 100644 index 0000000000..61481fb918 --- /dev/null +++ b/BaseTools/Scripts/DetectNotUsedItem.py @@ -0,0 +1,198 @@ +## @file +# Detect unreferenced PCD and GUID/Protocols/PPIs. +# +# Copyright (c) 2019, Intel Corporation. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# + +''' +DetectNotUsedItem +''' +import re +import os +import sys +import argparse + +# +# Globals for help information +# +__prog__ = 'DetectNotUsedItem' +__version__ = '%s Version %s' % (__prog__, '0.1') +__copyright__ = 'Copyright (c) 2019, Intel Corporation. All rights reserved.' +__description__ = "Detect unreferenced PCD and GUID/Protocols/PPIs.\n" + +SectionList = ["LibraryClasses", "Guids", "Ppis", "Protocols", "Pcd"] + + +class PROCESS(object): + + def __init__(self, DecPath, InfDirs): + self.Dec = DecPath + self.InfPath = InfDirs + self.Log = [] + + def ParserDscFdfInfFile(self): + AllContentList = [] + for File in self.SearchbyExt([".dsc", ".fdf", ".inf"]): + AllContentList += self.ParseDscFdfInfContent(File) + return AllContentList + + # Search File by extension name + def SearchbyExt(self, ExtList): + FileList = [] + for path in self.InfPath: + if type(ExtList) == type(''): + for root, _, files in os.walk(path, topdown=True, followlinks=False): + for filename in files: + if filename.endswith(ExtList): + FileList.append(os.path.join(root, filename)) + elif type(ExtList) == type([]): + for root, _, files in os.walk(path, topdown=True, followlinks=False): + for filename in files: + for Ext in ExtList: + if filename.endswith(Ext): + FileList.append(os.path.join(root, filename)) + return FileList + + # Parse DEC file to get Line number and Name + # return section name, the Item Name and comments line number + def ParseDecContent(self): + SectionRE = re.compile(r'\[(.*)\]') + Flag = False + Comments = {} + Comment_Line = [] + ItemName = {} + with open(self.Dec, 'r') as F: + for Index, content in enumerate(F): + NotComment = not content.strip().startswith("#") + Section = SectionRE.findall(content) + if Section and NotComment: + Flag = self.IsNeedParseSection(Section[0]) + if Flag: + Comment_Line.append(Index) + if NotComment: + if content != "\n" and content != "\r\n": + ItemName[Index] = content.split('=')[0].split('|')[0].split('#')[0].strip() + Comments[Index] = Comment_Line + Comment_Line = [] + return ItemName, Comments + + def IsNeedParseSection(self, SectionName): + for item in SectionList: + if item in SectionName: + return True + return False + + # Parse DSC, FDF, INF File, remove comments, return Lines list + def ParseDscFdfInfContent(self, File): + with open(File, 'r') as F: + lines = F.readlines() + for Index in range(len(lines) - 1, -1, -1): + if lines[Index].strip().startswith("#") or lines[Index] == "\n" or lines[Index] == "\r\n": + lines.remove(lines[Index]) + elif "#" in lines[Index]: + lines[Index] = lines[Index].split("#")[0].strip() + else: + lines[Index] = lines[Index].strip() + return lines + + def DetectNotUsedItem(self): + NotUsedItem = {} + DecItem, DecComments = self.ParseDecContent() + InfDscFdfContent = self.ParserDscFdfInfFile() + for LineNum in list(DecItem.keys()): + DecItemName = DecItem[LineNum] + Match_reg = re.compile("(?