summaryrefslogtreecommitdiff
path: root/cleopatre/buildroot/target/device/Spidcom/desc.py
blob: 25db05bd5085bd34cec8f11c40b02fbbd680f9bf (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#! /usr/bin/env python

from __future__ import print_function
import optparse
import os.path
import re
import sys

class GeneratorBase(object):

    def gen(self, recipe, recipe_dir, common_dir):
        self.common_dir = common_dir
        for ingredient in recipe:
            path = self._get_path(ingredient, recipe_dir)
            self._add(path)
        self._print_output()

    def _get_path(self, ingredient, recipe_dir):
        m = re.match(r"(common|local)/(.*)", ingredient)
        if not m:
            raise RuntimeError(
                "Error while parsing:\n\t{0}".format(ingredient))
        if m.group(1) == "local":
            path = os.path.join(recipe_dir, m.group(2))
        elif m.group(1) == "common":
            path = os.path.join(self.common_dir, m.group(2))
        return path


class DirListGenerator(GeneratorBase):

    def __init__(self):
        self.output = []

    def _add(self, ingredient):
        self.output.append(ingredient)

    def _print_output(self):
        for line in self.output:
            print(line)


class TextFileGenerator(GeneratorBase):

    def __init__(self):
        self.output = []

    def _add(self, ingredient):
        self.output.extend(open(ingredient).readlines())

    def _print_output(self):
        for line in self.output:
            print(line, end='')


class KconfigGenerator(GeneratorBase):

    class ConfigLine(object):

        def __init__(self, line):
            self.line = line
            self.symbol = None
            self.overwritten = False

    def _parse_config_line(self, line):
        config_line = KconfigGenerator.ConfigLine(line)

        m = re.match(r"(?P<symbol>\w+)=.*$", line)
        if not m:
            m = re.match(r"# (?P<symbol>\w+) is not set$", line)
        if m:
            config_line.symbol = m.group("symbol")

        return config_line

    def _add_symbol(self, new_config_line):
        found = False
        for (i, config_line) in enumerate(self.config):
            if config_line.symbol == new_config_line.symbol:
                found = True
                break
        if found:
            if self.config[i].overwritten:
                raise RuntimeError(
                    "Multiple overwrite of symbol {0}.".format(
                        self.config[i].symbol))
            self.config[i] = new_config_line
            self.config[i].overwritten = True
        else:
            self.config.append(new_config_line)

    def _add_config_part(self, config_part):
        for line in open(config_part).readlines():
            config_line = self._parse_config_line(line)
            if config_line.symbol:
                self._add_symbol(config_line)
            else:
                self.config.append(config_line)

    def __init__(self):
        self.config = []

    def _add(self, ingredient):
        self._add_config_part(ingredient)

    def _print_output(self):
        for config_line in self.config:
            print(config_line.line, end='')

def get_recipe(desc, gen):
    recipe = []
    found = False
    for line in open(desc).readlines():
        if line[0].isspace():
            if not target:
                raise RuntimeError(
                    "Error while parsing line:\n\t\{0}".format(line))
            if found:
                line = line.strip()
                if line:
                    recipe.append(line.strip())
        else:
            if found:
               break
            else:
                target = line.strip()
                if target == gen:
                    found = True

    if found:
        return recipe
    else:
        return None


def main(args):
    usage="Generate from a description."
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("", "--desc-file", type="string", dest="desc",
                      help="description file")
    parser.add_option("", "--common-dir", type="string", dest="common_dir",
                      help="common directory")
    parser.add_option("", "--gen", type="string", dest="gen",
                      help="what to generate")
    parser.add_option("", "--list-gen", action="store_true", dest="list_gen",
                      help="list possible values for --gen")
    (options, args) = parser.parse_args()

    generators = { "busybox.config": KconfigGenerator,
                   "device_table.txt": TextFileGenerator,
                   "linux26.config": KconfigGenerator,
                   "target_skeleton": DirListGenerator }

    if options.list_gen:
        for generator in sorted(generators.keys()):
            print(generator)
        return

    if (not options.desc) or (not options.common_dir) or (not options.gen):
        parser.print_help()
        return

    if options.gen in generators:
        generator = generators[options.gen]()
    else:
        raise RuntimeError("Don't know how to generate \"{0}\"".format(
                options.gen))

    recipe = get_recipe(options.desc, options.gen)
    if not recipe:
        raise RuntimeError(
            "Cannot find recipe to generate \"{0}\"".format(options.gen))

    generator.gen(recipe, os.path.dirname(options.desc), options.common_dir)

if __name__ == "__main__":
   main(sys.argv)