From 92ff5cf8a8eead53ae450ac6ea0f6f60233afc06 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Sun, 10 Feb 2013 17:04:35 +0100 Subject: It's an executable, no need for extension --- rencon | 38 ++++++++++++++++++++++++++++++++++++++ rencon.py | 38 -------------------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) create mode 100755 rencon delete mode 100755 rencon.py diff --git a/rencon b/rencon new file mode 100755 index 0000000..a84ea09 --- /dev/null +++ b/rencon @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import os +import sys +from string import Template +import argparse +import hashlib + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Rename files based on content.') + + parser.add_argument('files', metavar='FILE', type=str, nargs='+', + help='Files to rename') + parser.add_argument('-m', '--mask', nargs='?', + help='File destination mask', default='${hash}.${ext}') + parser.add_argument('-p', '--pretend', action='store_true', + help='Do not rename, just print') + + args = parser.parse_args() + print 'Renaming with mask: %s' % args.mask + mask = Template(args.mask) + for f in args.files: + if not os.path.exists(f): + print >>sys.stderr, 'File %s does not exists.' % f + else: + with open(f) as fp: + h = hashlib.sha1(fp.read()).hexdigest() + ext = os.path.splitext(f)[1][1:] + name = mask.substitute(hash=h, ext=ext) + dest = os.path.join(os.path.dirname(f), name) + if os.path.basename(f) == name: + print 'OK %s' % name + else: + print "`%s' -> `%s'" % (f, dest) + if os.path.exists(dest): + print >>sys.stderr, 'Destination %s already exists.' % dest + elif not args.pretend: + os.rename(f, dest) diff --git a/rencon.py b/rencon.py deleted file mode 100755 index a84ea09..0000000 --- a/rencon.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python - -import os -import sys -from string import Template -import argparse -import hashlib - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description='Rename files based on content.') - - parser.add_argument('files', metavar='FILE', type=str, nargs='+', - help='Files to rename') - parser.add_argument('-m', '--mask', nargs='?', - help='File destination mask', default='${hash}.${ext}') - parser.add_argument('-p', '--pretend', action='store_true', - help='Do not rename, just print') - - args = parser.parse_args() - print 'Renaming with mask: %s' % args.mask - mask = Template(args.mask) - for f in args.files: - if not os.path.exists(f): - print >>sys.stderr, 'File %s does not exists.' % f - else: - with open(f) as fp: - h = hashlib.sha1(fp.read()).hexdigest() - ext = os.path.splitext(f)[1][1:] - name = mask.substitute(hash=h, ext=ext) - dest = os.path.join(os.path.dirname(f), name) - if os.path.basename(f) == name: - print 'OK %s' % name - else: - print "`%s' -> `%s'" % (f, dest) - if os.path.exists(dest): - print >>sys.stderr, 'Destination %s already exists.' % dest - elif not args.pretend: - os.rename(f, dest) -- cgit v1.2.3