## @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("(?