From 1a90ceb3e02e50a54bc0267571e0f4554201b579 Mon Sep 17 00:00:00 2001 From: Sol Jerome Date: Thu, 23 Apr 2009 16:33:20 +0000 Subject: More python 2to3 updates along with pylint/code cleanups Signed-off-by: Sol Jerome git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@5173 ce84e21b-d406-0410-9b95-82705330c041 --- src/lib/Daemon.py | 11 +++++---- src/lib/Logger.py | 31 ++++++++++++++++-------- src/lib/Options.py | 20 +++++++++------- src/sbin/bcfg2-admin | 12 ++++++---- src/sbin/bcfg2-repo-validate | 16 ++++++------- src/sbin/bcfg2-server | 56 ++++++++++++++++++++++++++++---------------- 6 files changed, 91 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/lib/Daemon.py b/src/lib/Daemon.py index bdce2c179..8798c7461 100644 --- a/src/lib/Daemon.py +++ b/src/lib/Daemon.py @@ -1,7 +1,8 @@ '''Bcfg2 daemon support''' __revision__ = '$Revision$' -import os, sys +import os +import sys def daemonize(filename): '''Do the double fork/setsession dance''' @@ -15,9 +16,9 @@ def daemonize(filename): # If we got this far without exceptions, there is another instance # running. Exit gracefully. - print "PID File (%s) exists and listed PID (%d) is active." % \ - (filename, oldpid) - raise SystemExit, 1 + print("PID File (%s) exists and listed PID (%d) is active." % \ + (filename, oldpid)) + raise SystemExit(1) except OSError: pidfile.close() except (IOError, ValueError): @@ -35,7 +36,7 @@ def daemonize(filename): pidfile.write("%i" % pid) pidfile.close() except: - print "Failed to write pid file %s" % filename + print("Failed to write pid file %s" % filename) os._exit(0) os.chdir("/") os.umask(0) diff --git a/src/lib/Logger.py b/src/lib/Logger.py index bb3c703a6..129d10314 100644 --- a/src/lib/Logger.py +++ b/src/lib/Logger.py @@ -1,13 +1,21 @@ '''Bcfg2 logging support''' __revision__ = '$Revision$' -import copy, fcntl, logging, logging.handlers, math, socket, struct, sys, termios, types +import copy +import fcntl +import logging +import logging.handlers +import math +import socket +import struct +import sys +import termios -logging.raiseExceptions=0 +logging.raiseExceptions = 0 def print_attributes(attrib): ''' Add the attributes for an element''' - return ' '.join(['%s="%s"' % data for data in attrib.iteritems()]) + return ' '.join(['%s="%s"' % data for data in list(attrib.items())]) def print_text(text): ''' Add text to the output (which will need normalising ''' @@ -54,15 +62,15 @@ class TermiosFormatter(logging.Formatter): '''format a record for display''' returns = [] line_len = self.width - if type(record.msg) in types.StringTypes: + if isinstance(record.msg, str): for line in record.msg.split('\n'): if len(line) <= line_len: returns.append(line) else: inner_lines = int(math.floor(float(len(line)) / line_len))+1 - for i in xrange(inner_lines): + for i in range(inner_lines): returns.append("%s" % (line[i*line_len:(i+1)*line_len])) - elif type(record.msg) == types.ListType: + elif isinstance(record.msg, list): if not record.msg: return '' record.msg.sort() @@ -70,7 +78,7 @@ class TermiosFormatter(logging.Formatter): columnWidth = max([len(item) for item in record.msg]) columns = int(math.floor(float(msgwidth) / (columnWidth+2))) lines = int(math.ceil(float(len(record.msg)) / columns)) - for lineNumber in xrange(lines): + for lineNumber in range(lines): indices = [idx for idx in [(colNum * lines) + lineNumber for colNum in range(columns)] if idx < len(record.msg)] format = (len(indices) * (" %%-%ds " % columnWidth)) @@ -84,7 +92,10 @@ class TermiosFormatter(logging.Formatter): return '\n'.join(returns) class FragmentingSysLogHandler(logging.handlers.SysLogHandler): - '''This handler fragments messages into chunks smaller than 250 characters''' + ''' + This handler fragments messages into + chunks smaller than 250 characters + ''' def __init__(self, procname, path, facility): self.procname = procname @@ -116,7 +127,7 @@ class FragmentingSysLogHandler(logging.handlers.SysLogHandler): except socket.error: while True: try: - if isinstance(self.address, types.TupleType): + if isinstance(self.address, tuple): self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) else: self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) @@ -150,7 +161,7 @@ def setup_logging(procname, to_console=True, to_syslog=True, syslog_facility='da except socket.error: logging.root.error("failed to activate syslogging") except: - print "Failed to activate syslogging" + print("Failed to activate syslogging") if not to_file == None: filelog = logging.FileHandler(to_file) filelog.setLevel(logging.DEBUG) diff --git a/src/lib/Options.py b/src/lib/Options.py index c1c6d9df8..a3bf90604 100644 --- a/src/lib/Options.py +++ b/src/lib/Options.py @@ -123,20 +123,20 @@ class OptionSet(dict): self.hm = self.buildHelpMessage() def buildGetopt(self): - return ''.join([opt.buildGetopt() for opt in self.values()]) + return ''.join([opt.buildGetopt() for opt in list(self.values())]) def buildLongGetopt(self): - return [opt.buildLongGetopt() for opt in self.values() if opt.long] + return [opt.buildLongGetopt() for opt in list(self.values()) if opt.long] def buildHelpMessage(self): if hasattr(self, 'hm'): return self.hm - return ' '.join([opt.buildHelpMessage() for opt in self.values()]) + return ' '.join([opt.buildHelpMessage() for opt in list(self.values())]) def helpExit(self, msg='', code=1): if msg: - print msg - print "Usage:\n %s" % self.buildHelpMessage() + print(msg) + print("Usage:\n %s" % self.buildHelpMessage()) raise SystemExit(code) def parse(self, argv, do_getopt=True): @@ -151,7 +151,7 @@ class OptionSet(dict): if '-h' in argv: self.helpExit('', 0) self['args'] = args - for key in self.keys(): + for key in list(self.keys()): if key == 'args': continue option = self[key] @@ -268,13 +268,17 @@ ENCODING = Option('Encoding of cfg files', default=sys.getdefaultencoding(), cmd OMIT_LOCK_CHECK = Option('Omit lock check', default=False, cmd='-O') -LOGGING_FILE_PATH = Option('Set path of file log', default=None, cmd='-o', odesc='', cf=('logging', 'path')) +LOGGING_FILE_PATH = Option('Set path of file log', default=None, + cmd='-o', odesc='', cf=('logging', 'path')) CLIENT_SERVICE_MODE = Option('Set client service mode', default='default', cmd='-s', odesc='') class OptionParser(OptionSet): - '''OptionParser bootstraps option parsing, getting the value of the config file''' + ''' + OptionParser bootstraps option parsing, + getting the value of the config file + ''' def __init__(self, args): self.Bootstrap = OptionSet([('configfile', CFILE)]) self.Bootstrap.parse(sys.argv[1:], do_getopt=False) diff --git a/src/sbin/bcfg2-admin b/src/sbin/bcfg2-admin index 2023808e8..af461dfbc 100755 --- a/src/sbin/bcfg2-admin +++ b/src/sbin/bcfg2-admin @@ -1,8 +1,12 @@ #!/usr/bin/env python '''bcfg2-admin is a script that helps to administrate a bcfg2 deployment''' -import getopt, logging, sys -import Bcfg2.Server.Core, Bcfg2.Logger, Bcfg2.Options +import getopt +import logging +import sys +import Bcfg2.Server.Core +import Bcfg2.Logger +import Bcfg2.Options log = logging.getLogger('bcfg-admin') @@ -23,7 +27,7 @@ if __name__ == '__main__': try: opts, args = getopt.getopt(sys.argv[1:], 'hC:', ['help', 'configfile=']) except getopt.GetoptError, msg: - print msg + print(msg) raise SystemExit(1) @@ -38,7 +42,7 @@ if __name__ == '__main__': if len(args) < 1 or args[0] == 'help': print ("Usage: bcfg2-admin [OPTIONS] MODE [ARGS]\n\n" "Available options are:") - for (opt, arg) in avail_opts.items(): + for (opt, arg) in list(avail_opts.items()): print((" %-15s " % opt + arg)) print ("\nAvailable modes are:") if len(args) > 1: diff --git a/src/sbin/bcfg2-repo-validate b/src/sbin/bcfg2-repo-validate index 4c692e362..6fb157ec8 100755 --- a/src/sbin/bcfg2-repo-validate +++ b/src/sbin/bcfg2-repo-validate @@ -86,46 +86,46 @@ if __name__ == '__main__': } failures = 0 - for k, (filelist, schemaname) in filesets.iteritems(): + for k, (filelist, schemaname) in list(filesets.items()): try: schema = lxml.etree.XMLSchema(lxml.etree.parse(open(schemaname%(schemadir)))) except: - print "Failed to process schema %s" % (schemaname%(schemadir)) + print("Failed to process schema %s" % (schemaname%(schemadir))) failures = 1 continue for filename in filelist: try: datafile = lxml.etree.parse(open(filename)) except SyntaxError: - print "%s ***FAILS*** to parse \t\t<----" % (filename) + print("%s ***FAILS*** to parse \t\t<----" % (filename)) os.system("xmllint %s" % filename) failures = 1 continue except IOError: - print "Failed to open file %s \t\t<---" % (filename) + print("Failed to open file %s \t\t<---" % (filename)) failures = 1 continue if schema.validate(datafile): if verbose: - print "%s checks out" % (filename) + print("%s checks out" % (filename)) else: rc = os.system("xmllint --noout --xinclude --schema \ %s %s > /dev/null 2>/dev/null" % \ (schemaname % schemadir, filename)) if rc: failures = 1 - print "%s ***FAILS*** to verify \t\t<----" % (filename) + print("%s ***FAILS*** to verify \t\t<----" % (filename)) os.system("xmllint --noout --xinclude --schema %s %s" % \ (schemaname % schemadir, filename)) elif verbose: - print "%s checks out" % (filename) + print("%s checks out" % (filename)) # print out missing bundle information if verbose: print("") for bundle in ref_bundles: if bundle not in bundle_list: - print ("*** Warning: Bundle %s referenced, but does not " + print("*** Warning: Bundle %s referenced, but does not " "exist." % bundle) raise SystemExit, failures diff --git a/src/sbin/bcfg2-server b/src/sbin/bcfg2-server index bf850d8e5..fa6605f1c 100755 --- a/src/sbin/bcfg2-server +++ b/src/sbin/bcfg2-server @@ -3,21 +3,28 @@ '''The XML-RPC Bcfg2 Server''' __revision__ = '$Revision$' -import Bcfg2.Server.Plugins.Metadata - -from Bcfg2.Server.Core import Core, CoreInitError +import logging +import md5 +import select +import socket +import sys +import time from xmlrpclib import Fault from lxml.etree import XML, Element, tostring -import logging, md5, select, socket, sys, time -import Bcfg2.Logger, Bcfg2.Options, Bcfg2.Component, Bcfg2.Daemon +import Bcfg2.Logger +import Bcfg2.Options +import Bcfg2.Component +import Bcfg2.Daemon +import Bcfg2.Server.Plugins.Metadata +from Bcfg2.Server.Core import Core, CoreInitError logger = logging.getLogger('bcfg2-server') def critical_error(operation): '''Log and err, traceback and return an xmlrpc fault to client''' logger.error(operation, exc_info=1) - raise Fault, (7, "Critical unexpected failure: %s" % (operation)) + raise Fault(7, "Critical unexpected failure: %s" % (operation)) class SetupError(Exception): '''Used when the server cant be setup''' @@ -38,7 +45,7 @@ class Bcfg2Serv(Bcfg2.Component.Component): setup['encoding'], setup['filemonitor']) except CoreInitError, msg: logger.critical("Fatal error: %s" % (msg)) - raise SystemExit, 1 + raise SystemExit(1) if 'DBStats' in self.Core.plugins: self.fork_funcs.append("RecvStats") @@ -52,7 +59,8 @@ class Bcfg2Serv(Bcfg2.Component.Component): if events: break else: - logger.error("Hit event timeout without getting any events; GAMIN/FAM problem?") + logger.error("Hit event timeout without getting " + "any events; GAMIN/FAM problem?") continue events = True i = 0 @@ -79,7 +87,7 @@ class Bcfg2Serv(Bcfg2.Component.Component): }) # init functions to be exposed as XML-RPC functions - for plugin in self.Core.plugins.values(): + for plugin in list(self.Core.plugins.values()): for method in plugin.__rmi__: self.register_function(getattr(self.Core.plugins[plugin.__name__], method), "%s.%s" % (plugin.__name__, method)) @@ -87,7 +95,10 @@ class Bcfg2Serv(Bcfg2.Component.Component): def get_request(self): - '''We need to do work between requests, so select with timeout instead of blocking in accept''' + ''' + We need to do work between requests, so select + with timeout instead of blocking in accept + ''' rsockinfo = [] famfd = self.Core.fam.fileno() while self.socket not in rsockinfo: @@ -111,7 +122,7 @@ class Bcfg2Serv(Bcfg2.Component.Component): name = self.Core.metadata.resolve_client(address) meta = self.Core.build_metadata(name) - for plugin in [p for p in self.Core.plugins.values() \ + for plugin in [p for p in list(self.Core.plugins.values()) \ if isinstance(p, Bcfg2.Server.Plugin.Probing)]: for probe in plugin.GetProbes(meta): resp.append(probe) @@ -119,7 +130,7 @@ class Bcfg2Serv(Bcfg2.Component.Component): except Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError: warning = 'Client metadata resolution error for %s; check server log' % address[0] self.logger.warning(warning) - raise Fault, (6, warning) + raise Fault(6, warning) except: critical_error("error determining client probes") @@ -131,7 +142,7 @@ class Bcfg2Serv(Bcfg2.Component.Component): except Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError: warning = 'metadata consistency error' self.logger.warning(warning) - raise Fault, (6, warning) + raise Fault(6, warning) # clear dynamic groups self.Core.metadata.cgroups[meta.hostname] = [] try: @@ -162,7 +173,7 @@ class Bcfg2Serv(Bcfg2.Component.Component): except (Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError, Bcfg2.Server.Plugins.Metadata.MetadataRuntimeError): warning = 'metadata consistency error' self.logger.warning(warning) - raise Fault, (6, warning) + raise Fault(6, warning) return True def Bcfg2GetConfig(self, address, checksum=False): @@ -179,7 +190,7 @@ class Bcfg2Serv(Bcfg2.Component.Component): return tostring(config, encoding='UTF-8', xml_declaration=True) except Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError: self.logger.warning("Metadata consistency failure for %s" % (address)) - raise Fault, (6, "Metadata consistency failure") + raise Fault(6, "Metadata consistency failure") def Bcfg2RecvStats(self, address, stats): '''Act on statistics upload''' @@ -227,19 +238,24 @@ if __name__ == '__main__': level = 0 if setup['daemon']: - Bcfg2.Logger.setup_logging('bcfg2-server', to_console=False, level=level, to_file=setup['filelog']) + Bcfg2.Logger.setup_logging('bcfg2-server', + to_console=False, + level=level, + to_file=setup['filelog']) Bcfg2.Daemon.daemonize(setup['daemon']) else: - Bcfg2.Logger.setup_logging('bcfg2-server', level=level, to_file=setup['filelog']) + Bcfg2.Logger.setup_logging('bcfg2-server', + level=level, + to_file=setup['filelog']) if not setup['key']: - print "No key specified in '%s'" % setup['configfile'] - raise SystemExit, 1 + print("No key specified in '%s'" % setup['configfile']) + raise SystemExit(1) try: BSERV = Bcfg2Serv(setup) except SetupError: - raise SystemExit, 1 + raise SystemExit(1) while not BSERV.shut: try: BSERV.serve_forever() -- cgit v1.2.3-1-g7c22