at 22.05-pre 771 B view raw
1import xml.etree.ElementTree as ET 2import sys 3 4tree = ET.parse(sys.argv[1]) 5# the xml tree is of the form 6# <expr><list> {all options, each an attrs} </list></expr> 7options = list(tree.getroot().find('list')) 8 9def sortKey(opt): 10 def order(s): 11 if s.startswith("enable"): 12 return 0 13 if s.startswith("package"): 14 return 1 15 return 2 16 17 return [ 18 (order(p.attrib['value']), p.attrib['value']) 19 for p in opt.findall('attr[@name="loc"]/list/string') 20 ] 21 22# always ensure that the sort order matches the order used in the nix expression! 23options.sort(key=sortKey) 24 25doc = ET.Element("expr") 26newOptions = ET.SubElement(doc, "list") 27newOptions.extend(options) 28ET.ElementTree(doc).write(sys.argv[2], encoding='utf-8')