Belle II Software  light-2403-persian
doxygen.py
1 #!/usr/bin/env python
2 
3 
10 
11 # Custom builder for the module IO images.
12 
13 import os
14 import re
15 import subprocess
16 from pathlib import Path
17 from SCons.Builder import Builder
18 
19 # module in b2modmap file
20 module_re = \
21  re.compile(r'^REG_MODULE\‍((\S*)\‍)$', re.M)
22 
23 
24 def module_io_emitter(target, source, env):
25  target = []
26  if not env.get('HAS_DOT', False):
27  return (target, source)
28  for source_file in source:
29  contents = source_file.get_text_contents()
30  for entry in module_re.findall(contents):
31  target.append(os.path.join('build', 'module_io', entry + '.png'))
32  return (target, source)
33 
34 
35 def module_io(target, source, env):
36  for target_file in target:
37  # make sure the target exists even if the plot creation fails
38  Path(str(target_file)).touch()
39 
40  dir = os.path.dirname(str(target_file))
41  module = os.path.splitext(os.path.basename(str(target_file)))[0]
42  if module in ['EclDisplay', 'Rbuf2Ds', 'FastRbuf2Ds', 'Rbuf2Rbuf', 'Ds2Raw']:
43  return None
44 
45  try:
46  subprocess.run(['basf2', '--module-io', module],
47  stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, cwd=dir, timeout=60)
48  except Exception:
49  continue
50  try:
51  subprocess.run(['dot', module + '.dot', '-Tpng', '-o', module + '.png'],
52  stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, cwd=dir, timeout=60)
53  except Exception:
54  pass
55 
56  return None
57 
58 
59 def doxygen_groups_emitter(target, source, env):
60  return (['Doxygen.h'], [])
61 
62 
63 def doxygen_groups(target, source, env):
64  groups_file = open(str(target[0]), 'w')
65  groups_file.write("""
66 /** @defgroup DataObjects Data objects
67  */
68 
69 /** @defgroup Modules Modules
70  */
71 
72 /** @defgroup Packages Packages
73  */
74 
75 """)
76 
77  for package in env['AVAILABLE_PACKAGES']:
78  groups_file.write(f"""
79 /** @defgroup {package} {package}
80  * @ingroup Packages
81  */
82 
83 /** @defgroup {package}_dataobjects {package} data objects
84  * @ingroup {package}
85  * @ingroup DataObjects
86  */
87 
88 /** @defgroup {package}_modules {package} modules
89  * @ingroup {package}
90  * @ingroup Modules
91  */
92 
93  """)
94  groups_file.close()
95 
96 
97 def doxyfile(target, source, env):
98  content = source[0].get_text_contents().replace('BELLE2_RELEASE', env.GetOption('doxygen'))
99  if env.get('HAS_DOT', False):
100  content = content.replace('HAVE_DOT = NO', 'HAVE_DOT = YES')
101  target_file = open(str(target[0]), 'w')
102  target_file.write(content)
103  target_file.close()
104 
105 
106 # define builder for module-io plots
107 moduleio = Builder(action=module_io, emitter=module_io_emitter)
108 moduleio.action.cmdstr = '${MODULEIOCOMSTR}'
109 
110 # define builder for doxygen
111 doxygen = Builder(action=f'doxygen $SOURCE 2>&1 > build/doxygen.log | sed "s;^{os.environ.get("BELLE2_LOCAL_DIR", "")}/;;g" 1>&2',
112  emitter=lambda target, source, env: (['build/doxygen/html/index.html'], source))
113 
114 
115 def generate(env):
116  env['BUILDERS']['ModuleIo'] = moduleio
117  env['BUILDERS']['DoxygenGroups'] = Builder(action=doxygen_groups, emitter=doxygen_groups_emitter)
118  env['BUILDERS']['Doxyfile'] = Builder(action=doxyfile)
119  env['BUILDERS']['Doxygen'] = doxygen
120  for builder in ['DoxygenGroups', 'Doxyfile', 'Doxygen']:
121  env['BUILDERS'][builder].action.cmdstr = '${DOXYGENCOMSTR}'
122 
123 
124 def exists(env):
125  return True