Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-16 03:19:59

0001 #! /usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import subprocess
0005 import shutil
0006 import sys
0007 import re
0008 
0009 kProducer = 0
0010 kFilter = 1
0011 
0012 def find_all_module_classes():
0013   s = set()
0014   found = subprocess.check_output(["git","grep", "class *[A-Za-z0-9_<>]* *: *public "])
0015   s.update(found.split("\n"))
0016 
0017   ret = dict()
0018   for l in s:
0019     parts = l.split(":")
0020     if len(parts)>2:
0021       file = parts[0]
0022       name = parts[1]
0023       name = name[name.find("class")+5:].strip()
0024       ret.setdefault(name,[]).append((":".join(parts[2:]), file))
0025   
0026   return ret
0027 
0028 def print_lines(lines):
0029   for l in lines:
0030     if len(l)>0:
0031       print("  ",l)
0032 
0033 def find_file_for_module(name,all_modules):
0034   if name in all_modules:
0035     info = all_modules[name]
0036     if len(info) != 1:
0037       print("ERROR: more than one declaration for '"+name+"'\n")
0038       for inherits,file in info:
0039         print("  ",file, inherits)
0040       return (None,None)
0041     inherits,file = info[0]
0042     if -1 != inherits.find("edm::EDProducer"):
0043       type = kProducer
0044     elif -1 != inherits.find("edm::EDFilter"):
0045       type = kFilter
0046     else:
0047       print("ERROR: class '"+name+"' does not directly inherit from EDProducer or EDFilter\n  "+inherits)
0048       return (None,None)
0049     return (file,type)
0050   print("ERROR: did not find a standard class declaration for '"+name+"'")
0051   try:
0052     found = subprocess.check_output(["git","grep", "class *"+name+" *:"])
0053     print_lines( found.split("\n") )
0054   except:
0055     try:
0056       found = subprocess.check_output(["git","grep", "typedef *.* "+name])
0057       print_lines( found.split("\n") )
0058     except:
0059       pass
0060   return (None,None)
0061   
0062 def checkout_package(fileName):
0063   c = fileName.split("/")
0064   print("checking out "+c[0]+"/"+c[1])
0065   sparce_checkout = ".git/info/sparse-checkout"
0066   f = open(sparce_checkout,"r")
0067   linesInSparse = set(f.readlines())
0068   f.close()
0069   linesInSparse.add(c[0]+"/"+c[1]+"\n")
0070   
0071   f = open(sparce_checkout+"_new","w")
0072   for l in linesInSparse:
0073     if l:
0074       f.write(l)
0075   f.close()
0076   shutil.move(sparce_checkout,sparce_checkout+"_old")
0077   shutil.move(sparce_checkout+"_new",sparce_checkout)
0078   subprocess.call(["git","read-tree","-mu","HEAD"])
0079 
0080 def edit_file(fileName,moduleType,moduleName):
0081   print(" editting "+fileName)
0082   fOld = open(fileName)
0083   fNew = open(fileName+"_NEW","w")
0084   
0085   lookingForChanges = True
0086   addedInclude = False
0087   for l in fOld.readlines():
0088     if lookingForChanges:
0089       if -1 != l.find("#include"):
0090         if moduleType == kProducer:
0091           if -1 != l.find("FWCore/Framework/interface/EDProducer.h"):
0092             l='#include "FWCore/Framework/interface/stream/EDProducer.h"\n'
0093             addedInclude = True
0094         elif moduleType == kFilter:
0095           if -1 != l.find("FWCore/Framework/interface/EDFilter.h"):
0096             l = '#include "FWCore/Framework/interface/stream/EDFilter.h"\n'
0097             addedInclude = True
0098       elif -1 != l.find("class"):
0099         if -1 != l.find(moduleName):
0100           if moduleType == kProducer:
0101             if -1 != l.find("edm::EDProducer"):
0102               l = l.replace("edm::EDProducer","edm::stream::EDProducer<>")
0103               lookingForChanges = False
0104           elif moduleType == kFilter:
0105             if -1 != l.find("edm::EDFilter"):
0106               l=l.replace("edm::EDFilter","edm::stream::EDFilter<>")
0107     fNew.write(l)
0108     if -1 != l.find(" beginJob("):
0109       print(" WARNING: beginJob found but not supported by stream")
0110       print("  ",l)
0111     if -1 != l.find(" endJob("):
0112       print(" WARNING: endJob found but not supported by stream")
0113       print("  ",l)
0114   if not addedInclude:
0115     print(" WARNING: did not write include into "+fileName)
0116   fNew.close()
0117   fOld.close()
0118   shutil.move(fileName,fileName+"_OLD")
0119   shutil.move(fileName+"_NEW",fileName)
0120 
0121 modules = sys.argv[1:]
0122 
0123 print("getting info")
0124 all_mods_info= find_all_module_classes()
0125 
0126 
0127 for m in modules:
0128   f,t = find_file_for_module(m,all_mods_info)
0129   if f:
0130     checkout_package(f)
0131     edit_file(f,t,m)
0132