annotate bitten/build/config.py @ 904:288791ac7f02 0.6.x

0.6dev: Merge r984 from trunk.
author hodgestar
date Fri, 11 Mar 2011 13:24:35 +0000
parents f4d07544722b
children
rev   line source
379
0df178e07fdb Use UTF-8 as encoding of source files.
cmlenz
parents: 313
diff changeset
1 # -*- coding: utf-8 -*-
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
2 #
408
933105ab516b Update file headers and other stuff pointing to the old home.
cmlenz
parents: 392
diff changeset
3 # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de>
833
f4d07544722b 0.6dev: Merged [910] from trunk.
osimons
parents: 675
diff changeset
4 # Copyright (C) 2007-2010 Edgewall Software
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
5 # All rights reserved.
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
6 #
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
7 # This software is licensed as described in the file COPYING, which
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
8 # you should have received as part of this distribution. The terms
408
933105ab516b Update file headers and other stuff pointing to the old home.
cmlenz
parents: 392
diff changeset
9 # are also available at http://bitten.edgewall.org/wiki/License.
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
10
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
11 """Support for build slave configuration."""
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
12
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
13 from ConfigParser import SafeConfigParser
240
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
14 import logging
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
15 import os
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
16 import platform
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
17 import re
670
a9d8359f4dc9 0.6dev: Follow-up on #436 / [737], changing to use `string.Template()` instead of `os.path.expandvars()` as this produces a consistent result across platforms and python version.
osimons
parents: 663
diff changeset
18 from string import Template
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
19
240
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
20 log = logging.getLogger('bitten.config')
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
21
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
22 __docformat__ = 'restructuredtext en'
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
23
675
251be647314c 0.6dev: Adding some error-handling to bitten-slave for config files. It now reports errors and exists gracefully if a) config file isn't found, or b) problem parsing content (wrong format).
osimons
parents: 670
diff changeset
24 class ConfigFileNotFound(Exception):
251be647314c 0.6dev: Adding some error-handling to bitten-slave for config files. It now reports errors and exists gracefully if a) config file isn't found, or b) problem parsing content (wrong format).
osimons
parents: 670
diff changeset
25 pass
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
26
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
27 class Configuration(object):
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
28 """Encapsulates the configuration of a build machine.
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
29
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
30 Configuration values can be provided through a configuration file (in INI
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
31 format) or through command-line parameters (properties). In addition to
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
32 explicitly defined properties, this class automatically collects platform
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
33 information and stores them as properties. These defaults can be
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
34 overridden (useful for cross-compilation).
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
35 """
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
36
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
37 def __init__(self, filename=None, properties=None):
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
38 """Create the configuration object.
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
39
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
40 :param filename: the path to the configuration file, if any
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
41 :param properties: a dictionary of the configuration properties
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
42 provided on the command-line
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
43 """
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
44 self.properties = {}
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
45 self.packages = {}
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
46 parser = SafeConfigParser()
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
47 if filename:
675
251be647314c 0.6dev: Adding some error-handling to bitten-slave for config files. It now reports errors and exists gracefully if a) config file isn't found, or b) problem parsing content (wrong format).
osimons
parents: 670
diff changeset
48 if not (os.path.isfile(filename) or os.path.islink(filename)):
251be647314c 0.6dev: Adding some error-handling to bitten-slave for config files. It now reports errors and exists gracefully if a) config file isn't found, or b) problem parsing content (wrong format).
osimons
parents: 670
diff changeset
49 raise ConfigFileNotFound(
251be647314c 0.6dev: Adding some error-handling to bitten-slave for config files. It now reports errors and exists gracefully if a) config file isn't found, or b) problem parsing content (wrong format).
osimons
parents: 670
diff changeset
50 "Configuration file %r not found." % filename)
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
51 parser.read(filename)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
52 self._merge_sysinfo(parser, properties)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
53 self._merge_packages(parser, properties)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
54
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
55 def _merge_sysinfo(self, parser, properties):
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
56 """Merge the platform information properties into the configuration."""
463
977a6c122205 Make slave names available for use in target platform rules, and added some documentation to the admin panel. Closes #190.
cmlenz
parents: 411
diff changeset
57 system, _, release, version, machine, processor = platform.uname()
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
58 system, release, version = platform.system_alias(system, release,
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
59 version)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
60 self.properties['machine'] = machine
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
61 self.properties['processor'] = processor
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
62 self.properties['os'] = system
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
63 self.properties['family'] = os.name
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
64 self.properties['version'] = release
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
65
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
66 mapping = {'machine': ('machine', 'name'),
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
67 'processor': ('machine', 'processor'),
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
68 'os': ('os', 'name'),
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
69 'family': ('os', 'family'),
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
70 'version': ('os', 'version')}
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
71 for key, (section, option) in mapping.items():
586
246f211c1328 0.6dev: Fixing some minor issues with Configuration:
osimons
parents: 463
diff changeset
72 if parser.has_option(section, option):
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
73 value = parser.get(section, option)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
74 if value is not None:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
75 self.properties[key] = value
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
76
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
77 if properties:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
78 for key, value in properties.items():
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
79 if key in mapping:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
80 self.properties[key] = value
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
81
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
82 def _merge_packages(self, parser, properties):
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
83 """Merge package information into the configuration."""
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
84 for section in parser.sections():
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
85 if section in ('os', 'machine', 'maintainer'):
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
86 continue
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
87 package = {}
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
88 for option in parser.options(section):
586
246f211c1328 0.6dev: Fixing some minor issues with Configuration:
osimons
parents: 463
diff changeset
89 if option == 'name':
246f211c1328 0.6dev: Fixing some minor issues with Configuration:
osimons
parents: 463
diff changeset
90 log.warning("Reserved configuration option 'name' used "
246f211c1328 0.6dev: Fixing some minor issues with Configuration:
osimons
parents: 463
diff changeset
91 "for package/section '%s'. Skipping." % section)
246f211c1328 0.6dev: Fixing some minor issues with Configuration:
osimons
parents: 463
diff changeset
92 continue
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
93 package[option] = parser.get(section, option)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
94 self.packages[section] = package
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
95
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
96 if properties:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
97 for key, value in properties.items():
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
98 if '.' in key:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
99 package, propname = key.split('.', 1)
586
246f211c1328 0.6dev: Fixing some minor issues with Configuration:
osimons
parents: 463
diff changeset
100 if propname == 'name':
246f211c1328 0.6dev: Fixing some minor issues with Configuration:
osimons
parents: 463
diff changeset
101 log.warning("Reserved configuration option 'name' "
246f211c1328 0.6dev: Fixing some minor issues with Configuration:
osimons
parents: 463
diff changeset
102 "used for property '%s'. Skipping." % key)
246f211c1328 0.6dev: Fixing some minor issues with Configuration:
osimons
parents: 463
diff changeset
103 continue
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
104 if package not in self.packages:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
105 self.packages[package] = {}
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
106 self.packages[package][propname] = value
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
107
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
108 def __contains__(self, key):
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
109 """Return whether the configuration contains a value for the specified
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
110 key.
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
111
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
112 :param key: name of the configuration option using dotted notation
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
113 (for example, "python.path")
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
114 """
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
115 if '.' in key:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
116 package, propname = key.split('.', 1)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
117 return propname in self.packages.get(package, {})
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
118 return key in self.properties
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
119
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
120 def __getitem__(self, key):
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
121 """Return the value for the specified configuration key.
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
122
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
123 :param key: name of the configuration option using dotted notation
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
124 (for example, "python.path")
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
125 """
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
126 if '.' in key:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
127 package, propname = key.split('.', 1)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
128 return self.packages.get(package, {}).get(propname)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
129 return self.properties.get(key)
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
130
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
131 def __str__(self):
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
132 return str({'properties': self.properties, 'packages': self.packages})
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
133
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
134 def get_dirpath(self, key):
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
135 """Return the value of the specified configuration key, but verify that
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
136 the value refers to the path of an existing directory.
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
137
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
138 If the value does not exist, or is not a directory path, return `None`.
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
139
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
140 :param key: name of the configuration option using dotted notation
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
141 (for example, "ant.home")
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
142 """
240
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
143 dirpath = self[key]
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
144 if dirpath:
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
145 if os.path.isdir(dirpath):
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
146 return dirpath
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
147 log.warning('Invalid %s: %s is not a directory', key, dirpath)
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
148 return None
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
149
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
150 def get_filepath(self, key):
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
151 """Return the value of the specified configuration key, but verify that
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
152 the value refers to the path of an existing file.
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
153
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
154 If the value does not exist, or is not a file path, return `None`.
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
155
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
156 :param key: name of the configuration option using dotted notation
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
157 (for example, "python.path")
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
158 """
240
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
159 filepath = self[key]
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
160 if filepath:
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
161 if os.path.isfile(filepath):
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
162 return filepath
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
163 log.warning('Invalid %s: %s is not a file', key, filepath)
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
164 return None
24e91cbae6e0 New recipe command `<java:ant>` for running Ant builds.
cmlenz
parents: 233
diff changeset
165
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
166 _VAR_RE = re.compile(r'\$\{(?P<ref>\w[\w.]*?\w)(?:\:(?P<def>.+))?\}')
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
167
392
026d9aa41b85 Merged HTTP branch into trunk.
cmlenz
parents: 379
diff changeset
168 def interpolate(self, text, **vars):
663
820583ca4dc3 0.6dev: Adding interpolation of environment variables, supporting `$VAR` and `${VAR}` on all platforms, and `%VAR%` on Windows. Updated docs + new test. Closes #436.
osimons
parents: 599
diff changeset
169 """Interpolate configuration and environment properties into a string.
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
170
663
820583ca4dc3 0.6dev: Adding interpolation of environment variables, supporting `$VAR` and `${VAR}` on all platforms, and `%VAR%` on Windows. Updated docs + new test. Closes #436.
osimons
parents: 599
diff changeset
171 Configuration properties can be referenced in the text using the notation
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
172 ``${property.name}``. A default value can be provided by appending it to
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
173 the property name separated by a colon, for example
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
174 ``${property.name:defaultvalue}``. This value will be used when there's
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
175 no such property in the configuration. Otherwise, if no default is
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
176 provided, the reference is not replaced at all.
663
820583ca4dc3 0.6dev: Adding interpolation of environment variables, supporting `$VAR` and `${VAR}` on all platforms, and `%VAR%` on Windows. Updated docs + new test. Closes #436.
osimons
parents: 599
diff changeset
177
820583ca4dc3 0.6dev: Adding interpolation of environment variables, supporting `$VAR` and `${VAR}` on all platforms, and `%VAR%` on Windows. Updated docs + new test. Closes #436.
osimons
parents: 599
diff changeset
178 Environment properties substitute from environment variables, supporting
670
a9d8359f4dc9 0.6dev: Follow-up on #436 / [737], changing to use `string.Template()` instead of `os.path.expandvars()` as this produces a consistent result across platforms and python version.
osimons
parents: 663
diff changeset
179 the common notations of ``$VAR`` and ``${VAR}``.
313
90422699a594 More and improved docstrings (using epydoc format).
cmlenz
parents: 240
diff changeset
180
411
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
181 :param text: the string containing variable references
a169d2e96463 Use reStructuredText as the API documentation syntax.
cmlenz
parents: 408
diff changeset
182 :param vars: extra variables to use for the interpolation
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
183 """
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
184 def _replace(m):
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
185 refname = m.group('ref')
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
186 if refname in self:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
187 return self[refname]
392
026d9aa41b85 Merged HTTP branch into trunk.
cmlenz
parents: 379
diff changeset
188 elif refname in vars:
026d9aa41b85 Merged HTTP branch into trunk.
cmlenz
parents: 379
diff changeset
189 return vars[refname]
233
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
190 elif m.group('def'):
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
191 return m.group('def')
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
192 else:
8f816147620f * Moved SlaveConfiguration logic into new module ([source:/trunk/bitten/build/config.py bitten.build.config]).
cmlenz
parents:
diff changeset
193 return m.group(0)
670
a9d8359f4dc9 0.6dev: Follow-up on #436 / [737], changing to use `string.Template()` instead of `os.path.expandvars()` as this produces a consistent result across platforms and python version.
osimons
parents: 663
diff changeset
194 return Template(self._VAR_RE.sub(_replace, text)
a9d8359f4dc9 0.6dev: Follow-up on #436 / [737], changing to use `string.Template()` instead of `os.path.expandvars()` as this produces a consistent result across platforms and python version.
osimons
parents: 663
diff changeset
195 ).safe_substitute(os.environ)
Copyright (C) 2012-2017 Edgewall Software