From d59c274547b6aecb3bcbfb99d1b874d403c51bea Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Wed, 22 Aug 2012 10:00:38 -0400 Subject: fixed lxml.etree.tostring invocations --- src/lib/Bcfg2/Server/Admin/Bundle.py | 2 -- src/lib/Bcfg2/Server/Lint/__init__.py | 6 ++++-- src/lib/Bcfg2/Server/Plugin.py | 2 +- src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py | 4 ++-- src/lib/Bcfg2/Server/Plugins/FileProbes.py | 9 +++++---- src/lib/Bcfg2/Server/Plugins/Metadata.py | 5 +++-- src/lib/Bcfg2/Server/Plugins/Packages/__init__.py | 2 +- src/lib/Bcfg2/Server/Plugins/Probes.py | 4 ++-- src/lib/Bcfg2/Server/Plugins/Properties.py | 7 ++++--- src/lib/Bcfg2/Server/Plugins/Statistics.py | 2 +- src/sbin/bcfg2-build-reports | 6 +++--- src/sbin/bcfg2-crypt | 2 +- src/sbin/bcfg2-info | 3 ++- testsuite/Testlib/TestServer/TestPlugin.py | 22 +++++++++++++--------- .../Testlib/TestServer/TestPlugins/TestProbes.py | 6 ++++-- 15 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/lib/Bcfg2/Server/Admin/Bundle.py b/src/lib/Bcfg2/Server/Admin/Bundle.py index 49e530b15..e5e4eadf3 100644 --- a/src/lib/Bcfg2/Server/Admin/Bundle.py +++ b/src/lib/Bcfg2/Server/Admin/Bundle.py @@ -71,8 +71,6 @@ class Bundle(Bcfg2.Server.Admin.MetadataCore): print('Details for the "%s" bundle:' % \ (bundle_name[int(lineno)].split('.')[0])) tree = lxml.etree.parse(bundle_list[int(lineno)]) - #Prints bundle content - #print(lxml.etree.tostring(tree)) names = ['Action', 'Package', 'Path', 'Service'] for name in names: for node in tree.findall("//" + name): diff --git a/src/lib/Bcfg2/Server/Lint/__init__.py b/src/lib/Bcfg2/Server/Lint/__init__.py index 7ee18f924..e3b4c8ea7 100644 --- a/src/lib/Bcfg2/Server/Lint/__init__.py +++ b/src/lib/Bcfg2/Server/Lint/__init__.py @@ -90,9 +90,11 @@ class Plugin (object): if el.text and not keep_text: el.text = '...' [el.remove(c) for c in el.iterchildren()] - xml = lxml.etree.tostring(el, encoding='unicode').strip() + xml = lxml.etree.tostring(el, + xml_declaration=False).decode("UTF-8").strip() else: - xml = lxml.etree.tostring(element, encoding='unicode').strip() + xml = lxml.etree.tostring(element, + xml_declaration=False).decode("UTF-8").strip() return " line %s: %s" % (element.sourceline, xml) diff --git a/src/lib/Bcfg2/Server/Plugin.py b/src/lib/Bcfg2/Server/Plugin.py index fa2e16c10..97892c42d 100644 --- a/src/lib/Bcfg2/Server/Plugin.py +++ b/src/lib/Bcfg2/Server/Plugin.py @@ -266,7 +266,7 @@ class ThreadedStatistics(Statistics, threading.Thread): try: pending_data.append((metadata.hostname, lxml.etree.tostring(data, - encoding='unicode'))) + xml_declaration=False).decode("UTF-8"))) except: err = sys.exc_info()[1] self.logger.warning("Dropping interaction for %s: %s" % diff --git a/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py b/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py index 5b8dd24d3..4b9403320 100644 --- a/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py +++ b/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py @@ -386,8 +386,8 @@ class CfgEntrySet(Bcfg2.Server.Plugin.EntrySet): [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, encoding='unicode', - pretty_print=True)) + ofile.write(lxml.etree.tostring(infoxml, xml_declaration=False, + pretty_print=True).decode('UTF-8')) ofile.close() self.debug_log("Wrote file %s" % os.path.join(self.path, "info.xml"), diff --git a/src/lib/Bcfg2/Server/Plugins/FileProbes.py b/src/lib/Bcfg2/Server/Plugins/FileProbes.py index a48524ac9..632d586e8 100644 --- a/src/lib/Bcfg2/Server/Plugins/FileProbes.py +++ b/src/lib/Bcfg2/Server/Plugins/FileProbes.py @@ -34,7 +34,7 @@ data = lxml.etree.Element("ProbedFileData", group=grp.getgrgid(stat[5])[0], perms=oct(stat[0] & 07777)) data.text = b64encode(open(path).read()) -print(lxml.etree.tostring(data, encoding="unicode")) +print(lxml.etree.tostring(data, xml_declaration=False).decode('UTF-8')) """ class FileProbes(Bcfg2.Server.Plugin.Plugin, @@ -214,9 +214,10 @@ class FileProbes(Bcfg2.Server.Plugin.Plugin, root = lxml.etree.Element("FileInfo") root.append(info) try: - open(infoxml, "w").write(lxml.etree.tostring(root, - encoding='unicode', - pretty_print=True)) + open(infoxml, + "w").write(lxml.etree.tostring(root, + xml_declaration=False, + pretty_print=True).decode('UTF-8')) except IOError: err = sys.exc_info()[1] self.logger.error("Could not write %s: %s" % (fileloc, err)) diff --git a/src/lib/Bcfg2/Server/Plugins/Metadata.py b/src/lib/Bcfg2/Server/Plugins/Metadata.py index 8f4f42c96..bc3470273 100644 --- a/src/lib/Bcfg2/Server/Plugins/Metadata.py +++ b/src/lib/Bcfg2/Server/Plugins/Metadata.py @@ -151,8 +151,9 @@ class XMLMetadataConfig(Bcfg2.Server.Plugin.XMLFileBacked): raise Bcfg2.Server.Plugin.MetadataRuntimeError(msg) # prep data dataroot = xmltree.getroot() - newcontents = lxml.etree.tostring(dataroot, pretty_print=True, - encoding='unicode') + newcontents = lxml.etree.tostring(dataroot, xml_declaration=False, + pretty_print=True).decode('UTF-8') + fd = datafile.fileno() while locked(fd) == True: diff --git a/src/lib/Bcfg2/Server/Plugins/Packages/__init__.py b/src/lib/Bcfg2/Server/Plugins/Packages/__init__.py index 71ae6a038..d3095300a 100644 --- a/src/lib/Bcfg2/Server/Plugins/Packages/__init__.py +++ b/src/lib/Bcfg2/Server/Plugins/Packages/__init__.py @@ -178,7 +178,7 @@ class Packages(Bcfg2.Server.Plugin.Plugin, else: self.logger.error("Packages: Malformed Package: %s" % lxml.etree.tostring(pkg, - encoding='unicode')) + xml_declaration=False).decode('UTF-8')) 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 34b5ab187..cae06b659 100644 --- a/src/lib/Bcfg2/Server/Plugins/Probes.py +++ b/src/lib/Bcfg2/Server/Plugins/Probes.py @@ -194,8 +194,8 @@ class Probes(Bcfg2.Server.Plugin.Probing, lxml.etree.SubElement(cx, "Group", name=group) try: datafile = open(os.path.join(self.data, 'probed.xml'), 'w') - datafile.write(lxml.etree.tostring(top, encoding='unicode', - pretty_print='true')) + datafile.write(lxml.etree.tostring(top, xml_declaration=False, + pretty_print='true').decode('UTF-8')) 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 3405ad50c..78019933a 100644 --- a/src/lib/Bcfg2/Server/Plugins/Properties.py +++ b/src/lib/Bcfg2/Server/Plugins/Properties.py @@ -42,9 +42,10 @@ class PropertyFile(Bcfg2.Server.Plugin.StructFile): raise Bcfg2.Server.Plugin.PluginExecutionError(msg) try: - open(self.name, "wb").write(lxml.etree.tostring(self.xdata, - encoding='unicode', - pretty_print=True)) + open(self.name, + "wb").write(lxml.etree.tostring(self.xdata, + xml_declaration=False, + pretty_print=True).decode('UTF-8')) return True except IOError: err = sys.exc_info()[1] diff --git a/src/lib/Bcfg2/Server/Plugins/Statistics.py b/src/lib/Bcfg2/Server/Plugins/Statistics.py index 33b78047a..984efb76c 100644 --- a/src/lib/Bcfg2/Server/Plugins/Statistics.py +++ b/src/lib/Bcfg2/Server/Plugins/Statistics.py @@ -35,7 +35,7 @@ class StatisticsStore(object): self.logger.error("Failed to open %s for writing: %s" % (self.filename + '.new', ioerr)) else: fout.write(lxml.etree.tostring(self.element, - encoding='unicode')) + xml_declaration=False).decode('UTF-8')) fout.close() os.rename(self.filename + '.new', self.filename) self.dirty = 0 diff --git a/src/sbin/bcfg2-build-reports b/src/sbin/bcfg2-build-reports index e49446385..318e9de5d 100755 --- a/src/sbin/bcfg2-build-reports +++ b/src/sbin/bcfg2-build-reports @@ -110,7 +110,7 @@ def rss(reportxml, delivery, report): for item in items: channel.append(item) - tree = tostring(rssdata, encoding='unicode') + tree = tostring(rssdata, xml_declaration=False).decode('UTF-8') fil.write(tree) fil.close() @@ -260,7 +260,7 @@ if __name__ == '__main__': # Apply XSLT, different ones based on report type, and options if deliverymechanism == 'null-operator': # Special Cases - fileout(tostring(ElementTree(procnodereport).getroot(), encoding='unicode'), deliv) + fileout(tostring(ElementTree(procnodereport).getroot(), xml_declaration=False).decode('UTF-8'), deliv) break transform = delivtype + '-' + deliverymechanism + '.xsl' @@ -312,7 +312,7 @@ if __name__ == '__main__': (toastring, socket.getfqdn(), outputstring) mail(outputstring, c) #call function to send else: - outputstring = tostring(stylesheet.apply(ElementTree(procnodereport)).getroot(), encoding='unicode') + outputstring = tostring(stylesheet.apply(ElementTree(procnodereport)).getroot(), xml_declaration=False).decode('UTF-8') if deliverymechanism == 'rss': rss(outputstring, deliv, reprt) else: # Must be deliverymechanism == 'www': diff --git a/src/sbin/bcfg2-crypt b/src/sbin/bcfg2-crypt index 9da4a25d1..9ce21da82 100755 --- a/src/sbin/bcfg2-crypt +++ b/src/sbin/bcfg2-crypt @@ -255,7 +255,7 @@ class PropertiesEncryptor(Encryptor): while xdata.getparent() != None: xdata = xdata.getparent() xdata.set("encryption", "true") - return lxml.etree.tostring(xdata, encoding='unicode') + return lxml.etree.tostring(xdata, xml_declaration=False).decode('UTF-8') def _get_passphrase(self, chunk): pname = chunk.get("encrypted") or chunk.get("encryption") diff --git a/src/sbin/bcfg2-info b/src/sbin/bcfg2-info index 3f7f33344..7fd00032d 100755 --- a/src/sbin/bcfg2-info +++ b/src/sbin/bcfg2-info @@ -395,7 +395,8 @@ class infoCore(cmd.Cmd, Bcfg2.Server.Core.BaseCore): try: metadata = self.build_metadata(client) self.Bind(entry, metadata) - data = lxml.etree.tostring(entry, encoding="unicode") + data = lxml.etree.tostring(entry, + xml_declaration=False).decode('UTF-8') if outfile: open(outfile, 'w').write(data) else: diff --git a/testsuite/Testlib/TestServer/TestPlugin.py b/testsuite/Testlib/TestServer/TestPlugin.py index b6d2a852b..20dae083e 100644 --- a/testsuite/Testlib/TestServer/TestPlugin.py +++ b/testsuite/Testlib/TestServer/TestPlugin.py @@ -9,6 +9,10 @@ from mock import Mock, MagicMock, patch from Bcfg2.Server.Plugin import * from ...common import * +def tostring(el): + return lxml.etree.tostring(el, xml_declaration=False).decode('UTF-8') + + class FakeElementTree(lxml.etree._ElementTree): xinclude = Mock() @@ -808,12 +812,12 @@ class TestXMLFileBacked(TestFileBacked): xdata = lxml.etree.Element("Test", name="test") children = [lxml.etree.SubElement(xdata, "Foo"), lxml.etree.SubElement(xdata, "Bar", name="bar")] - xfb.data = lxml.etree.tostring(xdata) + xfb.data = tostring(xdata) xfb.Index() mock_follow.assert_any_call() self.assertEqual(xfb.xdata.base, fname) - self.assertItemsEqual([lxml.etree.tostring(e) for e in xfb.entries], - [lxml.etree.tostring(e) for e in children]) + self.assertItemsEqual([tostring(e) for e in xfb.entries], + [tostring(e) for e in children]) # with xincludes reset() @@ -838,13 +842,13 @@ class TestXMLFileBacked(TestFileBacked): xfb.xdata.replace(el, replacements[el.get("href")]) FakeElementTree.xinclude.side_effect = xinclude - xfb.data = lxml.etree.tostring(xdata) + xfb.data = tostring(xdata) xfb.Index() mock_follow.assert_any_call() FakeElementTree.xinclude.assert_any_call self.assertEqual(xfb.xdata.base, fname) - self.assertItemsEqual([lxml.etree.tostring(e) for e in xfb.entries], - [lxml.etree.tostring(e) for e in children]) + self.assertItemsEqual([tostring(e) for e in xfb.entries], + [tostring(e) for e in children]) def test_add_monitor(self): fname = "/test/test1.xml" @@ -1358,18 +1362,18 @@ class TestXMLSrc(TestXMLFileBacked): xsrc = self.get_obj("/test/foo.xml") xsrc.__node__ = Mock() - mock_open.return_value.read.return_value = lxml.etree.tostring(xdata) + mock_open.return_value.read.return_value = tostring(xdata) self.assertRaises(PluginExecutionError, xsrc.HandleEvent, Mock()) xdata.set("priority", "cow") - mock_open.return_value.read.return_value = lxml.etree.tostring(xdata) + mock_open.return_value.read.return_value = tostring(xdata) self.assertRaises(PluginExecutionError, xsrc.HandleEvent, Mock()) xdata.set("priority", "10") - mock_open.return_value.read.return_value = lxml.etree.tostring(xdata) + mock_open.return_value.read.return_value = tostring(xdata) mock_open.reset_mock() xsrc = self.get_obj("/test/foo.xml") diff --git a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py index 38161215d..1bee16d59 100644 --- a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py +++ b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py @@ -52,7 +52,8 @@ class TestProbeData(Bcfg2TestCase): def test_xdata(self): xdata = lxml.etree.Element("test") lxml.etree.SubElement(xdata, "test2") - data = ProbeData(lxml.etree.tostring(xdata, encoding='unicode')) + data = ProbeData(lxml.etree.tostring(xdata, + xml_declaration=False).decode('UTF-8')) self.assertIsNotNone(data.xdata) self.assertIsNotNone(data.xdata.find("test2")) @@ -192,7 +193,8 @@ class TestProbes(TestProbing, TestConnector, TestDatabaseBacked): rv = dict() rv["foo.example.com"] = ClientProbeDataSet(timestamp=time.time()) rv["foo.example.com"]["xml"] = \ - ProbeData(lxml.etree.tostring(test_xdata, encoding='unicode')) + ProbeData(lxml.etree.tostring(test_xdata, + xml_declaration=False).decode('UTF-8')) rv["foo.example.com"]["text"] = ProbeData("freeform text") rv["foo.example.com"]["multiline"] = ProbeData("""multiple lines -- cgit v1.2.3-1-g7c22