1
# Copyright (C) 2012 Canonical Ltd.
2
# Author: Colin Watson <cjwatson@ubuntu.com>
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.
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.
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/>.
16
"""Read cdimage configuration.
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.
24
from collections import defaultdict
43
"TRIGGER_MIRRORS_ASYNC",
48
class Config(defaultdict):
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"]
56
def _read_nullsep_output(self, command):
57
raw = subprocess.Popen(
58
command, stdout=subprocess.PIPE,
59
universal_newlines=True).communicate()[0]
61
for line in raw.split("\0"):
63
key, value = line.split("=", 1)
69
def _shell_escape(self, arg):
70
if re.match(r"^[a-zA-Z0-9+,./:=@_-]+$", arg):
73
return "'%s'" % arg.replace("'", "'\\''")
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)])