cffaa1939eeb94ae4e8a5414cc1553927193ee65
[scons.git] / src / engine / SCons / Options.py
1 """engine.SCons.Options
2
3 This file defines the Options class that is used to add user-friendly customizable
4 variables to a scons build.
5 """
6
7 #
8 # Copyright (c) 2001, 2002, 2003 Steven Knight
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
17 #
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
20 #
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
23 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #
29
30 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
31
32 import SCons.Errors
33 import os.path
34
35
36 class Options:
37     """
38     Holds all the options, updates the environment with the variables,
39     and renders the help text.
40     """
41     def __init__(self, file=None):
42         """
43         file - [optional] the name of the customizable file.
44         """
45
46         self.options = []
47         self.file = file
48
49     def Add(self, key, help="", default=None, validater=None, converter=None):
50         """
51         Add an option.
52
53         key - the name of the variable
54         help - optional help text for the options
55         default - optional default value
56         validater - optional function that is called to validate the option's value
57         converter - optional function that is called to convert the option's value before
58             putting it in the environment.
59         """
60
61         class Option:
62             pass
63
64         option = Option()
65         option.key = key
66         option.help = help
67         option.default = default
68         option.validater = validater
69         option.converter = converter
70
71         self.options.append(option)
72
73     def Update(self, env, args):
74         """
75         Update an environment with the option variables.
76
77         env - the environment to update.
78         args - the dictionary to get the command line arguments from.
79         """
80
81         values = {}
82
83         # first set the defaults:
84         for option in self.options:
85             if not option.default is None:
86                 values[option.key] = option.default
87
88         # next set the value specified in the options file
89         if self.file and os.path.exists(self.file):
90             execfile(self.file, values)
91
92         # finally set the values specified on the command line
93         values.update(args)
94
95         # put the variables in the environment:
96         # (don't copy over variables that are not declared
97         #  as options)
98         for option in self.options:
99             try:
100                 env[option.key] = values[option.key]
101             except KeyError:
102                 pass
103
104         # Call the convert functions:
105         for option in self.options:
106             if option.converter:
107                 value = env.subst('${%s}'%option.key)
108                 try:
109                     env[option.key] = option.converter(value)
110                 except ValueError, x:
111                     raise SCons.Errors.UserError, 'Error converting option: %s\n%s'%(option.key, x)
112
113
114         # Finally validate the values:
115         for option in self.options:
116             if option.validater:
117                 option.validater(option.key, env.subst('${%s}'%option.key))
118
119
120     def GenerateHelpText(self, env):
121         """
122         Generate the help text for the options.
123
124         env - an environment that is used to get the current values of the options.
125         """
126
127         help_text = ""
128
129         for option in self.options:
130             help_text = help_text + '\n%s: %s\n    default: %s\n'%(option.key, option.help, option.default)
131             if env.has_key(option.key):
132                 help_text = help_text + '    actual: %s\n'%env.subst('${%s}'%option.key)
133             else:
134                 help_text = help_text + '    actual: None\n'
135
136         return help_text
137