|
| 1 | +import hashlib |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def sync(source, dest): |
| 8 | + # Walk the source folder and build a dict of filenames and their hashes |
| 9 | + source_hashes = {} |
| 10 | + for folder, _, files in os.walk(source): |
| 11 | + for fn in files: |
| 12 | + source_hashes[hash_file(Path(folder) / fn)] = fn |
| 13 | + |
| 14 | + seen = set() # Keep track of the files we've found in the target |
| 15 | + |
| 16 | + # Walk the target folder and get the filenames and hashes |
| 17 | + for folder, _, files in os.walk(dest): |
| 18 | + for fn in files: |
| 19 | + dest_path = Path(folder) / fn |
| 20 | + dest_hash = hash_file(dest_path) |
| 21 | + seen.add(dest_hash) |
| 22 | + |
| 23 | + # if there's a file in target that's not in source, delete it |
| 24 | + if dest_hash not in source_hashes: |
| 25 | + dest_path.remove() |
| 26 | + |
| 27 | + # if there's a file in target that has a different path in source, |
| 28 | + # move it to the correct path |
| 29 | + elif dest_hash in source_hashes and fn != source_hashes[dest_hash]: |
| 30 | + shutil.move(dest_path, Path(folder) / source_hashes[dest_hash]) |
| 31 | + |
| 32 | + # for every file that appears in source but not target, copy the file to |
| 33 | + # the target |
| 34 | + for source_hash, fn in source_hashes.items(): |
| 35 | + if source_hash not in seen: |
| 36 | + shutil.copy(Path(source) / fn, Path(dest) / fn) |
| 37 | + |
| 38 | + |
| 39 | +BLOCKSIZE = 65536 |
| 40 | + |
| 41 | + |
| 42 | +def hash_file(path): |
| 43 | + hasher = hashlib.sha1() |
| 44 | + with path.open("rb") as file: |
| 45 | + buf = file.read(BLOCKSIZE) |
| 46 | + while buf: |
| 47 | + hasher.update(buf) |
| 48 | + buf = file.read(BLOCKSIZE) |
| 49 | + return hasher.hexdigest() |
0 commit comments