3
# This program is meant to be called by the debian installer in order to
4
# create configuration files for the checkbox package and derived packages
5
# based on the preseed values.
12
from ConfigParser import ConfigParser
13
from optparse import OptionParser
15
from debconf import Debconf, DebconfCommunicator
18
DEFAULT_SECTION = "DEFAULT"
21
class Config(ConfigParser):
24
"""Write an .ini-format representation of the configuration state."""
26
fp.write("[%s]\n" % DEFAULT_SECTION)
27
defaults = dict(self._defaults)
29
# Write includes first
30
if 'includes' in defaults:
32
value = defaults.pop(key)
33
value = str(value).replace('\n', '\n\t')
34
fp.write("%s = %s\n" % (key, value))
36
for (key, value) in defaults.items():
37
value = str(value).replace('\n', '\n\t')
38
fp.write("%s = %s\n" % (key, value))
42
for section in self._sections:
43
fp.write("[%s]\n" % section)
44
for (key, value) in self._sections[section].items():
46
fp.write("%s = %s\n" %
47
(key, str(value).replace('\n', '\n\t')))
52
class Install(object):
53
"""Install module for generating checkbox configuration files.
55
The checkbox module and derivatives use a configuration file format
56
compatible with ConfigParser. The values for the keys defined in
57
this file can be preseeded during the installation of the package by
58
using this module during the config phase of the package installation
62
configs_base = "/usr/share/%(base_name)s/configs/%(name)s.ini"
63
examples_base = "/usr/share/%(base_name)s/examples/%(name)s.ini"
64
templates_base = "/var/lib/dpkg/info/%(name)s.templates"
66
def __init__(self, name, configs_path=None, examples_path=None,
69
self.base_name = re.sub(r"(-cli|-gtk)$", "", name)
70
self._configs_path = configs_path or self.configs_base \
71
% {"name": name, "base_name": self.base_name}
72
self._examples_path = examples_path or self.examples_base \
73
% {"name": name, "base_name": self.base_name}
74
self._templates_path = templates_path or self.templates_base \
75
% {"name": name, "base_name": self.base_name}
77
self._config = Config()
78
if os.environ.get("DEBIAN_HAS_FRONTEND"):
79
if os.environ.get("DEBCONF_REDIR"):
80
write = os.fdopen(3, "w")
83
self._debconf = Debconf(write=write)
85
self._debconf = DebconfCommunicator(self.name)
87
def write(self, file):
89
Write phase of the config process which takes a file object
92
for path in [self._examples_path, self._configs_path]:
93
if path and posixpath.isfile(path):
94
self._config.read(path)
96
# Hack to retrieve questions from templates file
97
if posixpath.exists(self._templates_path):
98
templates_file = open(self._templates_path)
99
for line in templates_file.readlines():
100
match = re.match(r"Template: (.*)", line)
102
question = match.group(1)
103
value = self._debconf.get(question)
105
section, name = question.rsplit(self.separator, 1)
106
self._config.set(section, name, value)
109
self._config.write(file)
114
Main routine for running this script. The arguments are:
116
package_name Name of the package to configure.
117
optional Optional arguments specific to the given command.
119
parser = OptionParser()
120
parser.add_option("-o", "--output",
122
help="Output file, - for stdout.")
123
(options, args) = parser.parse_args(args)
128
package = args.pop(0)
129
install = Install(package)
131
if options.output == "-":
134
file = open(options.output, "w")
140
if __name__ == "__main__":
141
sys.exit(main(sys.argv[1:]))