#!/usr/bin/python -tt # -*- coding: utf-8 -*- # yum2smart -- Convert Yum configuration for Smart package manager # # Copyright (c) 2007 Ville Skyttä # # 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA __version__ = "1.1" import optparse import os import sys import yum try: import logging logging.raiseExceptions = 0 except: pass def printline(tofile, line): """Print a line to the given file or stdout.""" if tofile: tofile.write(line) tofile.write("\n") else: print line def output_channel(tofile, repo, urls): """Output channel information for given repo and list of urls.""" printline(tofile, "[%s]" % repo.id) printline(tofile, "type = rpm-md") if repo.name: printline(tofile, "name = %s" % repo.name) if not repo.enabled: printline(tofile, "disabled = yes") first = True for url in urls: printline(tofile, "%sbaseurl = %s" % (not first and "#" or "", url)) first = False printline(tofile, "") def output_mirrors(tofile, urls): """Output mirror information for given list of urls.""" if len(urls) < 2: return first = True for url in urls: printline(tofile, "%s%s" % (not first and " " or "", url)) first = False printline(tofile, "") def main(): """Do our thing.""" parser = optparse.OptionParser("yum2smart.py version %s" % __version__) parser.add_option("-o", "--output", dest="filename", help="File to write channel/mirror info to. If not " "specified, stdout is used. Warning: yum may print some " "messages during processing, corrupting the output if " "it goes to stdout.") parser.add_option("-m", "--mirrors", action="store_true", help="Output mirror configuration instead of channels.") (opts, args) = parser.parse_args() yumm = yum.YumBase() yumm.doConfigSetup(init_plugins=False) if os.geteuid() != 0: yumm.repos.setCacheDir(yum.misc.getCacheDir()) yumm.doRepoSetup() tofile = None if opts.filename: tofile = open(opts.filename, "w") try: for repo in yumm.repos.findRepos("*"): # Populate mirrorlists etc urls = [] try: repo.dirSetup() urls = repo.urls except Exception, e: sys.stderr.write("\n%s\n\n" % e) continue # Not all yum versions populate urls eg. for disabled repos if not urls: sys.stderr.write( "\nNo URLs for repo %s, skipping it\n" % repo.id) continue if opts.mirrors: output_mirrors(tofile, urls) else: output_channel(tofile, repo, urls) finally: if tofile: tofile.close() if __name__ == "__main__": main()