]> git.ipfire.org Git - ipfire.org.git/blobdiff - src/backend/releases.py
release: Iterate through all releases when searching for files
[ipfire.org.git] / src / backend / releases.py
index ba898e703ff9ad9ccc59ee4bc3073f2d4c1e3939..9b2670dee19ee87fc1880c5de10710ce3df9bf95 100644 (file)
@@ -4,7 +4,7 @@ import hashlib
 import logging
 import os
 import re
-import urllib.request, urllib.parse, urllib.error
+import tornado.gen
 import urllib.parse
 
 from . import database
@@ -28,8 +28,13 @@ class File(Object):
                # get all data from database
                self.__data = data
 
-       def __cmp__(self, other):
-               return cmp(self.prio, other.prio)
+       def __eq__(self, other):
+               if isinstance(other, self.__class__):
+                       return self.id == otherid
+
+       def __lt__(self, other):
+               if isinstance(other, self.__class__):
+                       return self.prio < other.prio
 
        @property
        def data(self):
@@ -89,7 +94,7 @@ class File(Object):
 
        @property
        def url(self):
-                return urllib.parse.urljoin("https://downloads.ipfire.org", self.filename)
+               return urllib.parse.urljoin("https://downloads.ipfire.org", self.filename)
 
        @property
        def desc(self):
