Skip to content

Commit 832bab7

Browse files
committed
alternative approach using DI and FakeFilesystem
1 parent ffbcf90 commit 832bab7

2 files changed

Lines changed: 63 additions & 40 deletions

File tree

‎sync.py‎

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,40 @@
33
import shutil
44
from pathlib import Path
55

6+
class FileSystem:
67

7-
def sync(source, dest):
8-
# imperative shell step 1, gather inputs
9-
source_hashes = read_paths_and_hashes(source)
10-
dest_hashes = read_paths_and_hashes(dest)
8+
def read(self, path):
9+
return read_paths_and_hashes(path)
1110

12-
# step 2: call functional core
13-
actions = determine_actions(source_hashes, dest_hashes, source, dest)
11+
def copy(self, source, dest):
12+
shutil.copyfile(source, dest)
13+
14+
def move(self, source, dest):
15+
shutil.move(source, dest)
16+
17+
def delete(self, dest):
18+
os.remove(dest)
19+
20+
21+
def sync(source, dest, filesystem=FileSystem()):
22+
source_hashes = filesystem.read(source)
23+
dest_hashes = filesystem.read(dest)
24+
25+
for sha, filename in source_hashes.items():
26+
if sha not in dest_hashes:
27+
sourcepath = Path(source) / filename
28+
destpath = Path(dest) / filename
29+
filesystem.copy(sourcepath, destpath)
30+
31+
elif dest_hashes[sha] != filename:
32+
olddestpath = Path(dest) / dest_hashes[sha]
33+
newdestpath = Path(dest) / filename
34+
filesystem.move(olddestpath, newdestpath)
35+
36+
for sha, filename in dest_hashes.items():
37+
if sha not in source_hashes:
38+
filesystem.delete(dest / filename)
1439

15-
# imperative shell step 3, apply outputs
16-
for action, *paths in actions:
17-
if action == "COPY":
18-
shutil.copyfile(*paths)
19-
if action == "MOVE":
20-
shutil.move(*paths)
21-
if action == "DELETE":
22-
os.remove(paths[0])
2340

2441

2542
BLOCKSIZE = 65536
@@ -41,20 +58,3 @@ def read_paths_and_hashes(root):
4158
for fn in files:
4259
hashes[hash_file(Path(folder) / fn)] = fn
4360
return hashes
44-
45-
46-
def determine_actions(source_hashes, dst_hashes, source_folder, dst_folder):
47-
for sha, filename in source_hashes.items():
48-
if sha not in dst_hashes:
49-
sourcepath = Path(source_folder) / filename
50-
destpath = Path(dst_folder) / filename
51-
yield "COPY", sourcepath, destpath
52-
53-
elif dst_hashes[sha] != filename:
54-
olddestpath = Path(dst_folder) / dst_hashes[sha]
55-
newdestpath = Path(dst_folder) / filename
56-
yield "MOVE", olddestpath, newdestpath
57-
58-
for sha, filename in dst_hashes.items():
59-
if sha not in source_hashes:
60-
yield "DELETE", dst_folder / filename

‎test_sync.py‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import tempfile
22
from pathlib import Path
33
import shutil
4-
from sync import sync, determine_actions
4+
from sync import FileSystem, sync
55

66

77
class TestE2E:
@@ -47,15 +47,38 @@ def test_when_a_file_has_been_renamed_in_the_source():
4747
shutil.rmtree(dest)
4848

4949

50+
class FakeFilesystem:
51+
def __init__(self, path_hashes):
52+
self.path_hashes = path_hashes
53+
self.actions = []
54+
55+
def read(self, path):
56+
return self.path_hashes[path]
57+
58+
def copy(self, src, dest):
59+
self.actions.append(('COPY', src, dest))
60+
61+
def move(self, src, dest):
62+
self.actions.append(('MOVE', src, dest))
63+
64+
def delete(self, dest):
65+
self.actions.append(('DELETE', dest))
66+
67+
68+
5069
def test_when_a_file_exists_in_the_source_but_not_the_destination():
51-
src_hashes = {"hash1": "fn1"}
52-
dst_hashes = {}
53-
actions = determine_actions(src_hashes, dst_hashes, Path("/src"), Path("/dst"))
54-
assert list(actions) == [("COPY", Path("/src/fn1"), Path("/dst/fn1"))]
70+
fakefs = FakeFilesystem({
71+
'/src': {"hash1": "fn1"},
72+
'/dst': {},
73+
})
74+
sync('/src', '/dst', filesystem=fakefs)
75+
assert fakefs.actions == [("COPY", Path("/src/fn1"), Path("/dst/fn1"))]
5576

5677

5778
def test_when_a_file_has_been_renamed_in_the_source():
58-
src_hashes = {"hash1": "fn1"}
59-
dst_hashes = {"hash1": "fn2"}
60-
actions = determine_actions(src_hashes, dst_hashes, Path("/src"), Path("/dst"))
61-
assert list(actions) == [("MOVE", Path("/dst/fn2"), Path("/dst/fn1"))]
79+
fakefs = FakeFilesystem({
80+
'/src': {"hash1": "fn1"},
81+
'/dst': {"hash1": "fn2"},
82+
})
83+
sync('/src', '/dst', filesystem=fakefs)
84+
assert fakefs.actions == [("MOVE", Path("/dst/fn2"), Path("/dst/fn1"))]

0 commit comments

Comments
 (0)