]>
git.ipfire.org Git - people/stevee/pakfire.git/blob - python/pakfire/config.py
2 ###############################################################################
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2011 Pakfire development team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
26 from ConfigParser
import ConfigParser
29 log
= logging
.getLogger("pakfire")
32 from system
import system
34 from constants
import *
37 class _Config(object):
40 global_default_settings
= {
42 "file" : "/var/log/pakfire.log",
45 "rotation_threshold" : 10485760,
53 # A dict with default settings for this config class.
56 def __init__(self
, files
=None):
57 # Configuration settings.
58 self
._config
= self
.global_default_settings
.copy()
59 self
._config
.update(self
.default_settings
)
61 # List of files that were already loaded.
64 # If no files were given, load the default files.
66 # Read default configuration file.
67 self
.read(*self
.files
)
69 repo_path
= self
.get(None, "repo_path", CONFIG_REPOS_DIR
)
71 self
.read_dir(repo_path
, ext
=".repo")
73 # Always read overwrite.conf.
74 # This is a undocumented feature to make bootstrapping easier.
75 self
.read("overwrite.conf")
80 for name
, settings
in self
._config
.items():
81 if not name
.startswith("repo:"):
84 # Strip "repo:" from name of the repository.
87 repos
.append((name
, settings
))
91 def read_dir(self
, where
, ext
=".conf"):
92 for file in os
.listdir(where
):
93 if not file.endswith(ext
):
96 file = os
.path
.join(where
, file)
99 def read(self
, *files
):
100 # Do nothing for no files.
105 if not file.startswith("/"):
106 file = os
.path
.join(CONFIG_DIR
, file)
108 if not os
.path
.exists(file):
111 # Normalize filename.
112 file = os
.path
.abspath(file)
114 # Check if file has already been read or
115 # does not exist. Then skip it.
116 if file in self
._files
or not os
.path
.exists(file):
120 with
open(file) as f
:
123 # Save the filename to the list of read files.
124 self
._files
.append(file)
133 config
= ConfigParser()
136 # Read all data from the configuration file in the _config dict.
137 for section
in config
.sections():
138 items
= dict(config
.items(section
))
140 if section
== "DEFAULT":
144 self
._config
[section
].update(items
)
146 self
._config
[section
] = items
148 # Update the logger, because the logging configuration may
150 logger
.setup_logging(self
)
152 def set(self
, section
, key
, value
):
154 self
._config
[section
][key
] = value
156 self
._config
[section
] = { key
: value
}
158 def get_section(self
, section
):
160 return self
._config
[section
]
164 def get(self
, section
, key
, default
=None):
165 s
= self
.get_section(section
)
172 def get_int(self
, section
, key
, default
=None):
173 val
= self
.get(section
=section
, key
=key
, default
=default
)
179 def get_bool(self
, section
, key
, default
=None):
180 val
= self
.get(section
=section
, key
=key
, default
=default
)
182 if val
in (True, "true", "1", "on"):
184 elif val
in (False, "false", "0", "off"):
189 def update(self
, section
, what
):
190 if not type(what
) == type({}):
191 log
.error(_("Unhandled configuration update: %s = %s") % (section
, what
))
195 self
._config
[section
].update(what
)
197 self
._config
[section
] = what
201 Dump the configuration that was read.
203 (Only in debugging mode.)
205 log
.debug(_("Configuration:"))
206 for section
, settings
in self
._config
.items():
207 log
.debug(" " + _("Section: %s") % section
)
209 for k
, v
in settings
.items():
210 log
.debug(" %-20s: %s" % (k
, v
))
212 log
.debug(" " + _("No settings in this section."))
214 log
.debug(" " + _("Loaded from files:"))
215 for f
in self
._files
:
218 def has_distro_conf(self
):
219 return self
._config
.has_key("distro")
221 def get_distro_conf(self
):
222 return self
.get_section("distro")
225 class Config(_Config
):
226 files
= ["general.conf", "distro.conf"]
229 class ConfigBuilder(_Config
):
230 files
= ["general.conf", "builder.conf"]
232 def load_distro_config(self
, distro_name
):
233 if distro_name
is None:
236 filename
= os
.path
.join(CONFIG_DISTRO_DIR
, "%s.conf" % distro_name
)
238 if not os
.path
.exists(filename
):
245 class ConfigClient(_Config
):
246 files
= ["general.conf", "client.conf"]
250 # The default server is the official Pakfire
252 "server" : PAKFIRE_HUB
,
256 def get_hub_credentials(self
):
257 hub_url
= self
.get("client", "server")
258 username
= self
.get("client", "username")
259 password
= self
.get("client", "password")
261 return hub_url
, username
, password
264 class ConfigDaemon(_Config
):
265 files
= ["general.conf", "daemon.conf"]
269 # The default server is the official Pakfire
271 "server" : PAKFIRE_HUB
,
273 # The default hostname is the host name of this
275 "hostname" : system
.hostname
,
279 def get_hub_credentials(self
):
280 hub_url
= self
.get("daemon", "server")
281 hostname
= self
.get("daemon", "hostname")
282 password
= self
.get("daemon", "secret")
284 return hub_url
, hostname
, password