From e890c121465333d48662be879db441e552281920 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 25 Mar 2010 06:10:16 +0100 Subject: Replaced multihash with pure-python ed2k-hash implementation. --- pyanidb/hash.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/pyanidb/hash.py b/pyanidb/hash.py index 4aab45f..9727ca2 100644 --- a/pyanidb/hash.py +++ b/pyanidb/hash.py @@ -1,9 +1,39 @@ -import multihash, threading, time, os +import threading, time, os, hashlib try: import xattr except ImportError: xattr = None +class Hash: + def __init__(self, filename, algorithms): + update_list = [getattr(self, 'update_%s' % a) for a in algorithms] + self.md4_partial = hashlib.new('md4') + self.md4_final = hashlib.new('md4') + self.size_total = 0 + f = open(filename) + data = f.read(131072) + while data: + for u in update_list: + u(data) + data = f.read(131072) + + def update_ed2k(self, data): + pos = 0 + while pos < len(data): + if (not (self.size_total % 9728000)) and self.size_total: + self.md4_final.update(self.md4_partial.digest()) + self.md4_partial = hashlib.new('md4') + size = min(len(data) - pos, 9728000 - (self.size_total % 9728000)) + self.md4_partial.update(data[pos:pos + size]) + pos += size + self.size_total += size + + def ed2k(self): + if self.size_total > 9728000: + self.md4_final.update(self.md4_partial.digest()) + return self.md4_final.hexdigest() + return self.md4_partial.hexdigest() + class File: def __init__(self, name, algorithms, cache): self.name = name @@ -14,7 +44,7 @@ class File: self.read_cache() if False in [hasattr(self, a) for a in algorithms]: self.cached = False - h = multihash.hash_file(name, algorithms) + h = Hash(name, algorithms) for a in algorithms: setattr(self, a, getattr(h, a)()) self.write_cache() -- cgit v1.2.3 From 8a7ddb39932fd34a2cae5fc556c1644244343ff2 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 11 Sep 2010 19:44:52 +0200 Subject: Skipping hidden files and directories while searching recursively. --- anidb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/anidb b/anidb index 81f6850..079bc07 100755 --- a/anidb +++ b/anidb @@ -6,6 +6,7 @@ warnings.filterwarnings('ignore', 'Python C API version mismatch for module _mul import pyanidb, pyanidb.hash import ConfigParser, optparse, os, sys, getpass +from collections import deque # Colors. @@ -81,7 +82,9 @@ if options.login: # Input files. files = [] -for name in args: +remaining = deque(args) +while remaining: + name = remaining.popleft() if not os.access(name, os.R_OK): print red('Invalid file:'), name elif os.path.isfile(name): @@ -90,10 +93,14 @@ for name in args: if not options.recursive: print red('Is a directory:'), name else: - for root, subdirs, subfiles in os.walk(name): - subdirs.sort() - subfiles.sort() - files += [os.path.join(root, file) for file in subfiles if True in [file.endswith('.' + suffix) for suffix in options.suffix]] + for sub in sorted(os.listdir(name)): + if sub.startswith('.'): + continue + sub = os.path.join(name, sub) + if os.path.isfile(name) and any(sub.endswith('.' + suffix) for suffix in options.suffix): + files.append(sub) + elif os.path.isdir(sub): + remaining.appendleft(sub) if not files: print blue('Nothing to do.') -- cgit v1.2.3