annotate 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
rev   line source
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
1 import os
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
2 import yaml
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
3
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
4 from .buildtasks import Compile, Assemble, Link
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
5 from .objectfile import ObjectFile
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 355
diff changeset
6 from .target.target_list import targets
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
7
353
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 342
diff changeset
8
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
9 class RecipeLoader:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
10 """ Loads a recipe into a runner from a dictionary or file """
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 353
diff changeset
11 def __init__(self, runner):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 353
diff changeset
12 self.runner = runner
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
13 self.directive_handlers = {}
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
14 for a in dir(self):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
15 if a.startswith('handle_'):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
16 f = getattr(self, a)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
17 self.directive_handlers[a[7:]] = f
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
18
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 353
diff changeset
19 def load_file(self, recipe_file):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 353
diff changeset
20 """ Loads a recipe dictionary from file """
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
21 self.recipe_dir = os.path.abspath(os.path.dirname(recipe_file))
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
22 with open(recipe_file, 'r') as f:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
23 recipe = yaml.load(f)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
24 self.load_dict(recipe)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
25
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
26 def relpath(self, filename):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
27 return os.path.join(self.recipe_dir, filename)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
28
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
29 def openfile(self, filename):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
30 return open(self.relpath(filename), 'r')
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
31
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
32 def handle_compile(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
33 sources = [self.openfile(s) for s in value['sources']]
367
577ed7fb3fe4 Try to make thumb work again
Windel Bouwman
parents: 366
diff changeset
34 if 'includes' in value:
577ed7fb3fe4 Try to make thumb work again
Windel Bouwman
parents: 366
diff changeset
35 includes = [self.openfile(i) for i in value['includes']]
577ed7fb3fe4 Try to make thumb work again
Windel Bouwman
parents: 366
diff changeset
36 else:
577ed7fb3fe4 Try to make thumb work again
Windel Bouwman
parents: 366
diff changeset
37 includes = []
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
38 target = targets[value['machine']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
39 output = ObjectFile()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
40 task = Compile(sources, includes, target, output)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
41 self.runner.add_task(task)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
42 return task
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
43
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
44 def handle_assemble(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
45 asm_src = self.openfile(value['source'])
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
46 target = targets[value['machine']]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
47 output = ObjectFile()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
48 task = Assemble(asm_src, target, output)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
49 self.runner.add_task(task)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
50 return task
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
51
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
52 def handle_link(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
53 inputs = value['inputs']
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
54 objs = []
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
55 for i in inputs:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
56 task = self.load_dict(i)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
57 objs.append(task.output)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
58 layout = value['layout']
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
59 output = self.relpath(value['output'])
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
60 self.runner.add_task(Link(objs, layout, output))
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
61
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
62 def handle_apps(self, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
63 for a in value:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
64 self.load_dict(a)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
65
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
66 def load_dict(self, recipe):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
67 for command, value in recipe.items():
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
68 return self.directive_handlers[command](value)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
69