Clean up the Node.FS class.
[scons.git] / src / engine / SCons / Builder.py
1 """SCons.Builder
2
3 XXX
4
5 """
6
7 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
8
9
10
11 import os
12 import SCons.Node.FS
13 import types
14
15
16
17 class Builder:
18     """Base class for Builders, objects that create output
19     nodes (files) from input nodes (files).
20     """
21
22     def __init__(self,  name = None,
23                         action = None,
24                         input_suffix = None,
25                         output_suffix = None,
26                         node_factory = SCons.Node.FS.default_fs.File):
27         self.name = name
28         self.action = Action(action)
29         self.insuffix = input_suffix
30         self.outsuffix = output_suffix
31         self.node_factory = node_factory
32         if not self.insuffix is None and self.insuffix[0] != '.':
33             self.insuffix = '.' + self.insuffix
34         if not self.outsuffix is None and self.outsuffix[0] != '.':
35             self.outsuffix = '.' + self.outsuffix
36
37     def __cmp__(self, other):
38         return cmp(self.__dict__, other.__dict__)
39
40     def __call__(self, env, target = None, source = None):
41         node = self.node_factory(target)
42         node.builder_set(self)
43         node.env_set(self)
44         node.sources = source   # XXX REACHING INTO ANOTHER OBJECT
45         return node
46
47     def execute(self, **kw):
48         """Execute a builder's action to create an output object.
49         """
50         apply(self.action.execute, (), kw)
51
52
53
54 print_actions = 1;
55 execute_actions = 1;
56
57
58
59 def Action(act):
60     """A factory for action objects."""
61     if type(act) == types.FunctionType:
62         return FunctionAction(act)
63     elif type(act) == types.StringType:
64         return CommandAction(act)
65     else:
66         return None
67
68 class ActionBase:
69     """Base class for actions that create output objects.
70     
71     We currently expect Actions will only be accessible through
72     Builder objects, so they don't yet merit their own module."""
73     def __cmp__(self, other):
74         return cmp(self.__dict__, other.__dict__)
75
76     def show(self, string):
77         print string
78
79 class CommandAction(ActionBase):
80     """Class for command-execution actions."""
81     def __init__(self, string):
82         self.command = string
83
84     def execute(self, **kw):
85         cmd = self.command % kw
86         if print_actions:
87             self.show(cmd)
88         if execute_actions:
89             os.system(cmd)
90
91 class FunctionAction(ActionBase):
92     """Class for Python function actions."""
93     def __init__(self, function):
94         self.function = function
95
96     def execute(self, **kw):
97         # if print_actions:
98         # XXX:  WHAT SHOULD WE PRINT HERE?
99         if execute_actions:
100             self.function(kw)