Skip to content

Commit 368a5f3

Browse files
committed
move to our nicer solution with abstractions
1 parent e1c55a2 commit 368a5f3

2 files changed

Lines changed: 85 additions & 50 deletions

File tree

‎sync.py‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,16 @@
44
from pathlib import Path
55

66
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)
7+
source_hashes = read_paths_and_hashes(source)
8+
dest_hashes = read_paths_and_hashes(dest)
9+
actions = determine_actions(source_hashes, dest_hashes, source, dest)
10+
for action, *paths in actions:
11+
if action == 'copy':
12+
shutil.copyfile(*paths)
13+
if action == 'move':
14+
shutil.move(*paths)
15+
if action == 'delete':
16+
os.remove(paths[0])
2717

2818

2919
BLOCKSIZE = 65536
@@ -37,3 +27,28 @@ def hash_file(path):
3727
buf = file.read(BLOCKSIZE)
3828
return hasher.hexdigest()
3929

30+
31+
def read_paths_and_hashes(root):
32+
hashes = {}
33+
for folder, _, files in os.walk(root):
34+
for fn in files:
35+
hashes[hash_file(Path(folder) / fn)] = fn
36+
return hashes
37+
38+
39+
def determine_actions(src_hashes, dst_hashes, src_folder, dst_folder):
40+
for sha, filename in src_hashes.items():
41+
if sha not in dst_hashes:
42+
sourcepath = Path(src_folder) / filename
43+
destpath = Path(dst_folder) / filename
44+
yield 'copy', sourcepath, destpath
45+
46+
elif dst_hashes[sha] != filename:
47+
olddestpath = Path(dst_folder) / dst_hashes[sha]
48+
newdestpath = Path(dst_folder) / filename
49+
yield 'move', olddestpath, newdestpath
50+
51+
for sha, filename in dst_hashes.items():
52+
if sha not in src_hashes:
53+
yield 'delete', dst_folder / filename
54+

‎test_sync.py‎

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
11
import tempfile
22
from pathlib import Path
33
import shutil
4-
from sync import sync
4+
from sync import sync, determine_actions
55

6-
def test_when_a_file_exists_in_the_source_but_not_the_destination():
7-
try:
8-
source = tempfile.mkdtemp()
9-
dest = tempfile.mkdtemp()
106

11-
content = "I am a very useful file"
12-
(Path(source) / 'my-file').write_text(content)
7+
class TestE2E:
138

14-
sync(source, dest)
9+
@staticmethod
10+
def test_when_a_file_exists_in_the_source_but_not_the_destination():
11+
try:
12+
source = tempfile.mkdtemp()
13+
dest = tempfile.mkdtemp()
1514

16-
expected_path = Path(dest) / 'my-file'
17-
assert expected_path.exists()
18-
assert expected_path.read_text() == content
15+
content = "I am a very useful file"
16+
(Path(source) / 'my-file').write_text(content)
1917

20-
finally:
21-
shutil.rmtree(source)
22-
shutil.rmtree(dest)
18+
sync(source, dest)
2319

20+
expected_path = Path(dest) / 'my-file'
21+
assert expected_path.exists()
22+
assert expected_path.read_text() == content
2423

25-
def test_when_a_file_has_been_renamed_in_the_source():
26-
try:
27-
source = tempfile.mkdtemp()
28-
dest = tempfile.mkdtemp()
24+
finally:
25+
shutil.rmtree(source)
26+
shutil.rmtree(dest)
2927

30-
content = "I am a file that was renamed"
31-
source_path = Path(source) / 'source-filename'
32-
old_dest_path = Path(dest) / 'dest-filename'
33-
expected_dest_path = Path(dest) / 'source-filename'
34-
source_path.write_text(content)
35-
old_dest_path.write_text(content)
28+
@staticmethod
29+
def test_when_a_file_has_been_renamed_in_the_source():
30+
try:
31+
source = tempfile.mkdtemp()
32+
dest = tempfile.mkdtemp()
3633

37-
sync(source, dest)
34+
content = "I am a file that was renamed"
35+
source_path = Path(source) / 'source-filename'
36+
old_dest_path = Path(dest) / 'dest-filename'
37+
expected_dest_path = Path(dest) / 'source-filename'
38+
source_path.write_text(content)
39+
old_dest_path.write_text(content)
3840

39-
assert old_dest_path.exists() is False
40-
assert expected_dest_path.read_text() == content
41+
sync(source, dest)
4142

43+
assert old_dest_path.exists() is False
44+
assert expected_dest_path.read_text() == content
4245

43-
finally:
44-
shutil.rmtree(source)
45-
shutil.rmtree(dest)
46+
finally:
47+
shutil.rmtree(source)
48+
shutil.rmtree(dest)
49+
50+
51+
class TestAction:
52+
53+
@staticmethod
54+
def test_when_a_file_exists_in_the_source_but_not_the_destination():
55+
src_hashes = {'hash1': 'fn1'}
56+
dst_hashes = {}
57+
actions = list(determine_actions(src_hashes, dst_hashes, Path('/src'), Path('/dst')))
58+
assert actions == [('copy', Path('/src/fn1'), Path('/dst/fn1'))]
59+
60+
@staticmethod
61+
def test_when_a_file_has_been_renamed_in_the_source():
62+
src_hashes = {'hash1': 'fn1'}
63+
dst_hashes = {'hash1': 'fn2'}
64+
actions = list(determine_actions(src_hashes, dst_hashes, Path('/src'), Path('/dst')))
65+
assert actions == [('move', Path('/dst/fn2'), Path('/dst/fn1'))]
4666

0 commit comments

Comments
 (0)