Fix 'buildout.cfg' modification code
[zope-bootstrap.git] / Makefile.buildout
index 3728542..47c5738 100644 (file)
@@ -1,13 +1,88 @@
 # -*- makefile -*-
 
+# Usage:
+#     @$(buildout_opt) <cfg-file> [set|add|append|remove] <section> <variable> [<value>]
+#
+# Change setting for <variable> in <section>. Actions are
+#
+#    set       Set <variable> to <value> replacing any existing value
+#    add       If <variable> is unset, set to <value>
+#    append    Append <value> to the end of the current value of <variable>
+#    remove    Remove any assignment to <variable>
+#
+define buildout_opt
+    _buildout_getopt_() {                                                                      \
+        sed -n -e "/^\[$$1\]/,/^\[/ba" -eb -e:a -e "/^$$2 *=/,/^\([^ \t]\|\$$\)/p" $$cfgfile | \
+            sed -e '$$d';                                                                      \
+    };                                                                                         \
+    _buildout_haveopt() {                                                                      \
+       [ -n "`_buildout_getopt_ "$$1" "$$2"`" ];                                               \
+    };                                                                                         \
+    _buildout_getopt() {                                                                       \
+       _buildout_checkopt_ "$$1" "$$2" | -e '1s/[^=]* *= *//';                                 \
+    };                                                                                         \
+    _buildout_addopt() {                                                                       \
+       sed -i -e "/\[$$1\]/ba" -eb -e:a -ea\\ -e "$$2 = $$3" $$cfgfile;                        \
+    };                                                                                         \
+    _buildout_replaceopt() {                                                                   \
+       sed -i -e "/^\[$$1\]/,/^\[/ba" -eb -e:a                                                 \
+            -e "/^$$2 *=/,/^\([^ \t]\|\$$\)/bb" -eb -e:b                                       \
+            -e "/^$$2 *=/bc" -ebd -e:c                                                         \
+            -ec\\ -e "$$2 = $$3" -eb -e:d                                                      \
+           -e "/^\([^ \t]\|\$$\)/b" -ed                                                        \
+               $$cfgfile;                                                                      \
+    };                                                                                         \
+    _buildout_appendopt() {                                                                    \
+       sed -i -e "/^\[$$1\]/,/^\[/ba" -eb -e:a                                                 \
+            -e "/^$$2 *=/,/^\([^ \t]\|\$$\)/bb" -eb -e:b                                       \
+           -e /^\([^ \t]\|$$\)/bc -eb -e:c                                                     \
+            -ei\\ -e "    $$3"                                                                 \
+               $$cfgfile;                                                                      \
+    };                                                                                         \
+    _buildout_removeopt() {                                                                    \
+       sed -i -e "/^\[$$1\]/,/^\[/ba" -eb -e:a                                                 \
+            -e "/^$$2 *=/,/^\([^ \t]\|\$$\)/bb" -eb -e:b                                       \
+           -e "/^\([^ \t]\|\$$\)/b" -ed                                                        \
+               $$cfgfile;                                                                      \
+    };                                                                                         \
+    _buildout_opt_set() {                                                                      \
+       if _buildout_haveopt "$$1" "$$2"; then                                                  \
+           _buildout_replaceopt "$$1" "$$2" "$$3";                                             \
+       else                                                                                    \
+           _buildout_addopt "$$1" "$$2" "$$3";                                                 \
+       fi;                                                                                     \
+    };                                                                                         \
+    _buildout_opt_add() {                                                                      \
+       if ! _buildout_haveopt "$$1" "$$2"; then                                                \
+           _buildout_addopt "$$1" "$$2" "$$3";                                                 \
+       fi;                                                                                     \
+    };                                                                                         \
+    _buildout_opt_append() {                                                                   \
+       if builout_haveopt "$$1" "$$2"; then                                                    \
+           _buildout_appendopt "$$1" "$$2" "$$3";                                              \
+       else                                                                                    \
+           _buildouT_addopt "$$1" "$$2" "$$3";                                                 \
+       fi;                                                                                     \
+    };                                                                                         \
+    _builout_opt_remove() {                                                                    \
+       if buildout_haveopt "$$1" "$$2"; then                                                   \
+           _buildout_removeopt "$$1" "$$2";                                                    \
+       fi;                                                                                     \
+    };                                                                                         \
+    _buildout_opt() {                                                                          \
+       echo "$$1: option '[$$3]/$$4' $$2 '$$5'";                                               \
+       cfgfile="$$1";                                                                          \
+       _buildout_opt_$$2 "$$3" "$$4" "$$5";                                                    \
+    };                                                                                         \
+    _buildout_opt
+endef
+
 buildout-net.cfg: buildout.cfg
-       @sed -i -e '/\[buildout\]/ba' -eb -e:a                  \
-           -ea\\ -e 'download-cache = $(DLCACHE)/downloads'    \
-           -ea\\ -e 'install-from-cache = true'                \
-           -ea\\ -e 'extends-cache = $(DLCACHE)/extends'       \
-           -ea\\ -e 'newest = false'                           \
-                buildout.cfg || ( rm -f buildout.cfg; false )
-       @mkdir -p $(DLCACHE)/downloads $(DLCACHE)/extends || ( rm -f buildout.cfg; false )
+       @$(buildout_opt) buildout.cfg add buildout download_cache $(DLCACHE)/downloads
+       @$(buildout_opt) buildout.cfg add buildout extends-cache $(DLCACHE)/extends
+       @$(buildout_opt) buildout.cfg set buildout install_from_cache true
+       @$(buildout_opt) buildout.cfg set buildout newest true
+       mkdir -p $(DLCACHE)/downloads $(DLCACHE)/extends
        @(                                      \
            echo "[buildout]";                  \
            echo "extends = buildout.cfg";      \