~ctf/checkbox/bug811177

« back to all changes in this revision

Viewing changes to hwtest/install.py

  • Committer: Marc Tardif
  • Date: 2007-10-09 13:30:59 UTC
  • mfrom: (42.1.12 bug-149630)
  • Revision ID: marc.tardif@canonical.com-20071009133059-7mfmzlbwehoq537w
Extended configuration file model to centralize variables in a single location to fix bug #149630.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import sys
 
3
import ConfigParser
 
4
 
 
5
from debconf import Debconf, DebconfCommunicator, DebconfError, MEDIUM
 
6
 
 
7
 
 
8
class Install(object):
 
9
 
 
10
    separator = "/"
 
11
    config_base = "/etc/hwtest.d/%(name)s.conf"
 
12
    example_base = "/usr/share/doc/%(name)s/examples/%(name)s.conf"
 
13
 
 
14
    def __init__(self, name, config_path=None, example_path=None, variables=[]):
 
15
        self.name = name
 
16
        self._variables = variables
 
17
        self._config_path = config_path or self.config_base % {"name": name}
 
18
        self._example_path = example_path or self.example_base % {"name": name}
 
19
 
 
20
        self._config = ConfigParser.ConfigParser()
 
21
        if os.environ.get("DEBIAN_HAS_FRONTEND"):
 
22
            if os.environ.get("DEBCONF_REDIR"):
 
23
                write = os.fdopen(3, "w")
 
24
            else:
 
25
                write = sys.stdout
 
26
            self._debconf = Debconf(write=write)
 
27
        else:
 
28
            self._debconf = DebconfCommunicator(self.name)
 
29
 
 
30
    def set_example(self, path):
 
31
        self._example_path = path
 
32
 
 
33
    def set_config(self, path):
 
34
        self._config_path = path
 
35
 
 
36
    def set_variables(self, variables):
 
37
        self._variables = variables
 
38
 
 
39
    def write(self, output):
 
40
        for path in [self._example_path, self._config_path]:
 
41
            if path and os.path.isfile(path):
 
42
                self._config.read(path)
 
43
 
 
44
        # Set configuration variables
 
45
        for variable in self._variables:
 
46
            section, name = variable.rsplit(self.separator, 1)
 
47
            value = self._debconf.get(variable)
 
48
            self._config.set(section, name, value)
 
49
 
 
50
        # Write config
 
51
        f = open(output, "w")
 
52
        self._config.write(f)
 
53
        f.close()
 
54
 
 
55
    def configure(self, priority=MEDIUM):
 
56
        path = self._config_path
 
57
        if path and os.path.isfile(path):
 
58
            self._config.read(path)
 
59
 
 
60
        # Set debconf variables
 
61
        for variable in self._variables:
 
62
            section, name = variable.rsplit(self.separator, 1)
 
63
            if self._config.has_option(section, name):
 
64
                self._debconf.set(variable, self._config.get(section, name))
 
65
 
 
66
        # Ask questions and set new values, if needed.
 
67
        step = 0
 
68
        while step < len(self._variables):
 
69
            if step < 0:
 
70
                raise Exception, "Stepped too far back."
 
71
            variable = self._variables[step]
 
72
            try:
 
73
                self._debconf.input(priority, variable)
 
74
            except DebconfError, e:
 
75
                if e.args[0] != 30:
 
76
                    raise
 
77
                # Question preivously answered and skipped.
 
78
                step += 1
 
79
            else:
 
80
                try:
 
81
                    self._debconf.go()
 
82
                except DebconfError, e:
 
83
                    if e.args[0] != 30:
 
84
                        raise
 
85
                    # User requested to go back.
 
86
                    step -= 1
 
87
                else:
 
88
                    step += 1