170 from optparse import OptionParser
171
172 parser = OptionParser(
173 prog=os.path.basename(sys.argv[0]),
174 usage="%prog [options] <PackageName> [<Module1> ...]",
175 )
176 parser.add_option(
177 "-o",
178 "--output",
179 action="store",
180 type="string",
181 help="output file for confDb data [default = '../genConfDir/<PackageName>_user_confDb.py'].",
182 )
183 parser.add_option(
184 "-r",
185 "--root",
186 action="store",
187 type="string",
188 help="root directory of the python modules [default = '../python'].",
189 )
190 parser.add_option(
191 "-v", "--verbose", action="store_true", help="print some debugging information"
192 )
193 parser.add_option(
194 "--debug", action="store_true", help="print more debugging information"
195 )
196 parser.add_option(
197 "--build-dir",
198 action="store",
199 help="build directory where to look for .confdb files (search all subdirectories)",
200 )
201 parser.add_option(
202 "--project-name",
203 action="store",
204 help="name of the current project (used to exclude spurious versions of the .confdb file for the current project)",
205 )
206 parser.set_defaults(root=os.path.join("..", "python"))
207
208 opts, args = parser.parse_args()
209
210 if opts.debug:
211 log_level = logging.DEBUG
212 elif opts.verbose:
213 log_level = logging.VERBOSE
214 else:
215 log_level = logging.INFO if os.environ.get("VERBOSE") else logging.WARNING
216 logging.basicConfig(
217 format="%(levelname)s: %(message)s", stream=sys.stdout, level=log_level
218 )
219
220 if len(args) < 1:
221 parser.error("PackageName is required")
222
223 package_name = args.pop(0)
224
225 usingConvention = False
226 if not args:
227
228 args = [package_name + ".Configuration"]
229 usingConvention = True
230
231 genConfDir = os.path.join("..", os.environ.get("CMTCONFIG", ""), "genConfDir")
232 if not os.path.exists(genConfDir):
233 genConfDir = os.path.join("..", "genConfDir")
234
235 if not opts.output:
236 outputfile = os.path.join(genConfDir, package_name + "_user.confdb")
237 else:
238 outputfile = opts.output
239
240
241
242 try:
243 import Gaudi.Configurables
244
245 Gaudi.Configurables.ignoreMissingConfigurables = True
246 except ImportError:
247 pass
248
249 loadConfigurableDb(opts.build_dir, opts.project_name)
250
251 try:
252
253
254 sys.path.insert(0, genConfDir)
255 sys.path.insert(0, os.path.join("..", "python"))
256 localConfDb = os.path.join(genConfDir, package_name, package_name + ".confdb")
257 if os.path.exists(localConfDb):
258 cfgDb._loadModule(localConfDb)
259
260 package_module = __import__(package_name)
261 package_module.__path__.insert(0, os.path.join(genConfDir, package_name))
262 except Exception:
263 pass
264
265
266 cus = {}
267 for mod in args:
268 lst = None
269 try:
270 lst = getConfigurableUsers(mod, root=opts.root, mayNotExist=usingConvention)
271 except ImportError:
272 import traceback
273
274 logging.error(
275 "Cannot import module %r:\n%s", mod, traceback.format_exc().rstrip()
276 )
277 return 2
278 if lst:
279 cus[mod] = lst
280
281 for m in lst:
282 cfgDb.add(configurable=m, package="None", module="None", lib="None")
283 elif not usingConvention:
284 logging.warning(
285 "Specified module %r does not contain ConfigurableUser specializations",
286 mod,
287 )
288
289 if cus:
290 logging.info("ConfigurableUser found:\n%s", pformat(cus))
291
292 output = f"""## -*- ascii -*-
293# db file automatically generated by {parser.prog} on: {time.asctime()}
294"""
295
296 for mod in cus:
297 for cu in cus[mod]:
298 output +=
"{} {} {}\n".
format(mod,
"None", cu)
299
300
301 output += f"## {package_name}\n"
302 elif usingConvention:
303 logging.info("No ConfigurableUser found")
304 output = (
305 f"# db file automatically generated by {parser.prog} on: {time.asctime()}\n"
306 f"# No ConfigurableUser specialization in {package_name}\n"
307 )
308 else:
309 logging.error("No ConfigurableUser specialization found")
310 return 1
311
312
313 output_dir = os.path.dirname(outputfile)
314 try:
315 logging.info("Creating directory %r", output_dir)
316 os.makedirs(output_dir, 0o755)
317 except OSError as err:
318 import errno
319
320 if err.errno == errno.EEXIST:
321
322 pass
323 else:
324 raise
325
326
327 logging.verbose("Writing confDb data to %r", outputfile)
328 open(outputfile, "w").write(output)
329 return 0
330
331
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".