# 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # Copyright 2005 Dan Williams and Red Hat, Inc. import os from ConfigParser import ConfigParser from plague import BaseConfig from plague import ArchUtils class InvalidTargetException(Exception): pass def make_target_string(distro, target, arch, repo): return "%s-%s-%s-%s" % (distro, target, arch, repo) def make_target_string_from_dict(target_dict): return make_target_string(target_dict['distro'], target_dict['target'], target_dict['arch'], target_dict['repo']) class BuilderConfig(BaseConfig.BaseConfig): def __init__(self, filename): BaseConfig.BaseConfig.__init__(self, filename) try: self.open() except BaseConfig.ConfigError: print "Config file did not exist. Writing %s with default values." % filename self.save_default_config() self._targets = [] def targets(self): return self._targets def get_target(self, td, fuzzy=False): """ Returns a target for the ID specified, optionally just grabbing the first matching target that can build a particular arch. """ for target_cfg in self._targets: if not fuzzy and target_cfg.match_exactly(td): return target_cfg elif fuzzy and target_cfg.match_fuzzy_on_arch(td): return target_cfg raise InvalidTargetException def load_target_configs(self, allowed_arches): cfg_dir = self.get_str("Directories", "target_configs_dir") if not os.path.exists(cfg_dir) or not os.access(cfg_dir, os.R_OK): return # Don't ever load targets twice if len(self._targets) > 0: return files = os.listdir(cfg_dir) for f in files: if not f.endswith(".cfg"): continue cfg_file = os.path.join(cfg_dir, f) target_cfg = TargetConfig(self, cfg_file) if target_cfg.basearch() in allowed_arches: # Add additional supported "sub" arches to each target's # arch list, like i486, sparcv9, etc for sub_arch in allowed_arches: if ArchUtils.sub_arches.has_key(sub_arch) and ArchUtils.sub_arches[sub_arch] == target_cfg.basearch(): if not sub_arch in target_cfg.arches(): target_cfg.add_arch(sub_arch) target_cfg.add_arch('noarch') self._targets.append(target_cfg) else: del target_cfg def save_default_config(self, filename=None): self.add_section("General") self.set_option("General", "debug", "yes") self.set_option("General", "builder_cmd", "/usr/bin/mock") self.set_option("General", "builder_user", "plague-builder") self.set_option("General", "comm_type", "active") self.set_option("General", "hostname", "localhost") self.set_option("General", "server", "") self.add_section("Directories") self.set_option("Directories", "builder_work_dir", "/tmp/builder_work") self.set_option("Directories", "target_configs_dir", "/etc/plague/builder/targets") self.add_section("Active") self.set_option("Active", "xmlrpc_port", "8889") self.set_option("Active", "fileserver_port", "8890") self.add_section("Passive") self.set_option("Passive", "xmlrpc_port", "8888") self.set_option("Passive", "fileserver_port", "8889") self.add_section("SSL") self.set_option("SSL", "use_ssl", "yes") self.set_option("SSL", "builder_key_and_cert_dir", "/etc/plague/builder/certs") self.set_option("SSL", "ca_cert", "/etc/plague/builder/certs/ca_cert.pem") self.save() class TargetConfig(BaseConfig.BaseConfig): def __init__(self, cfg, filename): BaseConfig.BaseConfig.__init__(self, filename) try: self.open() except BaseConfig.ConfigError: print "Config file did not exist. Writing %s with default values." % filename self.save_default_config() self._parent_cfg = cfg self._distro = self.get_str("General", "distro") self._target = self.get_str("General", "target") self._basearch = self.get_str("General", "basearch") self._repo = self.get_str("General", "repo") self._mock_config = self.get_str("General", "mock_config") self._arches = [] self._arches.append(self._basearch) def target_dict(self): target_dict = {} target_dict['distro'] = self._distro target_dict['target'] = self._target target_dict['arch'] = self._basearch target_dict['repo'] = self._repo return target_dict def match_exactly(self, td): if td['distro'] == self._distro \ and td['target'] == self._target \ and td['arch'] == self._basearch \ and td['repo'] == self._repo: return True return False def match_fuzzy_on_arch(self, td): if td['distro'] == self._distro \ and td['target'] == self._target \ and td['arch'] in self._arches \ and td['repo'] == self._repo: return True return False def distro(self): return self._distro def target(self): return self._target def basearch(self): return self._basearch def repo(self): return self._repo def mock_config(self): return self._mock_config def arches(self): return self._arches def add_arch(self, arch): self._arches.append(arch) def parent_cfg(self): return self._parent_cfg def __repr__(self): return make_target_string(self._distro, self._target, self._basearch, self._repo) __str__ = __repr__ def save_default_config(self, filename=None): self.add_section("General") self.set_option("General", "distro", "fedora") self.set_option("General", "target", "development") self.set_option("General", "basearch", "i386") self.set_option("General", "repo", "core") self.set_option("General", "mock_config", "fedora-development-i386-core") self.save()