changeset 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. This drops the Windows-specific syntax of `%VAR%` for the greater good... Should fix the 2 broken tests.
author osimons
date Sat, 05 Sep 2009 00:09:18 +0000
parents 463cd827f567
children 7a8ddf54f012
files bitten/build/config.py bitten/build/tests/config.py doc/configure.txt
diffstat 3 files changed, 27 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/bitten/build/config.py
+++ b/bitten/build/config.py
@@ -15,6 +15,7 @@
 import os
 import platform
 import re
+from string import Template
 
 log = logging.getLogger('bitten.config')
 
@@ -170,7 +171,7 @@
         provided, the reference is not replaced at all.
         
         Environment properties substitute from environment variables, supporting
-        common notations; ``$VAR``, ``${VAR}`` and ``%var%`` (Windows only).
+        the common notations of ``$VAR`` and ``${VAR}``.
 
         :param text: the string containing variable references
         :param vars: extra variables to use for the interpolation
@@ -185,4 +186,5 @@
                 return m.group('def')
             else:
                 return m.group(0)
-        return os.path.expandvars(self._VAR_RE.sub(_replace, text))
+        return Template(self._VAR_RE.sub(_replace, text)
+                                                ).safe_substitute(os.environ)
--- a/bitten/build/tests/config.py
+++ b/bitten/build/tests/config.py
@@ -177,25 +177,27 @@
 
     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}'))
+        os.environ['BITTEN_TEST'] = 'foo'
+        try:
+            # regular substitutions
+            self.assertEquals(os.environ['BITTEN_TEST'],
+                        config.interpolate('$BITTEN_TEST'))
+            self.assertEquals(os.environ['BITTEN_TEST'],
+                        config.interpolate('${BITTEN_TEST}'))
+            if os.name == 'posix':
+                # case-sensitive
+                self.assertEquals('${bitten_test}',
+                            config.interpolate('${bitten_test}'))
+                self.assertEquals('$bitten_test',
+                            config.interpolate('$bitten_test'))
+            elif os.name == 'nt':
+                # case-insensitive
+                self.assertEquals(os.environ['bitten_test'],
+                            config.interpolate('$bitten_test'))
+                self.assertEquals(os.environ['bitten_test'],
+                            config.interpolate('${bitten_test}'))
+        finally:
+            del os.environ['BITTEN_TEST']
 
 def suite():
     suite = unittest.TestSuite()
--- a/doc/configure.txt
+++ b/doc/configure.txt
@@ -205,11 +205,11 @@
     args="Slave: ${family} ${os} ${version} ${machine} ${processor}"/>
 
 Additionally, environment variables are also interpolated, supporting
-common notations; ``$VAR``, ``${VAR}`` and ``%var%`` (Windows only).
+the common notations of ``$VAR`` and ``${VAR}``.
 
 .. code-block:: xml
 
-  <sh:exec executable="%windir%/system32/hostname.exe" />
+  <sh:exec executable="${PROGRAMFILES}/SomeDir/MyProg.exe" />
 
 
 Authentication
Copyright (C) 2012-2017 Edgewall Software