aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2014-08-24 16:49:11 +0200
committerLaurent Bachelier <laurent@bachelier.name>2014-08-24 16:49:11 +0200
commit429e66d145c3b3269299ad687d5013663f6b12d6 (patch)
tree2c463da6ee9cf7c8c1c2d7d940458d7f1bffeeb3
parentSeparate argument processing and renanimg (diff)
downloadrencon-429e66d145c3b3269299ad687d5013663f6b12d6.tar.xz
Support big files
-rwxr-xr-xrencon13
1 files changed, 11 insertions, 2 deletions
diff --git a/rencon b/rencon
index 9d2a9d8..e89abdb 100755
--- a/rencon
+++ b/rencon
@@ -8,14 +8,23 @@ import sys
from string import Template
+def hashfile(hasher, fp, bs=2**16):
+ h = hasher()
+ buf = fp.read(bs)
+ while len(buf):
+ h.update(buf)
+ buf = fp.read(bs)
+ return h
+
+
def rename(fpath, mask, out=sys.stdout, err=sys.stderr):
if not os.path.exists(fpath):
print("File %s does not exists." % fpath, file=err)
else:
with open(fpath) as fp:
- h = hashlib.sha1(fp.read()).hexdigest()
+ hsh = hashfile(hashlib.sha1, fp).hexdigest()
ext = os.path.splitext(fpath)[1][1:]
- name = mask.substitute(hash=h, ext=ext)
+ name = mask.substitute(hash=hsh, ext=ext)
dest = os.path.join(os.path.dirname(fpath), name)
if os.path.basename(fpath) == name:
print("OK %s" % name, file=out)