]> git.ipfire.org Git - people/stevee/pakfire.git/blobdiff - python/pakfire/downloader.py
Improve the repository code.
[people/stevee/pakfire.git] / python / pakfire / downloader.py
index baa243e553d08f81d96edfc755ac90b6420e0ca2..d40f14c4a58b14d6508b5672378a73378c273b5e 100644 (file)
@@ -26,7 +26,7 @@ import random
 import logging
 log = logging.getLogger("pakfire")
 
-from config import Config
+from config import _Config
 
 from urlgrabber.grabber import URLGrabber, URLGrabError
 from urlgrabber.mirror import MirrorGroup
@@ -43,18 +43,21 @@ class PakfireGrabber(URLGrabber):
                kwargs.update({
                        "quote" : 0,
                        "user_agent" : "pakfire/%s" % PAKFIRE_VERSION,
+
+                       "ssl_verify_host" : False,
+                       "ssl_verify_peer" : False,
                })
 
-               if isinstance(pakfire, Config):
+               if isinstance(pakfire, _Config):
                        config = pakfire
                else:
                        config = pakfire.config
 
-               if config.get("offline"):
+               if config.get("downloader", "offline"):
                        raise OfflineModeError, "Cannot use %s in offline mode." % self.__class__.__name__
 
                # Set throttle setting.
-               bandwidth_throttle = config.get("bandwidth_throttle")
+               bandwidth_throttle = config.get("downloader", "bandwidth_throttle")
                if bandwidth_throttle:
                        try:
                                bandwidth_throttle = int(bandwidth_throttle)
@@ -65,7 +68,7 @@ class PakfireGrabber(URLGrabber):
                        kwargs.update({ "throttle" : bandwidth_throttle })
 
                # Configure HTTP proxy.
-               http_proxy = config.get("http_proxy")
+               http_proxy = config.get("downloader", "http_proxy")
                if http_proxy:
                        kwargs.update({ "proxies" : { "http" : http_proxy }})
 
@@ -77,6 +80,12 @@ class PakfireGrabber(URLGrabber):
                # a unicode string.
                return URLGrabber.urlread(self, filename.encode("utf-8"), *args, **kwargs)
 
+       def urlopen(self, filename, *args, **kwargs):
+               # However, urlopen requires the filename to be an ordinary string object.
+               filename = str(filename)
+
+               return URLGrabber.urlopen(self, filename, *args, **kwargs)
+
 
 class PackageDownloader(PakfireGrabber):
        def __init__(self, pakfire, *args, **kwargs):
@@ -178,16 +187,25 @@ class Mirror(object):
 
 
 class MirrorList(object):
-       def __init__(self, pakfire, repo):
+       def __init__(self, pakfire, repo, mirrorlist):
                self.pakfire = pakfire
                self.repo = repo
 
                self.__mirrors = []
 
                # Save URL to more mirrors.
-               self.mirrorlist = repo.mirrorlist
+               self.mirrorlist = mirrorlist
 
-               self.update(force=False)
+       @property
+       def base_mirror(self):
+               if not self.repo.baseurl:
+                       return
+
+               return Mirror(self.repo.baseurl, preferred=False)
+
+       @property
+       def distro(self):
+               return self.repo.distro
 
        @property
        def cache(self):
@@ -206,8 +224,8 @@ class MirrorList(object):
                        return
 
                log.debug("Updating mirrorlist for repository '%s' (force=%s)" % (self.repo.name, force))
-
-               cache_filename = "mirrors/mirrorlist"
+               cache_filename = os.path.join("repodata", self.distro.sname, self.distro.release,
+                       self.repo.name, self.distro.arch, "mirrors")
 
                # Force the update if no mirrorlist is available.
                if not self.cache.exists(cache_filename):
@@ -238,6 +256,7 @@ class MirrorList(object):
                        f.close()
 
                # Read mirrorlist from cache and parse it.
+               self.forget_mirrors()
                with self.cache.open(cache_filename) as f:
                        self.parse_mirrordata(f.read())
 
@@ -252,6 +271,12 @@ class MirrorList(object):
 
                self.__mirrors.append(mirror)
 
+       def forget_mirrors(self):
+               self.__mirrors = []
+
+               if self.base_mirror:
+                       self.__mirrors.append(self.base_mirror)
+
        @property
        def preferred(self):
                """
@@ -282,6 +307,9 @@ class MirrorList(object):
                """
                        Return a MirrorGroup object for the given grabber.
                """
+               # Make sure the mirrorlist is up to date.
+               self.update()
+
                # A list of mirrors that is passed to MirrorGroup.
                mirrors = []