55
66
77def 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 ()
8+ # imperative shell step 1, gather inputs
9+ source_hashes = read_paths_and_hashes (source )
10+ dest_hashes = read_paths_and_hashes (dest )
2611
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 ])
12+ # step 2: call functional core
13+ actions = determine_actions (source_hashes , dest_hashes , source , dest )
3114
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 )
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 ])
3723
3824
3925BLOCKSIZE = 65536
@@ -47,3 +33,28 @@ def hash_file(path):
4733 hasher .update (buf )
4834 buf = file .read (BLOCKSIZE )
4935 return hasher .hexdigest ()
36+
37+
38+ def read_paths_and_hashes (root ):
39+ hashes = {}
40+ for folder , _ , files in os .walk (root ):
41+ for fn in files :
42+ hashes [hash_file (Path (folder ) / fn )] = fn
43+ 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
0 commit comments