import os
import re
import urllib.parse
-import yabencode
from . import database
from .misc import Object
from .decorators import *
-TRACKERS = (
- "http://ipv4.tracker.ipfire.org:6969/announce",
- "udp://ipv4.tracker.ipfire.org:6969",
- "http://ipv6.tracker.ipfire.org:6969/announce",
- "udp://ipv6.tracker.ipfire.org:6969",
-)
-
class File(Object):
def __init__(self, backend, release, id, data=None):
Object.__init__(self, backend)
if filename.endswith(".iso"):
return "iso"
- elif filename.endswith(".torrent"):
- return "torrent"
-
elif "xen" in filename:
if "downloader" in filename:
return "xen-downloader"
"armv5tel" : _("Flash Image"),
"armv5tel-scon" : _("Flash Image with serial console"),
"iso" : _("ISO Image"),
- "torrent" : _("Torrent File"),
"flash" : _("Flash Image"),
"alix" : _("Flash Image with serial console"),
"usbfdd" : _("USB FDD Image"),
def prio(self):
priorities = {
"iso" : 10,
- "torrent" : 20,
"flash" : 40,
"alix" : 41,
"usbfdd" : 31,
"xen" : 50,
"xen-downloader": 51,
}
-
+
try:
return priorities[self.type]
except KeyError:
return "N/A"
- @property
- 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 magnet_link(self):
- # Don't return anything if we have no torrent hash.
- if self.torrent_hash is None:
- return
-
- s = "magnet:?xt=urn:btih:%s" % self.torrent_hash
-
- #s += "&xl=%d" % self.size
- s += "&dn=%s" % urllib.parse.quote(self.basename)
-
- # Add our tracker.
- for tracker in TRACKERS:
- s += "&tr=%s" % tracker
-
- # Add web download URL
- s += "&as=%s" % urllib.parse.quote(self.url)
-
- return s
-
class Release(Object):
def __init__(self, backend, id, data=None):
if f.arch == arch:
yield f
- @property
- def torrents(self):
- torrents = []
-
- for file in self.files:
- if not file.torrent_hash:
- continue
-
- torrents.append(file)
-
- return torrents
-
@property
def name(self):
return self.__data.name
hash_sha1 = self.__file_hash(filename, "sha1")
filesize = os.path.getsize(filename)
- # Check if there is a torrent download available for this file:
- torrent_hash = ""
- torrent_file = "%s.torrent" % filename
- if os.path.exists(torrent_file):
- torrent_hash = self.torrent_read_hash(torrent_file)
-
self.db.execute("INSERT INTO files(releases, filename, filesize, \
- 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 \
- WHERE releases = %s AND torrent_hash IS NULL", self.id)
-
- for file in files:
- path = os.path.join(basepath, file.filename)
-
- torrent_file = "%s.torrent" % path
- if os.path.exists(torrent_file):
- torrent_hash = self.torrent_read_hash(torrent_file)
-
- self.db.execute("UPDATE files SET torrent_hash = %s WHERE id = %s",
- torrent_hash, file.id)
-
- def torrent_read_hash(self, filename):
- with open(filename, "rb") as f:
- metainfo = yabencode.decode(f)
- metainfo = yabencode.encode(metainfo["info"])
-
- h = hashlib.new("sha1")
- h.update(metainfo)
-
- return h.hexdigest()
+ sha256, sha1) VALUES(%s, %s, %s, %s, %s)",
+ self.id, _filename, filesize, hash_sha256, hash_sha1)
def supports_arch(self, arch):
return arch in ("x86_64", "i586")
return releases
- def get_file_for_torrent_hash(self, torrent_hash):
- file = self.db.get("SELECT id, releases FROM files WHERE torrent_hash = %s LIMIT 1",
- torrent_hash)
-
- if not file:
- return
-
- release = Release(self.backend, file.releases)
- file = File(self.backend, release, file.id)
-
- return file
-
async def scan_files(self, basepath="/pub/mirror"):
for release in self:
logging.debug("Scanning %s..." % release)