changeset 2:e6a9f4037f68

readconfig.py is smarter now about conversions and supports shlex
author Bernhard Herzog <bh@intevation.de>
date Thu, 08 Mar 2007 19:55:44 +0100
parents 02d5702314a3
children 7e9db903ba16
files demo.cfg treepkg/packager.py treepkg/readconfig.py
diffstat 3 files changed, 52 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/demo.cfg	Tue Mar 06 18:50:53 2007 +0100
+++ b/demo.cfg	Thu Mar 08 19:55:44 2007 +0100
@@ -12,12 +12,10 @@
 #
 #   sudo pbuilder build <name-of-dsc-file>
 #
-# The way it's implemented now, the command is interpreted as a single
-# shell word which means that you cannot define any command line
-# parameters for the root_cmd.  The reason for this limitation is that
-# the code does not use a shell to interpret the command line.  A later
-# version might use Python's shlex module to split this command into
-# words.
+# The value of this option is split into words with the python function
+# shlex.split.  See the python documentation for the precise semantics.
+# shlex.split works pretty much like a POSIX shell but it doesn't to any
+# expansions.
 #
 # Set an empty value to indicate that no special command is needed.
 #
--- a/treepkg/packager.py	Tue Mar 06 18:50:53 2007 +0100
+++ b/treepkg/packager.py	Thu Mar 08 19:55:44 2007 +0100
@@ -147,14 +147,9 @@
         self.status.set("creating_binary_package")
         util.ensure_directory(self.binary_dir)
         logging.info("Building binary package; loging to %r", self.logfile)
-        cmd = []
-        if self.plant.root_cmd:
-            cmd.append(self.plant.root_cmd)
-        run.call(cmd + ["/usr/sbin/pbuilder", "build",
-                  "--logfile", self.logfile,
-                  "--buildresult", self.binary_dir,
-                  self.dsc_file],
-                 suppress_output=True)
+        cmd = ["/usr/sbin/pbuilder", "build", "--logfile", self.logfile,
+               "--buildresult", self.binary_dir, self.dsc_file]
+        run.call(self.plant.root_cmd + cmd, suppress_output=True)
         self.status.set("binary_package_created")
 
 
--- a/treepkg/readconfig.py	Tue Mar 06 18:50:53 2007 +0100
+++ b/treepkg/readconfig.py	Thu Mar 08 19:55:44 2007 +0100
@@ -8,43 +8,66 @@
 """Reads the configuration file"""
 
 import sys
+import shlex
 from ConfigParser import SafeConfigParser, NoOptionError
 
-
-assembly_line_keys = ["name", "base_dir", "svn_url", "root_cmd",
-                      "deb_email", "deb_fullname"]
-
 defaults = dict(root_cmd="sudo")
 
+packager_desc = [
+    "name", "base_dir", "svn_url",
+    ("root_cmd", shlex.split),
+    "deb_email", "deb_fullname",
+    ]
+
+treepkg_desc = [
+    ("check_interval", int),
+    ]
+
+
+def read_config_section(parser, section, item_desc, defaults=None):
+    if defaults is None:
+        defaults = dict()
+    options = dict()
+    for item in item_desc:
+        if isinstance(item, tuple):
+            key, converter = item
+        else:
+            key = item
+            converter = str
+        try:
+            value = parser.get(section, key, vars=defaults)
+            options[key] = converter(value)
+        except NoOptionError:
+            print >>sys.stderr, "%s: Missing option %r in section %r" \
+                  % (filename, key, section)
+            sys.exit(1)
+    return options
 
 
 def read_config(filename):
-    """Reads the packagemill configuration from filename.
+    """Reads the tree packager configuration from the file given by filename.
 
-    The return value is a list of (name, option) pairs, one for each
-    assembly line of the mill.
+    The function returns a tuple with a ('treepkg') and a list of dicts
+    ('packagers').  The treepkg dict contains the main configuration of
+    the tree packager.  The packagers list contains one dict with the
+    configuratiin for each packager.
     """
-    assembly_lines = []
     parser = SafeConfigParser(defaults)
     parser.read([filename])
+
+    # extract packager configurations
+    packagers = []
     for section in parser.sections():
         if section.startswith("pkg_"):
-            pkg_defaults = dict(name=section[4:])
-            options = {}
-            for key in assembly_line_keys:
-                try:
-                    options[key] = parser.get(section, key, vars=pkg_defaults)
-                except NoOptionError:
-                    print >>sys.stderr, "%s: Missing option %r in section %r" \
-                                        % (filename, key, section)
-                    sys.exit(1)
-            assembly_lines.append(options)
+            packagers.append(read_config_section(parser, section, packager_desc,
+                                                 dict(name=section[4:])))
 
-    treepkg_opts = dict(check_interval=parser.getint("treepkg",
-                                                      "check_interval"))
+    # main config
+    treepkg = read_config_section(parser, "treepkg", treepkg_desc)
 
-    return treepkg_opts, assembly_lines
+    return treepkg, packagers
 
 
 if __name__ == "__main__":
-    print read_config(sys.argv[1])
+    import pprint
+    print pprint.pprint(read_config(sys.argv[1]))
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)