Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """ferret_files.py: Recursively find and copy files with some file type."""
  4.  
  5. import hashlib
  6. import os
  7. import shutil
  8.  
  9. import argh
  10. import magic
  11.  
  12.  
  13. def get_matches(input_dir, target_type):
  14. for root, dirnames, filenames in os.walk(input_dir):
  15. for filename in filenames:
  16. file_path = '%s/%s' % (root, filename)
  17. if os.path.islink(file_path):
  18. print(file_path + " is link")
  19. # NICE TRY BRO
  20. continue
  21. the_type = magic.from_file(file_path)
  22. if the_type.lower().find(target_type) >= 0:
  23. yield file_path
  24.  
  25. @argh.arg('--target_type', default='mach-o', help='String to look for in magic file type')
  26. def main(input_dir, output_dir, target_type='mach-o', pretend=False):
  27. if not os.path.exists(output_dir):
  28. os.makedirs(output_dir)
  29.  
  30. for path in get_matches(input_dir, target_type):
  31. with open(path, 'rb') as f:
  32. sha256 = hashlib.sha256(f.read()).hexdigest()
  33. out_path = '%s/%s' % (output_dir, sha256)
  34. print("COPY SRC=%s\n DEST=%s" % (path, out_path))
  35. if not pretend:
  36. shutil.copyfile(path, out_path)
  37.  
  38.  
  39. if __name__ == '__main__':
  40. argh.dispatch_command(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement