File: sg_xml_unit_converter.py

package info (click to toggle)
simgrid 4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,236 kB
  • sloc: cpp: 124,918; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (95 lines) | stat: -rw-r--r-- 3,182 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2013-2025. The SimGrid Team. All rights reserved.

# This program is free software; you can redistribute it and/or modify it
# under the terms of the license (GNU LGPL) which comes with this package.

# grep -ohrI 'bw=".*"' . | sort | uniq

import sys
import fnmatch
import os
from decimal import Decimal, DecimalException
import re


def to_str(dec):
    return re.sub(r"(\.\d*?)0*$", r"\1", dec.to_eng_string()).rstrip(".")


def convert(xml, formats, attrib):
    res = []
    m = re.search(r'%s="(.*?)"' % attrib, xml)
    while m:
        b, e = m.span(1)
        res.append(xml[:b])
        val = xml[b:e]
        xml = xml[e:]
        try:
            power = Decimal(val)
            tmp = to_str(power)
            for p, f in formats:
                d = power / p
                if d >= 1.0:
                    tmp = "%s%s" % (to_str(d), f)
                    break
            res.append(tmp)
        except DecimalException:
            print "Error with:", val
            res.append(val)
        m = re.search(r'%s="(.*?)"' % attrib, xml)

    res.append(xml)
    return "".join(res)


def formats(ll):
    return sorted(((Decimal(i), j) for i, j in ll), key=lambda x: x[0], reverse=True)


for root, dirnames, filenames in os.walk(sys.argv[1]):
    for filename in fnmatch.filter(filenames, '*.xml'):
        print root, dirnames, filename
        path = os.path.join(root, filename)
        xml = open(path).read()

        power_formats = formats([("1E0", "f"),
                                 ("1E3", "kf"),
                                 ("1E6", "Mf"),
                                 ("1E9", "Gf"),
                                 ("1E12", "Tf"),
                                 ("1E15", "Pt"),
                                 ("1E18", "Ef"),
                                 ("1E21", "Zf")])
        xml = convert(xml, power_formats, "power")

        bandwidth_formats = formats([("1E0", "Bps"),
                                     ("1E3", "kBps"),
                                     ("1E6", "MBps"),
                                     ("1E9", "GBps"),
                                     ("1E12", "TBps")])
        xml = convert(xml, bandwidth_formats, "bandwidth")
        xml = convert(xml, bandwidth_formats, "bw")
        xml = convert(xml, bandwidth_formats, "bb_bw")
        xml = convert(xml, bandwidth_formats, "bw_in")
        xml = convert(xml, bandwidth_formats, "bw_out")

        time_formats = formats([("1E-12", "ps"),
                                ("1E-9", "ns"),
                                ("1E-6", "us"),
                                ("1E-3", "ms"),
                                ("1E0", "s"),
                                ("60E0", "m"),
                                ("3600E0", "h"),
                                ("86400E0", "d"),
                                ("604800E0", "w")])
        xml = convert(xml, time_formats, "latency")
        xml = convert(xml, time_formats, "lat")
        xml = convert(xml, time_formats, "bb_lat")

        # print xml
        outfile = open(path, "w")
        outfile.write(xml)
        outfile.close()