236 msg=None):
237 """API for the sg-dump script.
238 `files` a list of input filenames to be dumped by SgDump
239 `output` the name of the output (ASCII) file
240 `nevts` the number of events to dump (default: -1 ie all)
241 `skip` the number of events to skip at the start (default: 0)
242 `dump_jobo` switch to store or not the automatically generated jobo (put
243 the name of the jobo output name in there if you want to keep
244 it)
245 `pyalg_cls` the fully qualified name of the PyAthena.Alg class to process the file(s) content (PySgDumper or DataProxyLoader)
246 `include`: comma-separates list of type#key container names to dump.
247 `exclude`: comma-separated list of glob patterns for keys/types to ignore.
248 `do_clean_up` flag to enable the attempt at removing all the files sg-dump
249 produces during the course of its execution
250 `athena_opts` a space-separated list of athena command-line options (e.g '--perfmon --stdcmalloc --nprocs=-1')
251 `conditions_tag` force a specific global conditions tag
252 `msg` a logging.Logger instance
253
254 returns the exit code of the sub-athena process
255 """
256
257 if msg is None:
258 import PyUtils.Logging as L
259 msg = L.logging.getLogger('sg-dumper')
260 msg.setLevel(L.logging.INFO)
261
262 if isinstance(files, str):
263 files = files.split()
264
265 if not isinstance(files, (list,tuple)):
266 err = "'files' needs to be a list (or tuple) of file names"
267 msg.error(err)
268 raise TypeError(err)
269
270 if not isinstance(output, str):
271 err = "'output' needs to be a filename"
272 msg.error(err)
273 raise TypeError(err)
274
275 _allowed_values = ('PyDumper.PyComps:PySgDumper',
276 'PyDumper.PyComps:DataProxyLoader')
277 if not (pyalg_cls in _allowed_values):
278 err = "'pyalg_cls' allowed values are: %s. got: [%s]" % (
279 _allowed_values,
280 pyalg_cls)
281 msg.error(err)
282 raise ValueError(err)
283 pyalg_pkg,pyalg_cls = pyalg_cls.split(':')
284
285 conditions_tag_frag = ''
286 if conditions_tag:
287 conditions_tag_frag = "conddb.setGlobalTag('%s')" % conditions_tag
288 jobo = _gen_jobo({
289 'ofile-name' : output,
290 'input-files': files,
291 'evts' : nevts,
292 'skip' : skip,
293 'include' : include,
294 'exclude' : exclude,
295 'pyalg_pkg': pyalg_pkg,
296 'pyalg_cls': pyalg_cls,
297 'conditions_tag_frag' : conditions_tag_frag,
298 })
299
300 msg.info(':'*40)
301 msg.info('input files: %s', files)
302 msg.info('events: %s', nevts)
303 msg.info('skip: %s', skip)
304 msg.info('out (ascii): %s', output)
305 msg.info('pyalg-class: %s:%s', pyalg_pkg, pyalg_cls)
306 msg.info('include: %s', include)
307 msg.info('exclude: %s', exclude)
308 msg.info('conditions_tag: %s', conditions_tag)
309
310 if dump_jobo and isinstance(dump_jobo, str):
311 try:
312 with open(dump_jobo, 'w') as f:
313 f.write(jobo)
314 except Exception as err:
315 msg.warning('problem while dumping joboption file to [%s]:\n%s',
316 dump_jobo, err)
317
318 from collections import namedtuple
319 Options = namedtuple('Options',
320 'oname do_clean_up athena_opts full_log')
321 opts = Options(oname=output,
322 do_clean_up=do_clean_up,
323 full_log=full_log,
324 athena_opts=athena_opts)
325
326 sc, out = 1, "<N/A>"
327 msg.info('running dumper...')
328 sc,out = _run_jobo(jobo, msg, opts)
329 msg.info('dumper done')
330 if output != os.devnull:
331 msg.info('writing logfile: %s.log', output)
332 try:
333 with open('%s.log'%output, 'w') as f:
334 for l in out.splitlines():
335 print (l, file=f)
336 print ("### EOF ###", file=f)
337
338 except Exception as err:
339 msg.warning('problem writing out logfile [%s.log]:\n%s',
340 output, err)
341
342 msg.info('bye.')
343 msg.info(':'*40)
344 return sc, out