~xubuntu-dev/ubuntu-cdimage/xubuntu-base

« back to all changes in this revision

Viewing changes to lib/cdimage/config.py

  • Committer: Colin Watson
  • Date: 2012-08-03 15:38:59 UTC
  • mto: This revision was merged to the branch mainline in revision 929.
  • Revision ID: cjwatson@canonical.com-20120803153859-jixq70nf2fgcs221
Add Python config module to read shell-based cdimage configuration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012 Canonical Ltd.
 
2
# Author: Colin Watson <cjwatson@ubuntu.com>
 
3
 
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; version 3 of the License.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
"""Read cdimage configuration.
 
17
 
 
18
This is a transitional measure to permit shell and Python programs to
 
19
co-exist until such time as the whole of cdimage is rewritten.
 
20
"""
 
21
 
 
22
__metaclass__ = type
 
23
 
 
24
from collections import defaultdict
 
25
import os
 
26
import re
 
27
import subprocess
 
28
 
 
29
 
 
30
_whitelisted_keys = (
 
31
    "PROJECT",
 
32
    "CAPPROJECT",
 
33
    "ALL_DISTS",
 
34
    "DIST",
 
35
    "ALL_PROJECTS",
 
36
    "ARCHES",
 
37
    "CPUARCHES",
 
38
    "GNUPG_DIR",
 
39
    "SIGNING_KEYID",
 
40
    "BRITNEY",
 
41
    "LOCAL_SEEDS",
 
42
    "TRIGGER_MIRRORS",
 
43
    "TRIGGER_MIRRORS_ASYNC",
 
44
    "DEBOOTSTRAPROOT",
 
45
    )
 
46
 
 
47
 
 
48
class Config(defaultdict):
 
49
    def __init__(self):
 
50
        super(Config, self).__init__(str)
 
51
        if "CDIMAGE_ROOT" not in os.environ:
 
52
            os.environ["CDIMAGE_ROOT"] = "/srv/cdimage.ubuntu.com"
 
53
        self.root = os.environ["CDIMAGE_ROOT"]
 
54
        self._read()
 
55
 
 
56
    def _read_nullsep_output(self, command):
 
57
        raw = subprocess.Popen(
 
58
            command, stdout=subprocess.PIPE,
 
59
            universal_newlines=True).communicate()[0]
 
60
        out = {}
 
61
        for line in raw.split("\0"):
 
62
            try:
 
63
                key, value = line.split("=", 1)
 
64
                out[key] = value
 
65
            except ValueError:
 
66
                continue
 
67
        return out
 
68
 
 
69
    def _shell_escape(self, arg):
 
70
        if re.match(r"^[a-zA-Z0-9+,./:=@_-]+$", arg):
 
71
            return arg
 
72
        else:
 
73
            return "'%s'" % arg.replace("'", "'\\''")
 
74
 
 
75
    def _read(self):
 
76
        config_path = os.path.join(self.root, "etc", "config")
 
77
        commands = [". %s" % self._shell_escape(config_path)]
 
78
        for key in _whitelisted_keys:
 
79
            commands.append("printf '%%s\\0' \"%s=$%s\"" % (key, key))
 
80
        env = self._read_nullsep_output(["sh", "-c", "; ".join(commands)])
 
81
        self.update(env)