r93 - /debtorrent/branches/hippy/hippy.py
camrdale-guest at users.alioth.debian.org
camrdale-guest at users.alioth.debian.org
Fri Jun 8 22:35:44 UTC 2007
Author: camrdale-guest
Date: Fri Jun 8 22:35:44 2007
New Revision: 93
URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=93
Log:
Added hippy file to generate -extrapieces files.
Added:
debtorrent/branches/hippy/hippy.py (with props)
Added: debtorrent/branches/hippy/hippy.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/hippy/hippy.py?rev=93&op=file
==============================================================================
--- debtorrent/branches/hippy/hippy.py (added)
+++ debtorrent/branches/hippy/hippy.py Fri Jun 8 22:35:44 2007
@@ -1,0 +1,84 @@
+#!/usr/bin/env python
+
+import sha
+import os, sys
+from gzip import GzipFile
+from StringIO import StringIO
+
+def hash(file, piecesize):
+ h = []
+ while 1:
+ x = file.read(piecesize)
+ if x == "": break
+ h.append((sha.new(x).hexdigest(), len(x)))
+ return h
+
+piecesize = 512*1024
+chunksize = 16*1024
+
+def optimalpiecesize(size):
+ def eval(s,c,m):
+ b = m/c
+ return [ i*c for i in range(int(b/2), b+1) if s - i*c*int(s/m) <= i*c ]
+
+ def score(s,c,m):
+ l = int(s/m)
+ return [ (abs(i - (s - l*i)), i) for i in eval(s,c,m) ]
+
+ def bestest(s,c,m):
+ return min( score(s,c,m) )
+
+ return bestest(size,chunksize,piecesize)[1]
+
+cache_file = sys.argv[1]
+pieces = {}
+
+if os.path.exists(cache_file):
+ try:
+ f = open(cache_file)
+
+ compressed = StringIO(f.read())
+ data = GzipFile(fileobj = compressed)
+ f = data.read().split('\n')
+
+ 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 = {}
+
+ try:
+ f.close()
+ except:
+ pass
+
+for filename in sys.argv[2:]:
+ size = os.stat(filename).st_size
+ if size <= piecesize: continue
+
+ 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 ""
Propchange: debtorrent/branches/hippy/hippy.py
------------------------------------------------------------------------------
svn:executable = *
More information about the Debtorrent-commits
mailing list