Skip to content

Commit 808e567

Browse files
committed
first cut with pathlib [sync_first_cut]
1 parent 4e74cfd commit 808e567

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎sync.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import hashlib
2+
import os
3+
import shutil
4+
from pathlib import Path
5+
6+
def sync(source, dest):
7+
source_hashes = {}
8+
for folder, _, files in os.walk(source):
9+
for fn in files:
10+
source_hashes[hash_file(Path(folder) / fn)] = fn
11+
12+
seen = set()
13+
for folder, _, files in os.walk(dest):
14+
for fn in files:
15+
dest_path = Path(folder) / fn
16+
dest_hash = hash_file(dest_path)
17+
seen.add(dest_hash)
18+
19+
if dest_hash not in source_hashes:
20+
dest_path.remove()
21+
elif dest_hash in source_hashes and fn != source_hashes[dest_hash]:
22+
shutil.move(dest_path, Path(folder) / source_hashes[dest_hash])
23+
24+
for src_hash, fn in source_hashes.items():
25+
if src_hash not in seen:
26+
shutil.copy(Path(source) / fn, Path(dest) / fn)
27+
28+
29+
BLOCKSIZE = 65536
30+
31+
def hash_file(path):
32+
hasher = hashlib.sha1()
33+
with path.open("rb") as file:
34+
buf = file.read(BLOCKSIZE)
35+
while buf:
36+
hasher.update(buf)
37+
buf = file.read(BLOCKSIZE)
38+
return hasher.hexdigest()
39+

0 commit comments

Comments
 (0)