r120 - /debtorrent/trunk/hippy.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Wed Jun 20 23:47:21 UTC 2007


Author: camrdale-guest
Date: Wed Jun 20 23:47:21 2007
New Revision: 120

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=120
Log:
Added aj's updates to hippy to store the piece hashes in a bdb cache.

Modified:
    debtorrent/trunk/hippy.py

Modified: debtorrent/trunk/hippy.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/hippy.py?rev=120&op=diff
==============================================================================
--- debtorrent/trunk/hippy.py (original)
+++ debtorrent/trunk/hippy.py Wed Jun 20 23:47:21 2007
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-import sha
+import bsddb, sha, binascii
 import os, sys
 from gzip import GzipFile
 from StringIO import StringIO
@@ -33,52 +33,45 @@
 cache_file = sys.argv[1]
 pieces = {}
 
-if os.path.exists(cache_file):
-    try:
-        f = open(cache_file)
+cache = bsddb.btopen(cache_file, "w")
 
-        compressed = StringIO(f.read())
-        data = GzipFile(fileobj = compressed)
-        f = data.read().split('\n')
+def str2hash(s):
+    r = []
+    while len(s) > 0:
+        (l,h,s) = s[:4], s[4:24], s[24:]
+	r.append( (binascii.b2a_hex(h), long(binascii.b2a_hex(l), 16)) )
+    return r
 
-        p = [None, []]
-        read_data = False
-        for line in f:
-            line = line.rstrip()
-            if line == "":
-                if (p[0] and p[1]):
-                    pieces[p[0]] = p[1]
-                p = [None, []]
-                read_data = False
-            if read_data == True and line[:1] != " ":
-                read_data = False
-            if line[:9] == "Filename:":
-                p[0] = line[10:]
-            if line == "SHA1-Pieces:":
-                read_data = True
-            if read_data == True and line[:1] == " ":
-                p[1].append((line[1:41], long(line[42:])))
-    except:
-        pieces = {}
+def hash2str(hs):
+    s = ""
+    for (h, l) in hs:
+	s += binascii.a2b_hex("%08x" % l) + binascii.a2b_hex(h)
+    return s
 
-    try:
-        f.close()
-    except:
-        pass
+for filename in sys.stdin:
+    filename = filename.rstrip()
+    fnkey = filename + ":pc"
+    if cache.has_key(fnkey):
+    	result = str2hash(cache[fnkey])
+    else:
+    	size = os.stat(filename).st_size
+    	if size <= piecesize:
+		values = ""
+		result = []
+	else:
+        	ps = optimalpiecesize(size)
+        	file = open(filename)
+        	result = hash(file, ps)
+		values = hash2str(result)
+        	file.close()
+	cache[fnkey] = values
 
-for filename in sys.argv[2:]:
-    size = os.stat(filename).st_size
-    if size <= piecesize: continue
+    if result:
+    	print "Filename: %s" % (filename)
+    	print "SHA1-Pieces:"
+    	for x in result:
+            print " %s %d" % x
+    	print ""
 
-    print "Filename: %s" % (filename)
-    print "SHA1-Pieces:"
-    if pieces.has_key(filename):
-        y = pieces[filename]
-    else:
-        ps = optimalpiecesize(size)
-        file = open(filename)
-        y = hash(file, ps)
-        file.close()
-    for x in y:
-        print " %s %d" % x
-    print ""
+cache.sync()
+cache.close()




More information about the Debtorrent-commits mailing list