[Checkins] SVN: zc.buildout/trunk/ Handle addition and subtraction in the same section correctly

Martijn Pieters cvs-admin at zope.org
Thu Jul 12 08:52:16 UTC 2012


Log message for revision 127336:
  Handle addition and subtraction in the same section correctly

Changed:
  U   zc.buildout/trunk/CHANGES.txt
  U   zc.buildout/trunk/src/zc/buildout/buildout.py
  U   zc.buildout/trunk/src/zc/buildout/buildout.txt

-=-
Modified: zc.buildout/trunk/CHANGES.txt
===================================================================
--- zc.buildout/trunk/CHANGES.txt	2012-07-12 08:48:20 UTC (rev 127335)
+++ zc.buildout/trunk/CHANGES.txt	2012-07-12 08:52:13 UTC (rev 127336)
@@ -36,6 +36,9 @@
 - https://bugs.launchpad.net/bugs/697913 : Buildout doesn't honor exit code
   from scripts. Fixed.
 
+- Handle both addition and subtraction of elements (+= and -=) on the same key
+  in the same section.
+
 1.5.2 (2010-10-11)
 ==================
 

Modified: zc.buildout/trunk/src/zc/buildout/buildout.py
===================================================================
--- zc.buildout/trunk/src/zc/buildout/buildout.py	2012-07-12 08:48:20 UTC (rev 127335)
+++ zc.buildout/trunk/src/zc/buildout/buildout.py	2012-07-12 08:52:13 UTC (rev 127336)
@@ -1527,19 +1527,26 @@
     return result
 
 def _update_section(s1, s2):
+    # Base section 2 on section 1; section 1 is copied, with key-value pairs
+    # in section 2 overriding those in section 1. If there are += or -=
+    # operators in section 2, process these to add or substract items (delimited
+    # by newlines) from the preexisting values.
     s2 = s2.copy() # avoid mutating the second argument, which is unexpected
-    for k, v in s2.items():
+    # Sort on key, then on the addition or substraction operator (+ comes first)
+    for k, v in sorted(s2.items(), key=lambda x: (x[0].rstrip(' +'), x[0][-1])):
         v2, note2 = v
         if k.endswith('+'):
             key = k.rstrip(' +')
-            v1, note1 = s1.get(key, ("", ""))
+            # Find v1 in s2 first; it may have been defined locally too.
+            v1, note1 = s2.get(key, s1.get(key, ("", "")))
             newnote = ' [+] '.join((note1, note2)).strip()
             s2[key] = "\n".join((v1).split('\n') +
                 v2.split('\n')), newnote
             del s2[k]
         elif k.endswith('-'):
             key = k.rstrip(' -')
-            v1, note1 = s1.get(key, ("", ""))
+            # Find v1 in s2 first; it may have been set by a += operation first
+            v1, note1 = s2.get(key, s1.get(key, ("", "")))
             newnote = ' [-] '.join((note1, note2)).strip()
             s2[key] = ("\n".join(
                 [v for v in v1.split('\n')

Modified: zc.buildout/trunk/src/zc/buildout/buildout.txt
===================================================================
--- zc.buildout/trunk/src/zc/buildout/buildout.txt	2012-07-12 08:48:20 UTC (rev 127335)
+++ zc.buildout/trunk/src/zc/buildout/buildout.txt	2012-07-12 08:52:13 UTC (rev 127336)
@@ -1091,6 +1091,12 @@
     ... recipe =
     ... option = c1 c2
     ...
+    ... [part4]
+    ... recipe =
+    ... option = d2
+    ...     d3
+    ...     d5
+    ...
     ... """)
 
 Extending this configuration, we can "adjust" the values set in the
@@ -1113,10 +1119,15 @@
     ... [part3]
     ... option+=c3 c4 c5
     ...
+    ... # combining both adding and removing
+    ... [part4]
+    ... option += d1 
+    ...      d4
+    ... option -= d5
+    ...
     ... # normal assignment
-    ... [part4]
+    ... [part5]
     ... option = h1 h2
-    ...
     ... """)
 
 An additional extension.
@@ -1186,7 +1197,7 @@
     ... """)
 
     >>> print system(os.path.join('bin', 'buildout')),
-    ['a1 a2/na3 a4/na5', 'b1 b2 b3 b4', 'c1 c2/nc3 c4 c5', 'h1 h2']
+    ['a1 a2/na3 a4/na5', 'b1 b2 b3 b4', 'c1 c2/nc3 c4 c5', 'd2/nd3/nd1/nd4', 'h1 h2']
     Develop: '/sample-buildout/demo'
 
 Annotated sections output shows which files are responsible for which
@@ -1226,6 +1237,17 @@
         /sample-buildout/base.cfg
     <BLANKLINE>
     [part4]
+    option= d2
+    d3
+    d1
+    d4
+        /sample-buildout/base.cfg
+    +=  /sample-buildout/extension1.cfg
+    -=  /sample-buildout/extension1.cfg
+    recipe= 
+        /sample-buildout/base.cfg
+    <BLANKLINE>
+    [part5]
     option= h1 h2
         /sample-buildout/extension1.cfg
 



More information about the checkins mailing list