Skip to content

Instantly share code, notes, and snippets.

@omad
Created May 5, 2014 12:13
Show Gist options
  • Save omad/11535092 to your computer and use it in GitHub Desktop.
Save omad/11535092 to your computer and use it in GitHub Desktop.
Replace corrupted images that were imported into Lightroom, with originals from a memory stick.
#!/usr/bin/env python
#
# Damien Ayers 2014
#
# Replace corrupted images that were imported into Lightroom, with originals from a memory stick.
#
# Walk through a target directory structure of possibly corrupted files, and replace them
# with files from a source directory. Files in the source may be at completely different
# relative paths, but will have the same file name.
#
# Usage:
# replace_files <source_dir> <target_dir>
#
#
import sys
import os
import os.path
import hashlib
import shutil
source_dir = sys.argv[1]
target_dir = sys.argv[2]
BLOCKSIZE = 65536
def checksum_file(path):
hasher = hashlib.sha1()
with open(path, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
return hasher.hexdigest()
def find(name, path):
for root, dirs, files in os.walk(path):
if name in files:
return os.path.join(root, name)
def compare_sizes(source, target):
source_size = os.path.getsize(source)
target_size = os.path.getsize(target)
if source_size != target_size:
print "files don't match %s %s" % (source, target)
def checksums_match(source, target):
source_hash = checksum_file(source)
target_hash = checksum_file(target)
if source_hash != target_hash:
print "files don't match %s %s" % (source, target)
return False
return True
def fake_copy(source, target):
print "Copy %s onto %s" % (source, target)
def real_copy(source, target):
shutil.copy(source, target)
print "File replaced: %s" % target
def process_path(path):
checked = 0
replaced = 0
for root, dirs, files in os.walk(path):
for name in files:
if name in ('Thumbs.db', '.DS_Store'):
continue
target = os.path.join(root, name)
source = find(name, source_dir)
if not source:
print "ERROR: could not find source file for target: %s" % target
if not checksums_match(source, target):
replaced += 1
real_copy(source, target)
checked += 1
print "Checked %s files" % checked
print "Replaced %s files" % replaced
process_path(target_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment