Mercurial > treepkg > treepkg
annotate treepkg/readconfig.py @ 293:faeeac2c4c71
Replace debrevision_prefix with pkg_revision_template. Their meaning is
similar, but the template is not just a prefix, it's a template for
Python's %-based string formatting. This allows more complex
configuration to be done with command line settings so that it's easy to
do a one-off build with a package revision like "kk5.2".
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Tue, 06 Oct 2009 13:13:04 +0000 |
parents | bb98e728f25b |
children | 4dd6ec3a1151 |
rev | line source |
---|---|
234
eaa696629a91
Add a way to specify the svn URL of the repository with the debian rules
Bernhard Herzog <bh@intevation.de>
parents:
167
diff
changeset
|
1 # Copyright (C) 2007, 2008, 2009 by Intevation GmbH |
0 | 2 # Authors: |
3 # Bernhard Herzog <bh@intevation.de> | |
4 # | |
5 # This program is free software under the GPL (>=v2) | |
6 # Read the file COPYING coming with the software for details. | |
7 | |
8 """Reads the configuration file""" | |
9 | |
10 import sys | |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
11 import shlex |
0 | 12 from ConfigParser import SafeConfigParser, NoOptionError |
13 | |
4
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
2
diff
changeset
|
14 import util |
114
02c261e4443f
Extend treepkg.readconfig.read_config:
Bernhard Herzog <bh@intevation.de>
parents:
93
diff
changeset
|
15 import packager |
4
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
2
diff
changeset
|
16 |
129
ce9f046058b5
Add the handle_dependencies Option for the config file. Extend the test
Bernhard Herzog <bh@intevation.de>
parents:
115
diff
changeset
|
17 def convert_bool(s): |
ce9f046058b5
Add the handle_dependencies Option for the config file. Extend the test
Bernhard Herzog <bh@intevation.de>
parents:
115
diff
changeset
|
18 s = s.lower() |
ce9f046058b5
Add the handle_dependencies Option for the config file. Extend the test
Bernhard Herzog <bh@intevation.de>
parents:
115
diff
changeset
|
19 if s in ("true", "yes", "1"): |
ce9f046058b5
Add the handle_dependencies Option for the config file. Extend the test
Bernhard Herzog <bh@intevation.de>
parents:
115
diff
changeset
|
20 return True |
ce9f046058b5
Add the handle_dependencies Option for the config file. Extend the test
Bernhard Herzog <bh@intevation.de>
parents:
115
diff
changeset
|
21 if s in ("false", "no", "0"): |
ce9f046058b5
Add the handle_dependencies Option for the config file. Extend the test
Bernhard Herzog <bh@intevation.de>
parents:
115
diff
changeset
|
22 return False |
ce9f046058b5
Add the handle_dependencies Option for the config file. Extend the test
Bernhard Herzog <bh@intevation.de>
parents:
115
diff
changeset
|
23 raise ValueError("cannot determine boolean value of %r" % (s,)) |
ce9f046058b5
Add the handle_dependencies Option for the config file. Extend the test
Bernhard Herzog <bh@intevation.de>
parents:
115
diff
changeset
|
24 |
0 | 25 |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
26 packager_desc = [ |
258
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
27 "name", "base_dir", "svn_url", ("rules_svn_url", str, ""), "packager_class", |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
28 ("root_cmd", shlex.split, "sudo"), "pbuilderrc", |
293
faeeac2c4c71
Replace debrevision_prefix with pkg_revision_template. Their meaning is
Bernhard Herzog <bh@intevation.de>
parents:
258
diff
changeset
|
29 "deb_email", "deb_fullname", "pkg_revision_template", |
129
ce9f046058b5
Add the handle_dependencies Option for the config file. Extend the test
Bernhard Herzog <bh@intevation.de>
parents:
115
diff
changeset
|
30 ("handle_dependencies", convert_bool), |
258
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
31 ("signing_key_id", str, ""), |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
32 ] |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
33 |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
34 treepkg_desc = [ |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
35 ("check_interval", int), |
91
3ed079a7174a
Implement a way to stop a running treepackager.
Bernhard Herzog <bh@intevation.de>
parents:
52
diff
changeset
|
36 "instructions_file", |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
37 ] |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
38 |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
39 |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
40 def read_config_section(parser, section, item_desc, defaults=None): |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
41 if defaults is None: |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
42 defaults = dict() |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
43 options = dict() |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
44 for item in item_desc: |
258
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
45 has_default_value = False |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
46 if isinstance(item, tuple): |
258
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
47 key, converter = item[:2] |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
48 if len(item) == 3: |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
49 default_value = item[-1] |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
50 has_default_value = True |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
51 else: |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
52 key = item |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
53 converter = str |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
54 try: |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
55 value = parser.get(section, key, vars=defaults) |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
56 except NoOptionError: |
258
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
57 if has_default_value: |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
58 value = default_value |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
59 else: |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
60 print >>sys.stderr, "Missing option %r in section %r" \ |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
61 % (key, section) |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
62 sys.exit(1) |
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
63 options[key] = converter(value) |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
64 return options |
0 | 65 |
66 | |
67 def read_config(filename): | |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
68 """Reads the tree packager configuration from the file given by filename. |
0 | 69 |
115 | 70 The function returns a tuple with a dict ('treepkg') and a list of |
71 dicts ('packagers'). The treepkg dict contains the main | |
72 configuration of the tree packager. The packagers list contains one | |
73 dict with the configuratiin for each packager. | |
0 | 74 """ |
258
bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
Bernhard Herzog <bh@intevation.de>
parents:
234
diff
changeset
|
75 parser = SafeConfigParser() |
0 | 76 parser.read([filename]) |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
77 |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
78 # extract packager configurations |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
79 packagers = [] |
0 | 80 for section in parser.sections(): |
81 if section.startswith("pkg_"): | |
114
02c261e4443f
Extend treepkg.readconfig.read_config:
Bernhard Herzog <bh@intevation.de>
parents:
93
diff
changeset
|
82 vars = dict(name=section[4:]) |
02c261e4443f
Extend treepkg.readconfig.read_config:
Bernhard Herzog <bh@intevation.de>
parents:
93
diff
changeset
|
83 packager_class = parser.get(section, "packager_class", vars=vars) |
02c261e4443f
Extend treepkg.readconfig.read_config:
Bernhard Herzog <bh@intevation.de>
parents:
93
diff
changeset
|
84 module = packager.import_packager_module(packager_class) |
52
78cf5f6778ec
Rename 'packagel ine' -> 'package track'
Bernhard Herzog <bh@intevation.de>
parents:
47
diff
changeset
|
85 desc = packager_desc + module.PackageTrack.extra_config_desc |
4
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
2
diff
changeset
|
86 packagers.append(read_config_section(parser, section, desc, |
114
02c261e4443f
Extend treepkg.readconfig.read_config:
Bernhard Herzog <bh@intevation.de>
parents:
93
diff
changeset
|
87 defaults=vars)) |
0 | 88 |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
89 # main config |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
90 treepkg = read_config_section(parser, "treepkg", treepkg_desc) |
0 | 91 |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
92 return treepkg, packagers |
0 | 93 |
94 | |
95 if __name__ == "__main__": | |
2
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
96 import pprint |
e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
97 print pprint.pprint(read_config(sys.argv[1])) |