#!/usr/bin/python # -*- mode: Python; indent-tabs-mode: nil; -*- # # 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os, tempfile class RepoSupport: """helper functions and constants for yum/repomd usage""" def __init__(self,cfg): # List of distribution release names in repository. self.allreleases = cfg.alldists # Architecture directory names per distribution release in repository. self.archdict = cfg.archdict self.targetarchs = cfg.targetarchs self.baserepos = cfg.baserepos self.testrepos = cfg.testrepos self.extrepos = {} for r in self.allreleases: self.extrepos.setdefault(r,[]) self.reponames = cfg.reponames self.repourls = cfg.repourls def AllReleases(self): """return list of all distribution release names""" return self.allreleases def ReleaseArchsDict(self): """return map with list of architectures per release""" return self.archdict def ReleaseRepoList(self,dist): """return list of base repository ids for the given release""" return self.baserepos.get(dist) or [] def TestRepoList(self,dist): """return list of testing repository ids for the given release""" return self.testrepos.get(dist) or [] def ExtRepoList(self,dist): return self.extrepos.get(dist) or [] def RepoName(self, repo, release, arch): name = self.reponames.get(repo) or None if name: return '%s %s - %s' % (name,release,arch) else: return '' def RepoNamesDict(self): """return map with list of repository descriptions per release""" return self.reponames def GenerateConfig(self, releases, cachedir=''): """return temporary yum.conf with repository definitions for the specified releases""" try: (fd, conffile) = tempfile.mkstemp() except: conffile = tempfile.mktemp() fd = os.open(conffile,os.O_RDWR|os.O_CREAT) confheader = """[main] cachedir=%s debuglevel=2 logfile=/dev/null pkgpolicy=newest reposdir=/dev/null exactarch=1 obsoletes=1 metadata_expire=300 retries=20 """ % (cachedir) os.write(fd,confheader) for release in releases: assert release in self.AllReleases() for repo in self.baserepos[release]+self.testrepos[release]+self.extrepos[release]: for arch in self.archdict[release]: reposection = """[%s] name=%s baseurl=%s enabled=0 """ % (self.RepoId(repo,release,arch), self.RepoName(repo,release,arch), self.repourls[repo] % (release,arch)) os.write(fd,reposection) os.close(fd) return conffile def RepoId(self, repo, release, arch): return '%s-%s-%s' % (repo,release,arch) def GenerateRepoIds(self, release, testing=False): """return list of all repository ids for the specified release""" repoids = [] assert release in self.AllReleases() for arch in self.archdict[release]: for r in self.baserepos[release]+self.extrepos[release]: repoid = self.RepoId(r,release,arch) repoids.append(repoid) if testing: for r in self.testrepos[release]: repoid = self.RepoId(r,release,arch) repoids.append(repoid) return repoids def AddRepo(self, id, name, url): for release in self.AllReleases(): self.extrepos[release].append(id) self.reponames.setdefault(id,name) self.repourls.setdefault(id,url)