import tornado.options
import tornado.web
+from db import HashDatabase
from handlers import *
from ui_modules import *
tornado.web.Application.__init__(self, **settings)
+ # Initialize database connections
+ self.hash_db = HashDatabase()
+
self.settings["static_path"] = static_path = os.path.join(BASEDIR, "static")
static_handlers = [
(r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = static_path)),
--- /dev/null
+#!/usr/bin/python
+
+import hashlib
+import sqlite3
+import os.path
+
+
+class HashDatabase(object):
+ def __init__(self):
+ self.conn = sqlite3.connect("/srv/www/ipfire.org/source/hashes.db")
+
+ def __del__(self):
+ self.conn.close()
+
+ def prepare(self):
+ c = self.conn.cursor()
+ c.execute("CREATE TABLE IF NOT EXISTS hashes(file, sha1)")
+ c.close()
+
+ def _save_hash(self, path, hash):
+ c = self.conn.cursor()
+ c.execute("INSERT INTO hashes VALUES('%s', '%s')" % (os.path.basename(path), hash))
+ c.close()
+
+ def get_hash(self, path):
+ c = self.conn.cursor()
+ c.execute("SELECT sha1 FROM hashes WHERE file = '%s'" % os.path.basename(path))
+
+ hash = c.fetchone()
+ c.close()
+
+ if not hash:
+ hash = self._calc_hash(path)
+ self._save_hash(path, hash)
+
+ if hash:
+ return "%s" % hash
+
+ def _calc_hash(self, path):
+ if not os.path.exists(path):
+ return
+
+ m = hashlib.sha1()
+ f = open(path)
+ m.update(f.read())
+ f.close()
+
+ return m.hexdigest()
else:
return tornado.web.RequestHandler.get_error_html(self, status_code, **kwargs)
+ @property
+ def hash_db(self):
+ return self.application.hash_db
class MainHandler(BaseHandler):
def get(self):
class SourceHandler(BaseHandler):
- def prepare(self):
- if not hasattr(self, "db"):
- self.db = sqlite3.connect("/srv/www/ipfire.org/source/hashes.db")
- c = self.db.cursor()
- c.execute("CREATE TABLE IF NOT EXISTS hashes(file, sha1)")
- c.close()
-
def get(self):
source_path = "/srv/sources"
fileobjects = []
if file in [f["name"] for f in fileobjects]:
continue
- c = self.db.cursor()
- c.execute("SELECT sha1 FROM hashes WHERE file = '%s'" % file)
- hash = "%s" % c.fetchone()
+ hash = self.hash_db.get_hash(file)
- if hash == "None":
+ if not hash:
hash = "0000000000000000000000000000000000000000"
fileobjects.append({
"size" : size(os.path.getsize(os.path.join(source_path, dir, file))),
})
- c.close()
-
fileobjects.sort(key=operator.itemgetter("name"))
self.render("sources.html", files=fileobjects)
class SourceDownloadHandler(BaseHandler):
- def prepare(self):
- if not hasattr(self, "db"):
- self.db = sqlite3.connect("/srv/www/ipfire.org/source/hashes.db")
- c = self.db.cursor()
- c.execute("CREATE TABLE IF NOT EXISTS hashes(file, sha1)")
- c.close()
-
def head(self, path):
self.get(path, include_body=False)
if mime_type:
self.set_header("Content-Type", mime_type)
- c = self.db.cursor()
- c.execute("SELECT sha1 FROM hashes WHERE file = '%s'" % os.path.basename(path))
- hash = c.fetchone()
+ hash = self.hash_db.get_hash(path)
if hash:
self.set_header("X-Hash-Sha1", "%s" % hash)