Logo AND Algorithmique Numérique Distribuée

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