From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.136, mailfrom: zhijux.fan@intel.com) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by groups.io with SMTP; Wed, 19 Jun 2019 17:32:29 -0700 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jun 2019 17:32:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,394,1557212400"; d="dat'59?scan'59,208,59";a="162372292" Received: from fmsmsx106.amr.corp.intel.com ([10.18.124.204]) by orsmga003.jf.intel.com with ESMTP; 19 Jun 2019 17:32:28 -0700 Received: from fmsmsx152.amr.corp.intel.com (10.18.125.5) by FMSMSX106.amr.corp.intel.com (10.18.124.204) with Microsoft SMTP Server (TLS) id 14.3.439.0; Wed, 19 Jun 2019 17:32:28 -0700 Received: from shsmsx154.ccr.corp.intel.com (10.239.6.54) by FMSMSX152.amr.corp.intel.com (10.18.125.5) with Microsoft SMTP Server (TLS) id 14.3.439.0; Wed, 19 Jun 2019 17:32:27 -0700 Received: from shsmsx101.ccr.corp.intel.com ([169.254.1.87]) by SHSMSX154.ccr.corp.intel.com ([169.254.7.156]) with mapi id 14.03.0439.000; Thu, 20 Jun 2019 08:32:26 +0800 From: "Fan, ZhijuX" To: "devel@edk2.groups.io" CC: "Gao, Liming" , "Feng, Bob C" Subject: [PATCH V3] BaseTools:Add DetectNotUsedItem.py to Edk2\BaseTools\Scripts Thread-Topic: [PATCH V3] BaseTools:Add DetectNotUsedItem.py to Edk2\BaseTools\Scripts Thread-Index: AdUm/6D9MqCvoJniSmeNtuQNBeSgIw== Date: Thu, 20 Jun 2019 00:32:25 +0000 Message-ID: Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-product: dlpe-windows dlp-version: 11.0.600.7 dlp-reaction: no-action x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Return-Path: zhijux.fan@intel.com X-Groupsio-MsgNum: 42607 Content-Type: multipart/mixed; boundary="_000_FAD0D7E0AE0FA54D987F6E72435CAFD50AF83D3ASHSMSX101ccrcor_" Content-Language: en-US --_000_FAD0D7E0AE0FA54D987F6E72435CAFD50AF83D3ASHSMSX101ccrcor_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable BZ:https://bugzilla.tianocore.org/show_bug.cgi?id=3D1850 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 --- BaseTools/Scripts/DetectNotUsedItem.py | 185 +++++++++++++++++++++++++++++= ++++ 1 file changed, 185 insertions(+) create mode 100644 BaseTools/Scripts/DetectNotUsedItem.py diff --git a/BaseTools/Scripts/DetectNotUsedItem.py b/BaseTools/Scripts/Det= ectNotUsedItem.py new file mode 100644 index 0000000000..655fb65a96 --- /dev/null +++ b/BaseTools/Scripts/DetectNotUsedItem.py @@ -0,0 +1,185 @@ +## @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__ =3D 'DetectNotUsedItem' +__version__ =3D '%s Version %s' % (__prog__, '0.1') +__copyright__ =3D 'Copyright (c) 2019, Intel Corporation. All rights res= erved.' +__description__ =3D "Detect unreferenced PCD and GUID/Protocols/PPIs.\n" + +SectionList =3D ["LibraryClasses", "Guids", "Ppis", "Protocols", "Pcd"] + +class PROCESS(object): + + def __init__(self, DecPath, InfDirs): + self.Dec =3D DecPath + self.InfPath =3D InfDirs + self.Log =3D [] + + def ParserDscFdfInfFile(self): + AllContentList =3D [] + for File in (self.SearchbyExt(".dsc") + self.SearchbyExt(".fdf") + sel= f.SearchbyExt(".inf")): + AllContentList +=3D self.ParseDscFdfInfContent(File) + return AllContentList + + #Search File by extension name + def SearchbyExt(self, Ext): + FileList =3D [] + for path in self.InfPath: + for root, _, files in os.walk(path, topdown=3DTrue, followlinks=3DFa= lse): + for filename in files: + 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 =3D re.compile(r'\[(.*)\]') + Flag =3D False + Comments =3D{} + Comment_Line =3D [] + ItemName =3D {} + with open(self.Dec, 'r') as F: + for Index, content in enumerate(F): + NotComment =3D not content.strip().startswith("#") + Section =3D SectionRE.findall(content) + if Section and NotComment: + Flag =3D self.IsNeedParseSection(Section[0]) + if Flag: + Comment_Line.append(Index) + if NotComment: + if content !=3D "\n" and content !=3D "\r\n": + ItemName[Index] =3D content.split('=3D')[0].split('|')[0].sp= lit('#')[0].strip() + Comments[Index] =3D Comment_Line + Comment_Line =3D [] + 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 =3D F.readlines() + for Index in range(len(lines)-1, -1, -1): + if lines[Index].strip().startswith("#") or lines[Index] =3D=3D "\n" = or lines[Index] =3D=3D "\r\n": + lines.remove(lines[Index]) + elif "#" in lines[Index]: + lines[Index] =3D lines[Index].split("#")[0].strip() + else: + lines[Index] =3D lines[Index].strip() + return lines + + def DetectNotUsedItem(self): + NotUsedItem =3D {} + DecItem, DecComments =3D self.ParseDecContent() + InfDscFdfContent =3D self.ParserDscFdfInfFile() + for LineNum in list(DecItem.keys()): + DecItemName =3D DecItem[LineNum] + Match_reg =3D re.compile("(?