CMS 3D CMS Logo

edmConvertToStreamModule.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 import subprocess
5 import shutil
6 import sys
7 import re
8 
9 kProducer = 0
10 kFilter = 1
11 
13  s = set()
14  found = subprocess.check_output(["git","grep", "class *[A-Za-z0-9_<>]* *: *public "])
15  s.update(found.split("\n"))
16 
17  ret = dict()
18  for l in s:
19  parts = l.split(":")
20  if len(parts)>2:
21  file = parts[0]
22  name = parts[1]
23  name = name[name.find("class")+5:].strip()
24  ret.setdefault(name,[]).append((":".join(parts[2:]), file))
25 
26  return ret
27 
28 def print_lines(lines):
29  for l in lines:
30  if len(l)>0:
31  print(" ",l)
32 
33 def find_file_for_module(name,all_modules):
34  if name in all_modules:
35  info = all_modules[name]
36  if len(info) != 1:
37  print("ERROR: more than one declaration for '"+name+"'\n")
38  for inherits,file in info:
39  print(" ",file, inherits)
40  return (None,None)
41  inherits,file = info[0]
42  if -1 != inherits.find("edm::EDProducer"):
43  type = kProducer
44  elif -1 != inherits.find("edm::EDFilter"):
45  type = kFilter
46  else:
47  print("ERROR: class '"+name+"' does not directly inherit from EDProducer or EDFilter\n "+inherits)
48  return (None,None)
49  return (file,type)
50  print("ERROR: did not find a standard class declaration for '"+name+"'")
51  try:
52  found = subprocess.check_output(["git","grep", "class *"+name+" *:"])
53  print_lines( found.split("\n") )
54  except:
55  try:
56  found = subprocess.check_output(["git","grep", "typedef *.* "+name])
57  print_lines( found.split("\n") )
58  except:
59  pass
60  return (None,None)
61 
62 def checkout_package(fileName):
63  c = fileName.split("/")
64  print("checking out "+c[0]+"/"+c[1])
65  sparce_checkout = ".git/info/sparse-checkout"
66  f = open(sparce_checkout,"r")
67  linesInSparse = set(f.readlines())
68  f.close()
69  linesInSparse.add(c[0]+"/"+c[1]+"\n")
70 
71  f = open(sparce_checkout+"_new","w")
72  for l in linesInSparse:
73  if l:
74  f.write(l)
75  f.close()
76  shutil.move(sparce_checkout,sparce_checkout+"_old")
77  shutil.move(sparce_checkout+"_new",sparce_checkout)
78  subprocess.call(["git","read-tree","-mu","HEAD"])
79 
80 def edit_file(fileName,moduleType,moduleName):
81  print(" editting "+fileName)
82  fOld = open(fileName)
83  fNew = open(fileName+"_NEW","w")
84 
85  lookingForChanges = True
86  addedInclude = False
87  for l in fOld.readlines():
88  if lookingForChanges:
89  if -1 != l.find("#include"):
90  if moduleType == kProducer:
91  if -1 != l.find("FWCore/Framework/interface/EDProducer.h"):
92  l='#include "FWCore/Framework/interface/stream/EDProducer.h"\n'
93  addedInclude = True
94  elif moduleType == kFilter:
95  if -1 != l.find("FWCore/Framework/interface/EDFilter.h"):
96  l = '#include "FWCore/Framework/interface/stream/EDFilter.h"\n'
97  addedInclude = True
98  elif -1 != l.find("class"):
99  if -1 != l.find(moduleName):
100  if moduleType == kProducer:
101  if -1 != l.find("edm::EDProducer"):
102  l = l.replace("edm::EDProducer","edm::stream::EDProducer<>")
103  lookingForChanges = False
104  elif moduleType == kFilter:
105  if -1 != l.find("edm::EDFilter"):
106  l=l.replace("edm::EDFilter","edm::stream::EDFilter<>")
107  fNew.write(l)
108  if -1 != l.find(" beginJob("):
109  print(" WARNING: beginJob found but not supported by stream")
110  print(" ",l)
111  if -1 != l.find(" endJob("):
112  print(" WARNING: endJob found but not supported by stream")
113  print(" ",l)
114  if not addedInclude:
115  print(" WARNING: did not write include into "+fileName)
116  fNew.close()
117  fOld.close()
118  shutil.move(fileName,fileName+"_OLD")
119  shutil.move(fileName+"_NEW",fileName)
120 
121 modules = sys.argv[1:]
122 
123 print("getting info")
124 all_mods_info= find_all_module_classes()
125 
126 
127 for m in modules:
128  f,t = find_file_for_module(m,all_mods_info)
129  if f:
131  edit_file(f,t,m)
132 
digitizers_cfi.strip
strip
Definition: digitizers_cfi.py:19
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
edmConvertToStreamModule.edit_file
def edit_file(fileName, moduleType, moduleName)
Definition: edmConvertToStreamModule.py:80
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
edmConvertToStreamModule.checkout_package
def checkout_package(fileName)
Definition: edmConvertToStreamModule.py:62
mps_setup.append
append
Definition: mps_setup.py:85
edmConvertToStreamModule.find_file_for_module
def find_file_for_module(name, all_modules)
Definition: edmConvertToStreamModule.py:33
edmConvertToStreamModule.print_lines
def print_lines(lines)
Definition: edmConvertToStreamModule.py:28
edmConvertToStreamModule.find_all_module_classes
def find_all_module_classes()
Definition: edmConvertToStreamModule.py:12