summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Plugins
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-08-21 13:32:51 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-08-21 13:33:20 -0400
commit22016ee593d6047855964734a17120d2aea6b5a5 (patch)
tree92adbca098e89295d4217561ee53454134b781a2 /src/lib/Bcfg2/Server/Plugins
parentd6bf20100848dc61b971844e21d24da44b9f65f7 (diff)
downloadbcfg2-22016ee593d6047855964734a17120d2aea6b5a5.tar.gz
bcfg2-22016ee593d6047855964734a17120d2aea6b5a5.tar.bz2
bcfg2-22016ee593d6047855964734a17120d2aea6b5a5.zip
lots of various py3k fixes
Diffstat (limited to 'src/lib/Bcfg2/Server/Plugins')
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py29
-rw-r--r--src/lib/Bcfg2/Server/Plugins/DBStats.py5
-rw-r--r--src/lib/Bcfg2/Server/Plugins/FileProbes.py18
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Metadata.py8
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Packages/__init__.py3
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Probes.py9
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Properties.py1
-rw-r--r--src/lib/Bcfg2/Server/Plugins/SEModules.py7
-rw-r--r--src/lib/Bcfg2/Server/Plugins/SSHbase.py12
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Snapshots.py14
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Statistics.py21
-rw-r--r--src/lib/Bcfg2/Server/Plugins/TCheetah.py8
-rw-r--r--src/lib/Bcfg2/Server/Plugins/TGenshi.py8
13 files changed, 64 insertions, 79 deletions
diff --git a/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py b/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py
index f7577d60e..aef239fce 100644
--- a/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py
+++ b/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py
@@ -6,11 +6,10 @@ import sys
import stat
import pkgutil
import logging
-import binascii
import lxml.etree
import Bcfg2.Options
import Bcfg2.Server.Plugin
-from Bcfg2.Bcfg2Py3k import u_str
+from Bcfg2.Bcfg2Py3k import u_str, b64encode
import Bcfg2.Server.Lint
logger = logging.getLogger(__name__)
@@ -290,7 +289,7 @@ class CfgEntrySet(Bcfg2.Server.Plugin.EntrySet):
raise Bcfg2.Server.Plugin.PluginExecutionError(msg)
if entry.get('encoding') == 'base64':
- data = binascii.b2a_base64(data)
+ data = b64encode(data)
else:
try:
data = u_str(data, self.encoding)
@@ -371,26 +370,26 @@ class CfgEntrySet(Bcfg2.Server.Plugin.EntrySet):
if attr in new_entry]
if badattr:
# check for info files and inform user of their removal
- if os.path.exists(self.path + "/:info"):
- logger.info("Removing :info file and replacing with "
- "info.xml")
- os.remove(self.path + "/:info")
- if os.path.exists(self.path + "/info"):
- logger.info("Removing info file and replacing with "
- "info.xml")
- os.remove(self.path + "/info")
+ for ifile in ['info', ':info']:
+ info = os.path.join(self.path, ifile)
+ if os.path.exists(info):
+ logger.info("Removing %s and replacing with info.xml" %
+ info)
+ os.remove(info)
metadata_updates = {}
metadata_updates.update(self.metadata)
for attr in badattr:
metadata_updates[attr] = new_entry.get(attr)
infoxml = lxml.etree.Element('FileInfo')
infotag = lxml.etree.SubElement(infoxml, 'Info')
- [infotag.attrib.__setitem__(attr, metadata_updates[attr]) \
- for attr in metadata_updates]
+ [infotag.attrib.__setitem__(attr, metadata_updates[attr])
+ for attr in metadata_updates]
ofile = open(self.path + "/info.xml", "w")
- ofile.write(lxml.etree.tostring(infoxml, pretty_print=True))
+ ofile.write(lxml.etree.tostring(infoxml, encoding='unicode',
+ pretty_print=True))
ofile.close()
- self.debug_log("Wrote file %s" % (self.path + "/info.xml"),
+ self.debug_log("Wrote file %s" % os.path.join(self.path,
+ "info.xml"),
flag=log)
diff --git a/src/lib/Bcfg2/Server/Plugins/DBStats.py b/src/lib/Bcfg2/Server/Plugins/DBStats.py
index b8750f850..63c590f0f 100644
--- a/src/lib/Bcfg2/Server/Plugins/DBStats.py
+++ b/src/lib/Bcfg2/Server/Plugins/DBStats.py
@@ -1,4 +1,3 @@
-import binascii
import difflib
import logging
import lxml.etree
@@ -14,6 +13,8 @@ except ImportError:
import Bcfg2.Server.Plugin
from Bcfg2.Server.Reports.importscript import load_stat
from Bcfg2.Server.Reports.reports.models import Client
+from Bcfg2.Bcfg2Py3k import b64decode
+
# for debugging output only
logger = logging.getLogger('Bcfg2.Plugins.DBStats')
@@ -88,7 +89,7 @@ class DBStats(Bcfg2.Server.Plugin.ThreadedStatistics,
ret.append('\n'.join(entry.reason.unpruned))
elif entry.reason.current_diff != '':
if entry.reason.is_binary:
- ret.append(binascii.a2b_base64(entry.reason.current_diff))
+ ret.append(b64decode(entry.reason.current_diff))
else:
ret.append('\n'.join(difflib.restore(\
entry.reason.current_diff.split('\n'), 1)))
diff --git a/src/lib/Bcfg2/Server/Plugins/FileProbes.py b/src/lib/Bcfg2/Server/Plugins/FileProbes.py
index b4a20b0eb..a48524ac9 100644
--- a/src/lib/Bcfg2/Server/Plugins/FileProbes.py
+++ b/src/lib/Bcfg2/Server/Plugins/FileProbes.py
@@ -7,19 +7,19 @@ the client """
import os
import sys
import errno
-import binascii
import lxml.etree
import Bcfg2.Options
import Bcfg2.Server
import Bcfg2.Server.Plugin
+from Bcfg2.Bcfg2Py3k import b64decode
probecode = """#!/usr/bin/env python
import os
import pwd
import grp
-import binascii
import lxml.etree
+from Bcfg2.Bcfg2Py3k import b64encode
path = "%s"
@@ -33,8 +33,8 @@ data = lxml.etree.Element("ProbedFileData",
owner=pwd.getpwuid(stat[4])[0],
group=grp.getgrgid(stat[5])[0],
perms=oct(stat[0] & 07777))
-data.text = binascii.b2a_base64(open(path).read())
-print(lxml.etree.tostring(data))
+data.text = b64encode(open(path).read())
+print(lxml.etree.tostring(data, encoding="unicode"))
"""
class FileProbes(Bcfg2.Server.Plugin.Plugin,
@@ -103,7 +103,7 @@ class FileProbes(Bcfg2.Server.Plugin.Plugin,
def write_data(self, data, metadata):
"""Write the probed file data to the bcfg2 specification."""
filename = data.get("name")
- contents = binascii.a2b_base64(data.text)
+ contents = b64decode(data.text)
entry = self.entries[metadata.hostname][filename]
cfg = self.core.plugins['Cfg']
specific = "%s.H_%s" % (os.path.basename(filename), metadata.hostname)
@@ -120,7 +120,7 @@ class FileProbes(Bcfg2.Server.Plugin.Plugin,
# get current entry data
if entry.text and entry.get("encoding") == "base64":
- entrydata = binascii.a2b_base64(entry.text)
+ entrydata = b64decode(entry.text)
else:
entrydata = entry.text
@@ -187,7 +187,7 @@ class FileProbes(Bcfg2.Server.Plugin.Plugin,
# get current entry data
if entry.get("encoding") == "base64":
- entrydata = binascii.a2b_base64(entry.text)
+ entrydata = b64decode(entry.text)
else:
entrydata = entry.text
if entrydata == contents:
@@ -199,8 +199,7 @@ class FileProbes(Bcfg2.Server.Plugin.Plugin,
if os.path.exists(infoxml):
return
- self.logger.info("Writing info.xml at %s for %s" %
- (infoxml, data.get("name")))
+ self.logger.info("Writing %s for %s" % (infoxml, data.get("name")))
info = \
lxml.etree.Element("Info",
owner=data.get("owner",
@@ -216,6 +215,7 @@ class FileProbes(Bcfg2.Server.Plugin.Plugin,
root.append(info)
try:
open(infoxml, "w").write(lxml.etree.tostring(root,
+ encoding='unicode',
pretty_print=True))
except IOError:
err = sys.exc_info()[1]
diff --git a/src/lib/Bcfg2/Server/Plugins/Metadata.py b/src/lib/Bcfg2/Server/Plugins/Metadata.py
index 3225b6c85..8f4f42c96 100644
--- a/src/lib/Bcfg2/Server/Plugins/Metadata.py
+++ b/src/lib/Bcfg2/Server/Plugins/Metadata.py
@@ -151,8 +151,8 @@ class XMLMetadataConfig(Bcfg2.Server.Plugin.XMLFileBacked):
raise Bcfg2.Server.Plugin.MetadataRuntimeError(msg)
# prep data
dataroot = xmltree.getroot()
- newcontents = str(lxml.etree.tostring(dataroot, pretty_print=True,
- encoding='unicode'))
+ newcontents = lxml.etree.tostring(dataroot, pretty_print=True,
+ encoding='unicode')
fd = datafile.fileno()
while locked(fd) == True:
@@ -807,7 +807,7 @@ class Metadata(Bcfg2.Server.Plugin.Metadata,
# _any_ port numbers - perhaps a priority queue could
# be faster?
curtime = time.time()
- for addrpair in self.session_cache.keys():
+ for addrpair in list(self.session_cache.keys()):
if addresspair[0] == addrpair[0]:
(stamp, _) = self.session_cache[addrpair]
if curtime - stamp > cache_ttl:
@@ -1104,7 +1104,7 @@ class Metadata(Bcfg2.Server.Plugin.Metadata,
"secure mode" % address[0])
return False
# populate the session cache
- if user.decode('utf-8') != 'root':
+ if user != 'root':
self.session_cache[address] = (time.time(), client)
return True
diff --git a/src/lib/Bcfg2/Server/Plugins/Packages/__init__.py b/src/lib/Bcfg2/Server/Plugins/Packages/__init__.py
index 702ff64d2..71ae6a038 100644
--- a/src/lib/Bcfg2/Server/Plugins/Packages/__init__.py
+++ b/src/lib/Bcfg2/Server/Plugins/Packages/__init__.py
@@ -177,7 +177,8 @@ class Packages(Bcfg2.Server.Plugin.Plugin,
to_remove.append(pkg)
else:
self.logger.error("Packages: Malformed Package: %s" %
- lxml.etree.tostring(pkg))
+ lxml.etree.tostring(pkg,
+ encoding='unicode'))
gpkgs = collection.get_groups(groups)
for group, pkgs in gpkgs.items():
diff --git a/src/lib/Bcfg2/Server/Plugins/Probes.py b/src/lib/Bcfg2/Server/Plugins/Probes.py
index e08f52a28..34b5ab187 100644
--- a/src/lib/Bcfg2/Server/Plugins/Probes.py
+++ b/src/lib/Bcfg2/Server/Plugins/Probes.py
@@ -52,7 +52,8 @@ if has_django:
class ClientProbeDataSet(dict):
- """ dict of probe => [probe data] that records a for each host """
+ """ dict of probe => [probe data] that records a timestamp for
+ each host """
def __init__(self, *args, **kwargs):
if "timestamp" in kwargs and kwargs['timestamp'] is not None:
self.timestamp = kwargs.pop("timestamp")
@@ -191,12 +192,10 @@ class Probes(Bcfg2.Server.Plugin.Probing,
value=str(self.probedata[client][probe]))
for group in sorted(self.cgroups[client]):
lxml.etree.SubElement(cx, "Group", name=group)
- data = lxml.etree.tostring(top, encoding='UTF-8',
- xml_declaration=True,
- pretty_print='true')
try:
datafile = open(os.path.join(self.data, 'probed.xml'), 'w')
- datafile.write(data.decode('utf-8'))
+ datafile.write(lxml.etree.tostring(top, encoding='unicode',
+ pretty_print='true'))
except IOError:
err = sys.exc_info()[1]
self.logger.error("Failed to write probed.xml: %s" % err)
diff --git a/src/lib/Bcfg2/Server/Plugins/Properties.py b/src/lib/Bcfg2/Server/Plugins/Properties.py
index a879b064f..3405ad50c 100644
--- a/src/lib/Bcfg2/Server/Plugins/Properties.py
+++ b/src/lib/Bcfg2/Server/Plugins/Properties.py
@@ -43,6 +43,7 @@ class PropertyFile(Bcfg2.Server.Plugin.StructFile):
try:
open(self.name, "wb").write(lxml.etree.tostring(self.xdata,
+ encoding='unicode',
pretty_print=True))
return True
except IOError:
diff --git a/src/lib/Bcfg2/Server/Plugins/SEModules.py b/src/lib/Bcfg2/Server/Plugins/SEModules.py
index a99e54bb2..0d75bb48f 100644
--- a/src/lib/Bcfg2/Server/Plugins/SEModules.py
+++ b/src/lib/Bcfg2/Server/Plugins/SEModules.py
@@ -1,15 +1,14 @@
import os
import logging
-import binascii
-import posixpath
-
import Bcfg2.Server.Plugin
+from Bcfg2.Bcfg2Py3k import b64encode
+
logger = logging.getLogger(__name__)
class SEModuleData(Bcfg2.Server.Plugin.SpecificData):
def bind_entry(self, entry, _):
entry.set('encoding', 'base64')
- entry.text = binascii.b2a_base64(self.data)
+ entry.text = b64encode(self.data)
class SEModules(Bcfg2.Server.Plugin.GroupSpool):
diff --git a/src/lib/Bcfg2/Server/Plugins/SSHbase.py b/src/lib/Bcfg2/Server/Plugins/SSHbase.py
index a1a29727f..cbe8d0d9b 100644
--- a/src/lib/Bcfg2/Server/Plugins/SSHbase.py
+++ b/src/lib/Bcfg2/Server/Plugins/SSHbase.py
@@ -1,20 +1,16 @@
"""This module manages ssh key files for bcfg2"""
-import binascii
import re
import os
+import sys
import socket
import shutil
-import sys
+import logging
import tempfile
from subprocess import Popen, PIPE
import Bcfg2.Server.Plugin
-from Bcfg2.Bcfg2Py3k import u_str
+from Bcfg2.Bcfg2Py3k import u_str, reduce, b64encode
-if sys.hexversion >= 0x03000000:
- from functools import reduce
-
-import logging
logger = logging.getLogger(__name__)
class KeyData(Bcfg2.Server.Plugin.SpecificData):
@@ -31,7 +27,7 @@ class KeyData(Bcfg2.Server.Plugin.SpecificData):
def bind_entry(self, entry, metadata):
entry.set('type', 'file')
if entry.get('encoding') == 'base64':
- entry.text = binascii.b2a_base64(self.data)
+ entry.text = b64encode(self.data)
else:
try:
entry.text = u_str(self.data, self.encoding)
diff --git a/src/lib/Bcfg2/Server/Plugins/Snapshots.py b/src/lib/Bcfg2/Server/Plugins/Snapshots.py
index 666beef21..e62638b4f 100644
--- a/src/lib/Bcfg2/Server/Plugins/Snapshots.py
+++ b/src/lib/Bcfg2/Server/Plugins/Snapshots.py
@@ -1,9 +1,5 @@
-#import lxml.etree
import logging
-import binascii
import difflib
-#import sqlalchemy
-#import sqlalchemy.orm
import Bcfg2.Server.Plugin
import Bcfg2.Server.Snapshots
import Bcfg2.Logger
@@ -13,8 +9,7 @@ import time
import threading
# Compatibility import
-from Bcfg2.Bcfg2Py3k import Queue
-from Bcfg2.Bcfg2Py3k import u_str
+from Bcfg2.Bcfg2Py3k import Queue, u_str, b64decode
logger = logging.getLogger('Snapshots')
@@ -45,13 +40,12 @@ def build_snap_ent(entry):
if entry.get('encoding', 'ascii') == 'ascii':
desired['contents'] = u_str(entry.text)
else:
- desired['contents'] = u_str(binascii.a2b_base64(entry.text))
+ desired['contents'] = u_str(b64decode(entry.text))
if 'current_bfile' in entry.attrib:
- state['contents'] = u_str(binascii.a2b_base64( \
- entry.get('current_bfile')))
+ state['contents'] = u_str(b64decode(entry.get('current_bfile')))
elif 'current_bdiff' in entry.attrib:
- diff = binascii.a2b_base64(entry.get('current_bdiff'))
+ diff = b64decode(entry.get('current_bdiff'))
state['contents'] = u_str( \
'\n'.join(difflib.restore(diff.split('\n'), 1)))
diff --git a/src/lib/Bcfg2/Server/Plugins/Statistics.py b/src/lib/Bcfg2/Server/Plugins/Statistics.py
index ce8d085cc..33b78047a 100644
--- a/src/lib/Bcfg2/Server/Plugins/Statistics.py
+++ b/src/lib/Bcfg2/Server/Plugins/Statistics.py
@@ -1,16 +1,14 @@
'''This file manages the statistics collected by the BCFG2 Server'''
-import binascii
import copy
import difflib
import logging
-from lxml.etree import XML, SubElement, Element, XMLSyntaxError
import lxml.etree
import os
import sys
from time import asctime, localtime, time, strptime, mktime
import threading
-
+from Bcfg2.Bcfg2Py3k import b64decode
import Bcfg2.Server.Plugin
@@ -20,7 +18,7 @@ class StatisticsStore(object):
def __init__(self, filename):
self.filename = filename
- self.element = Element('Dummy')
+ self.element = lxml.etree.Element('Dummy')
self.dirty = 0
self.lastwrite = 0
self.logger = logging.getLogger('Bcfg2.Server.Statistics')
@@ -36,7 +34,8 @@ class StatisticsStore(object):
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))
+ fout.write(lxml.etree.tostring(self.element,
+ encoding='unicode'))
fout.close()
os.rename(self.filename + '.new', self.filename)
self.dirty = 0
@@ -48,11 +47,11 @@ class StatisticsStore(object):
fin = open(self.filename, 'r')
data = fin.read()
fin.close()
- self.element = XML(data)
+ self.element = lxml.etree.XML(data)
self.dirty = 0
- except (IOError, XMLSyntaxError):
+ except (IOError, lxml.etree.XMLSyntaxError):
self.logger.error("Creating new statistics file %s"%(self.filename))
- self.element = Element('ConfigStatistics')
+ self.element = lxml.etree.Element('ConfigStatistics')
self.WriteBack()
self.dirty = 0
@@ -78,7 +77,7 @@ class StatisticsStore(object):
nummatch = len(nodes)
if nummatch == 0:
# Create an entry for this node
- node = SubElement(self.element, 'Node', name=client)
+ node = lxml.etree.SubElement(self.element, 'Node', name=client)
elif nummatch == 1 and not node_dirty:
# Delete old instance
node = nodes[0]
@@ -150,9 +149,9 @@ class Statistics(Bcfg2.Server.Plugin.ThreadedStatistics,
if cfentry.get('sensitive') in ['true', 'True']:
raise Bcfg2.Server.Plugin.PluginExecutionError
elif 'current_bfile' in cfentry.attrib:
- contents = binascii.a2b_base64(cfentry.get('current_bfile'))
+ contents = b64decode(cfentry.get('current_bfile'))
elif 'current_bdiff' in cfentry.attrib:
- diff = binascii.a2b_base64(cfentry.get('current_bdiff'))
+ diff = b64decode(cfentry.get('current_bdiff'))
contents = '\n'.join(difflib.restore(diff.split('\n'), 1))
else:
contents = None
diff --git a/src/lib/Bcfg2/Server/Plugins/TCheetah.py b/src/lib/Bcfg2/Server/Plugins/TCheetah.py
index d99baa512..2bf475363 100644
--- a/src/lib/Bcfg2/Server/Plugins/TCheetah.py
+++ b/src/lib/Bcfg2/Server/Plugins/TCheetah.py
@@ -1,13 +1,11 @@
'''This module implements a templating generator based on Cheetah'''
-import binascii
import logging
import sys
import traceback
import Bcfg2.Server.Plugin
-# py3k compatibility
-if sys.hexversion >= 0x03000000:
- unicode = str
+
+from Bcfg2.Bcfg2Py3k import unicode, b64encode
logger = logging.getLogger('Bcfg2.Plugins.TCheetah')
@@ -60,7 +58,7 @@ class TemplateFile:
else:
if entry.get('encoding') == 'base64':
# take care of case where file needs base64 encoding
- entry.text = binascii.b2a_base64(self.template)
+ entry.text = b64encode(self.template)
else:
entry.text = unicode(str(self.template), self.encoding)
except:
diff --git a/src/lib/Bcfg2/Server/Plugins/TGenshi.py b/src/lib/Bcfg2/Server/Plugins/TGenshi.py
index 846c2a464..c84e0a506 100644
--- a/src/lib/Bcfg2/Server/Plugins/TGenshi.py
+++ b/src/lib/Bcfg2/Server/Plugins/TGenshi.py
@@ -1,12 +1,10 @@
"""This module implements a templating generator based on Genshi."""
-import binascii
import logging
import sys
import Bcfg2.Server.Plugin
-# py3k compatibility
-if sys.hexversion >= 0x03000000:
- unicode = str
+
+from Bcfg2.Bcfg2Py3k import unicode, b64encode
logger = logging.getLogger('Bcfg2.Plugins.TGenshi')
@@ -99,7 +97,7 @@ class TemplateFile:
else:
if entry.get('encoding') == 'base64':
# take care of case where file needs base64 encoding
- entry.text = binascii.b2a_base64(textdata)
+ entry.text = b64encode(textdata)
else:
entry.text = unicode(textdata, self.encoding)
else: