attempt to remove any "encoding='unicode'" from ADMX/ADML files if lxml does not support the encoding

This commit is contained in:
Ethan Moore 2017-01-10 21:09:16 +00:00
parent 7a78c9fe6a
commit a17e0d8856

View File

@ -91,6 +91,7 @@ try:
import struct
from lxml import etree
from salt.modules.reg import Registry as Registry
import StringIO
HAS_WINDOWS_MODULES = True
TRUE_VALUE_XPATH = etree.XPath('.//*[local-name() = "trueValue"]')
FALSE_VALUE_XPATH = etree.XPath('.//*[local-name() = "falseValue"]')
@ -2685,6 +2686,19 @@ def _updatePolicyElements(policy_item, regkey):
return policy_item
def _remove_unicode_encoding(xml_file):
'''
attempts to remove the "encoding='unicode'" from an xml file
as lxml does not support that on a windows node currently
see issue #38100
'''
with open(xml_file, 'rb') as f:
xml_content = f.read()
modified_xml = re.sub(r' encoding=[\'"]+unicode[\'"]+', '', xml_content.decode('utf-16'), count=1)
xmltree = lxml.etree.parse(StringIO(modified_xml))
return xmltree
def _processPolicyDefinitions(policy_def_path='c:\\Windows\\PolicyDefinitions',
display_language='en-US'):
'''
@ -2710,13 +2724,17 @@ def _processPolicyDefinitions(policy_def_path='c:\\Windows\\PolicyDefinitions',
for t_admfile in files:
admfile = os.path.join(root, t_admfile)
parser = lxml.etree.XMLParser(remove_comments=True)
# see issue #38100
try:
xmltree = lxml.etree.parse(admfile, parser=parser)
except lxml.etree.XMLSyntaxError:
msg = ('A syntax error was found while processing admx file {0},'
' all policies from this file will be unavailable via this module')
log.error(msg.format(admfile))
continue
try:
xmltree = _remove_unicode_encoding(admfile)
except:
msg = ('A error was found while processing admx file {0},'
' all policies from this file will be unavailable via this module')
log.error(msg.format(admfile))
continue
namespaces = xmltree.getroot().nsmap
namespace_string = ''
if None in namespaces:
@ -2770,10 +2788,14 @@ def _processPolicyDefinitions(policy_def_path='c:\\Windows\\PolicyDefinitions',
try:
xmltree = lxml.etree.parse(adml_file)
except lxml.etree.XMLSyntaxError:
msg = ('A syntax error was found while processing adml file {0}, all policy'
' languange data from this file will be unavailable via this module')
log.error(msg.format(adml_file))
continue
# see issue #38100
try:
xmltree = _remove_unicode_encoding(adml_file)
except:
msg = ('An error was found while processing adml file {0}, all policy'
' languange data from this file will be unavailable via this module')
log.error(msg.format(adml_file))
continue
if None in namespaces:
namespaces['None'] = namespaces[None]
namespaces.pop(None)