Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Python: align style with PEP-008
[simgrid.git] / tools / platf_route_rulebased2full.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2013-2014. The SimGrid Team.
5 # All rights reserved.
6
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the license (GNU LGPL) which comes with this package.
9
10 import sys
11 import re
12 from lxml import etree
13
14 xml = etree.parse(sys.argv[1])
15 for e in xml.xpath('//*[@routing="RuleBased"]'):
16     e.attrib['routing'] = "Full"
17     ids = e.xpath('./*[@id]/@id')
18     done = set()
19     for asr in e.xpath("./ASroute"):
20         src_ids = {}
21         dst_ids = {}
22         for id in ids:
23             src_s = re.search(r"%s" % asr.attrib['src'], id)
24             dst_s = re.search(r"%s" % asr.attrib['dst'], id)
25             if src_s:
26                 src_ids[id] = src_s
27             if dst_s:
28                 dst_ids[id] = dst_s
29         for sid, smat in src_ids.items():
30             for did, dmat in dst_ids.items():
31                 todo = tuple(sorted((smat.group(1), dmat.group(1))))
32                 if todo not in done or asr.attrib.get("symmetrical") == "NO":
33                     done.add(todo)
34                     dasr = etree.tounicode(asr)
35                     dasr = dasr.replace("$1src", smat.group(1))
36                     dasr = dasr.replace("$1dst", dmat.group(1))
37                     dasr = etree.fromstring(dasr)
38                     dasr.tag = "__ASroute__"
39                     dasr.attrib['src'] = sid
40                     dasr.attrib['dst'] = did
41                     asr.addnext(dasr)
42         asr.getparent().remove(asr)
43
44 print etree.tounicode(xml).replace("__ASroute__", "ASroute")