Skip to content

Commit

Permalink
Allow missing files in sync-files --latest
Browse files Browse the repository at this point in the history
When running `sync-files` (or `sync-identical-files`) with the `--latest` switch, if one or more of the files in a group does not exist, the script will crash. This happens all the time when I add a new group, or add a new file path in an existing group. This has bothered me for a long time, so I finally fixed it when I ran into it again today.

I've changed the script as follows:
- If _none_ of the paths in the group exist, print an error message listing the paths in the group. This happens with or without `--latest`.
- If `--latest` is specified, copy the master file to the paths of the missing files.
  • Loading branch information
dbartol committed Jun 3, 2020
1 parent 9c50acc commit a18eba2
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions config/sync-files.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,41 @@ def file_checksum(filename):
return hashlib.sha1(file_handle.read()).hexdigest()

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

checksums = {file_checksum(f) for f in extant_files}

if len(checksums) == 1:
if len(checksums) == 1 and len(extant_files) == len(files):
# All files are present and identical.
return

master_file = master_file_picker(files)
master_file = master_file_picker(extant_files)
if master_file is None:
emit_error(__file__, 0,
"Files from group '"+ group_name +"' not in sync.")
emit_error(__file__, 0,
"Run this script with a file-name argument among the "
"following to overwrite the remaining files with the contents "
"of that file or run with the --latest switch to update each "
"of that file, or run with the --latest switch to update each "
"group of files from the most recently modified file in the group.")
for filename in files:
for filename in extant_files:
emit_error(__file__, 0, " " + filename)
else:
print(" Syncing others from", master_file)
for filename in files:
if filename == master_file:
continue
print(" " + filename)
os.replace(filename, filename + '~')
if path.isfile(filename):
os.replace(filename, filename + '~')
shutil.copy(master_file, filename)
print(" Backups written with '~' appended to file names")

Expand Down

0 comments on commit a18eba2

Please sign in to comment.