/usr/lib/python2.7/dist-packages/dput/profile.py is in python-dput 1.17.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | # -*- coding: utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
# Copyright (c) 2012 dput authors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
"""
Profile implementation & routines. This handles all external access
of the Profile data.
"""
import logging
import re
import shlex
import dput.core
from dput.core import logger
from dput.config import AbstractConfig
from dput.configs.dputcf import DputCfConfig
from dput.configs.dputng import DputProfileConfig
from dput.exceptions import DputConfigurationError
from dput.util import _config_cleanup, validate_object
classes = {
"dputng": DputProfileConfig,
"dputcf": DputCfConfig
}
def _blame_map(obj, blame):
ret = {}
for key in obj:
val = obj[key]
if isinstance(val, dict):
ret[key] = _blame_map(obj[key], blame)
if isinstance(val, list):
vals = {}
for v in val:
vals[v] = blame
ret[key] = vals
else:
ret[key] = blame
return ret
class MultiConfig(AbstractConfig):
"""
Multi-configuration abstraction. This helps abstract
the underlying configurations from the user.
This is a subclass of :class:`dput.config.AbstractConfig`
"""
def __init__(self):
configs = []
for config in dput.core.CONFIG_LOCATIONS:
configs.append({
"type": "dputng",
"loc": config,
"weight": dput.core.CONFIG_LOCATIONS[config]
})
for config in dput.core.DPUT_CONFIG_LOCATIONS:
configs.append({
"type": "dputcf",
"loc": config,
"weight": dput.core.DPUT_CONFIG_LOCATIONS[config]
})
configs = sorted(configs, key=lambda c: c['weight'])
configs.reverse()
self.preload(configs)
def set_replacements(self, replacements):
"""
See :meth:`dput.config.AbstractConfig.set_replacements`
"""
for config in self.configs:
config.set_replacements(replacements)
def preload(self, objs):
"""
See :meth:`dput.config.AbstractConfig.preload`
"""
configs = []
for obj in objs:
configs.append(
classes[obj['type']](
[obj['loc']]
)
)
self.configs = configs
defaults_blame = {}
defaults = {}
for config in configs:
defaults.update(config.get_defaults())
defaults = _config_cleanup(defaults)
defaults_blame.update(
_blame_map(config.get_defaults(), "%s (%s)" % (
config.path,
'DEFAULT'
))
)
self.defaults = defaults
self.defaults_blame = defaults_blame
def get_defaults(self):
"""
See :meth:`dput.config.AbstractConfig.get_defaults`
"""
return self.get_config("DEFAULT")
def get_config(self, name):
"""
See :meth:`dput.config.AbstractConfig.get_config`
"""
logger.trace("Loading entry %s" % (name))
ret = self.defaults.copy()
for config in self.configs:
obj = config.get_config(name)
logger.trace(obj)
ret.update(obj)
ret = _config_cleanup(ret)
logger.trace('Rewrote to:')
logger.trace(obj)
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Got configuration: %s" % (name))
for key in ret:
logger.debug("\t%s: %s" % (key, ret[key]))
validate_object('config', ret, 'profiles/%s' % (name))
return ret
def get_blame(self, name):
ret = self.defaults_blame
for config in self.configs:
obj = config.get_config(name, ignore_errors=True)
ret.update(_blame_map(obj, "%s (%s)" % (config.path, name)))
return ret
def get_config_blocks(self):
"""
See :meth:`dput.config.AbstractConfig.get_config_blocks`
"""
ret = set()
for config in self.configs:
for block in config.get_config_blocks():
ret.add(block)
if "DEFAULT" in ret:
ret.remove("DEFAULT")
return ret
_multi_config = None
def load_profile(host):
"""
Load a profile, for a given host ``host``. In the case where
``host`` has a ":", that'll be treated as an expansion for
config strings. For instance:
``ppa:paultag/fluxbox`` will expand any ``%(ppa)s`` strings to
``paultag/fluxbox``. Comes in super handy.
"""
global _multi_config
repls = {}
if host and ":" in host:
host, arg = host.split(":", 1)
repls[host] = arg
if _multi_config is None:
_multi_config = MultiConfig()
config = _multi_config
config.set_replacements(repls)
configs = config.get_config_blocks()
if host in configs:
return config.get_config(host)
if host is not None:
raise DputConfigurationError("Error, was given host, "
"but we don't know about it.")
for block in configs:
try:
obj = config.get_config(block)
except DputConfigurationError:
continue # We don't have fully converted blocks.
host_main = obj.get('default_host_main')
if host_main: # If we have a default_host_main, let's return that.
return config.get_config(host_main)
return config.get_config("ftp-master")
def profiles():
"""
Get a list of all profiles we know about. It returns a set of
strings, which can be accessed with :func:`load_profile`
"""
global _multi_config
if _multi_config is None:
_multi_config = MultiConfig()
config = _multi_config
configs = config.get_config_blocks()
if "DEFAULT" in configs:
configs.remove("DEFAULT")
return configs
def get_blame_map(name):
global _multi_config
if _multi_config is None:
_multi_config = MultiConfig()
config = _multi_config
configs = config.get_blame(name)
return configs
def parse_overrides(overrides):
"""
Translate a complex string structure into a JSON like object. For example
this function would translate foo.bar=baz like strings into objects
overriding the JSON profile.
Basically this function function will take any object separated by a dot on
the left side as a dict object, whereas the terminal value on the right
side is taken literally.
"""
# This function involves lots of black magic.
# Generally we expect a foo.bar=value format, with foo.bar being a profile
# key and value being $anything.
# However, people might provide that in weird combinations such as
# 'foo.bar = "value value"'
override_obj = {}
for override in overrides:
parent_obj = override_obj
if override.find("=") > 0 or override.startswith("-") > 0:
(profile_key, profile_value) = (None, None)
if override.startswith("-"):
profile_key = override[1:]
profile_value = None
else:
(profile_key, profile_value) = override.split("=", 1)
profile_value = shlex.split(profile_value)
profile_key = re.sub('\s', '', profile_key)
profile_key = profile_key.split(".")
last_item = profile_key.pop()
for key in profile_key:
if not key in parent_obj:
parent_obj[key] = {}
parent_obj = parent_obj[key]
if isinstance(parent_obj, list):
raise DputConfigurationError("Ambiguous override: object %s "
"can either be a composite type or a terminal, not both" %
(last_item))
if not last_item in parent_obj:
parent_obj[last_item] = []
if last_item in parent_obj and not (
isinstance(parent_obj[last_item], list)):
raise DputConfigurationError("Ambiguous override: object %s "
"can either be a composite type or a terminal, not both" %
(last_item))
parent_obj[last_item].append(profile_value)
else:
raise DputConfigurationError(
"Profile override %s does not seem to match the "
"expected format" % override)
return override_obj
|