]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blobdiff - zfec/zfec/filefec.py
trivial: hush pyflakes
[tahoe-lafs/zfec.git] / zfec / zfec / filefec.py
index 35d64c5cb6c2a5e366efbb492a059a6c4f3d3dcf..682c387a951d822bda8d24bd17a3ad37184c82fe 100644 (file)
@@ -1,11 +1,22 @@
 import easyfec, zfec
-from util import fileutil
-from util.mathutil import log_ceil
+from pyutil import fileutil
+from pyutil.mathutil import pad_size, log_ceil
 
-import array, os, re, struct, traceback
+import array, os, struct
 
 CHUNKSIZE = 4096
 
+from base64 import b32encode
+def ab(x): # debuggery
+    if len(x) >= 3:
+        return "%s:%s" % (len(x), b32encode(x[-3:]),)
+    elif len(x) == 2:
+        return "%s:%s" % (len(x), b32encode(x[-2:]),)
+    elif len(x) == 1:
+        return "%s:%s" % (len(x), b32encode(x[-1:]),)
+    elif len(x) == 0:
+        return "%s:%s" % (len(x), "--empty--",)
+
 class InsufficientShareFilesError(zfec.Error):
     def __init__(self, k, kb, *args, **kwargs):
         zfec.Error.__init__(self, *args, **kwargs)
@@ -28,7 +39,7 @@ def _build_header(m, k, pad, sh):
     @param pad: the number of bytes of padding added to the file before encoding; 0 <= pad < k
     @param sh: the shnum of this share; 0 <= k < m
 
-    @return: a string (which is hopefully short) encoding m, k, sh, and pad
+    @return: a compressed string encoding m, k, pad, and sh
     """
     assert m >= 1
     assert m <= 2**8
@@ -160,7 +171,7 @@ def encode_to_files(inf, fsize, dirname, prefix, k, m, suffix=".fec", overwrite=
     mlen = len(str(m))
     format = FORMAT_FORMAT % (mlen, mlen,)
 
-    padbytes = zfec.util.mathutil.pad_size(fsize, k)
+    padbytes = pad_size(fsize, k)
 
     fns = []
     fs = []
@@ -216,9 +227,9 @@ def encode_to_files(inf, fsize, dirname, prefix, k, m, suffix=".fec", overwrite=
     return 0
 
 # Note: if you really prefer base-2 and you change this code, then please
-# denote 2^20 as "MiB" instead of "MB" in order to avoid ambiguity.
-# Thanks.
+# denote 2^20 as "MiB" instead of "MB" in order to avoid ambiguity.  See:
 # http://en.wikipedia.org/wiki/Megabyte
+# Thanks.
 MILLION_BYTES=10**6
 
 def decode_from_files(outf, infiles, verbose=False):
@@ -336,7 +347,14 @@ def encode_file(inf, cb, k, m, chunksize=4096):
         res = enc.encode(l)
         cb(res, indatasize)
 
-import sha
+try:
+    from hashlib import sha1
+    sha1 = sha1 # hush pyflakes
+except ImportError:
+    # hashlib was added in Python 2.5.0.
+    import sha
+    sha1 = sha
+
 def encode_file_not_really(inf, cb, k, m, chunksize=4096):
     enc = zfec.Encoder(k, m)
     l = tuple([ array.array('c') for i in range(k) ])
@@ -369,7 +387,7 @@ def encode_file_not_really(inf, cb, k, m, chunksize=4096):
         cb(None, None)
 
 def encode_file_not_really_and_hash(inf, cb, k, m, chunksize=4096):
-    hasher = sha.new()
+    hasher = sha1.new()
     enc = zfec.Encoder(k, m)
     l = tuple([ array.array('c') for i in range(k) ])
     indatasize = k*chunksize # will be reset to shorter upon EOF
@@ -482,15 +500,5 @@ def encode_file_stringy_easyfec(inf, cb, k, m, chunksize=4096):
 # Author: Zooko Wilcox-O'Hearn
 # 
 # This file is part of zfec.
-# 
-# This program is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the Free
-# Software Foundation; either version 2 of the License, or (at your option)
-# any later version, with the added permission that, if you become obligated
-# to release a derived work under this licence (as per section 2.b), you may
-# delay the fulfillment of this obligation for up to 12 months.  See the file
-# COPYING for details.
 #
-# If you would like to inquire about a commercial relationship with Allmydata,
-# Inc., please contact partnerships@allmydata.com and visit
-# http://allmydata.com/.
+# See README.txt for licensing information.