Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:47

0001 #!/usr/bin/env python3
0002 
0003 import os
0004 import sys
0005 import argparse
0006 
0007 
0008 parser = argparse.ArgumentParser(description='Find includes only used in one non-interface directory.')
0009 parser.add_argument('packageName',
0010                    help='name of package to check interface usage')
0011 parser.add_argument('--fix', dest='shouldFix', action='store_true',
0012                     help='move file and fix includes if only used in 1 directory')
0013 parser.add_argument('--remove', dest='removeUnused', action='store_true',
0014                     help='remove interface files that are not included anywhere')
0015 
0016 args = parser.parse_args()
0017 
0018 packageName = args.packageName
0019 shouldFix = args.shouldFix
0020 removeUnused = args.removeUnused
0021 
0022 interfaceDir = packageName+"/interface"
0023 from os.path import isfile, join
0024 onlyfiles = [join(interfaceDir,f) for f in os.listdir(interfaceDir) if isfile(join(interfaceDir, f))]
0025 
0026 for f in onlyfiles:
0027     print("checking {filename}".format(filename=f))
0028     result = os.popen('git grep \'#include [",<]{filename}[",>]\' | awk -F\':\' \'{{print $1}}\' | sort -u'.format(filename=f))
0029 
0030     filesUsing = [l[:-1] for l in result]
0031 
0032     if 0 == len(filesUsing):
0033         print("  "+f+" is unused")
0034         if removeUnused:
0035             os.system('git rm {filename}'.format(filename=f))
0036             print("   "+f+" was removed")
0037         continue
0038     
0039     #directories using
0040     dirs = set( ( "/".join(name.split("/")[0:3]) for name in filesUsing) )
0041     if 1 == len(dirs):
0042         onlyDir = dirs.pop()
0043         if onlyDir.split("/")[2] != "interface":
0044             print("  "+f+" is only used in "+onlyDir)
0045             if shouldFix:
0046                 newFileName = onlyDir+"/"+f.split("/")[3]
0047                 mvCommand = "git mv {oldName} {newName}".format(oldName=f, newName=newFileName)
0048                 #print(mvCommand)
0049                 os.system(mvCommand)
0050                 sedCommand ="sed --in-place 's/{oldName}/{newName}/' {filesToChange}".format(oldName="\/".join(f.split("/")),newName="\/".join(newFileName.split("/")), filesToChange=" ".join( (n for n in filesUsing)) )
0051                 #print(sedCommand)
0052                 os.system(sedCommand)
0053