[Buildbot-commits] buildbot/buildbot/process properties.py, 1.6, 1.7

Brian Warner warner at users.sourceforge.net
Mon May 26 23:52:25 UTC 2008


Update of /cvsroot/buildbot/buildbot/buildbot/process
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv22229/buildbot/process

Modified Files:
	properties.py 
Log Message:
[project @ #234:shell-style-substitutions.patch]
Add support for shell-style substitutions, e.g.,
%(propname:-replacement)s and %(propname:+replacement)s

Original author: dustin at v.igoro.us
Date: 2008-04-15 04:31:40+00:00

Index: properties.py
===================================================================
RCS file: /cvsroot/buildbot/buildbot/buildbot/process/properties.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- properties.py	22 May 2008 22:13:26 -0000	1.6
+++ properties.py	26 May 2008 23:52:23 -0000	1.7
@@ -1,3 +1,5 @@
+import re
+import weakref
 from zope.interface import implements
 from buildbot import util
 from twisted.python import log
@@ -26,19 +28,28 @@
         @param kwargs: initial property values (for testing)
         """
         self.properties = {}
+        self.pmap = PropertyMap(self)
         if kwargs: self.update(kwargs, "TEST")
 
+    def __getstate__(self):
+        d = self.__dict__.copy()
+        del d['pmap']
+        return d
+
+    def __setstate__(self, d):
+        self.__dict__ = d
+        self.pmap = PropertyMap(self)
+
     def __getitem__(self, name):
-        """Just get the value for this property, special-casing None -> ''"""
+        """Just get the value for this property."""
         rv = self.properties[name][0]
-        if rv is None: rv = ''
         return rv
 
     def has_key(self, name):
         return self.properties.has_key(name)
 
     def getProperty(self, name, default=None):
-        """Get the value for the given property, with no None -> '' special case"""
+        """Get the value for the given property."""
         return self.properties.get(name, (default,))[0]
 
     def getPropertySource(self, name):
@@ -54,6 +65,7 @@
         return repr(dict([ (k,v[0]) for k,v in self.properties.iteritems() ]))
 
     def setProperty(self, name, value, source):
+        self._wpmap = None
         self.properties[name] = (value, source)
 
     def update(self, dict, source):
@@ -73,7 +85,7 @@
         if isinstance(value, (str, unicode)):
             return value
         elif isinstance(value, WithProperties):
-            return value.render(self)
+            return value.render(self.pmap)
         elif isinstance(value, list):
             return [ self.render(e) for e in value ]
         elif isinstance(value, tuple):
@@ -83,6 +95,39 @@
         else:
             return value
 
+class PropertyMap:
+    colon_minus_re = re.compile(r"(.*):-(.*)")
+    colon_plus_re = re.compile(r"(.*):\+(.*)")
+    def __init__(self, properties):
+        # use weakref here to avoid a reference loop
+        self.properties = weakref.ref(properties)
+
+    def __getitem__(self, key):
+        properties = self.properties()
+        assert properties is not None
+
+        mo = self.colon_minus_re.match(key)
+        if mo:
+            prop, repl = mo.group(1,2)
+            if properties.has_key(prop):
+                rv = properties[prop]
+            else:
+                rv = repl
+        else:
+            mo = self.colon_plus_re.match(key)
+            if mo:
+                prop, repl = mo.group(1,2)
+                if properties.has_key(prop):
+                    rv = repl
+                else:
+                    rv = ''
+            else:
+                rv = properties[key]
+
+        # translate 'None' to an empty string
+        if rv is None: rv = ''
+        return rv
+
 class WithProperties(util.ComparableMixin):
     """This is a marker class, used in ShellCommand's command= argument to
     indicate that we want to interpolate a build property.
@@ -94,12 +139,12 @@
         self.fmtstring = fmtstring
         self.args = args
 
-    def render(self, properties):
+    def render(self, pmap):
         if self.args:
             strings = []
             for name in self.args:
-                strings.append(properties[name])
+                strings.append(pmap[name])
             s = self.fmtstring % tuple(strings)
         else:
-            s = self.fmtstring % properties
+            s = self.fmtstring % pmap
         return s





More information about the Commits mailing list