From eb87e3e4cf0380fa596813e7fbd4180a3ca6bca3 Mon Sep 17 00:00:00 2001 From: scott snyder <snyder@bnl.gov> Date: Wed, 22 Jul 2020 16:20:05 +0200 Subject: [PATCH] PyDumper: python 3 fixes Get sg-dump.py utility working with python 3. --- Event/PyDumper/bin/sg-dump.py | 6 ++++-- Event/PyDumper/python/PyComps.py | 14 +++++++------- Event/PyDumper/python/SgDumpLib.py | 28 ++++++++++++++++------------ 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/Event/PyDumper/bin/sg-dump.py b/Event/PyDumper/bin/sg-dump.py index f0736d745b8..72668236918 100755 --- a/Event/PyDumper/bin/sg-dump.py +++ b/Event/PyDumper/bin/sg-dump.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration +# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration # @file: PyDumper/bin/sg-dump.py # @purpose: a simple python script to run pyathena and use PySgDumper to dump # a (set of) event(s) from a POOL (esd/aod) file into an ASCII file @@ -154,6 +154,8 @@ if __name__ == "__main__": athena_opts=options.athena_opts, msg=msg ) - except Exception,err: + except Exception as err: msg.error('problem while running sg-dump:\n%s', err) + import traceback + traceback.print_exc() sys.exit(sc) diff --git a/Event/PyDumper/python/PyComps.py b/Event/PyDumper/python/PyComps.py index 775bb2a378a..e13eedff6ee 100644 --- a/Event/PyDumper/python/PyComps.py +++ b/Event/PyDumper/python/PyComps.py @@ -1,4 +1,4 @@ -# Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration +# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration # @file: PyDumper/python/PyComps.py # @purpose: A set of PyAthena components to test reading/writing EDM classes @@ -156,7 +156,7 @@ class PyReader (PyAthena.Alg): str(self.ofile), type(self.ofile)) return StatusCode.Failure - from Dumpers import get_dumper_fct + from PyDuper.Dumpers import get_dumper_fct try: cont_type = getattr(PyAthena, self.cont_type) except AttributeError as err: @@ -268,7 +268,7 @@ class PySgDumper (PyAthena.Alg): str(self.ofile), type(self.ofile)) return StatusCode.Failure - from Dumpers import get_dumper_fct + from PyDumper.Dumpers import get_dumper_fct self._dumper_fct = get_dumper_fct if hasattr(self.ofile, 'name'): @@ -319,9 +319,9 @@ class PySgDumper (PyAthena.Alg): n, clid) _add_fail ((n,clid,'no typename from clid')) - items = [(i[1], i[0]) for i in sg.iteritems() if i[1] != "EventInfo"] + items = [(i[1], i[0]) for i in sg.items() if i[1] != "EventInfo"] items.sort() - evt_info = [(clid, key) for key,clid in sg.iteritems() if clid=="EventInfo"] + evt_info = [(clid, key) for key,clid in sg.items() if clid=="EventInfo"] if len(evt_info)==1: items.insert (0, evt_info[0]) else: @@ -394,7 +394,7 @@ class PySgDumper (PyAthena.Alg): reasons = defaultdict(list) for name,klass,reason in self.failed_dumps: reasons[reason].append ((name,klass)) - for reason in reasons.iterkeys(): + for reason in reasons.keys(): _info (' ==> [%s]', reason) for name,klass in reasons[reason]: _info (" [%s#%s]", klass, name) @@ -490,7 +490,7 @@ class DataProxyLoader(PyAthena.Alg): reasons = defaultdict(list) for name,klass,reason in self.failed_dumps: reasons[reason].append ((name,klass)) - for reason in reasons.iterkeys(): + for reason in reasons.keys(): _info (' ==> [%s]', reason) for name,klass in reasons[reason]: _info (" [%s#%s]", klass, name) diff --git a/Event/PyDumper/python/SgDumpLib.py b/Event/PyDumper/python/SgDumpLib.py index efcce5bbb85..e1380d81380 100644 --- a/Event/PyDumper/python/SgDumpLib.py +++ b/Event/PyDumper/python/SgDumpLib.py @@ -9,6 +9,9 @@ from __future__ import with_statement, print_function import sys import os +from future import standard_library +standard_library.install_aliases() + __doc__ = """\ API for the sg-dump script (which dumps an ASCII representation of events in POOL or RAW files @@ -22,7 +25,7 @@ __all__ = [ def _make_jobo(job): import tempfile - jobo = tempfile.NamedTemporaryFile(suffix='-jobo.py') + jobo = tempfile.NamedTemporaryFile(suffix='-jobo.py', mode='w+') import textwrap job = textwrap.dedent (job) jobo.writelines([l+os.linesep for l in job.splitlines()]) @@ -261,8 +264,8 @@ def _run_jobo(job, msg, options): if options.do_clean_up: atexit.register (_cleanup, keep_files) - import commands - sc,out = commands.getstatusoutput ('which athena.py') + import subprocess + sc,out = subprocess.getstatusoutput ('which athena.py') if sc != 0: msg.error("could not locate 'athena.py':\n%s", out) return sc, out @@ -270,13 +273,13 @@ def _run_jobo(job, msg, options): jobo = _make_jobo(job) if options.use_recex_links: - sc,out = commands.getstatusoutput ('RecExCommon_links.sh') + sc,out = subprocess.getstatusoutput ('RecExCommon_links.sh') if sc != 0: msg.error("could not run 'RecExCommon_links.sh':\n%s"%out) return sc, out msg.info ('installed RecExCommon links') - sc,out = commands.getstatusoutput ('which sh') + sc,out = subprocess.getstatusoutput ('which sh') if sc != 0: msg.error("could not locate 'sh':\n%s",out) return sc, out @@ -285,7 +288,8 @@ def _run_jobo(job, msg, options): from time import time logfile = tempfile.NamedTemporaryFile(prefix='sg_dumper_job_', suffix='.logfile.txt', - dir=os.getcwd()) + dir=os.getcwd(), + mode = 'w+') # do not require $HOME to be available for ROOT # see bug #82096 @@ -308,7 +312,7 @@ def _run_jobo(job, msg, options): import time, re pat = re.compile (r'^Py:pyalg .*') evt_pat = re.compile ( - r'^Py:pyalg .*? ==> processing event [[](?P<evtnbr>\d*?)[]].*' + r'^Py:pyalg .*? ==> processing event \[(?P<evtnbr>\d*?)\].*' ) def _monitor(pos): logfile.flush() @@ -342,7 +346,7 @@ def _run_jobo(job, msg, options): if sc != 0: logfile.seek(0) msg.error ('='*80) - from cStringIO import StringIO + from io import StringIO err = StringIO() for l in logfile: print (l, end='') @@ -352,7 +356,7 @@ def _run_jobo(job, msg, options): return sc, err.getvalue() logfile.seek (0) - from cStringIO import StringIO + from io import StringIO out = StringIO() for l in logfile: if pat.match(l): @@ -395,7 +399,7 @@ def run_sg_dump(files, output, msg = L.logging.getLogger('sg-dumper') msg.setLevel(L.logging.INFO) - if isinstance(files, basestring): + if isinstance(files, str): files = files.split() if not isinstance(files, (list,tuple)): @@ -403,7 +407,7 @@ def run_sg_dump(files, output, msg.error(err) raise TypeError(err) - if not isinstance(output, basestring): + if not isinstance(output, str): err = "'output' needs to be a filename" msg.error(err) raise TypeError(err) @@ -450,7 +454,7 @@ def run_sg_dump(files, output, msg.info('file_type: %s', file_type) msg.info('exclude: %s', exclude) - if dump_jobo and isinstance(dump_jobo, basestring): + if dump_jobo and isinstance(dump_jobo, str): try: with open(dump_jobo, 'w') as f: f.write(jobo) -- GitLab