From 4cb3de1c2738ec864407a0378c6db5b4db3416d8 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 21 Jan 2010 16:52:20 +0100 Subject: [PATCH] Add a global hash database for source code files. --- www/webapp/__init__.py | 4 ++++ www/webapp/db.py | 48 ++++++++++++++++++++++++++++++++++++++++++ www/webapp/handlers.py | 29 ++++++------------------- 3 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 www/webapp/db.py diff --git a/www/webapp/__init__.py b/www/webapp/__init__.py index 5dc0a66d..71a8e8b1 100644 --- a/www/webapp/__init__.py +++ b/www/webapp/__init__.py @@ -9,6 +9,7 @@ import tornado.locale import tornado.options import tornado.web +from db import HashDatabase from handlers import * from ui_modules import * @@ -39,6 +40,9 @@ class Application(tornado.web.Application): 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)), diff --git a/www/webapp/db.py b/www/webapp/db.py new file mode 100644 index 00000000..8d816140 --- /dev/null +++ b/www/webapp/db.py @@ -0,0 +1,48 @@ +#!/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() diff --git a/www/webapp/handlers.py b/www/webapp/handlers.py index e7e661b7..968068c0 100644 --- a/www/webapp/handlers.py +++ b/www/webapp/handlers.py @@ -73,6 +73,9 @@ class BaseHandler(tornado.web.RequestHandler): 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): @@ -207,13 +210,6 @@ class TranslationHandler(BaseHandler): 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 = [] @@ -225,11 +221,9 @@ class SourceHandler(BaseHandler): 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({ @@ -239,21 +233,12 @@ class SourceHandler(BaseHandler): "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) @@ -277,9 +262,7 @@ class SourceDownloadHandler(BaseHandler): 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) -- 2.47.3