]> git.ipfire.org Git - ipfire.org.git/blob - www/pages/source/__init__.py
Added source site.
[ipfire.org.git] / www / pages / source / __init__.py
1 #!/usr/bin/python
2
3 SOURCE_BASE="/srv/sources"
4 SOURCE_HASHES="/srv/www/ipfire.org/source/hashes.db"
5
6 SOURCE_URL="http://source.ipfire.org"
7
8 import os
9 import sha
10 from pysqlite2 import dbapi2 as sqlite
11
12 import web
13
14 class SourceObject:
15 def __init__(self, db, file):
16 self.file = file
17 self.name = os.path.basename(file)
18
19 if db:
20 self.db = db
21 else:
22 self.db = sqlite.connect(SOURCE_HASHES)
23 c = self.db.cursor()
24 c.execute("CREATE TABLE IF NOT EXISTS hashes(file, sha1)")
25 c.close()
26
27 def data(self):
28 f = open(self.file, "rb")
29 data = f.read()
30 f.close()
31 return data
32
33 def getHash(self, type="sha1"):
34 hash = None
35 c = self.db.cursor()
36 c.execute("SELECT %s FROM hashes WHERE file = '%s'" % (type, self.name,))
37 try:
38 hash = c.fetchone()[0]
39 except TypeError:
40 pass
41 c.close()
42
43 if not hash:
44 hash = sha.new(self.data()).hexdigest()
45 c = self.db.cursor()
46 c.execute("INSERT INTO hashes(file, sha1) VALUES('%s', '%s')" % \
47 (self.name, hash,))
48 c.close()
49 self.db.commit()
50 return hash
51
52
53 class Content(web.Content):
54 def __init__(self, name):
55 web.Content.__init__(self, name)
56
57 self.dirs = []
58
59 # Open database
60 db = sqlite.connect(SOURCE_HASHES)
61
62 for dir, subdirs, files in os.walk(SOURCE_BASE):
63 if not files:
64 continue
65 fileobjects = []
66 files.sort()
67 for file in files:
68 file = os.path.join(dir, file)
69 fileobjects.append(SourceObject(db, file))
70 self.dirs.append((os.path.basename(dir), fileobjects))
71
72 def __call__(self, lang):
73 ret = ""
74 self.w("<h3>IPFire Source Base</h3>")
75 for dir, files in self.dirs:
76 b = web.Box(dir)
77 b.w("<ul>")
78 for file in files:
79 b.w("""<li style="font-family: courier;">%(hash)s | <a href="%(url)s/%(dir)s/%(file)s">%(file)s</a></li>""" % \
80 { "file" : file.name,
81 "hash" : file.getHash() or "0000000000000000000000000000000000000000",
82 "dir" : dir,
83 "url" : SOURCE_URL, })
84 b.w("</ul>")
85 ret += b()
86 return ret
87
88 Sidebar = web.Sidebar
89