Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. from hashlib import sha1 as hasher
  5. import os
  6. import sys
  7. from multiprocessing import Pool, Manager
  8.  
  9.  
  10. active = True
  11.  
  12.  
  13. def get_hash(q):
  14. h = hasher()
  15. while active or not q.empty():
  16. with open(q.get(), 'rb') as f:
  17. for chunk in iter(lambda: f.read(2047), b''):
  18. h.update(chunk)
  19. print('{}\t{}'.format(path, h.hexdigest()))
  20.  
  21.  
  22. def find(target):
  23. if os.path.isdir(target):
  24. for root, dirs, files in os.walk(target):
  25. for file in files:
  26. yield os.path.join(root, file)
  27. else:
  28. yield target
  29.  
  30.  
  31. def main():
  32. if len(sys.argv) < 2:
  33. sys.exit(1)
  34. for target in sys.argv[1:]:
  35. with Pool(processes=4) as pool:
  36. q = Manager().Queue()
  37. pool.map(get_hash, (q,))
  38. for path in find(target):
  39. q.put(path)
  40. active = False
  41.  
  42.  
  43. if __name__ == '__main__':
  44. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement