From nobody Mon Apr 29 17:47:17 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+42739+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+42739+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1561371993; cv=none; d=zoho.com; s=zohoarc; b=N3N32qNoSv720kifxo6Btb6elHbL1IEFiN9Oqq0SiPBW1sQywxt4Eha0YomsUjS3gIsofYpKSd8VUI+0xZBpdxmztRt8l2dauMt3CN0yzCLoc8VbjKrGXaFliLq03OYOP0bhb9WR7MVTu73eaKIRWNdzSaU4Fs3F2cafUlNBIDY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561371993; h=Content-Type:Cc:Date:From:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To:ARC-Authentication-Results; bh=U07/h5V9IKodfUIeGYT2PSEJhCgAbMFxY4esDekPEKs=; b=lVD6z+vPocaq8CPYRx7XhQTbcjP3HcJuba5XuRGDrGQ73QupkXwSkFpeptHh/I7ma62H3U95PwAC08ZwEsjIcq3A+e6wVr5tiAHmM9ZA+FRwuDkWLWTWAoGYSWBuWHKxuJCF6kxAQkm4WBiATj6htVHCnl20UNxaXQzG+O781Po= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+42739+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1561371993016852.1358698963513; Mon, 24 Jun 2019 03:26:33 -0700 (PDT) Return-Path: X-Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by groups.io with SMTP; Mon, 24 Jun 2019 03:26:31 -0700 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False X-Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Jun 2019 03:26:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,411,1557212400"; d="dat'59?scan'59,208,59";a="184094212" X-Received: from fmsmsx106.amr.corp.intel.com ([10.18.124.204]) by fmsmga004.fm.intel.com with ESMTP; 24 Jun 2019 03:26:30 -0700 X-Received: from fmsmsx161.amr.corp.intel.com (10.18.125.9) by FMSMSX106.amr.corp.intel.com (10.18.124.204) with Microsoft SMTP Server (TLS) id 14.3.439.0; Mon, 24 Jun 2019 03:26:30 -0700 X-Received: from shsmsx153.ccr.corp.intel.com (10.239.6.53) by FMSMSX161.amr.corp.intel.com (10.18.125.9) with Microsoft SMTP Server (TLS) id 14.3.439.0; Mon, 24 Jun 2019 03:26:30 -0700 X-Received: from shsmsx101.ccr.corp.intel.com ([169.254.1.87]) by SHSMSX153.ccr.corp.intel.com ([169.254.12.76]) with mapi id 14.03.0439.000; Mon, 24 Jun 2019 18:26:28 +0800 From: "Fan, ZhijuX" To: "devel@edk2.groups.io" CC: "Gao, Liming" , "Feng, Bob C" Subject: [edk2-devel] [PATCH V4] BaseTools:Add DetectNotUsedItem.py to Edk2\BaseTools\Scripts Thread-Topic: [PATCH V4] BaseTools:Add DetectNotUsedItem.py to Edk2\BaseTools\Scripts Thread-Index: AdUqdz3HJY+EOM9WQs6+ALaQrF7ffg== Date: Mon, 24 Jun 2019 10:26:27 +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 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,zhijux.fan@intel.com Content-Type: multipart/mixed; boundary="_000_FAD0D7E0AE0FA54D987F6E72435CAFD50AF845B6SHSMSX101ccrcor_" Content-Language: en-US DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1561371992; bh=AOGNuKt3iAyKi1x8pSp6neSsuOrZ+g6UEkCfQek6czE=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=puyG4O7KLQbXzQhoMti0wDwVJqPR2zbPQZz3GYSvJSsiz/uibc9RaWZfMON0pLPks+m J0/jly32klcTBvhK6QPrb5Yrcr1cQ0XCWjvrFrs4Wrj1Q9trlwYyOa+2q7tYCoYE93Kk3 eJTJuKQKdJPw+2rL1LarJUHc9VoU4PMbw5I= X-Zoho-Virus-Status: 1 X-ZohoMail-DKIM: pass (identity @groups.io) --_000_FAD0D7E0AE0FA54D987F6E72435CAFD50AF845B6SHSMSX101ccrcor_ Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Bob Feng --- BaseTools/Scripts/DetectNotUsedItem.py | 198 +++++++++++++++++++++++++++++= ++++ 1 file changed, 198 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..7e3568fcf9 --- /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__ =3D 'DetectNotUsedItem' +__version__ =3D '%s Version %s' % (__prog__, '0.1') +__copyright__ =3D 'Copyright (c) 2019, Intel Corporation. All rights reser= ved.' +__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", ".fdf", ".inf"]): + AllContentList +=3D self.ParseDscFdfInfContent(File) + return AllContentList + + # Search File by extension name + def SearchbyExt(self, ExtList): + FileList =3D [] + for path in self.InfPath: + if type(ExtList) =3D=3D type(''): + for root, _, files in os.walk(path, topdown=3DTrue, follow= links=3DFalse): + for filename in files: + if filename.endswith(ExtList): + FileList.append(os.path.join(root, filename)) + elif type(ExtList) =3D=3D type([]): + for root, _, files in os.walk(path, topdown=3DTrue, follow= links=3DFalse): + for filename in files: + for Ext in ExtList: + if filename.endswith(Ext): + FileList.append(os.path.join(root, filenam= e)) + 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].sp= lit('|')[0].split('#')[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("(?