DetectNotUsedItem.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ## @file
  2. # Detect unreferenced PCD and GUID/Protocols/PPIs.
  3. #
  4. # Copyright (c) 2019, Intel Corporation. All rights reserved.
  5. #
  6. # SPDX-License-Identifier: BSD-2-Clause-Patent
  7. #
  8. '''
  9. DetectNotUsedItem
  10. '''
  11. import re
  12. import os
  13. import sys
  14. import argparse
  15. #
  16. # Globals for help information
  17. #
  18. __prog__ = 'DetectNotUsedItem'
  19. __version__ = '%s Version %s' % (__prog__, '0.1')
  20. __copyright__ = 'Copyright (c) 2019, Intel Corporation. All rights reserved.'
  21. __description__ = "Detect unreferenced PCD and GUID/Protocols/PPIs.\n"
  22. SectionList = ["LibraryClasses", "Guids", "Ppis", "Protocols", "Pcd"]
  23. class PROCESS(object):
  24. def __init__(self, DecPath, InfDirs):
  25. self.Dec = DecPath
  26. self.InfPath = InfDirs
  27. self.Log = []
  28. def ParserDscFdfInfFile(self):
  29. AllContentList = []
  30. for File in self.SearchbyExt([".dsc", ".fdf", ".inf"]):
  31. AllContentList += self.ParseDscFdfInfContent(File)
  32. return AllContentList
  33. # Search File by extension name
  34. def SearchbyExt(self, ExtList):
  35. FileList = []
  36. for path in self.InfPath:
  37. if type(ExtList) == type(''):
  38. for root, _, files in os.walk(path, topdown=True, followlinks=False):
  39. for filename in files:
  40. if filename.endswith(ExtList):
  41. FileList.append(os.path.join(root, filename))
  42. elif type(ExtList) == type([]):
  43. for root, _, files in os.walk(path, topdown=True, followlinks=False):
  44. for filename in files:
  45. for Ext in ExtList:
  46. if filename.endswith(Ext):
  47. FileList.append(os.path.join(root, filename))
  48. return FileList
  49. # Parse DEC file to get Line number and Name
  50. # return section name, the Item Name and comments line number
  51. def ParseDecContent(self):
  52. SectionRE = re.compile(r'\[(.*)\]')
  53. Flag = False
  54. Comments = {}
  55. Comment_Line = []
  56. ItemName = {}
  57. with open(self.Dec, 'r') as F:
  58. for Index, content in enumerate(F):
  59. NotComment = not content.strip().startswith("#")
  60. Section = SectionRE.findall(content)
  61. if Section and NotComment:
  62. Flag = self.IsNeedParseSection(Section[0])
  63. if Flag:
  64. Comment_Line.append(Index)
  65. if NotComment:
  66. if content != "\n" and content != "\r\n":
  67. ItemName[Index] = content.split('=')[0].split('|')[0].split('#')[0].strip()
  68. Comments[Index] = Comment_Line
  69. Comment_Line = []
  70. return ItemName, Comments
  71. def IsNeedParseSection(self, SectionName):
  72. for item in SectionList:
  73. if item in SectionName:
  74. return True
  75. return False
  76. # Parse DSC, FDF, INF File, remove comments, return Lines list
  77. def ParseDscFdfInfContent(self, File):
  78. with open(File, 'r') as F:
  79. lines = F.readlines()
  80. for Index in range(len(lines) - 1, -1, -1):
  81. if lines[Index].strip().startswith("#") or lines[Index] == "\n" or lines[Index] == "\r\n":
  82. lines.remove(lines[Index])
  83. elif "#" in lines[Index]:
  84. lines[Index] = lines[Index].split("#")[0].strip()
  85. else:
  86. lines[Index] = lines[Index].strip()
  87. return lines
  88. def DetectNotUsedItem(self):
  89. NotUsedItem = {}
  90. DecItem, DecComments = self.ParseDecContent()
  91. InfDscFdfContent = self.ParserDscFdfInfFile()
  92. for LineNum in list(DecItem.keys()):
  93. DecItemName = DecItem[LineNum]
  94. Match_reg = re.compile("(?<![a-zA-Z0-9_-])%s(?![a-zA-Z0-9_-])" % DecItemName)
  95. MatchFlag = False
  96. for Line in InfDscFdfContent:
  97. if Match_reg.search(Line):
  98. MatchFlag = True
  99. break
  100. if not MatchFlag:
  101. NotUsedItem[LineNum] = DecItemName
  102. self.Display(NotUsedItem)
  103. return NotUsedItem, DecComments
  104. def Display(self, UnuseDict):
  105. print("DEC File:\n%s\n%s%s" % (self.Dec, "{:<15}".format("Line Number"), "{:<0}".format("Unused Item")))
  106. self.Log.append(
  107. "DEC File:\n%s\n%s%s\n" % (self.Dec, "{:<15}".format("Line Number"), "{:<0}".format("Unused Item")))
  108. for num in list(sorted(UnuseDict.keys())):
  109. ItemName = UnuseDict[num]
  110. print("%s%s%s" % (" " * 3, "{:<12}".format(num + 1), "{:<1}".format(ItemName)))
  111. self.Log.append(("%s%s%s\n" % (" " * 3, "{:<12}".format(num + 1), "{:<1}".format(ItemName))))
  112. def Clean(self, UnUseDict, Comments):
  113. removednum = []
  114. for num in list(UnUseDict.keys()):
  115. if num in list(Comments.keys()):
  116. removednum += Comments[num]
  117. with open(self.Dec, 'r') as Dec:
  118. lines = Dec.readlines()
  119. try:
  120. with open(self.Dec, 'w+') as T:
  121. for linenum in range(len(lines)):
  122. if linenum in removednum:
  123. continue
  124. else:
  125. T.write(lines[linenum])
  126. print("DEC File has been clean: %s" % (self.Dec))
  127. except Exception as err:
  128. print(err)
  129. class Main(object):
  130. def mainprocess(self, Dec, Dirs, Isclean, LogPath):
  131. for dir in Dirs:
  132. if not os.path.exists(dir):
  133. print("Error: Invalid path for '--dirs': %s" % dir)
  134. sys.exit(1)
  135. Pa = PROCESS(Dec, Dirs)
  136. unuse, comment = Pa.DetectNotUsedItem()
  137. if Isclean:
  138. Pa.Clean(unuse, comment)
  139. self.Logging(Pa.Log, LogPath)
  140. def Logging(self, content, LogPath):
  141. if LogPath:
  142. try:
  143. if os.path.isdir(LogPath):
  144. FilePath = os.path.dirname(LogPath)
  145. if not os.path.exists(FilePath):
  146. os.makedirs(FilePath)
  147. with open(LogPath, 'w+') as log:
  148. for line in content:
  149. log.write(line)
  150. print("Log save to file: %s" % LogPath)
  151. except Exception as e:
  152. print("Save log Error: %s" % e)
  153. def main():
  154. parser = argparse.ArgumentParser(prog=__prog__,
  155. description=__description__ + __copyright__,
  156. conflict_handler='resolve')
  157. parser.add_argument('-i', '--input', metavar="", dest='InputDec', help="Input DEC file name.")
  158. parser.add_argument('--dirs', metavar="", action='append', dest='Dirs',
  159. help="The package directory. To specify more directories, please repeat this option.")
  160. parser.add_argument('--clean', action='store_true', default=False, dest='Clean',
  161. help="Clean the unreferenced items from DEC file.")
  162. parser.add_argument('--log', metavar="", dest="Logfile", default=False,
  163. help="Put log in specified file as well as on console.")
  164. options = parser.parse_args()
  165. if options.InputDec:
  166. if not (os.path.exists(options.InputDec) and options.InputDec.endswith(".dec")):
  167. print("Error: Invalid DEC file input: %s" % options.InputDec)
  168. if options.Dirs:
  169. M = Main()
  170. M.mainprocess(options.InputDec, options.Dirs, options.Clean, options.Logfile)
  171. else:
  172. print("Error: the following argument is required:'--dirs'.")
  173. else:
  174. print("Error: the following argument is required:'-i/--input'.")
  175. if __name__ == '__main__':
  176. main()