Back to home page

Project CMSSW displayed by LXR

 
 

    


Warning, /FWCore/ParameterSet/scripts/edmConfigIncludeChecker is written in an unsupported language. File is not indexed.

0001 #!/usr/bin/env python3
0002 
0003 # Check needed by Martin Gruenewald for HLT studies
0004 # This isn't intended to be an example for nice coding... ;-) 
0005 #
0006 # for questions: benedikt.hegner@cern.ch
0007 
0008 from __future__ import print_function
0009 from builtins import object
0010 import os.path
0011 import sys
0012 from os import environ
0013 import copy
0014 
0015 class IncludeChecker(object):
0016 
0017     def __init__(self):
0018         self.includedFileNames = []
0019         self.doneIncludes = []
0020         self.problematicIncludes = []
0021         self.missingIncludes = []
0022         self.traceback = []
0023         # read in the paths used by CMSSW
0024         try:
0025             paths = environ['CMSSW_SEARCH_PATH']
0026             self.localpath = environ['CMSSW_BASE']+'/src'
0027         except KeyError:
0028              raise RuntimeError("The environment variable 'CMSSW_SEARCH_PATH' must be set for include to work")
0029         self.lpaths = paths.split(':')
0030 
0031             
0032     def formatTraceback(self, traceback):
0033         tb = ""
0034         tr = copy.copy(traceback)
0035         tr.reverse()
0036         for item in tr:
0037             tb += "    %s\n" %item
0038         return tb    
0039 
0040 
0041     # return includes defined in a single file
0042     def getIncludesFromFile(self, filename, currentFile):
0043         includes = []
0044         f = None  
0045         # look for the file in the CMSSW_SEARCH_PATH
0046         for path in self.lpaths:
0047             path +='/'+filename
0048             if os.path.exists(path):
0049                 f=path
0050                 break
0051         if f is None:
0052             if os.path.exists(filename):
0053                 f = filename 
0054             else:
0055                 self.missingIncludes.append(filename + "\n   Traceback:\n" +self.formatTraceback(self.traceback))
0056                 return includes, ""
0057         # here now the part where we check if this include is problematic
0058         # what means problematic here?
0059         #  -> there is the right directory in the local area but the file is fetched from another place
0060         parts = filename.split("/")
0061         if len(parts) > 2:
0062             directoryPart = ""
0063             for part in parts[:-1]:
0064                 directoryPart += "/" +part
0065             if os.path.exists(self.localpath+directoryPart) and f != self.localpath+"/"+filename:
0066                 self.problematicIncludes.append(filename + "\n   Traceback:\n" +self.formatTraceback(self.traceback))
0067 
0068         # now continue and parse the other includes
0069         theFile = open(f, "r")
0070         self.includedFileNames.append(f)
0071         cStyleCommentsCounter = 0
0072         for line in theFile:
0073             if "/*" in line: cStyleCommentsCounter += 1
0074             if "*/" in line: cStyleCommentsCounter -= 1 
0075             if ("include " in line or " from " in line) and not ("#" in line or "//" in line or  "/*" in line or  "*/" in line or (cStyleCommentsCounter!=0) ):
0076                 includeCandidate = line.split('"')[1] 
0077                 if includeCandidate not in self.doneIncludes:
0078                   includes.append(includeCandidate)
0079                 theFile.close        
0080         return includes, f             
0081 
0082 
0083     # here the real recursive include process
0084     def getIncludesFromList(self, filelist,currentFile): 
0085         self.traceback.append(currentFile)
0086         for filename in filelist:
0087             includes, fullPathToFile = self.getIncludesFromFile(filename, currentFile)
0088             self.doneIncludes.append(filename) 
0089             self.getIncludesFromList(includes,fullPathToFile)
0090         self.traceback.pop()                
0091 
0092 
0093     def check(self, filename):
0094         includes, fullPathToFile = self.getIncludesFromFile(filename, "toplevel")
0095         self.getIncludesFromList(includes, fullPathToFile)
0096 
0097     
0098     def printResult(self):
0099         print("Included files:")
0100         print("---------------")
0101         for item in self.includedFileNames:
0102             print("  ", item)
0103 
0104         if len(self.problematicIncludes) != 0:
0105             print("\nProblematic files:")
0106             print("------------------")
0107             for item in self.problematicIncludes:
0108                 print("  ", item)
0109         else:
0110             print("\nHaven't found any problematic files")
0111 
0112         if len(self.missingIncludes) != 0:
0113             print("\nMissing files:")
0114             print("--------------")
0115             for item in self.missingIncludes:
0116                 print("  ", item)
0117 
0118 
0119                 
0120 ##########################
0121 if __name__ == "__main__":
0122 
0123     args = sys.argv
0124     if 2 == len(args):
0125         filename = args[1]
0126         checker = IncludeChecker()
0127         checker.check(filename)
0128         checker.printResult()        
0129     else:
0130         print("Please specify a file to check")