summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Bcfg2Py3k.py30
-rw-r--r--src/lib/Server/Plugin.py27
-rw-r--r--src/lib/Server/Plugins/Base.py7
-rw-r--r--src/lib/Server/Plugins/Bundler.py3
-rw-r--r--src/lib/Server/Plugins/Cfg.py16
-rw-r--r--src/lib/Server/Plugins/DBStats.py6
-rw-r--r--src/lib/Server/Plugins/Decisions.py3
-rw-r--r--src/lib/Server/Plugins/Hostbase.py25
-rw-r--r--src/lib/Server/Plugins/Ldap.py3
-rw-r--r--src/lib/Server/Plugins/Metadata.py3
-rw-r--r--src/lib/Server/Plugins/NagiosGen.py6
-rw-r--r--src/lib/Server/Plugins/Packages.py26
-rw-r--r--src/lib/Server/Plugins/Properties.py5
-rw-r--r--src/lib/Server/Plugins/SGenshi.py9
-rw-r--r--src/lib/Server/Plugins/SSHbase.py3
-rw-r--r--src/lib/Server/Plugins/SSLCA.py3
-rw-r--r--src/lib/Server/Plugins/Snapshots.py40
-rw-r--r--src/lib/Server/Plugins/Statistics.py4
-rw-r--r--src/lib/Server/Plugins/TCheetah.py8
-rw-r--r--src/lib/Server/Plugins/TGenshi.py18
20 files changed, 169 insertions, 76 deletions
diff --git a/src/lib/Bcfg2Py3k.py b/src/lib/Bcfg2Py3k.py
index b334c4466..0f5321a41 100644
--- a/src/lib/Bcfg2Py3k.py
+++ b/src/lib/Bcfg2Py3k.py
@@ -5,13 +5,21 @@ except ImportError:
# urllib imports
try:
- from urllib import urlopen
-except ImportError:
- from urllib.request import urlopen
-try:
from urlparse import urljoin
+ from urllib2 import HTTPBasicAuthHandler
+ from urllib2 import HTTPPasswordMgrWithDefaultRealm
+ from urllib2 import build_opener
+ from urllib2 import install_opener
+ from urllib import urlopen
+ from urllib2 import HTTPError
except ImportError:
from urllib.parse import urljoin
+ from urllib.request import HTTPBasicAuthHandler
+ from urllib.request import HTTPPasswordMgrWithDefaultRealm
+ from urllib.request import build_opener
+ from urllib.request import install_opener
+ from urllib.request import urlopen
+ from urllib.error import HTTPError
try:
from cStringIO import StringIO
@@ -22,3 +30,17 @@ try:
import ConfigParser
except ImportError:
import configparser as ConfigParser
+
+try:
+ import cPickle
+except ImportError:
+ import pickle as cPickle
+
+try:
+ from Queue import Queue
+ from Queue import Empty
+ from Queue import Full
+except ImportError:
+ from queue import Queue
+ from queue import Empty
+ from queue import Full
diff --git a/src/lib/Server/Plugin.py b/src/lib/Server/Plugin.py
index 69b38c4ff..e18bf09a1 100644
--- a/src/lib/Server/Plugin.py
+++ b/src/lib/Server/Plugin.py
@@ -8,13 +8,19 @@ import os
import pickle
import posixpath
import re
-import Queue
import threading
from lxml.etree import XML, XMLSyntaxError
import Bcfg2.Options
+# py3k compatibility
+if sys.hexversion >= 0x03000000:
+ from functools import reduce
+from Bcfg2.Bcfg2Py3k import Queue
+from Bcfg2.Bcfg2Py3k import Empty
+from Bcfg2.Bcfg2Py3k import Full
+
# grab default metadata info from bcfg2.conf
opts = {'owner': Bcfg2.Options.MDATA_OWNER,
'group': Bcfg2.Options.MDATA_GROUP,
@@ -169,7 +175,7 @@ class ThreadedStatistics(Statistics,
threading.Thread.__init__(self)
# Event from the core signaling an exit
self.terminate = core.terminate
- self.work_queue = Queue.Queue(100000)
+ self.work_queue = Queue(100000)
self.pending_file = "%s/etc/%s.pending" % (datastore, self.__class__.__name__)
self.daemon = True
self.start()
@@ -184,7 +190,7 @@ class ThreadedStatistics(Statistics,
pending_data.append((metadata.hostname, lxml.etree.tostring(data)))
except:
self.logger.warning("Dropping interaction for %s" % metadata.hostname)
- except Queue.Empty:
+ except Empty:
pass
try:
@@ -204,7 +210,8 @@ class ThreadedStatistics(Statistics,
savefile = open(self.pending_file, 'r')
pending_data = pickle.load(savefile)
savefile.close()
- except Exception, e:
+ except Exception:
+ e = sys.exc_info()[1]
self.logger.warning("Failed to load pending data: %s" % e)
for (pmetadata, pdata) in pending_data:
# check that shutdown wasnt called early
@@ -224,10 +231,11 @@ class ThreadedStatistics(Statistics,
return False
self.work_queue.put_nowait((metadata, lxml.etree.fromstring(pdata)))
- except Queue.Full:
+ except Full:
self.logger.warning("Queue.Full: Failed to load queue data")
break
- except lxml.etree.LxmlError, lxml_error:
+ except lxml.etree.LxmlError:
+ lxml_error = sys.exc_info()[1]
self.logger.error("Unable to load save interaction: %s" % lxml_error)
except Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError:
self.logger.error("Unable to load metadata for save interaction: %s" % pmetadata)
@@ -244,9 +252,10 @@ class ThreadedStatistics(Statistics,
while not self.terminate.isSet():
try:
(xdata, client) = self.work_queue.get(block=True, timeout=2)
- except Queue.Empty:
+ except Empty:
continue
- except Exception, e:
+ except Exception:
+ e = sys.exc_info()[1]
self.logger.error("ThreadedStatistics: %s" % e)
continue
self.handle_statistic(xdata, client)
@@ -258,7 +267,7 @@ class ThreadedStatistics(Statistics,
try:
self.work_queue.put_nowait((metadata, copy.deepcopy(data)))
warned = False
- except Queue.Full:
+ except Full:
if not warned:
self.logger.warning("%s: Queue is full. Dropping interactions." % self.__class__.__name__)
warned = True
diff --git a/src/lib/Server/Plugins/Base.py b/src/lib/Server/Plugins/Base.py
index 2b241da9d..5e7d89727 100644
--- a/src/lib/Server/Plugins/Base.py
+++ b/src/lib/Server/Plugins/Base.py
@@ -1,9 +1,14 @@
"""This module sets up a base list of configuration entries."""
__revision__ = '$Revision$'
-import Bcfg2.Server.Plugin
import copy
import lxml.etree
+import sys
+# py3k compatibility
+if sys.hexversion >= 0x03000000:
+ from functools import reduce
+
+import Bcfg2.Server.Plugin
class Base(Bcfg2.Server.Plugin.Plugin,
diff --git a/src/lib/Server/Plugins/Bundler.py b/src/lib/Server/Plugins/Bundler.py
index 04df8ea86..1a8e7348b 100644
--- a/src/lib/Server/Plugins/Bundler.py
+++ b/src/lib/Server/Plugins/Bundler.py
@@ -80,7 +80,8 @@ class Bundler(Bcfg2.Server.Plugin.Plugin,
elif len(entries) == 1:
try:
bundleset.append(entries[0].get_xml_value(metadata))
- except genshi.template.base.TemplateError, t:
+ except genshi.template.base.TemplateError:
+ t = sys.exc_info()[1]
self.logger.error("Bundler: Failed to template genshi bundle %s" \
% (bundlename))
self.logger.error(t)
diff --git a/src/lib/Server/Plugins/Cfg.py b/src/lib/Server/Plugins/Cfg.py
index 6c7a40a52..41cf6c9c1 100644
--- a/src/lib/Server/Plugins/Cfg.py
+++ b/src/lib/Server/Plugins/Cfg.py
@@ -6,6 +6,7 @@ import logging
import lxml
import os
import re
+import sys
import tempfile
import Bcfg2.Server.Plugin
@@ -21,6 +22,13 @@ except:
logger = logging.getLogger('Bcfg2.Plugins.Cfg')
+def u_str(string, encoding):
+ if sys.hexversion >= 0x03000000:
+ return str(string, encoding)
+ else:
+ return unicode(string, encoding)
+
+
# snipped from TGenshi
def removecomment(stream):
"""A genshi filter that removes comments from the stream."""
@@ -131,7 +139,8 @@ class CfgEntrySet(Bcfg2.Server.Plugin.EntrySet):
data = stream.render('text')
if data == '':
entry.set('empty', 'true')
- except Exception, e:
+ except Exception:
+ e = sys.exc_info()[1]
logger.error("Cfg: genshi exception: %s" % e)
raise Bcfg2.Server.Plugin.PluginExecutionError
else:
@@ -145,8 +154,9 @@ class CfgEntrySet(Bcfg2.Server.Plugin.EntrySet):
entry.text = binascii.b2a_base64(data)
else:
try:
- entry.text = unicode(data, self.encoding)
- except UnicodeDecodeError, e:
+ entry.text = u_str(data, self.encoding)
+ except UnicodeDecodeError:
+ e = sys.exc_info()[1]
logger.error("Failed to decode %s: %s" % (entry.get('name'), e))
logger.error("Please verify you are using the proper encoding.")
raise Bcfg2.Server.Plugin.PluginExecutionError
diff --git a/src/lib/Server/Plugins/DBStats.py b/src/lib/Server/Plugins/DBStats.py
index 27696a978..5ef1920e1 100644
--- a/src/lib/Server/Plugins/DBStats.py
+++ b/src/lib/Server/Plugins/DBStats.py
@@ -33,7 +33,8 @@ class DBStats(Bcfg2.Server.Plugin.Plugin,
logger.debug("Searching for new models to add to the statistics database")
try:
update_database()
- except Exception, inst:
+ except Exception:
+ inst = sys.exc_info()[1]
logger.debug(str(inst))
logger.debug(str(type(inst)))
@@ -61,7 +62,8 @@ class DBStats(Bcfg2.Server.Plugin.Plugin,
logger.info("Imported data for %s in %s seconds" \
% (metadata.hostname, time.time() - start))
return
- except MultipleObjectsReturned, e:
+ except MultipleObjectsReturned:
+ e = sys.exc_info()[1]
logger.error("DBStats: MultipleObjectsReturned while handling %s: %s" % \
(metadata.hostname, e))
logger.error("DBStats: Data is inconsistent")
diff --git a/src/lib/Server/Plugins/Decisions.py b/src/lib/Server/Plugins/Decisions.py
index 1f9525a0e..e239be5ee 100644
--- a/src/lib/Server/Plugins/Decisions.py
+++ b/src/lib/Server/Plugins/Decisions.py
@@ -26,7 +26,8 @@ class DecisionSet(Bcfg2.Server.Plugin.EntrySet):
DecisionFile, encoding)
try:
fam.AddMonitor(path, self)
- except OSError, e:
+ except OSError:
+ e = sys.exc_info()[1]
logger.error('Adding filemonitor for %s failed. '
'Make sure directory exists' % path)
raise Bcfg2.Server.Plugin.PluginInitError(e)
diff --git a/src/lib/Server/Plugins/Hostbase.py b/src/lib/Server/Plugins/Hostbase.py
index b1cbb9dfc..4180fd716 100644
--- a/src/lib/Server/Plugins/Hostbase.py
+++ b/src/lib/Server/Plugins/Hostbase.py
@@ -14,7 +14,8 @@ from sets import Set
from django.template import Context, loader
from django.db import connection
import re
-import cStringIO
+# Compatibility imports
+from Bcfg2.Bcfg2Py3k import StringIO
class Hostbase(Bcfg2.Server.Plugin.Plugin,
@@ -122,8 +123,8 @@ class Hostbase(Bcfg2.Server.Plugin.Plugin,
hosts = {}
for zone in zones:
- zonefile = cStringIO.StringIO()
- externalzonefile = cStringIO.StringIO()
+ zonefile = StringIO()
+ externalzonefile = StringIO()
cursor.execute("""SELECT n.name FROM hostbase_zone_nameservers z
INNER JOIN hostbase_nameserver n ON z.nameserver_id = n.id
WHERE z.zone_id = \'%s\'""" % zone[0])
@@ -160,20 +161,20 @@ class Hostbase(Bcfg2.Server.Plugin.Plugin,
cursor.execute(querystring)
zonehosts = cursor.fetchall()
prevhost = (None, None, None, None)
- cnames = cStringIO.StringIO()
- cnamesexternal = cStringIO.StringIO()
+ cnames = StringIO()
+ cnamesexternal = StringIO()
for host in zonehosts:
if not host[2].split(".", 1)[1] == zone[1]:
zonefile.write(cnames.getvalue())
externalzonefile.write(cnamesexternal.getvalue())
- cnames = cStringIO.StringIO()
- cnamesexternal = cStringIO.StringIO()
+ cnames = StringIO()
+ cnamesexternal = StringIO()
continue
if not prevhost[1] == host[1] or not prevhost[2] == host[2]:
zonefile.write(cnames.getvalue())
externalzonefile.write(cnamesexternal.getvalue())
- cnames = cStringIO.StringIO()
- cnamesexternal = cStringIO.StringIO()
+ cnames = StringIO()
+ cnamesexternal = StringIO()
zonefile.write("%-32s%-10s%-32s\n" %
(host[2].split(".", 1)[0], 'A', host[1]))
zonefile.write("%-32s%-10s%-3s%s.\n" %
@@ -259,8 +260,8 @@ class Hostbase(Bcfg2.Server.Plugin.Plugin,
WHERE p.ip_addr LIKE '%s%%%%' AND h.status = 'active' ORDER BY p.ip_addr
""" % filename[1])
reversehosts = cursor.fetchall()
- zonefile = cStringIO.StringIO()
- externalzonefile = cStringIO.StringIO()
+ zonefile = StringIO()
+ externalzonefile = StringIO()
if len(filename[0].split(".")) == 2:
originlist = []
[originlist.append((".".join([ip[1].split(".")[2], filename[0]]),
@@ -581,7 +582,7 @@ olivia.ctd.anl.gov\n\n"""
hostdata = row
for netgroup in netgroups:
- fileoutput = cStringIO.StringIO()
+ fileoutput = StringIO()
fileoutput.write(header % (netgroup, netgroup, len(netgroups[netgroup])))
for each in netgroups[netgroup]:
fileoutput.write(each + "\n")
diff --git a/src/lib/Server/Plugins/Ldap.py b/src/lib/Server/Plugins/Ldap.py
index 4f10d8ca6..b904dbe02 100644
--- a/src/lib/Server/Plugins/Ldap.py
+++ b/src/lib/Server/Plugins/Ldap.py
@@ -81,7 +81,8 @@ class Ldap(Bcfg2.Server.Plugin.Plugin, Bcfg2.Server.Plugin.Connector):
self.debug_log("LdapPlugin debug: query '" + query.name +
"' not applicable to host '" + metadata.hostname + "'")
return data
- except Exception, error_msg:
+ except Exception:
+ error_msg = sys.exc_info()[1]
if self.debug_flag:
raise
else:
diff --git a/src/lib/Server/Plugins/Metadata.py b/src/lib/Server/Plugins/Metadata.py
index ff723941f..ca6e43851 100644
--- a/src/lib/Server/Plugins/Metadata.py
+++ b/src/lib/Server/Plugins/Metadata.py
@@ -99,7 +99,8 @@ class XMLMetadataConfig(object):
tmpfile = "%s.new" % fname
try:
datafile = open("%s" % tmpfile, 'w')
- except IOError, e:
+ except IOError:
+ e = sys.exc_info()[1]
self.logger.error("Failed to write %s: %s" % (tmpfile, e))
raise MetadataRuntimeError
# prep data
diff --git a/src/lib/Server/Plugins/NagiosGen.py b/src/lib/Server/Plugins/NagiosGen.py
index 1724a1c8a..ca70ba80c 100644
--- a/src/lib/Server/Plugins/NagiosGen.py
+++ b/src/lib/Server/Plugins/NagiosGen.py
@@ -95,7 +95,8 @@ class NagiosGen(Bcfg2.Server.Plugin.Plugin,
(self.data, metadata.hostname), 'w')
fileh.write(host_config)
fileh.close()
- except OSError, ioerr:
+ except OSError:
+ ioerr = sys.exc_info()[1]
LOGGER.error("Failed to write %s/%s-host.cfg" % \
(self.data, metadata.hostname))
LOGGER.error(ioerr)
@@ -145,6 +146,7 @@ class NagiosGen(Bcfg2.Server.Plugin.Plugin,
fileh = open("%s/nagiosgen.cfg" % (self.data), 'w')
fileh.write(group_data + host_data)
fileh.close()
- except OSError, ioerr:
+ except OSError:
+ ioerr = sys.exc_info()[1]
LOGGER.error("Failed to write %s/nagiosgen.cfg" % (self.data))
LOGGER.error(ioerr)
diff --git a/src/lib/Server/Plugins/Packages.py b/src/lib/Server/Plugins/Packages.py
index a84d8dc70..5346111c4 100644
--- a/src/lib/Server/Plugins/Packages.py
+++ b/src/lib/Server/Plugins/Packages.py
@@ -1,4 +1,3 @@
-import cPickle
import copy
import gzip
import tarfile
@@ -8,7 +7,15 @@ import lxml.etree
import os
import re
import sys
-import urllib2
+
+# Compatibility imports
+from Bcfg2.Bcfg2Py3k import cPickle
+from Bcfg2.Bcfg2Py3k import HTTPBasicAuthHandler
+from Bcfg2.Bcfg2Py3k import HTTPPasswordMgrWithDefaultRealm
+from Bcfg2.Bcfg2Py3k import HTTPError
+from Bcfg2.Bcfg2Py3k import install_opener
+from Bcfg2.Bcfg2Py3k import build_opener
+from Bcfg2.Bcfg2Py3k import urlopen
# FIXME: Remove when server python dep is 2.5 or greater
if sys.version_info >= (2, 5):
@@ -72,10 +79,10 @@ def _fetch_url(url):
user = mobj.group(2)
passwd = mobj.group(3)
url = mobj.group(1) + mobj.group(4)
- auth = urllib2.HTTPBasicAuthHandler(urllib2.HTTPPasswordMgrWithDefaultRealm())
+ auth = HTTPBasicAuthHandler(HTTPPasswordMgrWithDefaultRealm())
auth.add_password(None, url, user, passwd)
- urllib2.install_opener(urllib2.build_opener(auth))
- return urllib2.urlopen(url).read()
+ install_opener(build_opener(auth))
+ return urlopen(url).read()
class Source(object):
@@ -165,7 +172,8 @@ class Source(object):
except ValueError:
logger.error("Packages: Bad url string %s" % url)
continue
- except urllib2.HTTPError, h:
+ except HTTPError:
+ h = sys.exc_info()[1]
logger.error("Packages: Failed to fetch url %s. code=%s" \
% (url, h.code))
continue
@@ -256,7 +264,8 @@ class YUMSource(Source):
except ValueError:
logger.error("Packages: Bad url string %s" % rmdurl)
continue
- except urllib2.HTTPError, h:
+ except HTTPError:
+ h = sys.exc_info()[1]
logger.error("Packages: Failed to fetch url %s. code=%s" \
% (rmdurl, h.code))
continue
@@ -841,7 +850,8 @@ class Packages(Bcfg2.Server.Plugin.Plugin,
xdata.xinclude()
xdata = xdata.getroot()
except (lxml.etree.XIncludeError, \
- lxml.etree.XMLSyntaxError), xmlerr:
+ lxml.etree.XMLSyntaxError):
+ xmlerr = sys.exc_info()[1]
self.logger.error("Package: Error processing xml: %s" % xmlerr)
raise Bcfg2.Server.Plugin.PluginInitError
except IOError:
diff --git a/src/lib/Server/Plugins/Properties.py b/src/lib/Server/Plugins/Properties.py
index b34bde998..dea797a10 100644
--- a/src/lib/Server/Plugins/Properties.py
+++ b/src/lib/Server/Plugins/Properties.py
@@ -63,9 +63,10 @@ class Properties(Bcfg2.Server.Plugin.Plugin,
Bcfg2.Server.Plugin.Connector.__init__(self)
try:
self.store = PropDirectoryBacked(self.data, core.fam)
- except OSError, e:
+ except OSError:
+ e = sys.exc_info()[1]
Bcfg2.Server.Plugin.logger.error("Error while creating Properties "
- "store: %s %s" % (e.strerror,e.filename))
+ "store: %s %s" % (e.strerror, e.filename))
raise Bcfg2.Server.Plugin.PluginInitError
def get_additional_data(self, _):
diff --git a/src/lib/Server/Plugins/SGenshi.py b/src/lib/Server/Plugins/SGenshi.py
index cead06e34..04942c2bd 100644
--- a/src/lib/Server/Plugins/SGenshi.py
+++ b/src/lib/Server/Plugins/SGenshi.py
@@ -23,11 +23,14 @@ class SGenshiTemplateFile(Bcfg2.Server.Plugins.TGenshi.TemplateFile):
Bcfg2.Server.Plugins.TGenshi.removecomment)
data = stream.render('xml', strip_whitespace=False)
return lxml.etree.XML(data)
- except LookupError, lerror:
+ except LookupError:
+ lerror = sys.exc_info()[1]
logger.error('Genshi lookup error: %s' % lerror)
- except genshi.template.TemplateError, terror:
+ except genshi.template.TemplateError:
+ terror = sys.exc_info()[1]
logger.error('Genshi template error: %s' % terror)
- except genshi.input.ParseError, perror:
+ except genshi.input.ParseError:
+ perror = sys.exc_info()[1]
logger.error('Genshi parse error: %s' % perror)
raise
diff --git a/src/lib/Server/Plugins/SSHbase.py b/src/lib/Server/Plugins/SSHbase.py
index 77c5e008f..ce08235eb 100644
--- a/src/lib/Server/Plugins/SSHbase.py
+++ b/src/lib/Server/Plugins/SSHbase.py
@@ -52,7 +52,8 @@ class SSHbase(Bcfg2.Server.Plugin.Plugin,
try:
Bcfg2.Server.Plugin.DirectoryBacked.__init__(self, self.data,
self.core.fam)
- except OSError, ioerr:
+ except OSError:
+ ioerr = sys.exc_info()[1]
self.logger.error("Failed to load SSHbase repository from %s" \
% (self.data))
self.logger.error(ioerr)
diff --git a/src/lib/Server/Plugins/SSLCA.py b/src/lib/Server/Plugins/SSLCA.py
index 00f67834d..baaa14ba9 100644
--- a/src/lib/Server/Plugins/SSLCA.py
+++ b/src/lib/Server/Plugins/SSLCA.py
@@ -5,7 +5,8 @@ import posixpath
import tempfile
import os
from subprocess import Popen, PIPE, STDOUT
-from ConfigParser import ConfigParser
+# Compatibility import
+from Bcfg2.Bcfg2Py3k import ConfigParser
class SSLCA(Bcfg2.Server.Plugin.GroupSpool):
diff --git a/src/lib/Server/Plugins/Snapshots.py b/src/lib/Server/Plugins/Snapshots.py
index a1f72ba3e..8b6bad574 100644
--- a/src/lib/Server/Plugins/Snapshots.py
+++ b/src/lib/Server/Plugins/Snapshots.py
@@ -8,10 +8,13 @@ import Bcfg2.Server.Plugin
import Bcfg2.Server.Snapshots
import Bcfg2.Logger
from Bcfg2.Server.Snapshots.model import Snapshot
-import Queue
+import sys
import time
import threading
+# Compatibility import
+from Bcfg2.Bcfg2Py3k import Queue
+
logger = logging.getLogger('Snapshots')
ftypes = ['ConfigFile', 'SymLink', 'Directory']
@@ -25,13 +28,20 @@ datafields = {
}
+def u_str(string):
+ if sys.hexversion >= 0x03000000:
+ return string
+ else:
+ return unicode(string)
+
+
def build_snap_ent(entry):
basefields = []
if entry.tag in ['Package', 'Service']:
basefields += ['type']
- desired = dict([(key, unicode(entry.get(key))) for key in basefields])
- state = dict([(key, unicode(entry.get(key))) for key in basefields])
- desired.update([(key, unicode(entry.get(key))) for key in \
+ desired = dict([(key, u_str(entry.get(key))) for key in basefields])
+ state = dict([(key, u_str(entry.get(key))) for key in basefields])
+ desired.update([(key, u_str(entry.get(key))) for key in \
datafields[entry.tag]])
if entry.tag == 'ConfigFile' or \
((entry.tag == 'Path') and (entry.get('type') == 'file')):
@@ -39,19 +49,19 @@ def build_snap_ent(entry):
desired['contents'] = None
else:
if entry.get('encoding', 'ascii') == 'ascii':
- desired['contents'] = unicode(entry.text)
+ desired['contents'] = u_str(entry.text)
else:
- desired['contents'] = unicode(binascii.a2b_base64(entry.text))
+ desired['contents'] = u_str(binascii.a2b_base64(entry.text))
if 'current_bfile' in entry.attrib:
- state['contents'] = unicode(binascii.a2b_base64( \
+ state['contents'] = u_str(binascii.a2b_base64( \
entry.get('current_bfile')))
elif 'current_bdiff' in entry.attrib:
diff = binascii.a2b_base64(entry.get('current_bdiff'))
- state['contents'] = unicode( \
+ state['contents'] = u_str( \
'\n'.join(difflib.restore(diff.split('\n'), 1)))
- state.update([(key, unicode(entry.get('current_' + key, entry.get(key)))) \
+ state.update([(key, u_str(entry.get('current_' + key, entry.get(key)))) \
for key in datafields[entry.tag]])
if entry.tag in ['ConfigFile', 'Path'] and entry.get('exists', 'true') == 'false':
state = None
@@ -67,7 +77,7 @@ class Snapshots(Bcfg2.Server.Plugin.Statistics,
Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
Bcfg2.Server.Plugin.Statistics.__init__(self)
self.session = Bcfg2.Server.Snapshots.setup_session(core.cfile)
- self.work_queue = Queue.Queue()
+ self.work_queue = Queue()
self.loader = threading.Thread(target=self.load_snapshot)
self.loader.start()
@@ -93,9 +103,9 @@ class Snapshots(Bcfg2.Server.Plugin.Statistics,
bad = []
state = xdata.find('.//Statistics')
correct = state.get('state') == 'clean'
- revision = unicode(state.get('revision', '-1'))
+ revision = u_str(state.get('revision', '-1'))
for entry in state.find('.//Bad'):
- data = [False, False, unicode(entry.get('name'))] \
+ data = [False, False, u_str(entry.get('name'))] \
+ build_snap_ent(entry)
if entry.tag in ftypes:
etag = 'Path'
@@ -108,15 +118,15 @@ class Snapshots(Bcfg2.Server.Plugin.Statistics,
else:
etag = entry.tag
if entry.get('name') in entries[etag]:
- data = [True, False, unicode(entry.get('name'))] + \
+ data = [True, False, u_str(entry.get('name'))] + \
build_snap_ent(entry)
else:
- data = [True, False, unicode(entry.get('name'))] + \
+ data = [True, False, u_str(entry.get('name'))] + \
build_snap_ent(entry)
for entry in state.find('.//Extra'):
if entry.tag in datafields:
data = build_snap_ent(entry)[1]
- ename = unicode(entry.get('name'))
+ ename = u_str(entry.get('name'))
data['name'] = ename
extra[entry.tag][ename] = data
else:
diff --git a/src/lib/Server/Plugins/Statistics.py b/src/lib/Server/Plugins/Statistics.py
index c7fa0e534..f4f4c7175 100644
--- a/src/lib/Server/Plugins/Statistics.py
+++ b/src/lib/Server/Plugins/Statistics.py
@@ -8,7 +8,6 @@ import logging
from lxml.etree import XML, SubElement, Element, XMLSyntaxError
import lxml.etree
import os
-import Queue
from time import asctime, localtime, time, strptime, mktime
import threading
@@ -33,7 +32,8 @@ class StatisticsStore(object):
or force:
try:
fout = open(self.filename + '.new', 'w')
- except IOError, ioerr:
+ except IOError:
+ ioerr = sys.exc_info()[1]
self.logger.error("Failed to open %s for writing: %s" % (self.filename + '.new', ioerr))
else:
fout.write(lxml.etree.tostring(self.element, encoding='UTF-8', xml_declaration=True))
diff --git a/src/lib/Server/Plugins/TCheetah.py b/src/lib/Server/Plugins/TCheetah.py
index d40f4baf3..151cc6543 100644
--- a/src/lib/Server/Plugins/TCheetah.py
+++ b/src/lib/Server/Plugins/TCheetah.py
@@ -36,7 +36,8 @@ class TemplateFile:
self.template = Cheetah.Template.Template(open(self.name).read(),
compilerSettings=s,
searchList=self.searchlist)
- except Cheetah.Parser.ParseError, perror:
+ except Cheetah.Parser.ParseError:
+ perror = sys.exc_info()[1]
logger.error("Cheetah parse error for file %s" % (self.name))
logger.error(perror.report())
@@ -52,11 +53,14 @@ class TemplateFile:
if entry.tag == 'Path':
entry.set('type', 'file')
try:
+ # py3k compatibility
+ if sys.hexversion >= 0x03000000:
+ unicode = str
if type(self.template) == unicode:
entry.text = self.template
else:
if entry.get('encoding') == 'base64':
- # take care of case where file needs base64 encoding
+ # take care of case where file needs base64 encoding
entry.text = binascii.b2a_base64(self.template)
else:
entry.text = unicode(str(self.template), self.encoding)
diff --git a/src/lib/Server/Plugins/TGenshi.py b/src/lib/Server/Plugins/TGenshi.py
index 2a12672cc..cd268e967 100644
--- a/src/lib/Server/Plugins/TGenshi.py
+++ b/src/lib/Server/Plugins/TGenshi.py
@@ -63,16 +63,22 @@ class TemplateFile:
try:
self.template = loader.load(self.name, cls=self.template_cls,
encoding=self.encoding)
- except LookupError, lerror:
+ except LookupError:
+ lerror = sys.exc_info()[1]
logger.error('Genshi lookup error: %s' % lerror)
- except TemplateError, terror:
+ except TemplateError:
+ terror = sys.exc_info()[1]
logger.error('Genshi template error: %s' % terror)
- except genshi.input.ParseError, perror:
+ except genshi.input.ParseError:
+ perror = sys.exc_info()[1]
logger.error('Genshi parse error: %s' % perror)
def bind_entry(self, entry, metadata):
"""Build literal file information."""
fname = entry.get('realname', entry.get('name'))
+ # py3k compatibility
+ if sys.hexversion >= 0x03000000:
+ unicode = str
if entry.tag == 'Path':
entry.set('type', 'file')
try:
@@ -107,10 +113,12 @@ class TemplateFile:
entry.text = unicode(xmldata, self.encoding)
if entry.text == '':
entry.set('empty', 'true')
- except TemplateError, terror:
+ except TemplateError:
+ terror = sys.exc_info()[1]
logger.error('Genshi template error: %s' % terror)
raise Bcfg2.Server.Plugin.PluginExecutionError
- except AttributeError, err:
+ except AttributeError:
+ err = sys.exc_info()[1]
logger.error('Genshi template loading error: %s' % err)
raise Bcfg2.Server.Plugin.PluginExecutionError