From fcf2f8a43dc369a7d4641cb3b9cc8b662cc5279b Mon Sep 17 00:00:00 2001 From: Faik Uygur Date: Mon, 4 Sep 2006 12:50:31 +0000 Subject: [PATCH] * mirrors:// support added. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ---- mirrors.conf example: faik@iago ~ $ cat /etc/pisi/mirrors.conf # Sourceforge mirrors sourceforge http://pardus.dl.sourceforge.net/sourceforge sourceforge http://easynews.dl.sourceforge.net/sourceforge # Gnu mirrors gnu ftp://mirrors.hbi.co.jp/gnu gnu ftp://core.ring.gr.jp/pub/GNU gnu ftp://mirror.aarnet.edu.au/pub/gnu # Local mirrors local http://192.168.3.8 ---- pspec.xml line example: mirrors://sourceforge/cracklib/cracklib-2.8.9.tar.gz ---- Output example: Kaynak system.base bileşeninin parçası Kaynak mirrors://sourceforge/cracklib/cracklib-2.8.9.tar.gz adresinden indiriliyor Fetching source from mirror: http://pardus.dl.sourceforge.net/sourceforge/cracklib/cracklib-2.8.9.tar.gz Fetching source from mirror: http://easynews.dl.sourceforge.net/sourceforge/cracklib/cracklib-2.8.9.tar.gz cracklib-2.8.9.tar.gz (562.0 KB)100% 72.42 KB/s [00:00:00] [bitti] Kaynak arşivi saklandı: /var/cache/pisi/archives/cracklib-2.8.9.tar.gz Arşiv açılıyor... açıldı (/var/tmp/pisi/cracklib-2.8.9-3/work) --- pisi/constants.py | 1 + pisi/mirrors.py | 47 +++++++++++++++++++++++++++++++++++++++++++ pisi/sourcearchive.py | 44 ++++++++++++++++++++++++++++++++-------- 3 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 pisi/mirrors.py diff --git a/pisi/constants.py b/pisi/constants.py index 075d7898..c6de6079 100644 --- a/pisi/constants.py +++ b/pisi/constants.py @@ -78,6 +78,7 @@ class Constants: self.__c.metadata_xml = "metadata.xml" self.__c.install_tar = "install.tar" self.__c.install_tar_lzma = "install.tar.lzma" + self.__c.mirrors_conf = "/etc/pisi/mirrors.conf" #file/directory permissions self.__c.umask = 0022 diff --git a/pisi/mirrors.py b/pisi/mirrors.py new file mode 100644 index 00000000..e0c75386 --- /dev/null +++ b/pisi/mirrors.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2006, TUBITAK/UEKAE +# +# 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. +# +# Please read the COPYING file. + +import os.path +import pisi.context as ctx +from pisi import Error + +import gettext +__trans = gettext.translation('pisi', fallback=True) +_ = __trans.ugettext + +class Mirrors: + def __init__(self, config=ctx.const.mirrors_conf): + self.mirrors = {} + self._parse(config) + + def get_mirrors(self, name): + if self.mirrors.has_key(name): + return list(self.mirrors[name]) + + return None + + def _add_mirror(self, name, url): + if self.mirrors.has_key(name): + self.mirrors[name].append(url) + else: + self.mirrors[name] = [url] + + def _parse(self, config): + if os.path.exists(config): + for line in open(config, "r").readlines(): + if not line.startswith('#') and not line == '\n': + mirror = line.strip().split() + if len(mirror) == 2: + (name, url) = mirror + self._add_mirror(name, url) + else: + raise Error(_('Mirrors file %s does not exist. Could not resolve mirrors://') % config) + diff --git a/pisi/sourcearchive.py b/pisi/sourcearchive.py index f2895008..c4b8ad49 100644 --- a/pisi/sourcearchive.py +++ b/pisi/sourcearchive.py @@ -28,6 +28,7 @@ import pisi.context as ctx from pisi.archive import Archive from pisi.uri import URI from pisi.fetcher import fetch_url +from pisi.mirrors import Mirrors class Error(pisi.Error): pass @@ -44,21 +45,48 @@ class SourceArchive: def fetch(self, interactive=True): if not self.is_cached(interactive): if interactive: - progress = ctx.ui.Progress + self.progress = ctx.ui.Progress else: - progress = None + self.progress = None + try: - fetch_url(self.url, ctx.config.archives_dir(), progress) + if self.url.get_uri().startswith("mirrors://"): + self.fetch_from_mirror() + else: + fetch_url(self.url, ctx.config.archives_dir(), self.progress) except pisi.fetcher.FetchError: - # if archive can not be reached from the url, try the fallback - # address. if ctx.config.values.build.fallback: - archive = basename(self.url.get_uri()) - src = join(ctx.config.values.build.fallback, archive) - fetch_url(src, ctx.config.archives_dir(), progress) + self.fetch_from_fallback() else: raise + def fetch_from_fallback(self): + archive = basename(self.url.get_uri()) + src = join(ctx.config.values.build.fallback, archive) + ctx.ui.warning(_('Trying fallback address: %s') % src) + fetch_url(src, ctx.config.archives_dir(), self.progress) + + def fetch_from_mirror(self): + uri = self.url.get_uri() + sep = uri[len("mirrors://"):].split("/") + name = sep.pop(0) + archive = "/".join(sep) + + mirrors = Mirrors().get_mirrors(name) + if not mirrors: + raise Error(_("%s mirrors are not defined.") % name) + + for mirror in mirrors: + try: + url = join(mirror, archive) + ctx.ui.warning(_('Fetching source from mirror: %s') % url) + fetch_url(url, ctx.config.archives_dir(), self.progress) + return + except pisi.fetcher.FetchError: + pass + + raise pisi.fetcher.FetchError(_('Could not fetch source from %s mirrors.') % name); + def is_cached(self, interactive=True): if not access(self.archiveFile, R_OK): return False