changeset 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.
author osimons
date Thu, 03 Sep 2009 00:20:43 +0000
parents b00da52e942f
children 1683869bc665
files bitten/build/config.py bitten/build/tests/config.py doc/configure.txt
diffstat 3 files changed, 35 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/bitten/build/config.py
+++ b/bitten/build/config.py
@@ -160,14 +160,17 @@
     _VAR_RE = re.compile(r'\$\{(?P<ref>\w[\w.]*?\w)(?:\:(?P<def>.+))?\}')
 
     def interpolate(self, text, **vars):
-        """Interpolate configuration properties into a string.
+        """Interpolate configuration and environment properties into a string.
         
-        Properties can be referenced in the text using the notation
+        Configuration properties can be referenced in the text using the notation
         ``${property.name}``. A default value can be provided by appending it to
         the property name separated by a colon, for example
         ``${property.name:defaultvalue}``. This value will be used when there's
         no such property in the configuration. Otherwise, if no default is
         provided, the reference is not replaced at all.
+        
+        Environment properties substitute from environment variables, supporting
+        common notations; ``$VAR``, ``${VAR}`` and ``%var%`` (Windows only).
 
         :param text: the string containing variable references
         :param vars: extra variables to use for the interpolation
@@ -182,4 +185,4 @@
                 return m.group('def')
             else:
                 return m.group(0)
-        return self._VAR_RE.sub(_replace, text)
+        return os.path.expandvars(self._VAR_RE.sub(_replace, text))
--- a/bitten/build/tests/config.py
+++ b/bitten/build/tests/config.py
@@ -175,6 +175,27 @@
         self.assertEqual('foo ${python.path} bar',
                          config.interpolate('foo ${python.path} bar'))
 
+    def test_interpolate_environment(self):
+        config = Configuration()
+        if os.name == 'posix':
+            self.assertEquals(os.environ['USER'],
+                        config.interpolate('$USER'))
+            self.assertEquals('$user',
+                        config.interpolate('$user'))
+            self.assertEquals(os.environ['USER'],
+                        config.interpolate('${USER}'))
+            self.assertEquals('${user}',
+                        config.interpolate('${user}'))
+        elif os.name == 'nt':
+            # Additional custom syntax + also case insensitive
+            self.assertEquals(os.environ['PROGRAMFILES'],
+                        config.interpolate('%programfiles%'))
+            self.assertEquals(os.environ['programfiles'],
+                        config.interpolate('%PROGRAMFILES%'))
+            self.assertEquals(os.environ['PROGRAMFILES'],
+                        config.interpolate('$programfiles'))
+            self.assertEquals(os.environ['PROGRAMFILES'],
+                        config.interpolate('${programfiles}'))
 
 def suite():
     suite = unittest.TestSuite()
--- a/doc/configure.txt
+++ b/doc/configure.txt
@@ -204,6 +204,14 @@
   <sh:exec executable="echo"
     args="Slave: ${family} ${os} ${version} ${machine} ${processor}"/>
 
+Additionally, environment variables are also interpolated, supporting
+common notations; ``$VAR``, ``${VAR}`` and ``%var%`` (Windows only).
+
+.. code-block:: xml
+
+  <sh:exec executable="%windir%/system32/hostname.exe" />
+
+
 Authentication
 ==============
 
Copyright (C) 2012-2017 Edgewall Software