CMS 3D CMS Logo

cmssw_fix_interface.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import os
4 import sys
5 import argparse
6 
7 
8 parser = argparse.ArgumentParser(description='Find includes only used in one non-interface directory.')
9 parser.add_argument('packageName',
10  help='name of package to check interface usage')
11 parser.add_argument('--fix', dest='shouldFix', action='store_true',
12  help='move file and fix includes if only used in 1 directory')
13 parser.add_argument('--remove', dest='removeUnused', action='store_true',
14  help='remove interface files that are not included anywhere')
15 
16 args = parser.parse_args()
17 
18 packageName = args.packageName
19 shouldFix = args.shouldFix
20 removeUnused = args.removeUnused
21 
22 interfaceDir = packageName+"/interface"
23 from os.path import isfile, join
24 onlyfiles = [join(interfaceDir,f) for f in os.listdir(interfaceDir) if isfile(join(interfaceDir, f))]
25 
26 for f in onlyfiles:
27  print("checking {filename}".format(filename=f))
28  result = os.popen('git grep \'#include [",<]{filename}[",>]\' | awk -F\':\' \'{{print $1}}\' | sort -u'.format(filename=f))
29 
30  filesUsing = [l[:-1] for l in result]
31 
32  if 0 == len(filesUsing):
33  print(" "+f+" is unused")
34  if removeUnused:
35  os.system('git rm {filename}'.format(filename=f))
36  print(" "+f+" was removed")
37  continue
38 
39  #directories using
40  dirs = set( ( "/".join(name.split("/")[0:3]) for name in filesUsing) )
41  if 1 == len(dirs):
42  onlyDir = dirs.pop()
43  if onlyDir.split("/")[2] != "interface":
44  print(" "+f+" is only used in "+onlyDir)
45  if shouldFix:
46  newFileName = onlyDir+"/"+f.split("/")[3]
47  mvCommand = "git mv {oldName} {newName}".format(oldName=f, newName=newFileName)
48  #print(mvCommand)
49  os.system(mvCommand)
50  sedCommand ="sed --in-place 's/{oldName}/{newName}/' {filesToChange}".format(oldName="\/".join(f.split("/")),newName="\/".join(newFileName.split("/")), filesToChange=" ".join( (n for n in filesUsing)) )
51  #print(sedCommand)
52  os.system(sedCommand)
53 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:21