view python/ppci/recipe.py @ 368:d2ddfe134c48

Remove yield from for python < 3.3
author Windel Bouwman
date Fri, 21 Mar 2014 11:21:50 +0100
parents 577ed7fb3fe4
children 9667d78ba79e
line wrap: on
line source

import os
import yaml

from .buildtasks import Compile, Assemble, Link
from .objectfile import ObjectFile
from .target.target_list import targets


class RecipeLoader:
    """ Loads a recipe into a runner from a dictionary or file """
    def __init__(self, runner):
        self.runner = runner
        self.directive_handlers = {}
        for a in dir(self):
            if a.startswith('handle_'):
                f = getattr(self, a)
                self.directive_handlers[a[7:]] = f

    def load_file(self, recipe_file):
        """ Loads a recipe dictionary from file """
        self.recipe_dir = os.path.abspath(os.path.dirname(recipe_file))
        with open(recipe_file, 'r') as f:
            recipe = yaml.load(f)
        self.load_dict(recipe)

    def relpath(self, filename):
        return os.path.join(self.recipe_dir, filename)

    def openfile(self, filename):
        return open(self.relpath(filename), 'r')

    def handle_compile(self, value):
        sources = [self.openfile(s) for s in value['sources']]
        if 'includes' in value:
            includes = [self.openfile(i) for i in value['includes']]
        else:
            includes = []
        target = targets[value['machine']]
        output = ObjectFile()
        task = Compile(sources, includes, target, output)
        self.runner.add_task(task)
        return task

    def handle_assemble(self, value):
        asm_src = self.openfile(value['source'])
        target = targets[value['machine']]
        output = ObjectFile()
        task = Assemble(asm_src, target, output)
        self.runner.add_task(task)
        return task

    def handle_link(self, value):
        inputs = value['inputs']
        objs = []
        for i in inputs:
            task = self.load_dict(i)
            objs.append(task.output)
        layout = value['layout']
        output = self.relpath(value['output'])
        self.runner.add_task(Link(objs, layout, output))

    def handle_apps(self, value):
        for a in value:
            self.load_dict(a)

    def load_dict(self, recipe):
        for command, value in recipe.items():
            return self.directive_handlers[command](value)