]> git.ipfire.org Git - ipfire.org.git/commitdiff
Add a global hash database for source code files.
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jan 2010 15:52:20 +0000 (16:52 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jan 2010 15:52:20 +0000 (16:52 +0100)
www/webapp/__init__.py
www/webapp/db.py [new file with mode: 0644]
www/webapp/handlers.py

index 5dc0a66dc5fea177dc6a04c8ffae6441f2d59992..71a8e8b1295541b2b85b8b7800feb5ee220b19c5 100644 (file)
@@ -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 (file)
index 0000000..8d81614
--- /dev/null
@@ -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()
index e7e661b7925b9903261e8cb044b0474b4642dcc7..968068c06f666b52c86908f79c1bfac981dcd14f 100644 (file)
@@ -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)