summaryrefslogtreecommitdiff
path: root/cleopatre/buildroot/target/device/Spidcom/desc.py
blob: 6bea840a6f8cf0cb2bcf8a7f57fb1cdcd5cc2742 (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
#! /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)


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 = { "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)