]> git.ipfire.org Git - pakfire.git/blame - python/pakfire/config.py
Add support for building on ARM architectures.
[pakfire.git] / python / pakfire / config.py
CommitLineData
47a4cb89 1#!/usr/bin/python
b792d887
MT
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###############################################################################
47a4cb89
MT
21
22import logging
23import os
24
25from ConfigParser import ConfigParser
26
27import base
28
29from constants import *
30
31class Config(object):
3ad4bb5a
MT
32 def __init__(self, type=None):
33 self.type = type
47a4cb89
MT
34
35 self._config = {
6174d54a 36 "debug" : False,
47a4cb89
MT
37 "logfile" : "/var/log/pakfire.log",
38 "source_download_url" : SOURCE_DOWNLOAD_URL,
0ec833c6 39 "local_build_repo_path" : LOCAL_BUILD_REPO_PATH,
47a4cb89
MT
40 }
41
42 self._config_repos = {}
43 self._distro = {}
677ff42a
MT
44 self._master = {}
45 self._slave = {}
47a4cb89
MT
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
afd1400d 63 if not filename or not os.path.exists(filename):
47a4cb89
MT
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
677ff42a
MT
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
47a4cb89
MT
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):
6a509182 118 logging.debug("Updating configuration parameter: %s = %s" % (key, val))
47a4cb89
MT
119 self._config[key] = val
120
6a509182
MT
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
47a4cb89
MT
129 def get_repos(self):
130 return self._config_repos.items()
131
132 @property
133 def config_files(self):
134 files = []
135
3ad4bb5a 136 if self.type == "builder":
47a4cb89
MT
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
3ad4bb5a
MT
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 = {
36f75602
MT
173 # x86
174 "x86_64" : [ "x86_64", ],
175 "i686" : [ "i686", "x86_64", ],
176 "i586" : [ "i586", "i686", "x86_64", ],
177 "i486" : [ "i486", "i586", "i686", "x86_64", ],
178
179 # ARM
180 "armv5tel" : [ "armv5tel", "armv5tejl", ],
181 "armv7hl " : [ "armv7l", ],
3ad4bb5a
MT
182 }
183
184 for host, can_be_built in host_arches.items():
185 if self.host_arch in can_be_built:
186 yield host
187
188 def host_supports_arch(self, arch):
189 """
190 Check if this host can build for the target architecture "arch".
191 """
192 return arch in self.supported_arches