~ubuntu-branches/ubuntu/precise/checkbox/precise

« back to all changes in this revision

Viewing changes to install/config

  • Committer: Bazaar Package Importer
  • Author(s): Marc Tardif
  • Date: 2009-01-20 16:46:15 UTC
  • Revision ID: james.westby@ubuntu.com-20090120164615-7iz6nmlef41h4vx2
Tags: 0.4
* Setup bzr-builddeb in native mode.
* Removed LGPL notice from the copyright file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
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.
 
6
 
 
7
import os
 
8
import re
 
9
import sys
 
10
import posixpath
 
11
 
 
12
from ConfigParser import ConfigParser
 
13
from optparse import OptionParser
 
14
 
 
15
from debconf import Debconf, DebconfCommunicator
 
16
 
 
17
 
 
18
DEFAULT_SECTION = "DEFAULT"
 
19
 
 
20
 
 
21
class Config(ConfigParser):
 
22
 
 
23
    def write(self, fp):
 
24
        """Write an .ini-format representation of the configuration state."""
 
25
        if self._defaults:
 
26
            fp.write("[%s]\n" % DEFAULT_SECTION)
 
27
            defaults = dict(self._defaults)
 
28
 
 
29
            # Write includes first
 
30
            if 'includes' in defaults:
 
31
                key = 'includes'
 
32
                value = defaults.pop(key)
 
33
                value = str(value).replace('\n', '\n\t')
 
34
                fp.write("%s = %s\n" % (key, value))
 
35
 
 
36
            for (key, value) in defaults.items():
 
37
                value = str(value).replace('\n', '\n\t')
 
38
                fp.write("%s = %s\n" % (key, value))
 
39
 
 
40
            fp.write("\n")
 
41
 
 
42
        for section in self._sections:
 
43
            fp.write("[%s]\n" % section)
 
44
            for (key, value) in self._sections[section].items():
 
45
                if key != "__name__":
 
46
                    fp.write("%s = %s\n" %
 
47
                             (key, str(value).replace('\n', '\n\t')))
 
48
 
 
49
            fp.write("\n")
 
50
 
 
51
 
 
52
class Install(object):
 
53
    """Install module for generating checkbox configuration files.
 
54
 
 
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
 
59
    process.
 
60
    """
 
61
    separator = "/"
 
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"
 
65
 
 
66
    def __init__(self, name, configs_path=None, examples_path=None,
 
67
                 templates_path=None):
 
68
        self.name = name
 
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}
 
76
 
 
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")
 
81
            else:
 
82
                write = sys.stdout
 
83
            self._debconf = Debconf(write=write)
 
84
        else:
 
85
            self._debconf = DebconfCommunicator(self.name)
 
86
 
 
87
    def write(self, file):
 
88
        """
 
89
        Write phase of the config process which takes a file object
 
90
        as argument.
 
91
        """
 
92
        for path in [self._examples_path, self._configs_path]:
 
93
            if path and posixpath.isfile(path):
 
94
                self._config.read(path)
 
95
 
 
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)
 
101
                if match:
 
102
                    question = match.group(1)
 
103
                    value = self._debconf.get(question)
 
104
 
 
105
                    section, name = question.rsplit(self.separator, 1)
 
106
                    self._config.set(section, name, value)
 
107
 
 
108
        # Write config file
 
109
        self._config.write(file)
 
110
 
 
111
 
 
112
def main(args):
 
113
    """
 
114
    Main routine for running this script. The arguments are:
 
115
 
 
116
    package_name    Name of the package to configure.
 
117
    optional        Optional arguments specific to the given command.
 
118
    """
 
119
    parser = OptionParser()
 
120
    parser.add_option("-o", "--output",
 
121
      default="-",
 
122
      help="Output file, - for stdout.")
 
123
    (options, args) = parser.parse_args(args)
 
124
 
 
125
    if len(args) < 1:
 
126
        return 1
 
127
 
 
128
    package = args.pop(0)
 
129
    install = Install(package)
 
130
 
 
131
    if options.output == "-":
 
132
         file = sys.stdout
 
133
    else:
 
134
         file = open(options.output, "w")
 
135
    install.write(file)
 
136
 
 
137
    return 0
 
138
 
 
139
 
 
140
if __name__ == "__main__":
 
141
    sys.exit(main(sys.argv[1:]))