OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [tools/] [cml2-tools/] [configtrans.py] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
#!/usr/bin/env python
2
"""
3
configtrans.py -- translate between CML1 and CML2 config formats.
4
 
5
This handles the impedance mismatch between CML2's explicit NAME=VALUE
6
output format and the formats expected by the Linux build machinery.
7
 
8
Note: it also makes backups whenever it touches a file.
9
 
10
configtrans.py -h includeout -s configout cml2file
11
configtrans.py -t <newconfig >oldconfig
12
"""
13
import sys, os, getopt, re
14
 
15
def linetrans(hook, instream, outstream, trailer=None):
16
    "Line-by-line translation between streams."
17
    if not hasattr(instream, "readline"):
18
        instream = open(instream, "r")
19
    if not hasattr(outstream, "readline"):
20
        outstream = open(outstream, "w")
21
    while 1:
22
        line = instream.readline()
23
        if not line:
24
            break
25
        new = hook(line)
26
        if new:
27
            outstream.write(new)
28
    instream.close()
29
    if trailer:
30
        outstream.write(trailer)
31
    outstream.close()
32
 
33
def write_include(line):
34
    "Transform a SYMBOL=VALUE line to CML1 include format."
35
    if line.find("PRIVATE") > -1 or line[:2] == "$$":
36
        return ""
37
    match = isnotset.match(line)
38
    if match:
39
        return "#undef  %s\n" % match.group(1)
40
    if line == "#\n":
41
        return None
42
    elif line[0] == "#":
43
        return "/* " + line[1:].strip() + " */\n"
44
    eq = line.find("=")
45
    if eq == -1:
46
        return line
47
    else:
48
        line = line.split('#')[0]
49
        symbol = line[:eq]
50
        value = line[eq+1 :].strip()
51
    if value == 'y':
52
        return "#define %s 1\n" % symbol
53
    elif value == 'm':
54
        return "#undef %s\n#define %s_MODULE 1\n" % (symbol, symbol)
55
    elif value == 'n':
56
        return "#undef  %s\n" % symbol
57
    else:
58
        return "#define %s %s\n" % (symbol, value)
59
 
60
def write_defconfig(line):
61
    "Transform a SYMBOL=VALUE line to CML1 defconfig format."
62
    if line[:2] == "$$":
63
        return ""
64
    eq = line.find("=")
65
    if eq == -1:
66
        return line
67
    else:
68
        line = line.split('#')[0]
69
        line = line.strip()
70
        if len(line) == 0 or line[-1] != "\n":
71
            line += "\n"
72
        symbol = line[:eq]
73
        value = line[eq+1:].strip()
74
    if value == 'n':
75
        return "# %s is not set\n" % symbol
76
    else:
77
        return line
78
 
79
def revert(line):
80
    "Translate a CML1 defconfig file to CML2 format."
81
    match = isnotset.match(line)
82
    if match:
83
        return "%s=n\n" % match.group(1)
84
    else:
85
        return line
86
 
87
if __name__ == '__main__':
88
    isnotset = re.compile("^# (.*) is not set")
89
    include = defconfig = translate = None
90
    (options, arguments) = getopt.getopt(sys.argv[1:], "h:s:t")
91
    for (switch, val) in options:
92
        if switch == '-h':
93
            includefile = val
94
            try:
95
                os.rename(val, val + ".old")
96
            except OSError:
97
                pass
98
        elif switch == '-s':
99
            defconfig = val
100
            try:
101
                os.rename(val, val + ".old")
102
            except OSError:
103
                pass
104
        elif switch == '-t':
105
            translate = 1
106
    if len(arguments) > 0:
107
        try:
108
            if includefile:
109
                linetrans(write_include, arguments[0], includefile, "#define AUTOCONF_INCLUDED\n")
110
            if defconfig:
111
                linetrans(write_defconfig, arguments[0], defconfig)
112
        except IOError, args:
113
            sys.stderr.write("configtrans: " + args[1] + "\n");
114
            raise SystemExit, 1
115
    elif translate:
116
        linetrans(revert, sys.stdin, sys.stdout)
117
    else:
118
        print "usage: configtrans.py -t [-h includefile] [-s defconfig] file"
119
        raise SystemExit, 1
120
 
121
# That's all, folks!
122
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.