Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #2 from cemsbr/master
[simgrid.git] / tools / sg_xml_unit_converter.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # grep -ohrI 'bw=".*"' . | sort | uniq
5
6 import sys, fnmatch, os
7 from decimal import Decimal
8 import re
9
10 def to_str(dec):
11   return re.sub(r"(\.\d*?)0*$", r"\1", dec.to_eng_string()).rstrip(".")
12
13 def format(xml, formats, attrib):
14   res = []
15   m = re.search(r'%s="(.*?)"'%attrib, xml)
16   while m:
17     b, e = m.span(1)
18     res.append(xml[:b])
19     val = xml[b:e]
20     xml = xml[e:]
21     try:
22       power = Decimal(val)
23       tmp = to_str(power)
24       for p, f in formats:
25         d = power / p
26         if d >= 1.0:
27           tmp = "%s%s"%(to_str(d), f)
28           break
29       res.append(tmp)
30     except:
31       print "Error with:", val
32       res.append(val)
33     m = re.search(r'%s="(.*?)"'%attrib, xml)
34
35   res.append(xml)
36   return "".join(res)
37
38 def formats(list):
39   return sorted(((Decimal(i), j) for i,j in list), key=lambda x: x[0], reverse=True)
40
41 for root, dirnames, filenames in os.walk(sys.argv[1]):
42   for filename in fnmatch.filter(filenames, '*.xml'):
43     print root, dirnames, filename
44     path = os.path.join(root, filename)
45     xml = open(path).read()
46       
47     power_formats = formats([( "1E0",  "f"),
48                              ( "1E3", "kf"),
49                              ( "1E6", "Mf"),
50                              ( "1E9", "Gf"),
51                              ("1E12", "Tf"),
52                              ("1E15", "Pt"),
53                              ("1E18", "Ef"),
54                              ("1E21", "Zf")])
55     xml = format(xml, power_formats, "power")
56     
57     bandwidth_formats = formats([( "1E0",  "Bps"),
58                                  ( "1E3", "kBps"),
59                                  ( "1E6", "MBps"),                     
60                                  ( "1E9", "GBps"),
61                                  ("1E12", "TBps")])
62     xml = format(xml, bandwidth_formats, "bandwidth")
63     xml = format(xml, bandwidth_formats, "bw")
64     xml = format(xml, bandwidth_formats, "bb_bw")
65     xml = format(xml, bandwidth_formats, "bw_in")
66     xml = format(xml, bandwidth_formats, "bw_out")
67     
68     time_formats = formats([(     "1E-12",  "ps"),
69                             (     "1E-9" ,  "ns"),
70                             (     "1E-6" ,  "us"),
71                             (     "1E-3" ,  "ms"),
72                             (     "1E0"  ,   "s"),
73                             (    "60E0"  ,   "m"),
74                             (  "3600E0"  ,   "h"),                     
75                             ( "86400E0"  ,   "d"),
76                             ("604800E0"  ,   "w")])
77     xml = format(xml, time_formats, "latency")
78     xml = format(xml, time_formats, "lat")
79     xml = format(xml, time_formats, "bb_lat")
80     
81     #print xml
82     file = open(path, "w")
83     file.write(xml)
84     file.close()