@@ -98,7 +103,7 @@ class File(Object):
                descriptions = {
                        "armv5tel"      : _("Flash Image"),
                        "armv5tel-scon" : _("Flash Image with serial console"),
-                       "iso"           : _("CD Image"),
+                       "iso"           : _("ISO Image"),
                        "torrent"       : _("Torrent File"),
                        "flash"         : _("Flash Image"),
                        "alix"          : _("Flash Image with serial console"),
@@ -133,28 +138,6 @@ class File(Object):
                except KeyError:
                        return 999
 
-       @property
-       def rem(self):
-               _ = lambda x: x
-       
-               remarks = {
-                       "armv5tel"      : _("This image runs on many ARM-based boards"),
-                       "armv5tel-scon" : _("This image runs on ARM boards with a serial console"),
-                       "iso"           : _("Use this image to burn a CD and install IPFire from it."),
-                       "torrent"       : _("Download the CD image from the torrent network."),
-                       "flash"         : _("An image that is meant to run on embedded devices."),
-                       "alix"          : _("Flash image where a serial console is enabled by default."),
-                       "usbfdd"        : _("Install IPFire from a floppy-formated USB key."),
-                       "usbhdd"        : _("If the floppy image doesn't work, use this image instead."),
-                       "xen"           : _("A ready-to-run image for Xen."),
-                       "xen-downloader": _("Generator for creating a Xen image."),
-               }
-
-               try:
-                       return remarks[self.type]
-               except KeyError:
-                       return _("Unknown image type")
-
        @property
        def sha1(self):
                return self.data.get("sha1")
@@ -185,10 +168,10 @@ class File(Object):
        def torrent_hash(self):
                return self.data.get("torrent_hash", None)
 
-        @property
-        def torrent_url(self):
-                if self.torrent_hash:
-                        return "%s.torrent" % self.url
+       @property
+       def torrent_url(self):
+               if self.torrent_hash:
+                       return "%s.torrent" % self.url
 
        @property
        def magnet_link(self):
@@ -202,11 +185,11 @@ class File(Object):
                s += "&dn=%s" % urllib.parse.quote(self.basename)
 
                # Add our tracker.
-                for tracker in TRACKERS:
-                    s += "&tr=%s" % tracker
+               for tracker in TRACKERS:
+                       s += "&tr=%s" % tracker
 
-                # Add web download URL
-                s += "&as=%s" % urllib.parse.quote(self.url)
+               # Add web download URL
+               s += "&as=%s" % urllib.parse.quote(self.url)
 
                return s
 
@@ -311,19 +294,19 @@ class Release(Object):
                        if file.type == type:
                                return file
 
-       def __file_hash(self, filename):
-               sha1 = hashlib.sha1()
+       def __file_hash(self, filename, algo="sha256"):
+               h = hashlib.new(algo)
 
                with open(filename) as f:
                        buf_size = 1024
                        buf = f.read(buf_size)
                        while buf:
-                               sha1.update(buf)
+                               h.update(buf)
                                buf = f.read(buf_size)
 
-               return sha1.hexdigest()
+               return h.hexdigest()
 
-       def scan_files(self, basepath="/srv/mirror0"):
+       def scan_files(self, basepath="/pub/mirror"):
                if not self.path:
                        return
 
@@ -354,7 +337,8 @@ class Release(Object):
                                continue
 
                        logging.info("Hashing %s..." % filename)
-                       filehash = self.__file_hash(filename)
+                       hash_sha256 = self.__file_hash(filename, "sha256")
+                       hash_sha1   = self.__file_hash(filename, "sha1")
                        filesize = os.path.getsize(filename)
 
                        # Check if there is a torrent download available for this file:
@@ -364,8 +348,8 @@ class Release(Object):
                                torrent_hash = self.torrent_read_hash(torrent_file)
 
                        self.db.execute("INSERT INTO files(releases, filename, filesize, \
-                               sha1, torrent_hash) VALUES(%s, %s, %s, %s, %s)",
-                               self.id, _filename, filesize, filehash, torrent_hash)
+                               sha256, sha1, torrent_hash) VALUES(%s, %s, %s, %s, %s, %s)",
+                               self.id, _filename, filesize, hash_sha256, hash_sha1, torrent_hash)
 
                # Search for all files that miss a torrent hash.
                files = self.db.query("SELECT id, filename FROM files \
@@ -416,18 +400,18 @@ class Release(Object):
                assert self.supports_platform(platform)
 
                if self.sname >= "ipfire-2.19-core100":
-                       return "https://downloads.ipfire.org/%s/images/%s/vmlinuz" % (self.path, arch)
+                       return "https://boot.ipfire.org/%s/images/%s/vmlinuz" % (self.path, arch)
 
-               return "https://downloads.ipfire.org/%s/images/vmlinuz" % self.path
+               return "https://boot.ipfire.org/%s/images/vmlinuz" % self.path
 
        def netboot_initrd_url(self, arch, platform):
                assert self.supports_arch(arch)
                assert self.supports_platform(platform)
 
                if self.sname >= "ipfire-2.19-core100":
-                       return "https://downloads.ipfire.org/%s/images/%s/instroot" % (self.path, arch)
+                       return "https://boot.ipfire.org/%s/images/%s/instroot" % (self.path, arch)
 
-               return "https://downloads.ipfire.org/%s/images/instroot" % self.path
+               return "https://boot.ipfire.org/%s/images/instroot" % self.path
 
        def netboot_args(self, arch, platform):
                return ""
@@ -441,19 +425,8 @@ class Release(Object):
 
        @property
        def penetration(self):
-               penetration = self.memcache.get("%s-penetration" % self.sname)
-
-               # Cache HIT
-               if penetration:
-                       return penetration
-
                # Get penetration from fireinfo
-               penetration = self.backend.fireinfo.get_release_penetration(self)
-
-               # Cache for 1 hour
-               self.memcache.set("%s-penetration" % self.sname, penetration, 3600)
-
-               return penetration
+               return self.backend.fireinfo.get_release_penetration(self)
 
 
 class Releases(Object):
@@ -469,6 +442,12 @@ class Releases(Object):
                for row in res:
                        yield Release(self.backend, row.id, data=row)
 
+       def __iter__(self):
+               releases = self._get_releases("SELECT * FROM releases \
+                       ORDER BY published DESC NULLS FIRST")
+
+               return iter(releases)
+
        def get_by_id(self, id):
                ret = self.db.get("SELECT * FROM releases WHERE id = %s", id)
 
@@ -567,3 +546,9 @@ class Releases(Object):
                file = File(self.backend, release, file.id)
 
                return file
+
+       @tornado.gen.coroutine
+       def scan_files(self, basepath="/pub/mirror"):
+               for release in self:
+                       logging.info("Scanning %s..." % release)
+                       release.scan_files(basepath=basepath)