dom0/core: Added the pretty print of qubes.xml

* Replaced xml.etree.ElementTree with lxml.etree
* Added argument to tree.write(file, encoding="UTF-8", pretty_print=True)
This commit is contained in:
Bruce A Downs 2012-10-17 13:26:13 -06:00 committed by Marek Marczykowski
parent 28b4f82b0a
commit a504600c20

View File

@ -25,7 +25,7 @@ import stat
import os import os
import os.path import os.path
import subprocess import subprocess
import xml.etree.ElementTree import lxml.etree
import xml.parsers.expat import xml.parsers.expat
import fcntl import fcntl
import re import re
@ -1203,7 +1203,7 @@ class QubesVm(object):
if item not in conf: if item not in conf:
conf[item] = defaults[item] conf[item] = defaults[item]
root = xml.etree.ElementTree.Element( root = lxml.etree.Element(
"QubesFirwallRules", "QubesFirwallRules",
policy = "allow" if conf["allow"] else "deny", policy = "allow" if conf["allow"] else "deny",
dns = "allow" if conf["allowDns"] else "deny", dns = "allow" if conf["allowDns"] else "deny",
@ -1218,7 +1218,7 @@ class QubesVm(object):
rule["proto"] = "tcp" rule["proto"] = "tcp"
else: else:
rule["proto"] = "any" rule["proto"] = "any"
element = xml.etree.ElementTree.Element( element = lxml.etree.Element(
"rule", "rule",
address=rule["address"], address=rule["address"],
proto=str(rule["proto"]), proto=str(rule["proto"]),
@ -1232,7 +1232,7 @@ class QubesVm(object):
root.append(element) root.append(element)
tree = xml.etree.ElementTree.ElementTree(root) tree = lxml.etree.ElementTree(root)
try: try:
f = open(self.firewall_conf, 'a') # create the file if not exist f = open(self.firewall_conf, 'a') # create the file if not exist
@ -1240,7 +1240,7 @@ class QubesVm(object):
with open(self.firewall_conf, 'w') as f: with open(self.firewall_conf, 'w') as f:
fcntl.lockf(f, fcntl.LOCK_EX) fcntl.lockf(f, fcntl.LOCK_EX)
tree.write(f, "UTF-8") tree.write(f, encoding="UTF-8", pretty_print=True)
fcntl.lockf(f, fcntl.LOCK_UN) fcntl.lockf(f, fcntl.LOCK_UN)
f.close() f.close()
except EnvironmentError as err: except EnvironmentError as err:
@ -1267,7 +1267,7 @@ class QubesVm(object):
conf = self.get_firewall_defaults() conf = self.get_firewall_defaults()
try: try:
tree = xml.etree.ElementTree.parse(self.firewall_conf) tree = lxml.etree.parse(self.firewall_conf)
root = tree.getroot() root = tree.getroot()
conf["allow"] = (root.get("policy") == "allow") conf["allow"] = (root.get("policy") == "allow")
@ -1598,7 +1598,7 @@ class QubesVm(object):
rx_type = re.compile (r"VM") rx_type = re.compile (r"VM")
attrs = self.get_xml_attrs() attrs = self.get_xml_attrs()
element = xml.etree.ElementTree.Element( element = lxml.etree.Element(
"Qubes" + rx_type.sub("Vm", self.type), "Qubes" + rx_type.sub("Vm", self.type),
**attrs) **attrs)
return element return element
@ -2811,7 +2811,7 @@ class QubesVmCollection(dict):
self.qubes_store_file.close() self.qubes_store_file.close()
def save(self): def save(self):
root = xml.etree.ElementTree.Element( root = lxml.etree.Element(
"QubesVmCollection", "QubesVmCollection",
default_template=str(self.default_template_qid) \ default_template=str(self.default_template_qid) \
@ -2837,7 +2837,7 @@ class QubesVmCollection(dict):
element = vm.create_xml_element() element = vm.create_xml_element()
if element is not None: if element is not None:
root.append(element) root.append(element)
tree = xml.etree.ElementTree.ElementTree(root) tree = lxml.etree.ElementTree(root)
try: try:
@ -2845,7 +2845,7 @@ class QubesVmCollection(dict):
# file as "r+" in the lock_db_for_writing() function # file as "r+" in the lock_db_for_writing() function
self.qubes_store_file.seek (0, os.SEEK_SET) self.qubes_store_file.seek (0, os.SEEK_SET)
self.qubes_store_file.truncate() self.qubes_store_file.truncate()
tree.write(self.qubes_store_file, "UTF-8") tree.write(self.qubes_store_file, encoding="UTF-8", pretty_print=True)
except EnvironmentError as err: except EnvironmentError as err:
print("{0}: export error: {1}".format( print("{0}: export error: {1}".format(
os.path.basename(sys.argv[0]), err)) os.path.basename(sys.argv[0]), err))
@ -2973,7 +2973,7 @@ class QubesVmCollection(dict):
dom0_vm = dom0vm dom0_vm = dom0vm
try: try:
tree = xml.etree.ElementTree.parse(self.qubes_store_file) tree = lxml.etree.parse(self.qubes_store_file)
except (EnvironmentError, except (EnvironmentError,
xml.parsers.expat.ExpatError) as err: xml.parsers.expat.ExpatError) as err:
print("{0}: import error: {1}".format( print("{0}: import error: {1}".format(