aboutsummaryrefslogtreecommitdiffstats
path: root/rencon
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2014-08-25 19:37:47 +0200
committerLaurent Bachelier <laurent@bachelier.name>2014-08-25 19:37:47 +0200
commit0c1829f6653afb2621783615ef446503d796c268 (patch)
treee3695ff04f6a31f3482d0845b447fad0208411a4 /rencon
parentSupport big files (diff)
downloadrencon-0c1829f6653afb2621783615ef446503d796c268.tar.xz
Support changing algorithm
Diffstat (limited to 'rencon')
-rwxr-xr-xrencon12
1 files changed, 7 insertions, 5 deletions
diff --git a/rencon b/rencon
index e89abdb..89dcb88 100755
--- a/rencon
+++ b/rencon
@@ -17,12 +17,14 @@ def hashfile(hasher, fp, bs=2**16):
return h
-def rename(fpath, mask, out=sys.stdout, err=sys.stderr):
+def rename(fpath, mask, algorithm, out=sys.stdout, err=sys.stderr):
+ print("Renaming with mask: %s" % args.mask, file=out)
if not os.path.exists(fpath):
print("File %s does not exists." % fpath, file=err)
else:
with open(fpath) as fp:
- hsh = hashfile(hashlib.sha1, fp).hexdigest()
+ hasher = getattr(hashlib, algorithm)
+ hsh = hashfile(hasher, fp).hexdigest()
ext = os.path.splitext(fpath)[1][1:]
name = mask.substitute(hash=hsh, ext=ext)
dest = os.path.join(os.path.dirname(fpath), name)
@@ -47,9 +49,9 @@ if __name__ == '__main__':
help="file destination mask", default='${hash}.${ext}')
parser.add_argument('-p', '--pretend', action='store_true',
help="do not rename, just print")
+ parser.add_argument('-a', '--algorithm',
+ help="hash algorithm", choices=hashlib.algorithms, default='sha1')
args = parser.parse_args()
- print("Renaming with mask: %s" % args.mask)
- mask = Template(args.mask)
for fpath in args.files:
- rename(fpath, mask)
+ rename(fpath, Template(args.mask), args.algorithm)