i should commit this.
This commit is contained in:
@@ -1,13 +1,34 @@
|
||||
#!/usr/bin/env python3.6
|
||||
|
||||
import copy
|
||||
from lxml import etree
|
||||
from lxml import etree, objectify
|
||||
|
||||
parser = etree.XMLParser(remove_blank_text = True)
|
||||
#parser = etree.XMLParser(remove_blank_text = True)
|
||||
parser = etree.XMLParser(remove_blank_text = False)
|
||||
|
||||
# We need to append to a new root because you can't edit nsmap, and you can't
|
||||
# xpath on an element with a naked namespace (e.g. 'xlmns="..."').
|
||||
ns = {None: 'http://bdisk.square-r00t.net/',
|
||||
'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
|
||||
xsi = {'{http://www.w3.org/2001/XMLSchema-instance}schemaLocation':
|
||||
'http://bdisk.square-r00t.net bdisk.xsd'}
|
||||
new_cfg = etree.Element('bdisk', nsmap = ns, attrib = xsi)
|
||||
new_cfg.text = '\n '
|
||||
|
||||
with open('single_profile.xml', 'rb') as f:
|
||||
xml = etree.fromstring(f.read(), parser)
|
||||
|
||||
|
||||
roottree = xml.getroottree()
|
||||
for elem in roottree.getiterator():
|
||||
if not hasattr(elem.tag, 'find'):
|
||||
continue
|
||||
i = elem.tag.find('}')
|
||||
if i >= 0:
|
||||
elem.tag = elem.tag[i + 1:]
|
||||
objectify.deannotate(roottree, cleanup_namespaces = True)
|
||||
|
||||
|
||||
single_profile = xml.xpath('/bdisk/profile[1]')[0]
|
||||
alt_profile = copy.deepcopy(single_profile)
|
||||
for c in alt_profile.xpath('//comment()'):
|
||||
@@ -42,18 +63,22 @@ for e in accounts.iter():
|
||||
if e.tag in accounts_tags:
|
||||
e.text = accounts_tags[e.tag]
|
||||
if e.tag == 'rootpass':
|
||||
e.attrib['hashed'] = 'no'
|
||||
e.attrib['hashed'] = 'false'
|
||||
elif e.tag == 'user':
|
||||
e.attrib['sudo'] = 'no'
|
||||
e.attrib['sudo'] = 'false'
|
||||
# Delete the second user
|
||||
accounts.remove(accounts[2])
|
||||
author = alt_profile.xpath('/profile/meta/dev/author')[0]
|
||||
author.addnext(etree.Comment(
|
||||
' You can reference other profiles within the same configuration. '))
|
||||
xml.append(alt_profile)
|
||||
#xml.append(alt_profile)
|
||||
|
||||
for child in xml.xpath('/bdisk/profile'):
|
||||
new_cfg.append(copy.deepcopy(child))
|
||||
new_cfg.append(alt_profile)
|
||||
|
||||
with open('multi_profile.xml', 'wb') as f:
|
||||
f.write(etree.tostring(xml,
|
||||
f.write(etree.tostring(new_cfg,
|
||||
pretty_print = True,
|
||||
encoding = 'UTF-8',
|
||||
xml_declaration = True))
|
||||
|
||||
Reference in New Issue
Block a user