All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
restructuring.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # This script changes C++ code, CMake files and FHiCL configuration.
4 #
5 # The fundamental patterns are set into a table.
6 # Additional substitutions are possible (it's a Python script after all...)
7 #
8 #
9 # Change log:
10 # 20200714 (petrillo@slac.stanford.edu)
11 # original version
12 #
13 
14 import sys, os, re
15 
16 import SerialSubstitution # from 'larsoft'
17 from SerialSubstitution import AddProcessor, RunSubstitutor
18 
19 
20 # main substitution table
21 # (in Python 3.7 this is supposed to be the stored order as well):
22 Substitutions = {
23 
24  # ----------------------------------------------------------------------------
25  'TriggerDataObjects': {
26  'source': 'icaruscode/PMT/Trigger/Data',
27  'dest': 'sbnobj/ICARUS/PMT/Trigger/Data',
28  }, # 'TriggerDataObjects'
29  # ----------------------------------------------------------------------------
30 
31  # ----------------------------------------------------------------------------
32  'SBNDCRTObjects': {
33  'source': 'sbndcode/CRT/CRTProducts',
34  'dest': 'sbnobj/SBND/CRT',
35  'headers': [ 'CRTData.hh', ],
36  'namespaces': [ ( 'crt', 'sbnd::crt', ( 'CRTData', ) ), ],
37  'plugins': [ ], # plugins (including the plain library) will require manual intervention
38  }, # 'SBNDCRTObjects'
39  # ----------------------------------------------------------------------------
40 
41  # ----------------------------------------------------------------------------
42  'CommonCRTObjectsForSBND': {
43  'source': 'sbndcode/CRT/CRTProducts',
44  'dest': 'sbnobj/Common/CRT',
45  'headers': [ 'CRTHit.hh', 'CRTTrack.hh', 'CRTTzero.hh', ],
46  'namespaces': [
47  ( 'crt', 'sbn::crt', ( 'CRTHit', 'CRTTrack', 'CRTTzero', ) ),
48  ( 'sbnd::crt', 'sbn::crt', ( 'CRTHit', 'CRTTrack', 'CRTTzero', ) ),
49  ],
50  }, # 'CommonCRTObjectsForSBND'
51  # ----------------------------------------------------------------------------
52 
53  # ----------------------------------------------------------------------------
54  'ICARUSCRTObjects': {
55  'source': 'icaruscode/CRT/CRTProducts',
56  'dest': 'sbnobj/ICARUS/CRT',
57  'headers': [ 'CRTData.hh', ],
58  'plugins': [ ], # plugins (including the plain library) will require manual intervention
59  }, # 'ICARUSCRTObjects'
60  # ----------------------------------------------------------------------------
61 
62  # ----------------------------------------------------------------------------
63  'CommonCRTObjectsForICARUS': {
64  'source': 'icaruscode/CRT/CRTProducts',
65  'dest': 'sbnobj/Common/CRT',
66  'headers': [ 'CRTHit.hh', 'CRTTrack.hh', 'CRTTzero.hh', ],
67  'namespaces': [
68  ( 'icarus::crt', 'sbn::crt', ( 'CRTHit', 'CRTTrack', 'CRTTzero', ) ),
69  ],
70  }, # 'CommonCRTObjectsForICARUS'
71  # ----------------------------------------------------------------------------
72 
73  # ----------------------------------------------------------------------------
74  'ICARUSPurityObjects': {
75  'source': 'icaruscode/IcarusObj',
76  'dest': 'sbnobj/Common/Analysis',
77  }, # 'ICARUSPurityObjects'
78  # ----------------------------------------------------------------------------
79 
80 } # Substitutions
81 
82 
83 ################################################################################
84 def intoLibraryPrefix(path): return path.replace('/', '_')
85 
86 def intoHeaderGuardPrefix(path): return path.replace('/', '_').replace('.', '_').upper()
87 
88 
89 ################################################################################
90 if __name__ == "__main__":
91 
92  #############################################################################
93  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
94  # FHiCL configuration
95  #
97 
98  Subst.AddFileType("fcl")
99 
100  for rule in Substitutions.values():
101 
102  # this is for the rare cases where a plugin is specified by full path, e.g.
103  # module_type: "icaruscode/Analysis/AnalysisTree";
104 
105  # if we have a list of plugins, we translate only those
106  plugins = rule.get('plugins', [ None, ])
107 
108  baseSrc = rule['source']
109  baseDest = rule['dest']
110 
111  for plugin in plugins:
112  Subst.AddSimplePattern(
113  baseSrc + "_" + plugin if plugin else baseSrc,
114  baseDest + "_" + plugin if plugin else baseDest,
115  )
116  # for
117 
118  # for
119 
120 
121  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
122  # CMakeLists.txt
123  #
125 
126  Subst.AddFileNamePattern("CMakeLists.txt")
127 
128  # here we parse the substitution table
129  for rule in Substitutions.values():
130 
131  # this is for the modules and libraries; for example, a replacement like
132  # 'larsim/LArG4' into 'larsim/LegacyLArG4' may yield
133  # changes like 'larsim_LArG4_LArG4_module' to 'larsim_LegacyLArG4_LArG4_module'
134  # and 'larsim_LArG4' to 'larsim_LegacyLArG4':
135  if 'plugins' in rule:
136  # if we have a list of plugins, we translate only those
137  baseSrc = intoLibraryPrefix(rule['source'])
138  baseDest = intoLibraryPrefix(rule['dest'])
139 
140  for plugin in rule['plugins']:
141  Subst.AddSimplePattern(
142  baseSrc + "_" + plugin if plugin else baseSrc,
143  baseDest + "_" + plugin if plugin else baseDest,
144  )
145  # for
146  else:
147  # no plugin list: we translate all the possible libraries
148  Subst.AddSimplePattern(
149  intoLibraryPrefix(rule['source']),
150  intoLibraryPrefix(rule['dest'])
151  )
152  # if ... else
153 
154  # for substitution table rules
155 
156  # special rule: CRTProducts libraries had non-standard name sbndcode_CRTProducts
157  Subst.AddWord("sbndcode_CRTProducts", "sbnobj_Common_CRT")
158  Subst.AddWord("icaruscode_CRTProducts", "sbnobj_Common_CRT")
159 
160 
161  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
162  # C++ source code (including modules and services)
163  #
165 
166  Subst.AddFileType("h", "hh", "cc", "cpp", "cxx", "icc", "tcc" )
167 
168  for rule in Substitutions.values():
169 
170  headers = rule.get('headers', [ '' ])
171 
172  for header in headers:
173 
174  baseSrc = os.path.join(rule['source'], header)
175  baseDest = os.path.join(rule['dest'], header)
176 
177  # this is for the paths, including the ones in the '#include' directives
178  # and the '@file' Doxygen documentation
179  (Subst.AddWord if header else Subst.AddSimplePattern)(
180  baseSrc,
181  baseDest
182  )
183 
184  # this is for standard header guard macros; for example, a replacement like
185  # 'larsim/LArG4' into 'larsim/LegacyLArG4' may yield changes like
186  # '#define LARSIM_LARG4_PARTICLELIST_H' into '#define LARSIM_LEGACYLARG4_PARTICLELIST_H'
187  Subst.AddSimplePattern(
188  intoHeaderGuardPrefix(baseSrc),
189  intoHeaderGuardPrefix(baseDest)
190  )
191  # for header
192 
193 
194  for nsSpec in rule.get('namespaces', []):
195 
196  try: (srcNS, destNS), classes = nsSpec, [ None, ]
197  except ValueError: srcNS, destNS, classes = nsSpec
198 
199  for className in classes:
200  srcClass = "::".join((srcNS, className)) if className else (srcNS + "::")
201  destClass = "::".join((destNS, className)) if className else (destNS + "::")
202  Subst.AddRegExPattern(r'([^:]|^)\b{}\b'.format(srcClass), r'\1{}'.format(destClass))
203  # for class name
204 
205  # for namespaces
206 
207 
208  # for substitution table rules
209 
210  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
211  #############################################################################
212 
213  sys.exit(RunSubstitutor())
214 #
static std::string format(PyObject *obj, unsigned int pos, unsigned int indent, unsigned int maxlen, unsigned int depth)
Definition: fclmodule.cxx:374
def intoHeaderGuardPrefix
S join(S const &sep, Coll const &s)
Returns a concatenation of strings in s separated by sep.