Skip to content

Commit ad2d1d5

Browse files
authored
Merge pull request github#3616 from dbartol/dbartol/sync-missing
Allow missing files in `sync-files --latest`
2 parents 22a651c + 0666a2e commit ad2d1d5

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

config/sync-files.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,41 @@ def file_checksum(filename):
5959
return hashlib.sha1(file_handle.read()).hexdigest()
6060

6161
def check_group(group_name, files, master_file_picker, emit_error):
62-
checksums = {file_checksum(f) for f in files}
62+
extant_files = [f for f in files if path.isfile(f)]
63+
if len(extant_files) == 0:
64+
emit_error(__file__, 0, "No files found from group '" + group_name + "'.")
65+
emit_error(__file__, 0,
66+
"Create one of the following files, and then run this script with "
67+
"the --latest switch to sync it to the other file locations.")
68+
for filename in files:
69+
emit_error(__file__, 0, " " + filename)
70+
return
71+
72+
checksums = {file_checksum(f) for f in extant_files}
6373

64-
if len(checksums) == 1:
74+
if len(checksums) == 1 and len(extant_files) == len(files):
75+
# All files are present and identical.
6576
return
6677

67-
master_file = master_file_picker(files)
78+
master_file = master_file_picker(extant_files)
6879
if master_file is None:
6980
emit_error(__file__, 0,
7081
"Files from group '"+ group_name +"' not in sync.")
7182
emit_error(__file__, 0,
7283
"Run this script with a file-name argument among the "
7384
"following to overwrite the remaining files with the contents "
74-
"of that file or run with the --latest switch to update each "
85+
"of that file, or run with the --latest switch to update each "
7586
"group of files from the most recently modified file in the group.")
76-
for filename in files:
87+
for filename in extant_files:
7788
emit_error(__file__, 0, " " + filename)
7889
else:
7990
print(" Syncing others from", master_file)
8091
for filename in files:
8192
if filename == master_file:
8293
continue
8394
print(" " + filename)
84-
os.replace(filename, filename + '~')
95+
if path.isfile(filename):
96+
os.replace(filename, filename + '~')
8597
shutil.copy(master_file, filename)
8698
print(" Backups written with '~' appended to file names")
8799

0 commit comments

Comments
 (0)