From fd3b732f714da07d5b5326489b6e4aec1bc4fb00 Mon Sep 17 00:00:00 2001 From: John Kacur Date: Tue, 8 May 2018 23:29:25 +0100 Subject: [PATCH 18/18] rteval: 2to3 transformations Applied a series of fixes from the 2to3 tool, as well as a few of my own tweaks Signed-off-by: John Kacur --- rteval-cmd | 14 +++--- rteval/Log.py | 12 ++--- rteval/__init__.py | 52 ++++++++++----------- rteval/misc.py | 8 ++-- rteval/modules/__init__.py | 20 ++++---- rteval/modules/loads/hackbench.py | 6 +-- rteval/modules/loads/kcompile.py | 8 ++-- rteval/modules/measurement/__init__.py | 2 +- rteval/modules/measurement/cyclictest.py | 16 +++---- rteval/modules/measurement/sysstat.py | 4 +- rteval/rtevalConfig.py | 50 ++++++++++---------- rteval/rtevalMailer.py | 6 +-- rteval/rtevalReport.py | 8 ++-- rteval/rtevalXMLRPC.py | 18 ++++---- rteval/rtevalclient.py | 12 ++--- rteval/sysinfo/__init__.py | 48 ++++++++++---------- rteval/sysinfo/cputopology.py | 12 ++--- rteval/sysinfo/dmi.py | 12 ++--- rteval/sysinfo/kernel.py | 10 ++-- rteval/sysinfo/memory.py | 12 ++--- rteval/sysinfo/network.py | 4 +- rteval/sysinfo/osinfo.py | 20 ++++---- rteval/sysinfo/services.py | 6 +-- rteval/sysinfo/tools.py | 4 +- rteval/systopology.py | 28 ++++++------ rteval/xmlout.py | 76 +++++++++++++++---------------- setup.py | 4 +- 34 files changed, 329 insertions(+), 329 deletions(-) diff --git a/rteval-cmd b/rteval-cmd index 3ae29dbd9fe3..739a929316a3 100755 --- a/rteval-cmd +++ b/rteval-cmd @@ -52,7 +52,7 @@ def summarize(repfile, xslt): try: t = tarfile.open(repfile) except: - print "Don't know how to summarize %s (tarfile open failed)" % repfile + print("Don't know how to summarize %s (tarfile open failed)" % repfile) return element = None for f in t.getnames(): @@ -60,7 +60,7 @@ def summarize(repfile, xslt): element = f break if element == None: - print "No summary.xml found in tar archive %s" % repfile + print("No summary.xml found in tar archive %s" % repfile) return tmp = tempfile.gettempdir() t.extract(element, path=tmp) @@ -83,7 +83,7 @@ def summarize(repfile, xslt): # Parse and print the report through the XSLT template - preserve proper encoding resdoc = xsltprs(xmldoc) - print unicode(resdoc).encode('UTF-8') + print(str(resdoc).encode('UTF-8')) # Clean up del resdoc @@ -162,7 +162,7 @@ def parse_options(cfg, parser, cmdargs): (cmd_opts, cmd_args) = parser.parse_args(args = cmdargs) if cmd_opts.rteval___version: - print("rteval version %s" % RTEVAL_VERSION) + print(("rteval version %s" % RTEVAL_VERSION)) sys.exit(0); if cmd_opts.rteval___duration: @@ -266,7 +266,7 @@ if __name__ == '__main__': # if --summarize was specified then just parse the XML, print it and exit if rtevcfg.summarize or rtevcfg.rawhistogram: if len(cmd_args) < 1: - raise RuntimeError, "Must specify at least one XML file with --summarize!" + raise RuntimeError("Must specify at least one XML file with --summarize!") for x in cmd_args: if rtevcfg.summarize: @@ -277,7 +277,7 @@ if __name__ == '__main__': sys.exit(0) if os.getuid() != 0: - print "Must be root to run rteval!" + print("Must be root to run rteval!") sys.exit(-1) logger.log(Log.DEBUG, '''rteval options: @@ -295,7 +295,7 @@ if __name__ == '__main__': rtevcfg.duration, rtevcfg.sysreport)) if not os.path.isdir(rtevcfg.workdir): - raise RuntimeError, "work directory %s does not exist" % rtevcfg.workdir + raise RuntimeError("work directory %s does not exist" % rtevcfg.workdir) rteval = RtEval(config, loadmods, measuremods, logger) diff --git a/rteval/Log.py b/rteval/Log.py index a41ef319be0f..66aa77a59431 100644 --- a/rteval/Log.py +++ b/rteval/Log.py @@ -80,26 +80,26 @@ def unit_test(rootdir): def run_log_test(l): for lt in range(min(logtypes), max(logtypes)*2): test = ", ".join([logtypes_s[logtypes.index(i)] for i in [p for p in takewhile(lambda x: x <= lt, (2**i for i in count())) if p & lt]]) - print "Testing verbosity flags set to: (%i) %s" % (lt, test) + print("Testing verbosity flags set to: (%i) %s" % (lt, test)) msg = "Log entry when verbosity is set to %i [%s]" % (lt, test) l.SetLogVerbosity(lt) test_log(l, msg) - print "-"*20 + print("-"*20) try: - print "** Testing stdout" + print("** Testing stdout") l = Log() run_log_test(l) - print "** Testing file logging - using test.log" + print("** Testing file logging - using test.log") l = Log("test.log") run_log_test(l) return 0 - except Exception, e: + except Exception as e: import traceback traceback.print_exc(file=sys.stdout) - print "** EXCEPTION %s", str(e) + print("** EXCEPTION %s", str(e)) return 1 diff --git a/rteval/__init__.py b/rteval/__init__.py index a7e44d9c41f1..176715f74ebd 100644 --- a/rteval/__init__.py +++ b/rteval/__init__.py @@ -34,13 +34,13 @@ __license__ = "GPLv2 License" import os, signal, sys, threading, time from datetime import datetime from distutils import sysconfig -from modules.loads import LoadModules -from modules.measurement import MeasurementModules, MeasurementProfile -from rtevalReport import rtevalReport -from rtevalXMLRPC import rtevalXMLRPC -from Log import Log -import rtevalConfig, rtevalMailer -import version +from .modules.loads import LoadModules +from .modules.measurement import MeasurementModules, MeasurementProfile +from .rtevalReport import rtevalReport +from .rtevalXMLRPC import rtevalXMLRPC +from .Log import Log +from . import rtevalConfig, rtevalMailer +from . import version RTEVAL_VERSION = version.RTEVAL_VERSION @@ -52,7 +52,7 @@ def sig_handler(signum, frame): if signum == signal.SIGINT or signum == signal.SIGTERM: global stopsig_received stopsig_received = True - print "*** stop signal received - stopping rteval run ***" + print("*** stop signal received - stopping rteval run ***") else: raise RuntimeError("SIGNAL received! (%d)" % signum) @@ -81,7 +81,7 @@ class RtEval(rtevalReport): self.__reportdir = None # Import SystemInfo here, to avoid DMI warnings if RtEval() is not used - from sysinfo import SystemInfo + from .sysinfo import SystemInfo self._sysinfo = SystemInfo(self.__rtevcfg, logger=self.__logger) # prepare a mailer, if that's configured @@ -105,11 +105,11 @@ class RtEval(rtevalReport): self.__xmlrpc = rtevalXMLRPC(self.__rtevcfg.xmlrpc, self.__logger, self.__mailer) if not self.__xmlrpc.Ping(): if not self.__rtevcfg.xmlrpc_noabort: - print "ERROR: Could not reach XML-RPC server '%s'. Aborting." % \ - self.__rtevcfg.xmlrpc + print("ERROR: Could not reach XML-RPC server '%s'. Aborting." % \ + self.__rtevcfg.xmlrpc) sys.exit(2) else: - print "WARNING: Could not ping the XML-RPC server. Will continue anyway." + print("WARNING: Could not ping the XML-RPC server. Will continue anyway.") else: self.__xmlrpc = None @@ -122,7 +122,7 @@ class RtEval(rtevalReport): if hours: r = r - (hours * 3600) minutes = r / 60 if minutes: r = r - (minutes * 60) - print "rteval time remaining: %d days, %d hours, %d minutes, %d seconds" % (days, hours, minutes, r) + print("rteval time remaining: %d days, %d hours, %d minutes, %d seconds" % (days, hours, minutes, r)) def Prepare(self, onlyload = False): @@ -135,7 +135,7 @@ class RtEval(rtevalReport): # or the loads logging is enabled if not onlyload or self.__rtevcfg.logging: self.__reportdir = self._make_report_dir(self.__rtevcfg.workdir, "summary.xml") - except Exception, e: + except Exception as e: raise RuntimeError("Cannot create report directory (NFS with rootsquash on?) [%s]", str(e)) self.__logger.log(Log.INFO, "Preparing load modules") @@ -173,23 +173,23 @@ class RtEval(rtevalReport): if with_loads: self._loadmods.Start() - print "rteval run on %s started at %s" % (os.uname()[2], time.asctime()) + print("rteval run on %s started at %s" % (os.uname()[2], time.asctime())) onlinecpus = self._sysinfo.cpu_getCores(True) cpulist = self._loadmods._cfg.GetSection("loads").cpulist if cpulist: - print "started %d loads on cores %s" % (self._loadmods.ModulesLoaded(), cpulist), + print("started %d loads on cores %s" % (self._loadmods.ModulesLoaded(), cpulist), end=' ') else: - print "started %d loads on %d cores" % (self._loadmods.ModulesLoaded(), onlinecpus), + print("started %d loads on %d cores" % (self._loadmods.ModulesLoaded(), onlinecpus), end=' ') if self._sysinfo.mem_get_numa_nodes() > 1: - print " with %d numa nodes" % self._sysinfo.mem_get_numa_nodes() + print(" with %d numa nodes" % self._sysinfo.mem_get_numa_nodes()) else: - print "" + print("") cpulist = self._measuremods._MeasurementModules__cfg.GetSection("measurement").cpulist if cpulist: - print "started measurement threads on cores %s" % cpulist + print("started measurement threads on cores %s" % cpulist) else: - print "started measurement threads on %d cores" % onlinecpus - print "Run duration: %s seconds" % str(self.__rtevcfg.duration) + print("started measurement threads on %d cores" % onlinecpus) + print("Run duration: %s seconds" % str(self.__rtevcfg.duration)) # start the cyclictest thread measure_profile.Start() @@ -220,7 +220,7 @@ class RtEval(rtevalReport): if with_loads: if len(threading.enumerate()) < nthreads: - raise RuntimeError, "load thread died!" + raise RuntimeError("load thread died!") if not load_avg_checked: self._loadmods.SaveLoadAvg() @@ -232,14 +232,14 @@ class RtEval(rtevalReport): left_to_run = stoptime - currtime self.__show_remaining_time(left_to_run) rpttime = currtime + report_interval - print "load average: %.2f" % self._loadmods.GetLoadAvg() + print("load average: %.2f" % self._loadmods.GetLoadAvg()) currtime = time.time() self.__logger.log(Log.DEBUG, "out of measurement loop") signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGTERM, signal.SIG_DFL) - except RuntimeError, e: + except RuntimeError as e: raise RuntimeError("appeared during measurement: %s" % e) finally: @@ -250,7 +250,7 @@ class RtEval(rtevalReport): if with_loads: self._loadmods.Stop() - print "stopping run at %s" % time.asctime() + print("stopping run at %s" % time.asctime()) # wait for measurement modules to finish calculating stats measure_profile.WaitForCompletion() diff --git a/rteval/misc.py b/rteval/misc.py index 0cfc69dfbd62..b9ff246e0f01 100644 --- a/rteval/misc.py +++ b/rteval/misc.py @@ -29,7 +29,7 @@ def expand_cpulist(cpulist): if '-' in part: a, b = part.split('-') a, b = int(a), int(b) - result.extend(range(a, b + 1)) + result.extend(list(range(a, b + 1))) else: a = int(part) result.append(a) @@ -61,9 +61,9 @@ def cpuinfo(): if __name__ == "__main__": info = cpuinfo() - idx = info.keys() + idx = list(info.keys()) idx.sort() for i in idx: - print "%s: %s" % (i, info[i]) + print("%s: %s" % (i, info[i])) - print "0: %s" % (info['0']['model name']) + print("0: %s" % (info['0']['model name'])) diff --git a/rteval/modules/__init__.py b/rteval/modules/__init__.py index b41653e62d09..0feb8a916179 100644 --- a/rteval/modules/__init__.py +++ b/rteval/modules/__init__.py @@ -211,7 +211,7 @@ class rtevalModulePrototype(threading.Thread): "Return libxml2.xmlNode object with the gathered timestamps" ts_n = libxml2.newNode("timestamps") - for k in self.__timestamps.keys(): + for k in list(self.__timestamps.keys()): ts_n.newChild(None, k, str(self.__timestamps[k])) return ts_n @@ -277,7 +277,7 @@ the information provided by the module""" metavar='LIST') parser.add_option_group(grparser) - for (modname, mod) in self.__modsloaded.items(): + for (modname, mod) in list(self.__modsloaded.items()): opts = mod.ModuleParameters() if len(opts) == 0: continue @@ -290,9 +290,9 @@ the information provided by the module""" cfg = None grparser = optparse.OptionGroup(parser, "Options for the %s module" % shortmod) - for (o, s) in opts.items(): - descr = s.has_key('descr') and s['descr'] or "" - metavar = s.has_key('metavar') and s['metavar'] or None + for (o, s) in list(opts.items()): + descr = 'descr' in s and s['descr'] or "" + metavar = 'metavar' in s and s['metavar'] or None try: default = cfg and getattr(cfg, o) or None @@ -301,7 +301,7 @@ the information provided by the module""" default = None if default is None: - default = s.has_key('default') and s['default'] or None + default = 'default' in s and s['default'] or None grparser.add_option('--%s-%s' % (shortmod, o), @@ -353,7 +353,7 @@ returned when a ModuleContainer object is iterated over""" def GetModulesList(self): "Returns a list of module names" - return self.__modobjects.keys() + return list(self.__modobjects.keys()) def GetNamedModuleObject(self, modname): @@ -364,11 +364,11 @@ returned when a ModuleContainer object is iterated over""" def __iter__(self): "Initiates the iterating process" - self.__iter_list = self.__modobjects.keys() + self.__iter_list = list(self.__modobjects.keys()) return self - def next(self): + def __next__(self): """Internal Python iterating method, returns the next module name and object to be processed""" @@ -511,7 +511,7 @@ start their workloads yet""" self._logger.log(Log.DEBUG, "\t - Stopping %s" % modname) if mod.is_alive(): mod.join(2.0) - except RuntimeError, e: + except RuntimeError as e: self._logger.log(Log.ERR, "\t\tFailed stopping %s: %s" % (modname, str(e))) self.__timestamps['stop'] = datetime.now() diff --git a/rteval/modules/loads/hackbench.py b/rteval/modules/loads/hackbench.py index 9d3f6c834356..4b0fc59c3518 100644 --- a/rteval/modules/loads/hackbench.py +++ b/rteval/modules/loads/hackbench.py @@ -128,7 +128,7 @@ class Hackbench(CommandLineLoad): stderr=self.__err) if not p: self._log(Log.DEBUG, "hackbench failed to start on node %s" % node) - raise RuntimeError, "hackbench failed to start on node %s" % node + raise RuntimeError("hackbench failed to start on node %s" % node) return p def _WorkloadTask(self): @@ -147,7 +147,7 @@ class Hackbench(CommandLineLoad): if self.tasks[n].poll() is not None: self.tasks[n].wait() self.tasks[n] = self.__starton(n) - except OSError, e: + except OSError as e: if e.errno != errno.ENOMEM: raise e # Exit gracefully without a traceback for out-of-memory errors @@ -166,7 +166,7 @@ class Hackbench(CommandLineLoad): return for node in self.nodes: - if self.tasks.has_key(node) and self.tasks[node].poll() is None: + if node in self.tasks and self.tasks[node].poll() is None: self._log(Log.INFO, "cleaning up hackbench on node %s" % node) self.tasks[node].send_signal(SIGKILL) if self.tasks[node].poll() == None: diff --git a/rteval/modules/loads/kcompile.py b/rteval/modules/loads/kcompile.py index ef636c235fb8..ca4932172b87 100644 --- a/rteval/modules/loads/kcompile.py +++ b/rteval/modules/loads/kcompile.py @@ -110,7 +110,7 @@ class Kcompile(CommandLineLoad): def _WorkloadSetup(self): # find our source tarball - if self._cfg.has_key('tarball'): + if 'tarball' in self._cfg: tarfile = os.path.join(self.srcdir, self._cfg.tarfile) if not os.path.exists(tarfile): raise rtevalRuntimeError(self, " tarfile %s does not exist!" % tarfile) @@ -176,7 +176,7 @@ class Kcompile(CommandLineLoad): stdin=null, stdout=out, stderr=err) if ret: raise rtevalRuntimeError(self, "kcompile setup failed: %d" % ret) - except KeyboardInterrupt, m: + except KeyboardInterrupt as m: self._log(Log.DEBUG, "keyboard interrupt, aborting") return self._log(Log.DEBUG, "ready to run") @@ -197,7 +197,7 @@ class Kcompile(CommandLineLoad): else: self.__outfd = self.__errfd = self.__nullfd - if self._cfg.has_key('cpulist') and self._cfg.cpulist: + if 'cpulist' in self._cfg and self._cfg.cpulist: cpulist = self._cfg.cpulist self.num_cpus = len(expand_cpulist(cpulist)) else: @@ -206,7 +206,7 @@ class Kcompile(CommandLineLoad): def _WorkloadTask(self): for n in self.topology: if not self.buildjobs[n]: - raise RuntimeError, "Build job not set up for node %d" % int(n) + raise RuntimeError("Build job not set up for node %d" % int(n)) if self.buildjobs[n].jobid is None or self.buildjobs[n].jobid.poll() is not None: self._log(Log.INFO, "Starting load on node %d" % n) self.buildjobs[n].run(self.__nullfd, self.__outfd, self.__errfd) diff --git a/rteval/modules/measurement/__init__.py b/rteval/modules/measurement/__init__.py index b382971052cf..5a179949ad36 100644 --- a/rteval/modules/measurement/__init__.py +++ b/rteval/modules/measurement/__init__.py @@ -204,7 +204,7 @@ measurement profiles, based on their characteristics""" return self - def next(self): + def __next__(self): """Internal Python iterating method, returns the next MeasurementProfile object to be processed""" diff --git a/rteval/modules/measurement/cyclictest.py b/rteval/modules/measurement/cyclictest.py index 04f78d1c9612..c992c0c9e4eb 100644 --- a/rteval/modules/measurement/cyclictest.py +++ b/rteval/modules/measurement/cyclictest.py @@ -87,7 +87,7 @@ class RunData(object): self._log(Log.INFO, "reducing %s" % self.__id) total = 0 - keys = self.__samples.keys() + keys = list(self.__samples.keys()) keys.sort() sorted = [] @@ -167,7 +167,7 @@ class RunData(object): hist_n = rep_n.newChild(None, 'histogram', None) hist_n.newProp('nbuckets', str(len(self.__samples))) - keys = self.__samples.keys() + keys = list(self.__samples.keys()) keys.sort() for k in keys: if self.__samples[k] == 0: @@ -255,7 +255,7 @@ class Cyclictest(rtevalModulePrototype): def _WorkloadPrepare(self): - self.__interval = self.__cfg.has_key('interval') and '-i%d' % int(self.__cfg.interval) or "" + self.__interval = 'interval' in self.__cfg and '-i%d' % int(self.__cfg.interval) or "" self.__cmd = ['cyclictest', self.__interval, @@ -269,10 +269,10 @@ class Cyclictest(rtevalModulePrototype): else: self.__cmd.append(self.__getmode()) - if self.__cfg.has_key('threads') and self.__cfg.threads: + if 'threads' in self.__cfg and self.__cfg.threads: self.__cmd.append("-t%d" % int(self.__cfg.threads)) - if self.__cfg.has_key('breaktrace') and self.__cfg.breaktrace: + if 'breaktrace' in self.__cfg and self.__cfg.breaktrace: self.__cmd.append("-b%d" % int(self.__cfg.breaktrace)) self.__cmd.append("--tracemark") self.__cmd.append("--notrace") @@ -290,7 +290,7 @@ class Cyclictest(rtevalModulePrototype): self.__nullfp = os.open('/dev/null', os.O_RDWR) debugdir = self.__get_debugfs_mount() - if self.__cfg.has_key('breaktrace') and self.__cfg.breaktrace and debugdir: + if 'breaktrace' in self.__cfg and self.__cfg.breaktrace and debugdir: # Ensure that the trace log is clean trace = os.path.join(debugdir, 'tracing', 'trace') fp = open(os.path.join(trace), "w") @@ -353,7 +353,7 @@ class Cyclictest(rtevalModulePrototype): self.__cyclicdata['system'].bucket(index, int(vals[i+1])) # generate statistics for each RunData object - for n in self.__cyclicdata.keys(): + for n in list(self.__cyclicdata.keys()): #print "reducing self.__cyclicdata[%s]" % n self.__cyclicdata[n].reduce() #print self.__cyclicdata[n] @@ -429,7 +429,7 @@ if __name__ == '__main__': cfg = rtevalConfig({}, logger=l) prms = {} modprms = ModuleParameters() - for c, p in modprms.items(): + for c, p in list(modprms.items()): prms[c] = p['default'] cfg.AppendConfig('cyclictest', prms) diff --git a/rteval/modules/measurement/sysstat.py b/rteval/modules/measurement/sysstat.py index 033e8825fa30..cbad928326dc 100644 --- a/rteval/modules/measurement/sysstat.py +++ b/rteval/modules/measurement/sysstat.py @@ -131,7 +131,7 @@ if __name__ == '__main__': cfg = rtevalConfig({}, logger=l) prms = {} modprms = ModuleParameters() - for c, p in modprms.items(): + for c, p in list(modprms.items()): prms[c] = p['default'] cfg.AppendConfig('MeasurementModuleTemplate', prms) @@ -144,7 +144,7 @@ if __name__ == '__main__': c._WorkloadSetup() c._WorkloadPrepare() c._WorkloadTask() - print "Running for approx %i seconds" % runtime + print("Running for approx %i seconds" % runtime) while runtime > 0: c.WorkloadAlive() time.sleep(1) diff --git a/rteval/rtevalConfig.py b/rteval/rtevalConfig.py index 16ac2d0199c4..36f1354b6a4f 100644 --- a/rteval/rtevalConfig.py +++ b/rteval/rtevalConfig.py @@ -31,9 +31,9 @@ # are deemed to be part of the source code. # import os, sys -import ConfigParser -from Log import Log -from systopology import SysTopology +import configparser +from .Log import Log +from .systopology import SysTopology def get_user_name(): name = os.getenv('SUDO_USER') @@ -111,7 +111,7 @@ class rtevalCfgSection(object): "Simple method for dumping config when object is used as a string" if len(self.__cfgdata) == 0: return "# empty" - return "\n".join(["%s: %s" % (k,v) for k,v in self.__cfgdata.items()]) + "\n" + return "\n".join(["%s: %s" % (k,v) for k,v in list(self.__cfgdata.items())]) + "\n" def __setattr__(self, key, val): @@ -119,22 +119,22 @@ class rtevalCfgSection(object): def __getattr__(self, key): - if key in self.__cfgdata.keys(): + if key in list(self.__cfgdata.keys()): return self.__cfgdata[key] return None def items(self): - return self.__cfgdata.items() + return list(self.__cfgdata.items()) def __iter__(self): "Initialises the iterator loop" - self.__dict__['_rtevalCfgSection__iter_list'] = self.__cfgdata.keys() + self.__dict__['_rtevalCfgSection__iter_list'] = list(self.__cfgdata.keys()) return self - def next(self): + def __next__(self): "Function used by the iterator" if not self.__dict__['_rtevalCfgSection__iter_list'] \ @@ -153,16 +153,16 @@ class rtevalCfgSection(object): def has_key(self, key): "has_key() wrapper for the configuration data" - return self.__cfgdata.has_key(key) + return key in self.__cfgdata def keys(self): "keys() wrapper for configuration data" - return self.__cfgdata.keys() + return list(self.__cfgdata.keys()) def setdefault(self, key, defvalue): - if not self.__cfgdata.has_key(key): + if key not in self.__cfgdata: self.__cfgdata[key] = defvalue return self.__cfgdata[key] @@ -171,7 +171,7 @@ class rtevalCfgSection(object): if type(newdict) is not dict: raise TypeError('update() method expects a dict as argument') - for key, val in newdict.iteritems(): + for key, val in newdict.items(): self.__cfgdata[key] = val @@ -190,10 +190,10 @@ class rtevalConfig(object): # get our system topology info self.__systopology = SysTopology() - print("got system topology: %s" % self.__systopology) + print(("got system topology: %s" % self.__systopology)) # Import the default config first - for sect, vals in default_config.items(): + for sect, vals in list(default_config.items()): self.__update_section(sect, vals) # Set the runtime provided init variables @@ -201,7 +201,7 @@ class rtevalConfig(object): if type(initvars) is not dict: raise TypeError('initvars argument is not a dict variable') - for sect, vals in initvars.items(): + for sect, vals in list(initvars.items()): self.__update_section(sect, vals) @@ -209,7 +209,7 @@ class rtevalConfig(object): if not section or not newvars: return - if not self.__config_data.has_key(section): + if section not in self.__config_data: self.__config_data[section] = rtevalCfgSection(newvars) else: self.__config_data[section].update(newvars) @@ -218,7 +218,7 @@ class rtevalConfig(object): def __str__(self): "Simple method for dumping config when object is used as a string" ret = "" - for sect in self.__config_data.keys(): + for sect in list(self.__config_data.keys()): ret += "[%s]\n%s\n" % (sect, str(self.__config_data[sect])) return ret @@ -236,7 +236,7 @@ class rtevalConfig(object): if os.path.exists(p): self.__info("found config file %s" % p) return p - raise RuntimeError, "Unable to find configfile" + raise RuntimeError("Unable to find configfile") def Load(self, fname = None, append = False): @@ -253,13 +253,13 @@ class rtevalConfig(object): return self.__info("reading config file %s" % cfgfile) - ini = ConfigParser.ConfigParser() + ini = configparser.ConfigParser() ini.optionxform = str ini.read(cfgfile) # wipe any previously read config info if not append: - for s in self.__config_data.keys(): + for s in list(self.__config_data.keys()): self.__config_data[s].wipe() # copy the section data into the __config_data dictionary @@ -308,14 +308,14 @@ class rtevalConfig(object): def HasSection(self, section): - return self.__config_data.has_key(section) + return section in self.__config_data def GetSection(self, section): try: # Return a new object with config settings of a given section return self.__config_data[section] - except KeyError, err: + except KeyError as err: raise KeyError("The section '%s' does not exist in the config file" % section) @@ -325,10 +325,10 @@ def unit_test(rootdir): l.SetLogVerbosity(Log.INFO) cfg = rtevalConfig(logger=l) cfg.Load(os.path.join(rootdir, 'rteval.conf')) - print cfg + print(cfg) return 0 - except Exception, e: - print "** EXCEPTION %s", str(e) + except Exception as e: + print("** EXCEPTION %s", str(e)) return 1 diff --git a/rteval/rtevalMailer.py b/rteval/rtevalMailer.py index 4588f149aaf6..f8f84d1916d0 100644 --- a/rteval/rtevalMailer.py +++ b/rteval/rtevalMailer.py @@ -38,11 +38,11 @@ class rtevalMailer(object): # * to_address # errmsg = "" - if not cfg.has_key('smtp_server'): + if 'smtp_server' not in cfg: errmsg = "\n** Missing smtp_server in config" - if not cfg.has_key('from_address'): + if 'from_address' not in cfg: errmsg += "\n** Missing from_address in config" - if not cfg.has_key('to_address'): + if 'to_address' not in cfg: errmsg += "\n** Missing to_address in config" if not errmsg == "": diff --git a/rteval/rtevalReport.py b/rteval/rtevalReport.py index b22fa39b1831..55594ae8f0bc 100644 --- a/rteval/rtevalReport.py +++ b/rteval/rtevalReport.py @@ -26,7 +26,7 @@ import os, tarfile from datetime import datetime -import xmlout +from . import xmlout class rtevalReport(object): @@ -95,11 +95,11 @@ class rtevalReport(object): def _show_report(self, xmlfile, xsltfile): '''summarize a previously generated xml file''' - print "Loading %s for summarizing" % xmlfile + print("Loading %s for summarizing" % xmlfile) xsltfullpath = os.path.join(self.__installdir, xsltfile) if not os.path.exists(xsltfullpath): - raise RuntimeError, "can't find XSL template (%s)!" % xsltfullpath + raise RuntimeError("can't find XSL template (%s)!" % xsltfullpath) xmlreport = xmlout.XMLOut('rteval', self.__version) xmlreport.LoadReport(xmlfile) @@ -126,7 +126,7 @@ class rtevalReport(object): def _tar_results(self): if not os.path.isdir(self.__reportdir): - raise RuntimeError, "no such directory: %s" % reportdir + raise RuntimeError("no such directory: %s" % reportdir) dirname = os.path.dirname(self.__reportdir) rptdir = os.path.basename(self.__reportdir) diff --git a/rteval/rtevalXMLRPC.py b/rteval/rtevalXMLRPC.py index ed080cb13b91..7b2d207e0088 100644 --- a/rteval/rtevalXMLRPC.py +++ b/rteval/rtevalXMLRPC.py @@ -25,8 +25,8 @@ # import socket, time -import rtevalclient, xmlrpclib -from Log import Log +import rtevalclient, xmlrpc.client +from .Log import Log class rtevalXMLRPC(object): def __init__(self, host, logger, mailer = None): @@ -48,12 +48,12 @@ class rtevalXMLRPC(object): res = self.__client.Hello() attempt = 10 ping_success = True - except xmlrpclib.ProtocolError: + except xmlrpc.client.ProtocolError: # Server do not support Hello(), but is reachable self.__logger.log(Log.INFO, "Got XML-RPC connection with %s but it did not support Hello()" % self.__host) res = None - except socket.error, err: + except socket.error as err: self.__logger.log(Log.INFO, "Could not establish XML-RPC contact with %s\n%s" % (self.__host, str(err))) @@ -67,7 +67,7 @@ class rtevalXMLRPC(object): "Server %s did not respond." % self.__host) warning_sent = True - print "Failed pinging XML-RPC server. Doing another attempt(%i) " % attempt + print("Failed pinging XML-RPC server. Doing another attempt(%i) " % attempt) time.sleep(attempt) #*15) # Incremental sleep - sleep attempts*15 seconds ping_success = False @@ -87,9 +87,9 @@ class rtevalXMLRPC(object): warning_sent = False while attempt < 6: try: - print "Submitting report to %s" % self.__url + print("Submitting report to %s" % self.__url) rterid = self.__client.SendReport(xmlreport) - print "Report registered with submission id %i" % rterid + print("Report registered with submission id %i" % rterid) attempt = 10 exitcode = 0 # Success except socket.error: @@ -103,10 +103,10 @@ class rtevalXMLRPC(object): % self.__host) warning_sent = True - print "Failed sending report. Doing another attempt(%i) " % attempt + print("Failed sending report. Doing another attempt(%i) " % attempt) time.sleep(attempt) #*5*60) # Incremental sleep - sleep attempts*5 minutes - except Exception, err: + except Exception as err: raise err diff --git a/rteval/rtevalclient.py b/rteval/rtevalclient.py index 7ccf3231ca37..26c953005326 100644 --- a/rteval/rtevalclient.py +++ b/rteval/rtevalclient.py @@ -25,9 +25,9 @@ # are deemed to be part of the source code. # -import xmlrpclib +import xmlrpc.client import libxml2 -import StringIO +import io import bz2 import base64 import platform @@ -37,7 +37,7 @@ class rtevalclient: rtevalclient is a library for sending rteval reports to an rteval server via XML-RPC. """ def __init__(self, url="http://rtserver.farm.hsv.redhat.com/rteval/API1/", hostn = None): - self.srv = xmlrpclib.ServerProxy(url) + self.srv = xmlrpc.client.ServerProxy(url) if hostn is None: self.hostname = platform.node() else: @@ -51,9 +51,9 @@ class rtevalclient: def SendReport(self, xmldoc): if xmldoc.type != 'document_xml': - raise Exception, "Input is not XML document" + raise Exception("Input is not XML document") - fbuf = StringIO.StringIO() + fbuf = io.StringIO() xmlbuf = libxml2.createOutputBuffer(fbuf, 'UTF-8') doclen = xmldoc.saveFileTo(xmlbuf, 'UTF-8') @@ -61,7 +61,7 @@ class rtevalclient: cmpr = compr.compress(fbuf.getvalue()) data = base64.b64encode(cmpr + compr.flush()) ret = self.srv.SendReport(self.hostname, data) - print "rtevalclient::SendReport() - Sent %i bytes (XML document length: %i bytes, compression ratio: %.02f%%)" % (len(data), doclen, (1-(float(len(data)) / float(doclen)))*100 ) + print("rtevalclient::SendReport() - Sent %i bytes (XML document length: %i bytes, compression ratio: %.02f%%)" % (len(data), doclen, (1-(float(len(data)) / float(doclen)))*100 )) return ret def SendDataAsFile(self, fname, data, decompr = False): diff --git a/rteval/sysinfo/__init__.py b/rteval/sysinfo/__init__.py index f7bf823aa36d..69b225bbbe47 100644 --- a/rteval/sysinfo/__init__.py +++ b/rteval/sysinfo/__init__.py @@ -26,14 +26,14 @@ import sys, libxml2 from rteval.Log import Log from glob import glob -from kernel import KernelInfo -from services import SystemServices -from cputopology import CPUtopology -from memory import MemoryInfo -from osinfo import OSInfo -from network import NetworkInfo -from cmdline import cmdlineInfo -import dmi +from .kernel import KernelInfo +from .services import SystemServices +from .cputopology import CPUtopology +from .memory import MemoryInfo +from .osinfo import OSInfo +from .network import NetworkInfo +from .cmdline import cmdlineInfo +from . import dmi class SystemInfo(KernelInfo, SystemServices, dmi.DMIinfo, CPUtopology, MemoryInfo, OSInfo, NetworkInfo, cmdlineInfo): def __init__(self, config, logger=None): @@ -78,29 +78,29 @@ if __name__ == "__main__": cfg.installdir = "." si = SystemInfo(cfg, logger=l) - print "\tRunning on %s" % si.get_base_os() - print "\tNUMA nodes: %d" % si.mem_get_numa_nodes() - print "\tMemory available: %03.2f %s\n" % si.mem_get_size() + print("\tRunning on %s" % si.get_base_os()) + print("\tNUMA nodes: %d" % si.mem_get_numa_nodes()) + print("\tMemory available: %03.2f %s\n" % si.mem_get_size()) - print "\tServices: " - for (s, r) in si.services_get().items(): - print "\t\t%s: %s" % (s, r) + print("\tServices: ") + for (s, r) in list(si.services_get().items()): + print("\t\t%s: %s" % (s, r)) (curr, avail) = si.kernel_get_clocksources() - print "\tCurrent clocksource: %s" % curr - print "\tAvailable clocksources: %s" % avail - print "\tModules:" + print("\tCurrent clocksource: %s" % curr) + print("\tAvailable clocksources: %s" % avail) + print("\tModules:") for m in si.kernel_get_modules(): - print "\t\t%s: %s" % (m['modname'], m['modstate']) - print "\tKernel threads:" - for (p, i) in si.kernel_get_kthreads().items(): - print "\t\t%-30.30s pid: %-5.5s policy: %-7.7s prio: %-3.3s" % ( + print("\t\t%s: %s" % (m['modname'], m['modstate'])) + print("\tKernel threads:") + for (p, i) in list(si.kernel_get_kthreads().items()): + print("\t\t%-30.30s pid: %-5.5s policy: %-7.7s prio: %-3.3s" % ( i["name"]+":", p, i["policy"], i["priority"] - ) + )) - print "\n\tCPU topology info - cores: %i online: %i sockets: %i" % ( + print("\n\tCPU topology info - cores: %i online: %i sockets: %i" % ( si.cpu_getCores(False), si.cpu_getCores(True), si.cpu_getSockets() - ) + )) xml = si.MakeReport() xml_d = libxml2.newDoc("1.0") diff --git a/rteval/sysinfo/cputopology.py b/rteval/sysinfo/cputopology.py index 73ea3254cbc5..773af64f48de 100644 --- a/rteval/sysinfo/cputopology.py +++ b/rteval/sysinfo/cputopology.py @@ -117,20 +117,20 @@ def unit_test(rootdir): cputop = CPUtopology() n = cputop._parse() - print " ---- XML Result ---- " + print(" ---- XML Result ---- ") x = libxml2.newDoc('1.0') x.setRootElement(n) x.saveFormatFileEnc('-','UTF-8',1) - print " ---- getCPUcores() / getCPUscokets() ---- " - print "CPU cores: %i (online: %i) - CPU sockets: %i" % (cputop.cpu_getCores(False), + print(" ---- getCPUcores() / getCPUscokets() ---- ") + print("CPU cores: %i (online: %i) - CPU sockets: %i" % (cputop.cpu_getCores(False), cputop.cpu_getCores(True), - cputop.cpu_getSockets()) + cputop.cpu_getSockets())) return 0 - except Exception, e: + except Exception as e: # import traceback # traceback.print_exc(file=sys.stdout) - print "** EXCEPTION %s", str(e) + print("** EXCEPTION %s", str(e)) return 1 if __name__ == '__main__': diff --git a/rteval/sysinfo/dmi.py b/rteval/sysinfo/dmi.py index f8e25005b9c1..8f05a6820332 100644 --- a/rteval/sysinfo/dmi.py +++ b/rteval/sysinfo/dmi.py @@ -56,7 +56,7 @@ def ProcessWarnings(): # All other warnings will be printed if len(warnline) > 0: - print "** DMI WARNING ** %s" % warnline + print("** DMI WARNING ** %s" % warnline) dmidecode.clear_warnings() @@ -91,7 +91,7 @@ class DMIinfo(object): xsltfile.close() return ret - raise RuntimeError, 'Could not locate XSLT template for DMI data (%s)' % (self.sharedir + '/' + fname) + raise RuntimeError('Could not locate XSLT template for DMI data (%s)' % (self.sharedir + '/' + fname)) def MakeReport(self): @@ -119,13 +119,13 @@ def unit_test(rootdir): self.__update_vars() def __update_vars(self): - for k in self.config.keys(): + for k in list(self.config.keys()): self.__dict__[k] = self.config[k] try: ProcessWarnings() if os.getuid() != 0: - print "** ERROR ** Must be root to run this unit_test()" + print("** ERROR ** Must be root to run this unit_test()") return 1 log = Log() @@ -137,8 +137,8 @@ def unit_test(rootdir): x.setRootElement(dx) x.saveFormatFileEnc("-", "UTF-8", 1) return 0 - except Exception, e: - print "** EXCEPTION: %s" % str(e) + except Exception as e: + print("** EXCEPTION: %s" % str(e)) return 1 if __name__ == '__main__': diff --git a/rteval/sysinfo/kernel.py b/rteval/sysinfo/kernel.py index 209afd7bac4e..fd261fe2ab77 100644 --- a/rteval/sysinfo/kernel.py +++ b/rteval/sysinfo/kernel.py @@ -73,7 +73,7 @@ class KernelInfo(object): "modstate": mod[4]}) line = fp.readline() fp.close() - except Exception, err: + except Exception as err: raise err return modlist @@ -82,7 +82,7 @@ class KernelInfo(object): '''get the available and curent clocksources for this kernel''' path = '/sys/devices/system/clocksource/clocksource0' if not os.path.exists(path): - raise RuntimeError, "Can't find clocksource path in /sys" + raise RuntimeError("Can't find clocksource path in /sys") f = open (os.path.join (path, "current_clocksource")) current_clocksource = f.readline().strip() f = open (os.path.join (path, "available_clocksource")) @@ -131,7 +131,7 @@ class KernelInfo(object): rep_n.addChild(kthreads_n) kthreads = self.kernel_get_kthreads() - keys = kthreads.keys() + keys = list(kthreads.keys()) if len(keys): keys.sort() for pid in keys: @@ -161,10 +161,10 @@ def unit_test(rootdir): xml_d.setRootElement(ki_xml) xml_d.saveFormatFileEnc("-", "UTF-8", 1) - except Exception, e: + except Exception as e: import traceback traceback.print_exc(file=sys.stdout) - print "** EXCEPTION %s", str(e) + print("** EXCEPTION %s", str(e)) return 1 if __name__ == '__main__': diff --git a/rteval/sysinfo/memory.py b/rteval/sysinfo/memory.py index 2c31d6593952..4df198be47f7 100644 --- a/rteval/sysinfo/memory.py +++ b/rteval/sysinfo/memory.py @@ -48,12 +48,12 @@ class MemoryInfo(object): if l.startswith('MemTotal:'): parts = l.split() if parts[2].lower() != 'kb': - raise RuntimeError, "Units changed from kB! (%s)" % parts[2] + raise RuntimeError("Units changed from kB! (%s)" % parts[2]) rawsize = int(parts[1]) f.close() break if rawsize == 0: - raise RuntimeError, "can't find memtotal in /proc/meminfo!" + raise RuntimeError("can't find memtotal in /proc/meminfo!") # Get a more readable result # Note that this depends on /proc/meminfo starting in Kb @@ -87,12 +87,12 @@ def unit_test(rootdir): import sys try: mi = MemoryInfo() - print "Numa nodes: %i" % mi.mem_get_numa_nodes() - print "Memory: %i %s" % mi.mem_get_size() - except Exception, e: + print("Numa nodes: %i" % mi.mem_get_numa_nodes()) + print("Memory: %i %s" % mi.mem_get_size()) + except Exception as e: import traceback traceback.print_exc(file=sys.stdout) - print "** EXCEPTION %s", str(e) + print("** EXCEPTION %s", str(e)) return 1 if __name__ == '__main__': diff --git a/rteval/sysinfo/network.py b/rteval/sysinfo/network.py index fa29db2bba55..9b933c13464b 100644 --- a/rteval/sysinfo/network.py +++ b/rteval/sysinfo/network.py @@ -106,10 +106,10 @@ def unit_test(rootdir): doc.setRootElement(cfg) doc.saveFormatFileEnc('-', 'UTF-8', 1) - except Exception, e: + except Exception as e: import traceback traceback.print_exc(file=sys.stdout) - print "** EXCEPTION %s", str(e) + print("** EXCEPTION %s", str(e)) return 1 if __name__ == '__main__': diff --git a/rteval/sysinfo/osinfo.py b/rteval/sysinfo/osinfo.py index e262c06d968d..333f82beb558 100644 --- a/rteval/sysinfo/osinfo.py +++ b/rteval/sysinfo/osinfo.py @@ -54,7 +54,7 @@ class OSInfo(object): if os.path.exists('/usr/bin/dmesg'): subprocess.call('/usr/bin/dmesg > %s' % os.path.join(repdir, "dmesg"), shell=True) return - print "dmesg file not found at %s and no dmesg exe found!" % dpath + print("dmesg file not found at %s and no dmesg exe found!" % dpath) @@ -64,7 +64,7 @@ class OSInfo(object): elif os.path.exists('/usr/sbin/sysreport'): exe = '/usr/sbin/sysreport' else: - raise RuntimeError, "Can't find sosreport/sysreport" + raise RuntimeError("Can't find sosreport/sysreport") self.__logger.log(Log.DEBUG, "report tool: %s" % exe) options = ['-k', 'rpm.rpmva=off', @@ -115,22 +115,22 @@ def unit_test(rootdir): log = Log() log.SetLogVerbosity(Log.DEBUG|Log.INFO) osi = OSInfo(logger=log) - print "Base OS: %s" % osi.get_base_os() + print("Base OS: %s" % osi.get_base_os()) - print "Testing OSInfo::copy_dmesg('/tmp'): ", + print("Testing OSInfo::copy_dmesg('/tmp'): ", end=' ') osi.copy_dmesg('/tmp') if os.path.isfile("/tmp/dmesg"): md5orig = subprocess.check_output(("md5sum","/var/log/dmesg")) md5copy = subprocess.check_output(("md5sum","/tmp/dmesg")) if md5orig.split(" ")[0] == md5copy.split(" ")[0]: - print "PASS" + print("PASS") else: - print "FAIL (md5sum)" + print("FAIL (md5sum)") os.unlink("/tmp/dmesg") else: - print "FAIL (copy failed)" + print("FAIL (copy failed)") - print "Running sysreport/sosreport with output to current dir" + print("Running sysreport/sosreport with output to current dir") osi.run_sysreport(".") osinfo_xml = osi.MakeReport() @@ -138,10 +138,10 @@ def unit_test(rootdir): xml_d.setRootElement(osinfo_xml) xml_d.saveFormatFileEnc("-", "UTF-8", 1) - except Exception, e: + except Exception as e: import traceback traceback.print_exc(file=sys.stdout) - print "** EXCEPTION %s", str(e) + print("** EXCEPTION %s", str(e)) return 1 if __name__ == '__main__': diff --git a/rteval/sysinfo/services.py b/rteval/sysinfo/services.py index 6465d35e1b73..1bab2de4eb59 100644 --- a/rteval/sysinfo/services.py +++ b/rteval/sysinfo/services.py @@ -49,7 +49,7 @@ class SystemServices(object): servicesdir = sdir break if not servicesdir: - raise RuntimeError, "No services dir (init.d) found on your system" + raise RuntimeError("No services dir (init.d) found on your system") self.__log(Log.DEBUG, "Services located in %s, going through each service file to check status" % servicesdir) ret_services = {} for service in glob.glob(os.path.join(servicesdir, '*')): @@ -127,8 +127,8 @@ def unit_test(rootdir): xml_d.saveFormatFileEnc("-", "UTF-8", 1) return 0 - except Exception, e: - print "** EXCEPTION: %s" % str(e) + except Exception as e: + print("** EXCEPTION: %s" % str(e)) return 1 diff --git a/rteval/sysinfo/tools.py b/rteval/sysinfo/tools.py index a61aa48160d3..ea14922b519a 100644 --- a/rteval/sysinfo/tools.py +++ b/rteval/sysinfo/tools.py @@ -32,13 +32,13 @@ def getcmdpath(which): getcmdpath is a method which allows finding an executable in the PATH directories to call it from full path """ - if not pathSave.has_key(which): + if which not in pathSave: for path in os.environ['PATH'].split(':'): cmdfile = os.path.join(path, which) if os.path.isfile(cmdfile) and os.access(cmdfile, os.X_OK): pathSave[which] = cmdfile break if not pathSave[which]: - raise RuntimeError, "Command '%s' is unknown on this system" % which + raise RuntimeError("Command '%s' is unknown on this system" % which) return pathSave[which] diff --git a/rteval/systopology.py b/rteval/systopology.py index fc3876c20bc9..97a6037e83aa 100644 --- a/rteval/systopology.py +++ b/rteval/systopology.py @@ -99,7 +99,7 @@ class CpuList(object): if '-' in part: a, b = part.split('-') a, b = int(a), int(b) - result.extend(range(a, b + 1)) + result.extend(list(range(a, b + 1))) else: a = int(part) result.append(a) @@ -112,7 +112,7 @@ class CpuList(object): # check whether cpu n is online def isonline(self, n): if n not in self.cpulist: - raise RuntimeError, "invalid cpu number %d" % n + raise RuntimeError("invalid cpu number %d" % n) if n == 0: return True path = os.path.join(CpuList.cpupath,'cpu%d' % n) @@ -183,11 +183,11 @@ class SysTopology(object): self.getinfo() def __len__(self): - return len(self.nodes.keys()) + return len(list(self.nodes.keys())) def __str__(self): - s = "%d node system" % len(self.nodes.keys()) - s += " (%d cores per node)" % (len(self.nodes[self.nodes.keys()[0]])) + s = "%d node system" % len(list(self.nodes.keys())) + s += " (%d cores per node)" % (len(self.nodes[list(self.nodes.keys())[0]])) return s # inplement the 'in' function @@ -207,7 +207,7 @@ class SysTopology(object): return self # iterator function - def next(self): + def __next__(self): if self.current >= len(self.nodes): raise StopIteration n = self.nodes[self.current] @@ -217,14 +217,14 @@ class SysTopology(object): def getinfo(self): nodes = glob.glob(os.path.join(SysTopology.nodepath, 'node[0-9]*')) if not nodes: - raise RuntimeError, "No valid nodes found in %s!" % SysTopology.nodepath + raise RuntimeError("No valid nodes found in %s!" % SysTopology.nodepath) nodes.sort() for n in nodes: node = int(os.path.basename(n)[4:]) self.nodes[node] = NumaNode(n) def getnodes(self): - return self.nodes.keys() + return list(self.nodes.keys()) def getcpus(self, node): return self.nodes[node] @@ -235,12 +235,12 @@ if __name__ == "__main__": def unit_test(): s = SysTopology() - print s - print "number of nodes: %d" % len(s) + print(s) + print("number of nodes: %d" % len(s)) for n in s: - print "node[%d]: %s" % (n.nodeid, n) - print "system has numa node 0: %s" % (0 in s) - print "system has numa node 2: %s" % (2 in s) - print "system has numa node 24: %s" % (24 in s) + print("node[%d]: %s" % (n.nodeid, n)) + print("system has numa node 0: %s" % (0 in s)) + print("system has numa node 2: %s" % (2 in s)) + print("system has numa node 24: %s" % (24 in s)) unit_test() diff --git a/rteval/xmlout.py b/rteval/xmlout.py index ddc29643e9ca..64642588b8b9 100644 --- a/rteval/xmlout.py +++ b/rteval/xmlout.py @@ -29,7 +29,7 @@ import libxml2 import lxml.etree import codecs import re -from string import maketrans +#from string import maketrans def convert_libxml2_to_lxml_doc(inxml): @@ -69,7 +69,7 @@ class XMLOut(object): def __del__(self): if self.level > 0: - raise RuntimeError, "XMLOut: open blocks at __del__ (last opened '%s')" % self.currtag.name + raise RuntimeError("XMLOut: open blocks at __del__ (last opened '%s')" % self.currtag.name) if self.xmldoc is not None: self.xmldoc.freeDoc() @@ -88,12 +88,12 @@ class XMLOut(object): return tagname.translate(self.tag_trans) def __encode(self, value, tagmode = False): - if type(value) is unicode: + if type(value) is str: val = value elif type(value) is str: - val = unicode(value) + val = str(value) else: - val = unicode(str(value)) + val = str(str(value)) if tagmode is True: rx = re.compile(" ") @@ -106,7 +106,7 @@ class XMLOut(object): def __add_attributes(self, node, attr): if attr is not None: - for k, v in attr.iteritems(): + for k, v in attr.items(): node.newProp(k, self.__encode(v)) @@ -116,7 +116,7 @@ class XMLOut(object): # unknown types. t = type(data) - if t is unicode or t is str or t is int or t is float: + if t is str or t is str or t is int or t is float: n = libxml2.newText(self.__encode(data)) node.addChild(n) elif t is bool: @@ -124,7 +124,7 @@ class XMLOut(object): n = libxml2.newText(self.__encode(v)) node.addChild(n) elif t is dict: - for (key, val) in data.iteritems(): + for (key, val) in data.items(): node2 = libxml2.newNode(self.__encode(self.parsedata_prefix + key, True)) self.__parseToXML(node2, val) node.addChild(node2) @@ -137,15 +137,15 @@ class XMLOut(object): self.__parseToXML(n, v) node.addChild(n) else: - raise TypeError, "unhandled type (%s) for value '%s'" % (type(data), unicode(data)) + raise TypeError("unhandled type (%s) for value '%s'" % (type(data), str(data))) def close(self): if self.status == 0: - raise RuntimeError, "XMLOut: No XML document is created nor loaded" + raise RuntimeError("XMLOut: No XML document is created nor loaded") if self.status == 3: - raise RuntimeError, "XMLOut: XML document already closed" + raise RuntimeError("XMLOut: XML document already closed") if self.level > 0: - raise RuntimeError, "XMLOut: open blocks at close() (last opened '%s')" % self.currtag.name + raise RuntimeError("XMLOut: open blocks at close() (last opened '%s')" % self.currtag.name) if self.status == 1: # Only set the root node in the doc on created reports (NewReport called) self.xmldoc.setRootElement(self.xmlroot) @@ -154,7 +154,7 @@ class XMLOut(object): def NewReport(self): if self.status != 0 and self.status != 3: - raise RuntimeError, "XMLOut: Cannot start a new report without closing the currently opened one" + raise RuntimeError("XMLOut: Cannot start a new report without closing the currently opened one") if self.status == 3: self.xmldoc.freeDoc() # Free the report from memory if we have one already @@ -175,30 +175,30 @@ class XMLOut(object): self.xmldoc = libxml2.parseFile(filename) if self.xmldoc.name != filename: self.status = 3 - raise RuntimeError, "XMLOut: Loading report failed" + raise RuntimeError("XMLOut: Loading report failed") root = self.xmldoc.children if root.name != self.roottag: self.status = 3 - raise RuntimeError, "XMLOut: Loaded report is not a valid %s XML file" % self.roottag + raise RuntimeError("XMLOut: Loaded report is not a valid %s XML file" % self.roottag) if validate_version is True: ver = root.hasProp('version') if ver is None: self.status = 3 - raise RuntimeError, "XMLOut: Loaded report is missing version attribute in root node" + raise RuntimeError("XMLOut: Loaded report is missing version attribute in root node") if ver.getContent() != self.version: self.status = 3 - raise RuntimeError, "XMLOut: Loaded report is not of version %s" % self.version + raise RuntimeError("XMLOut: Loaded report is not of version %s" % self.version) self.status = 2 # Confirm that we have loaded a report from file def Write(self, filename, xslt = None): if self.status != 2 and self.status != 3: - raise RuntimeError, "XMLOut: XML document is not closed" + raise RuntimeError("XMLOut: XML document is not closed") if xslt == None: # If no XSLT template is give, write raw XML @@ -223,7 +223,7 @@ class XMLOut(object): resdoc = parser(xmldoc) # Write the file with the requested output encoding - dstfile.write(unicode(resdoc).encode(self.encoding)) + dstfile.write(str(resdoc).encode(self.encoding)) if dstfile != sys.stdout: dstfile.close() @@ -238,12 +238,12 @@ class XMLOut(object): def GetXMLdocument(self): if self.status != 2 and self.status != 3: - raise RuntimeError, "XMLOut: XML document is not closed" + raise RuntimeError("XMLOut: XML document is not closed") return self.xmldoc def openblock(self, tagname, attributes=None): if self.status != 1: - raise RuntimeError, "XMLOut: openblock() cannot be called before NewReport() is called" + raise RuntimeError("XMLOut: openblock() cannot be called before NewReport() is called") ntag = libxml2.newNode(self.__fixtag(tagname)); self.__add_attributes(ntag, attributes) self.currtag.addChild(ntag) @@ -253,16 +253,16 @@ class XMLOut(object): def closeblock(self): if self.status != 1: - raise RuntimeError, "XMLOut: closeblock() cannot be called before NewReport() is called" + raise RuntimeError("XMLOut: closeblock() cannot be called before NewReport() is called") if self.level == 0: - raise RuntimeError, "XMLOut: no open tags to close" + raise RuntimeError("XMLOut: no open tags to close") self.currtag = self.currtag.get_parent() self.level -= 1 return self.currtag def taggedvalue(self, tag, value, attributes=None): if self.status != 1: - raise RuntimeError, "XMLOut: taggedvalue() cannot be called before NewReport() is called" + raise RuntimeError("XMLOut: taggedvalue() cannot be called before NewReport() is called") ntag = self.currtag.newTextChild(None, self.__fixtag(tag), self.__encode(value)) self.__add_attributes(ntag, attributes) return ntag @@ -270,7 +270,7 @@ class XMLOut(object): def ParseData(self, tagname, data, attributes=None, tuple_tagname="tuples", prefix = ""): if self.status != 1: - raise RuntimeError, "XMLOut: taggedvalue() cannot be called before NewReport() is called" + raise RuntimeError("XMLOut: taggedvalue() cannot be called before NewReport() is called") self.tuple_tagname = self.__fixtag(tuple_tagname) self.parsedata_prefix = prefix @@ -283,7 +283,7 @@ class XMLOut(object): def AppendXMLnodes(self, nodes): if not isinstance(nodes, libxml2.xmlNode): - raise ValueError, "Input value is not a libxml2.xmlNode" + raise ValueError("Input value is not a libxml2.xmlNode") return self.currtag.addChild(nodes) @@ -297,7 +297,7 @@ def unit_test(rootdir): x.taggedvalue('date', '2000-11-22') x.closeblock() x.openblock('uname') - x.taggedvalue('node', u'testing - \xe6\xf8') + x.taggedvalue('node', 'testing - \xe6\xf8') x.taggedvalue('kernel', 'my_test_kernel', {'is_RT': 0}) x.taggedvalue('arch', 'mips') x.closeblock() @@ -311,25 +311,25 @@ def unit_test(rootdir): x.taggedvalue('command_line','dd if=/dev/zero of=/dev/null', {'name': 'lightloader'}) x.closeblock() x.close() - print "------------- XML OUTPUT ----------------------------" + print("------------- XML OUTPUT ----------------------------") x.Write("-") - print "------------- XSLT PARSED OUTPUT --------------------" + print("------------- XSLT PARSED OUTPUT --------------------") x.Write("-", "rteval_text.xsl") - print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") x.Write("/tmp/xmlout-test.xml") del x - print "------------- LOAD XML FROM FILE -----------------------------" + print("------------- LOAD XML FROM FILE -----------------------------") x = XMLOut('rteval','UNIT-TEST', None, 'UTF-8') x.LoadReport("/tmp/xmlout-test.xml", True) - print "------------- LOADED XML DATA --------------------------------" + print("------------- LOADED XML DATA --------------------------------") x.Write("-") - print "------------- XSLT PARSED OUTPUT FROM LOADED XML--------------" + print("------------- XSLT PARSED OUTPUT FROM LOADED XML--------------") x.Write("-", "rteval_text.xsl") x.close() ## Test new data parser ... it eats most data types - print "------------- TESTING XMLOut::ParseData() --------------" + print("------------- TESTING XMLOut::ParseData() --------------") x.NewReport() x.ParseData("ParseTest", "test string", {"type": "simple_string"}) x.ParseData("ParseTest", 1234, {"type": "integer"}) @@ -348,14 +348,14 @@ def unit_test(rootdir): "varA4": {'another_level': True, 'another_value': "blabla"} }, - "utf8 data": u'æøå', - u"løpe": True} + "utf8 data": 'æøå', + "løpe": True} x.ParseData("ParseTest", test, {"type": "dict"}, prefix="test ") x.close() x.Write("-") return 0 - except Exception, e: - print "** EXCEPTION %s", str(e) + except Exception as e: + print("** EXCEPTION %s", str(e)) return 1 if __name__ == '__main__': diff --git a/setup.py b/setup.py index 39bc8247b704..fc78a93c8793 100644 --- a/setup.py +++ b/setup.py @@ -10,9 +10,9 @@ PYTHONLIB = join(get_python_lib(standard_lib=1, prefix=''), 'site-packages') # Tiny hack to make rteval-cmd become a rteval when building/installing the package try: - os.mkdir('dist', 0755) + os.mkdir('dist', 0o755) distcreated = True -except OSError, e: +except OSError as e: if e.errno == 17: # If it already exists, ignore this error distcreated = False