]> git.ipfire.org Git - pakfire.git/blob - python/pakfire/config.py
Bump version 0.9.9.
[pakfire.git] / python / pakfire / config.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2011 Pakfire development team #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 import logging
23 import os
24
25 from ConfigParser import ConfigParser
26
27 import base
28
29 from constants import *
30
31 class Config(object):
32 def __init__(self, type=None):
33 self.type = type
34
35 self._config = {
36 "debug" : False,
37 "logfile" : "/var/log/pakfire.log",
38 "source_download_url" : SOURCE_DOWNLOAD_URL,
39 "local_build_repo_path" : LOCAL_BUILD_REPO_PATH,
40 }
41
42 self._config_repos = {}
43 self._distro = {}
44 self._master = {}
45 self._slave = {}
46 self._files = []
47
48 # Read default configuration files
49 for file in self.config_files:
50 self.read(file)
51
52 def dump(self):
53 logging.debug("Configuration:")
54 for k, v in self._config.items():
55 logging.debug(" %s : %s" % (k, v))
56
57 logging.debug("Loaded from files:")
58 for f in self._files:
59 logging.debug(" %s" % f)
60
61 def read(self, filename):
62 # If filename does not exist we return silently
63 if not filename or not os.path.exists(filename):
64 return
65
66 filename = os.path.abspath(filename)
67
68 # If the file was already loaded, we return silently, too
69 if filename in self._files:
70 return
71
72 logging.debug("Reading configuration file: %s" % filename)
73
74 config = ConfigParser()
75 config.read(filename)
76
77 # Read the main section from the file if any
78 if "main" in config.sections():
79 for k,v in config.items("main"):
80 self._config[k] = v
81 config.remove_section("main")
82
83 # Read distribution information from the file
84 if "distro" in config.sections():
85 for k,v in config.items("distro"):
86 self._distro[k] = v
87 config.remove_section("distro")
88
89 # Read master settings from file
90 if "master" in config.sections():
91 for k,v in config.items("master"):
92 self._master[k] = v
93 config.remove_section("master")
94
95 # Read slave settings from file
96 if "slave" in config.sections():
97 for k,v in config.items("slave"):
98 self._slave[k] = v
99 config.remove_section("slave")
100
101 # Read repository definitions
102 for section in config.sections():
103 if not self._config_repos.has_key(section):
104 self._config_repos[section] = {}
105
106 options = {}
107 for option in config.options(section):
108 options[option] = config.get(section, option)
109
110 self._config_repos[section].update(options)
111
112 self._files.append(filename)
113
114 def get(self, key, default=None):
115 return self._config.get(key, default)
116
117 def set(self, key, val):
118 logging.debug("Updating configuration parameter: %s = %s" % (key, val))
119 self._config[key] = val
120
121 def update(self, values):
122 """
123 This function takes a dictionary which configuration
124 parameters and applies them to the configuration.
125 """
126 for key, val in values.items():
127 self.set(key, val)
128
129 def get_repos(self):
130 return self._config_repos.items()
131
132 @property
133 def config_files(self):
134 files = []
135
136 if self.type == "builder":
137 path = os.getcwd()
138
139 while not path == "/":
140 _path = os.path.join(path, "config")
141 if os.path.exists(_path):
142 break
143
144 _path = None
145 path = os.path.dirname(path)
146
147 if _path:
148 files.append(os.path.join(_path, "pakfire.conf"))
149 files.append(os.path.join(_path, "default.conf"))
150
151 # Remove non-existant files
152 for f in files:
153 if not os.path.exists(f):
154 files.remove(f)
155
156 if not files:
157 # Return system configuration files
158 files += [CONFIG_FILE]
159 files += [os.path.join(CONFIG_DIR, f) for f in os.listdir(CONFIG_DIR)]
160
161 return files
162
163 @property
164 def host_arch(self):
165 """
166 Return the architecture of the host we are running on.
167 """
168 return os.uname()[4]
169
170 @property
171 def supported_arches(self):
172 host_arches = {
173 "x86_64" : [ "x86_64", ],
174 "i686" : [ "i686", "x86_64", ],
175 "i586" : [ "i586", "i686", "x86_64", ],
176 "i486" : [ "i486", "i586", "i686", "x86_64", ],
177 }
178
179 for host, can_be_built in host_arches.items():
180 if self.host_arch in can_be_built:
181 yield host
182
183 def host_supports_arch(self, arch):
184 """
185 Check if this host can build for the target architecture "arch".
186 """
187 return arch in self.supported_arches