summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSol Jerome <sol.jerome@gmail.com>2011-04-07 14:04:36 -0500
committerSol Jerome <sol.jerome@gmail.com>2011-04-07 14:04:36 -0500
commit2e1a79fe401bea5b33551b9f94689524bf43cdca (patch)
tree7627677e9fcc2d7f6534c109a795bfdef87d0530
parent6f27f7f30ee0bcfcb3f6a4c48c153ea39a2accce (diff)
downloadbcfg2-2e1a79fe401bea5b33551b9f94689524bf43cdca.tar.gz
bcfg2-2e1a79fe401bea5b33551b9f94689524bf43cdca.tar.bz2
bcfg2-2e1a79fe401bea5b33551b9f94689524bf43cdca.zip
PY3K + PEP8 fixes for remaining files
Signed-off-by: Sol Jerome <sol.jerome@gmail.com>
-rw-r--r--src/lib/Logger.py53
-rw-r--r--src/lib/Proxy.py38
-rw-r--r--src/lib/SSLServer.py41
-rw-r--r--src/lib/Server/Core.py64
-rw-r--r--src/lib/Server/FileMonitor.py18
-rw-r--r--src/lib/Server/Hostbase/ldapauth.py82
-rw-r--r--src/lib/Server/Plugin.py2
-rw-r--r--src/lib/Server/Plugins/Pkgmgr.py2
-rw-r--r--src/lib/Server/Reports/reports/models.py6
9 files changed, 185 insertions, 121 deletions
diff --git a/src/lib/Logger.py b/src/lib/Logger.py
index e8cdd492d..a9c4372b7 100644
--- a/src/lib/Logger.py
+++ b/src/lib/Logger.py
@@ -13,15 +13,20 @@ import termios
logging.raiseExceptions = 0
+
def print_attributes(attrib):
"""Add the attributes for an element."""
return ' '.join(['%s="%s"' % data for data in list(attrib.items())])
+
def print_text(text):
"""Add text to the output (which will need normalising."""
- charmap = {'<':'&lt;', '>':'&gt;', '&':'&amp;'}
+ charmap = {'<': '&lt;',
+ '>': '&gt;',
+ '&': '&amp;'}
return ''.join([charmap.get(char, char) for char in text]) + '\n'
+
def xml_print(element, running_indent=0, indent=4):
"""Add an element and its children to the return string."""
if (len(element.getchildren()) == 0) and (not element.text):
@@ -32,7 +37,7 @@ def xml_print(element, running_indent=0, indent=4):
ret = (' ' * running_indent)
ret += '<%s%s>\n' % (element.tag, print_attributes(element))
if element.text:
- ret += (' '* child_indent) + print_text(element.text)
+ ret += (' ' * child_indent) + print_text(element.text)
for child in element.getchildren():
ret += xml_print(child, child_indent, indent)
ret += (' ' * running_indent) + '</%s>\n' % (element.tag)
@@ -40,16 +45,21 @@ def xml_print(element, running_indent=0, indent=4):
ret += (' ' * child_indent) + print_text(element.tail)
return ret
+
class TermiosFormatter(logging.Formatter):
- """The termios formatter displays output in a terminal-sensitive fashion."""
+ """The termios formatter displays output
+ in a terminal-sensitive fashion.
+ """
def __init__(self, fmt=None, datefmt=None):
logging.Formatter.__init__(self, fmt, datefmt)
if sys.stdout.isatty():
# now get termios info
try:
- self.width = struct.unpack('hhhh', fcntl.ioctl(0, termios.TIOCGWINSZ,
- "\000"*8))[1]
+ self.width = struct.unpack('hhhh',
+ fcntl.ioctl(0,
+ termios.TIOCGWINSZ,
+ "\000" * 8))[1]
if self.width == 0:
self.width = 80
except:
@@ -67,16 +77,16 @@ class TermiosFormatter(logging.Formatter):
if len(line) <= line_len:
returns.append(line)
else:
- inner_lines = int(math.floor(float(len(line)) / line_len))+1
+ inner_lines = int(math.floor(float(len(line)) / line_len)) + 1
for i in range(inner_lines):
- returns.append("%s" % (line[i*line_len:(i+1)*line_len]))
+ returns.append("%s" % (line[i * line_len:(i + 1) * line_len]))
elif isinstance(record.msg, list):
if not record.msg:
return ''
record.msg.sort()
msgwidth = self.width
columnWidth = max([len(item) for item in record.msg])
- columns = int(math.floor(float(msgwidth) / (columnWidth+2)))
+ columns = int(math.floor(float(msgwidth) / (columnWidth + 2)))
lines = int(math.ceil(float(len(record.msg)) / columns))
for lineNumber in range(lines):
indices = [idx for idx in [(colNum * lines) + lineNumber
@@ -91,6 +101,7 @@ class TermiosFormatter(logging.Formatter):
returns.append(self.formatException(record.exc_info))
return '\n'.join(returns)
+
class FragmentingSysLogHandler(logging.handlers.SysLogHandler):
"""
This handler fragments messages into
@@ -120,14 +131,16 @@ class FragmentingSysLogHandler(logging.handlers.SysLogHandler):
msgs = [record]
for newrec in msgs:
msg = self.log_format_string % (self.encodePriority(self.facility,
- newrec.levelname.lower()), self.format(newrec))
+ newrec.levelname.lower()),
+ self.format(newrec))
try:
self.socket.send(msg)
except socket.error:
- for i in xrange(10):
+ for i in range(10):
try:
if isinstance(self.address, tuple):
- self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ self.socket = socket.socket(socket.AF_INET,
+ socket.SOCK_DGRAM)
self.socket.connect(self.address)
else:
self._connect_unixsocket(self.address)
@@ -144,6 +157,7 @@ class FragmentingSysLogHandler(logging.handlers.SysLogHandler):
"""
pass
+
def setup_logging(procname, to_console=True, to_syslog=True, syslog_facility='daemon', level=0, to_file=None):
"""Setup logging for Bcfg2 software."""
if hasattr(logging, 'already_setup'):
@@ -158,9 +172,13 @@ def setup_logging(procname, to_console=True, to_syslog=True, syslog_facility='da
if to_syslog:
try:
try:
- syslog = FragmentingSysLogHandler(procname, '/dev/log', syslog_facility)
+ syslog = FragmentingSysLogHandler(procname,
+ '/dev/log',
+ syslog_facility)
except socket.error:
- syslog = FragmentingSysLogHandler(procname, ('localhost', 514), syslog_facility)
+ syslog = FragmentingSysLogHandler(procname,
+ ('localhost', 514),
+ syslog_facility)
syslog.setLevel(logging.DEBUG)
syslog.setFormatter(logging.Formatter('%(name)s[%(process)d]: %(message)s'))
logging.root.addHandler(syslog)
@@ -176,6 +194,7 @@ def setup_logging(procname, to_console=True, to_syslog=True, syslog_facility='da
logging.root.setLevel(level)
logging.already_setup = True
+
def trace_process(**kwargs):
"""Literally log every line of python code as it runs.
@@ -207,6 +226,7 @@ def trace_process(**kwargs):
sys.settrace(traceit)
+
def log_to_stderr(logger_name, level=logging.INFO):
"""Set up console logging."""
try:
@@ -214,11 +234,12 @@ def log_to_stderr(logger_name, level=logging.INFO):
except:
# assume logger_name is already a logger
logger = logger_name
- handler = logging.StreamHandler() # sys.stderr is the default stream
+ handler = logging.StreamHandler() # sys.stderr is the default stream
handler.setLevel(level)
- handler.setFormatter(TermiosFormatter()) # investigate this formatter
+ handler.setFormatter(TermiosFormatter()) # investigate this formatter
logger.addHandler(handler)
+
def log_to_syslog(logger_name, level=logging.INFO, format='%(name)s[%(process)d]: %(message)s'):
"""Set up syslog logging."""
try:
@@ -227,7 +248,7 @@ def log_to_syslog(logger_name, level=logging.INFO, format='%(name)s[%(process)d]
# assume logger_name is already a logger
logger = logger_name
# anticipate an exception somewhere below
- handler = logging.handlers.SysLogHandler() # investigate FragmentingSysLogHandler
+ handler = logging.handlers.SysLogHandler() # investigate FragmentingSysLogHandler
handler.setLevel(level)
handler.setFormatter(logging.Formatter(format))
logger.addHandler(handler)
diff --git a/src/lib/Proxy.py b/src/lib/Proxy.py
index 42ff6a12b..8b3fcb87c 100644
--- a/src/lib/Proxy.py
+++ b/src/lib/Proxy.py
@@ -37,10 +37,13 @@ import urlparse
import xmlrpclib
version = sys.version_info[:2]
-has_py23 = map(int, version) >= [2, 3]
-has_py26 = map(int, version) >= [2, 6]
+has_py23 = version >= (2, 3)
+has_py26 = version >= (2, 6)
-__all__ = ["ComponentProxy", "RetryMethod", "SSLHTTPConnection", "XMLRPCTransport"]
+__all__ = ["ComponentProxy",
+ "RetryMethod",
+ "SSLHTTPConnection",
+ "XMLRPCTransport"]
class CertificateError(Exception):
@@ -88,7 +91,9 @@ xmlrpclib._Method = RetryMethod
class SSLHTTPConnection(httplib.HTTPConnection):
- """Extension of HTTPConnection that implements SSL and related behaviors."""
+ """Extension of HTTPConnection that
+ implements SSL and related behaviors.
+ """
logger = logging.getLogger('Bcfg2.Proxy.SSLHTTPConnection')
@@ -157,7 +162,7 @@ class SSLHTTPConnection(httplib.HTTPConnection):
elif SSL_LIB == 'm2crypto':
self._connect_m2crypto()
else:
- raise Exception, "No SSL module support"
+ raise Exception("No SSL module support")
def _connect_py26ssl(self):
"""Initiates a connection using the ssl module."""
@@ -168,7 +173,7 @@ class SSLHTTPConnection(httplib.HTTPConnection):
ssl_protocol_ver = ssl.PROTOCOL_TLSv1
else:
self.logger.error("Unknown protocol %s" % (self.protocol))
- raise Exception, "unknown protocol %s" % self.protocol
+ raise Exception("unknown protocol %s" % self.protocol)
if self.ca:
other_side_required = ssl.CERT_REQUIRED
else:
@@ -192,7 +197,7 @@ class SSLHTTPConnection(httplib.HTTPConnection):
if peer_cert and self.scns:
scn = [x[0][1] for x in peer_cert['subject'] if x[0][0] == 'commonName'][0]
if scn not in self.scns:
- raise CertificateError, scn
+ raise CertificateError(scn)
self.sock.closeSocket = True
def _connect_m2crypto(self):
@@ -204,7 +209,7 @@ class SSLHTTPConnection(httplib.HTTPConnection):
ctx = SSL.Context('tlsv1')
else:
self.logger.error("Unknown protocol %s" % (self.protocol))
- raise Exception, "unknown protocol %s" % self.protocol
+ raise Exception("unknown protocol %s" % self.protocol)
if self.ca:
# Use the certificate authority to validate the cert
@@ -238,7 +243,7 @@ class SSLHTTPConnection(httplib.HTTPConnection):
self.sock.connect((hostname, self.port))
# automatically checks cert matches host
except M2Crypto.SSL.Checker.WrongHost, wr:
- raise CertificateError, wr
+ raise CertificateError(wr)
class XMLRPCTransport(xmlrpclib.Transport):
@@ -254,8 +259,12 @@ class XMLRPCTransport(xmlrpclib.Transport):
def make_connection(self, host):
host, self._extra_headers = self.get_host_info(host)[0:2]
- http = SSLHTTPConnection(host, key=self.key, cert=self.cert, ca=self.ca,
- scns=self.scns, timeout=self.timeout)
+ http = SSLHTTPConnection(host,
+ key=self.key,
+ cert=self.cert,
+ ca=self.ca,
+ scns=self.scns,
+ timeout=self.timeout)
https = httplib.HTTP()
https._setup(http)
return https
@@ -271,7 +280,10 @@ class XMLRPCTransport(xmlrpclib.Transport):
errcode, errmsg, headers = h.getreply()
if errcode != 200:
- raise xmlrpclib.ProtocolError(host + handler, errcode, errmsg, headers)
+ raise xmlrpclib.ProtocolError(host + handler,
+ errcode,
+ errmsg,
+ headers)
self.verbose = verbose
msglen = int(headers.dict['content-length'])
@@ -290,7 +302,7 @@ class XMLRPCTransport(xmlrpclib.Transport):
if not response:
break
if self.verbose:
- print "body:", repr(response), len(response)
+ print("body:", repr(response), len(response))
p.feed(response)
fd.close()
diff --git a/src/lib/SSLServer.py b/src/lib/SSLServer.py
index 1f4c1c8e4..2395bb84f 100644
--- a/src/lib/SSLServer.py
+++ b/src/lib/SSLServer.py
@@ -20,11 +20,14 @@ import ssl
import threading
import time
+
class ForkedChild(Exception):
pass
+
class XMLRPCDispatcher (SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
logger = logging.getLogger("Cobalt.Server.XMLRPCDispatcher")
+
def __init__(self, allow_none, encoding):
try:
SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self,
@@ -60,6 +63,7 @@ class XMLRPCDispatcher (SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
allow_none=self.allow_none, encoding=self.encoding)
return raw_response
+
class SSLServer (SocketServer.TCPServer, object):
"""TCP server supporting SSL encryption.
@@ -76,7 +80,8 @@ class SSLServer (SocketServer.TCPServer, object):
logger = logging.getLogger("Cobalt.Server.TCPServer")
def __init__(self, server_address, RequestHandlerClass, keyfile=None,
- certfile=None, reqCert=False, ca=None, timeout=None, protocol='xmlrpc/ssl'):
+ certfile=None, reqCert=False, ca=None, timeout=None,
+ protocol='xmlrpc/ssl'):
"""Initialize the SSL-TCP server.
@@ -106,17 +111,17 @@ class SSLServer (SocketServer.TCPServer, object):
if keyfile != None:
if keyfile == False or not os.path.exists(keyfile):
self.logger.error("Keyfile %s does not exist" % keyfile)
- raise Exception, "keyfile doesn't exist"
+ raise Exception("keyfile doesn't exist")
self.certfile = certfile
if certfile != None:
if certfile == False or not os.path.exists(certfile):
self.logger.error("Certfile %s does not exist" % certfile)
- raise Exception, "certfile doesn't exist"
+ raise Exception("certfile doesn't exist")
self.ca = ca
if ca != None:
if ca == False or not os.path.exists(ca):
self.logger.error("CA %s does not exist" % ca)
- raise Exception, "ca doesn't exist"
+ raise Exception("ca doesn't exist")
self.reqCert = reqCert
if ca and certfile:
self.mode = ssl.CERT_OPTIONAL
@@ -128,14 +133,18 @@ class SSLServer (SocketServer.TCPServer, object):
self.ssl_protocol = ssl.PROTOCOL_TLSv1
else:
self.logger.error("Unknown protocol %s" % (protocol))
- raise Exception, "unknown protocol %s" % protocol
+ raise Exception("unknown protocol %s" % protocol)
def get_request(self):
(sock, sockinfo) = self.socket.accept()
sock.settimeout(self.timeout)
- sslsock = ssl.wrap_socket(sock, server_side=True, certfile=self.certfile,
- keyfile=self.keyfile, cert_reqs=self.mode,
- ca_certs=self.ca, ssl_version=self.ssl_protocol)
+ sslsock = ssl.wrap_socket(sock,
+ server_side=True,
+ certfile=self.certfile,
+ keyfile=self.keyfile,
+ cert_reqs=self.mode,
+ ca_certs=self.ca,
+ ssl_version=self.ssl_protocol)
return sslsock, sockinfo
def close_request(self, request):
@@ -212,20 +221,21 @@ class XMLRPCRequestHandler (SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
### need to override do_POST here
def do_POST(self):
try:
- max_chunk_size = 10*1024*1024
+ max_chunk_size = 10 * 1024 * 1024
size_remaining = int(self.headers["content-length"])
L = []
while size_remaining:
try:
select.select([self.rfile.fileno()], [], [], 3)
except select.error:
- print "got select timeout"
+ print("got select timeout")
raise
chunk_size = min(size_remaining, max_chunk_size)
L.append(self.rfile.read(chunk_size))
size_remaining -= len(L[-1])
data = ''.join(L)
- response = self.server._marshaled_dispatch(self.client_address, data)
+ response = self.server._marshaled_dispatch(self.client_address,
+ data)
except:
try:
self.send_response(500)
@@ -233,7 +243,7 @@ class XMLRPCRequestHandler (SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
except:
(type, msg) = sys.exc_info()[:2]
self.logger.error("Error sending 500 response (%s): %s" % \
- (type, msg))
+ (type, msg))
raise
else:
# got a valid XML RPC response
@@ -267,7 +277,7 @@ class XMLRPCRequestHandler (SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
(self.client_address[0], msg))
else:
self.logger.error("Error sending response (%s): %s" % \
- (type, msg))
+ (type, msg))
def finish(self):
# shut down the connection
@@ -276,6 +286,7 @@ class XMLRPCRequestHandler (SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
self.wfile.close()
self.rfile.close()
+
class XMLRPCServer (SocketServer.ThreadingMixIn, SSLServer,
XMLRPCDispatcher, object):
@@ -355,6 +366,7 @@ class XMLRPCServer (SocketServer.ThreadingMixIn, SSLServer,
def _get_require_auth(self):
return getattr(self.RequestHandlerClass, "require_auth", False)
+
def _set_require_auth(self, value):
self.RequestHandlerClass.require_auth = value
require_auth = property(_get_require_auth, _set_require_auth)
@@ -364,6 +376,7 @@ class XMLRPCServer (SocketServer.ThreadingMixIn, SSLServer,
return self.RequestHandlerClass.credentials
except AttributeError:
return dict()
+
def _set_credentials(self, value):
self.RequestHandlerClass.credentials = value
credentials = property(_get_credentials, _set_credentials)
@@ -375,7 +388,7 @@ class XMLRPCServer (SocketServer.ThreadingMixIn, SSLServer,
except AttributeError:
name = "unknown"
if hasattr(instance, 'plugins'):
- for pname, pinst in instance.plugins.iteritems():
+ for pname, pinst in list(instance.plugins.items()):
for mname in pinst.__rmi__:
xmname = "%s.%s" % (pname, mname)
fn = getattr(pinst, mname)
diff --git a/src/lib/Server/Core.py b/src/lib/Server/Core.py
index ac67b8a69..e82e05a89 100644
--- a/src/lib/Server/Core.py
+++ b/src/lib/Server/Core.py
@@ -16,6 +16,7 @@ import Bcfg2.Server.Plugins.Metadata
logger = logging.getLogger('Bcfg2.Server.Core')
+
def critical_error(operation):
"""Log and err, traceback and return an xmlrpc fault to client."""
logger.error(operation, exc_info=1)
@@ -27,12 +28,16 @@ try:
except:
pass
+
class CoreInitError(Exception):
"""This error is raised when the core cannot be initialized."""
pass
+
class Core(Component):
- """The Core object is the container for all Bcfg2 Server logic and modules."""
+ """The Core object is the container for all
+ Bcfg2 Server logic and modules.
+ """
name = 'bcfg2-server'
implementation = 'bcfg2-server'
@@ -42,15 +47,16 @@ class Core(Component):
Component.__init__(self)
self.datastore = repo
if filemonitor not in Bcfg2.Server.FileMonitor.available:
- logger.error("File monitor driver %s not available; forcing to default" % filemonitor)
+ logger.error("File monitor driver %s not available; "
+ "forcing to default" % filemonitor)
filemonitor = 'default'
try:
self.fam = Bcfg2.Server.FileMonitor.available[filemonitor]()
except IOError:
logger.error("Failed to instantiate fam driver %s" % filemonitor,
exc_info=1)
- raise CoreInitError, "failed to instantiate fam driver (used %s)" % \
- filemonitor
+ raise CoreInitError("failed to instantiate fam driver (used %s)" % \
+ filemonitor)
self.pubspace = {}
self.cfile = cfile
self.cron = {}
@@ -70,44 +76,43 @@ class Core(Component):
if not plugin in self.plugins:
self.init_plugins(plugin)
# Remove blacklisted plugins
- for p, bl in self.plugin_blacklist.items():
+ for p, bl in list(self.plugin_blacklist.items()):
if len(bl) > 0:
logger.error("The following plugins conflict with %s;"
"Unloading %s" % (p, bl))
for plug in bl:
del self.plugins[plug]
# This section loads the experimental plugins
- expl = [plug for (name, plug) in self.plugins.iteritems()
+ expl = [plug for (name, plug) in list(self.plugins.items())
if plug.experimental]
if expl:
logger.info("Loading experimental plugin(s): %s" % \
(" ".join([x.name for x in expl])))
logger.info("NOTE: Interfaces subject to change")
- depr = [plug for (name, plug) in self.plugins.iteritems()
+ depr = [plug for (name, plug) in list(self.plugins.items())
if plug.deprecated]
# This section loads the deprecated plugins
if depr:
logger.info("Loading deprecated plugin(s): %s" % \
(" ".join([x.name for x in depr])))
-
- mlist = [p for p in self.plugins.values() if \
+ mlist = [p for p in list(self.plugins.values()) if \
isinstance(p, Bcfg2.Server.Plugin.Metadata)]
if len(mlist) == 1:
self.metadata = mlist[0]
else:
logger.error("No Metadata Plugin loaded; failed to instantiate Core")
- raise CoreInitError, "No Metadata Plugin"
- self.statistics = [plugin for plugin in self.plugins.values() if \
- isinstance(plugin, Bcfg2.Server.Plugin.Statistics)]
- self.pull_sources = [plugin for plugin in self.statistics if \
- isinstance(plugin, Bcfg2.Server.Plugin.PullSource)]
- self.generators = [plugin for plugin in self.plugins.values() if \
- isinstance(plugin, Bcfg2.Server.Plugin.Generator)]
- self.structures = [plugin for plugin in self.plugins.values() if \
- isinstance(plugin, Bcfg2.Server.Plugin.Structure)]
- self.connectors = [plugin for plugin in self.plugins.values() if \
- isinstance(plugin, Bcfg2.Server.Plugin.Connector)]
+ raise CoreInitError("No Metadata Plugin")
+ self.statistics = [plugin for plugin in list(self.plugins.values())
+ if isinstance(plugin, Bcfg2.Server.Plugin.Statistics)]
+ self.pull_sources = [plugin for plugin in self.statistics
+ if isinstance(plugin, Bcfg2.Server.Plugin.PullSource)]
+ self.generators = [plugin for plugin in list(self.plugins.values())
+ if isinstance(plugin, Bcfg2.Server.Plugin.Generator)]
+ self.structures = [plugin for plugin in list(self.plugins.values())
+ if isinstance(plugin, Bcfg2.Server.Plugin.Structure)]
+ self.connectors = [plugin for plugin in list(self.plugins.values())
+ if isinstance(plugin, Bcfg2.Server.Plugin.Connector)]
self.ca = ca
self.fam_thread = threading.Thread(target=self._file_monitor_thread)
if start_fam_thread:
@@ -128,7 +133,7 @@ class Core(Component):
except:
continue
# VCS plugin periodic updates
- for plugin in self.plugins.values():
+ for plugin in list(self.plugins.values()):
if isinstance(plugin, Bcfg2.Server.Plugin.Version):
self.revision = plugin.get_revision()
@@ -157,15 +162,15 @@ class Core(Component):
(plugin), exc_info=1)
def shutdown(self):
- """Shuting down the plugins."""
+ """Shutting down the plugins."""
if not self.terminate.isSet():
self.terminate.set()
- for plugin in self.plugins.values():
+ for plugin in list(self.plugins.values()):
plugin.shutdown()
def validate_data(self, metadata, data, base_cls):
"""Checks the data structure."""
- for plugin in self.plugins.values():
+ for plugin in list(self.plugins.values()):
if isinstance(plugin, base_cls):
try:
if base_cls == Bcfg2.Server.Plugin.StructureValidator:
@@ -182,7 +187,7 @@ class Core(Component):
def GetStructures(self, metadata):
"""Get all structures for client specified by metadata."""
- structures = reduce(lambda x, y:x+y,
+ structures = reduce(lambda x, y: x + y,
[struct.BuildStructures(metadata) for struct \
in self.structures], [])
sbundles = [b.get('name') for b in structures if b.tag == 'Bundle']
@@ -232,7 +237,8 @@ class Core(Component):
glist = [gen for gen in self.generators if
entry.get('name') in gen.Entries.get(entry.tag, {})]
if len(glist) == 1:
- return glist[0].Entries[entry.tag][entry.get('name')](entry, metadata)
+ return glist[0].Entries[entry.tag][entry.get('name')](entry,
+ metadata)
elif len(glist) > 1:
generators = ", ".join([gen.name for gen in glist])
logger.error("%s %s served by multiple generators: %s" % \
@@ -242,7 +248,7 @@ class Core(Component):
if len(g2list) == 1:
return g2list[0].HandleEntry(entry, metadata)
entry.set('failure', 'no matching generator')
- raise PluginExecutionError, (entry.tag, entry.get('name'))
+ raise PluginExecutionError(entry.tag, entry.get('name'))
def BuildConfiguration(self, client):
"""Build configuration for clients."""
@@ -290,7 +296,7 @@ class Core(Component):
def GetDecisions(self, metadata, mode):
"""Get data for the decision list."""
result = []
- for plugin in self.plugins.values():
+ for plugin in list(self.plugins.values()):
try:
if isinstance(plugin, Bcfg2.Server.Plugin.Decision):
result += plugin.GetDecisions(metadata, mode)
@@ -300,7 +306,7 @@ class Core(Component):
return result
def build_metadata(self, client_name):
- """Build the metadata structure."""
+ """Build the metadata structure."""
if not hasattr(self, 'metadata'):
# some threads start before metadata is even loaded
raise Bcfg2.Server.Plugins.Metadata.MetadataRuntimeError
diff --git a/src/lib/Server/FileMonitor.py b/src/lib/Server/FileMonitor.py
index 0f09f7751..d6b313e6b 100644
--- a/src/lib/Server/FileMonitor.py
+++ b/src/lib/Server/FileMonitor.py
@@ -7,6 +7,7 @@ from time import sleep, time
logger = logging.getLogger('Bcfg2.Server.FileMonitor')
+
def ShouldIgnore(event):
"""Test if the event should be suppresed."""
# FIXME should move event suppression out of the core
@@ -18,6 +19,7 @@ def ShouldIgnore(event):
return True
return False
+
class Event(object):
def __init__(self, request_id, filename, code):
self.requestID = request_id
@@ -29,6 +31,8 @@ class Event(object):
return self.action
available = {}
+
+
class FileMonitor(object):
"""File Monitor baseclass."""
def __init__(self, debug=False):
@@ -78,7 +82,7 @@ class FileMonitor(object):
if lock:
lock.release()
end = time()
- logger.info("Handled %d events in %.03fs" % (count, (end-start)))
+ logger.info("Handled %d events in %.03fs" % (count, (end - start)))
def handle_events_in_interval(self, interval):
end = time() + interval
@@ -91,7 +95,9 @@ class FileMonitor(object):
class FamFam(object):
- """The fam object is a set of callbacks for file alteration events (FAM support)."""
+ """The fam object is a set of callbacks for
+ file alteration events (FAM support).
+ """
def __init__(self):
object.__init__(self)
@@ -164,7 +170,6 @@ class FamFam(object):
return count
-
class Fam(FileMonitor):
"""
The fam object is a set of callbacks for
@@ -195,6 +200,7 @@ class Fam(FileMonitor):
def get_event(self):
return self.fm.nextEvent()
+
class Pseudo(FileMonitor):
"""
The fam object is a set of callbacks for
@@ -213,14 +219,16 @@ class Pseudo(FileMonitor):
def AddMonitor(self, path, obj):
"""add a monitor to path, installing a callback to obj.HandleEvent"""
- handleID = len(self.handles.keys())
+ handleID = len(list(self.handles.keys()))
mode = os.stat(path)[stat.ST_MODE]
handle = Event(handleID, path, 'exists')
if stat.S_ISDIR(mode):
dirList = os.listdir(path)
self.pending_events.append(handle)
for includedFile in dirList:
- self.pending_events.append(Event(handleID, includedFile, 'exists'))
+ self.pending_events.append(Event(handleID,
+ includedFile,
+ 'exists'))
self.pending_events.append(Event(handleID, path, 'endExist'))
else:
self.pending_events.append(Event(handleID, path, 'exists'))
diff --git a/src/lib/Server/Hostbase/ldapauth.py b/src/lib/Server/Hostbase/ldapauth.py
index f2148181f..21b462c86 100644
--- a/src/lib/Server/Hostbase/ldapauth.py
+++ b/src/lib/Server/Hostbase/ldapauth.py
@@ -1,16 +1,18 @@
-"""Checks with LDAP (ActiveDirectory) to see if the current user is an LDAP(AD) user,
-and returns a subset of the user's profile that is needed by Argonne/CIS to
-to set user level privleges in Django"""
-
-__revision__ = '$Revision: 2456 $'
+"""
+Checks with LDAP (ActiveDirectory) to see if the current user is an LDAP(AD)
+user, and returns a subset of the user's profile that is needed by Argonne/CIS
+to set user level privleges in Django
+"""
import os
import ldap
+
class LDAPAUTHError(Exception):
"""LDAPAUTHError is raised when somehting goes boom."""
pass
+
class ldapauth(object):
group_test = False
check_member_of = os.environ['LDAP_CHECK_MBR_OF_GRP']
@@ -20,35 +22,35 @@ class ldapauth(object):
telephoneNumber = None
title = None
memberOf = None
- department = None #this will be a list
+ department = None # this will be a list
mail = None
- extensionAttribute1 = None #badgenumber
+ extensionAttribute1 = None # badgenumber
badge_no = None
- def __init__(self,login,passwd):
+ def __init__(self, login, passwd):
"""get username (if using ldap as auth the
apache env var REMOTE_USER should be used)
from username get user profile from AD/LDAP
"""
#p = self.user_profile(login,passwd)
- d = self.user_dn(login) #success, distname
- print d[1]
+ d = self.user_dn(login) # success, distname
+ print(d[1])
if d[0] == 'success':
pass
- p = self.user_bind(d[1],passwd)
+ p = self.user_bind(d[1], passwd)
if p[0] == 'success':
#parse results
parsed = self.parse_results(p[2])
- print self.department
+ print(self.department)
self.group_test = self.member_of()
securitylevel = self.security_level()
- print "ACCESS LEVEL: " + str(securitylevel)
+ print("ACCESS LEVEL: " + str(securitylevel))
else:
raise LDAPAUTHError(p[2])
else:
raise LDAPAUTHError(p[2])
- def user_profile(self,login,passwd=None):
+ def user_profile(self, login, passwd=None):
"""NOT USED RIGHT NOW"""
ldap_login = "CN=%s" % login
svc_acct = os.environ['LDAP_SVC_ACCT_NAME']
@@ -60,33 +62,35 @@ class ldapauth(object):
try:
conn = ldap.initialize(os.environ['LDAP_URI'])
- conn.bind(svc_acct,svc_pass,ldap.AUTH_SIMPLE)
+ conn.bind(svc_acct, svc_pass, ldap.AUTH_SIMPLE)
result_id = conn.search(search_pth,
- ldap.SCOPE_SUBTREE,
- ldap_login,None)
- result_type,result_data = conn.result(result_id,0)
- return ('success','User profile found',result_data,)
- except ldap.LDAPError,e:
+ ldap.SCOPE_SUBTREE,
+ ldap_login,
+ None)
+ result_type, result_data = conn.result(result_id, 0)
+ return ('success', 'User profile found', result_data,)
+ except ldap.LDAPError, e:
#connection failed
- return ('error','LDAP connect failed',e,)
+ return ('error', 'LDAP connect failed', e,)
- def user_bind(self,distinguishedName,passwd):
+ def user_bind(self, distinguishedName, passwd):
"""Binds to LDAP Server"""
search_pth = os.environ['LDAP_SEARCH_PTH']
try:
conn = ldap.initialize(os.environ['LDAP_URI'])
- conn.bind(distinguishedName,passwd,ldap.AUTH_SIMPLE)
+ conn.bind(distinguishedName, passwd, ldap.AUTH_SIMPLE)
cn = distinguishedName.split(",")
result_id = conn.search(search_pth,
- ldap.SCOPE_SUBTREE,
- cn[0],None)
- result_type,result_data = conn.result(result_id,0)
- return ('success','User profile found',result_data,)
- except ldap.LDAPError,e:
+ ldap.SCOPE_SUBTREE,
+ cn[0],
+ None)
+ result_type, result_data = conn.result(result_id, 0)
+ return ('success', 'User profile found', result_data,)
+ except ldap.LDAPError, e:
#connection failed
- return ('error','LDAP connect failed',e,)
+ return ('error', 'LDAP connect failed', e,)
- def user_dn(self,cn):
+ def user_dn(self, cn):
"""Uses Service Account to get distinguishedName"""
ldap_login = "CN=%s" % cn
svc_acct = os.environ['LDAP_SVC_ACCT_NAME']
@@ -95,19 +99,20 @@ class ldapauth(object):
try:
conn = ldap.initialize(os.environ['LDAP_URI'])
- conn.bind(svc_acct,svc_pass,ldap.AUTH_SIMPLE)
+ conn.bind(svc_acct, svc_pass, ldap.AUTH_SIMPLE)
result_id = conn.search(search_pth,
- ldap.SCOPE_SUBTREE,
- ldap_login,None)
- result_type,result_data = conn.result(result_id,0)
+ ldap.SCOPE_SUBTREE,
+ ldap_login,
+ None)
+ result_type, result_data = conn.result(result_id, 0)
raw_obj = result_data[0][1]
distinguishedName = raw_obj['distinguishedName']
- return ('success',distinguishedName[0],)
- except ldap.LDAPError,e:
+ return ('success', distinguishedName[0],)
+ except ldap.LDAPError, e:
#connection failed
- return ('error','LDAP connect failed',e,)
+ return ('error', 'LDAP connect failed', e,)
- def parse_results(self,user_obj):
+ def parse_results(self, user_obj):
"""Clean up the huge ugly object handed to us in the LDAP query"""
#user_obj is a list formatted like this:
#[('LDAP_DN',{user_dict},),]
@@ -169,4 +174,3 @@ class ldapauth(object):
level = 4
return level
-
diff --git a/src/lib/Server/Plugin.py b/src/lib/Server/Plugin.py
index 5a6f3281b..ac9479d44 100644
--- a/src/lib/Server/Plugin.py
+++ b/src/lib/Server/Plugin.py
@@ -363,7 +363,7 @@ class DirectoryBacked(object):
return self.entries[key]
def __iter__(self):
- return iter(self.entries.items())
+ return iter(list(self.entries.items()))
def AddEntry(self, name):
"""Add new entry to data structures upon file creation."""
diff --git a/src/lib/Server/Plugins/Pkgmgr.py b/src/lib/Server/Plugins/Pkgmgr.py
index dc4a5f37f..b96e7ea7d 100644
--- a/src/lib/Server/Plugins/Pkgmgr.py
+++ b/src/lib/Server/Plugins/Pkgmgr.py
@@ -62,7 +62,7 @@ class PNode(Bcfg2.Server.Plugin.INode):
if 'Package' not in pdict:
pdict['Package'] = set()
for child in data.getchildren():
- for attr in [key for key in data.attrib.keys()
+ for attr in [key for key in list(data.attrib.keys())
if key != 'name' and key not in child.attrib]:
try:
child.set(attr, data.get(attr))
diff --git a/src/lib/Server/Reports/reports/models.py b/src/lib/Server/Reports/reports/models.py
index c2be40870..d94b2e1ba 100644
--- a/src/lib/Server/Reports/reports/models.py
+++ b/src/lib/Server/Reports/reports/models.py
@@ -48,7 +48,7 @@ class ClientManager(models.Manager):
if timestamp == None:
timestamp = datetime.now()
elif not isinstance(timestamp, datetime):
- raise ValueError, 'Expected a datetime object'
+ raise ValueError('Expected a datetime object')
else:
try:
timestamp = datetime(*strptime(timestamp,
@@ -113,7 +113,7 @@ class InteractiveManager(models.Manager):
"""
if maxdate and not isinstance(maxdate, datetime):
- raise ValueError, 'Expected a datetime object'
+ raise ValueError('Expected a datetime object')
return self.filter(id__in=self.get_interaction_per_client_ids(maxdate, active_only))
def get_interaction_per_client_ids(self, maxdate=None, active_only=True):
@@ -133,7 +133,7 @@ class InteractiveManager(models.Manager):
'as timer from reports_interaction'
if maxdate:
if not isinstance(maxdate, datetime):
- raise ValueError, 'Expected a datetime object'
+ raise ValueError('Expected a datetime object')
sql = sql + " where timestamp <= '%s' " % maxdate
cfilter = "(expiration is null or expiration > '%s') and creation <= '%s'" % (maxdate, maxdate)
sql = sql + ' GROUP BY client_id) x, reports_interaction where ' + \