Logo AND Algorithmique Numérique Distribuée

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