Refactor symlink code

Update variable names, favoring the terminology used by `ln`.
* source (original file containing data)
* target (the symlink file, pointing to source)
This commit is contained in:
Tim Byrne 2019-11-25 07:44:44 -06:00
parent 61576a6ae1
commit 5634c09a8a
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
3 changed files with 93 additions and 92 deletions

View File

@ -8,16 +8,16 @@ INIT_VARS = """
local_host=testhost local_host=testhost
local_user=testuser local_user=testuser
alt_scores=() alt_scores=()
alt_filenames=()
alt_targets=() alt_targets=()
alt_sources=()
alt_template_cmds=() alt_template_cmds=()
""" """
REPORT_RESULTS = """ REPORT_RESULTS = """
echo "SIZE:${#alt_scores[@]}" echo "SIZE:${#alt_scores[@]}"
echo "SCORES:${alt_scores[@]}" echo "SCORES:${alt_scores[@]}"
echo "FILENAMES:${alt_filenames[@]}"
echo "TARGETS:${alt_targets[@]}" echo "TARGETS:${alt_targets[@]}"
echo "SOURCES:${alt_sources[@]}"
""" """
@ -27,7 +27,7 @@ def test_dont_record_zeros(runner, yadm):
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
{INIT_VARS} {INIT_VARS}
record_score "0" "testfile" "testtarget" record_score "0" "testtgt" "testsrc"
{REPORT_RESULTS} {REPORT_RESULTS}
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)
@ -35,8 +35,8 @@ def test_dont_record_zeros(runner, yadm):
assert run.err == '' assert run.err == ''
assert 'SIZE:0\n' in run.out assert 'SIZE:0\n' in run.out
assert 'SCORES:\n' in run.out assert 'SCORES:\n' in run.out
assert 'FILENAMES:\n' in run.out
assert 'TARGETS:\n' in run.out assert 'TARGETS:\n' in run.out
assert 'SOURCES:\n' in run.out
def test_new_scores(runner, yadm): def test_new_scores(runner, yadm):
@ -45,9 +45,9 @@ def test_new_scores(runner, yadm):
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
{INIT_VARS} {INIT_VARS}
record_score "1" "file_one" "targ_one" record_score "1" "tgt_one" "src_one"
record_score "2" "file_two" "targ_two" record_score "2" "tgt_two" "src_two"
record_score "4" "file_three" "targ_three" record_score "4" "tgt_three" "src_three"
{REPORT_RESULTS} {REPORT_RESULTS}
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)
@ -55,8 +55,8 @@ def test_new_scores(runner, yadm):
assert run.err == '' assert run.err == ''
assert 'SIZE:3\n' in run.out assert 'SIZE:3\n' in run.out
assert 'SCORES:1 2 4\n' in run.out assert 'SCORES:1 2 4\n' in run.out
assert 'FILENAMES:file_one file_two file_three\n' in run.out assert 'TARGETS:tgt_one tgt_two tgt_three\n' in run.out
assert 'TARGETS:targ_one targ_two targ_three\n' in run.out assert 'SOURCES:src_one src_two src_three\n' in run.out
@pytest.mark.parametrize('difference', ['lower', 'equal', 'higher']) @pytest.mark.parametrize('difference', ['lower', 'equal', 'higher'])
@ -64,7 +64,7 @@ def test_existing_scores(runner, yadm, difference):
"""Test existing scores""" """Test existing scores"""
expected_score = '2' expected_score = '2'
expected_target = 'existing_target' expected_src = 'existing_src'
if difference == 'lower': if difference == 'lower':
score = '1' score = '1'
elif difference == 'equal': elif difference == 'equal':
@ -72,15 +72,15 @@ def test_existing_scores(runner, yadm, difference):
else: else:
score = '4' score = '4'
expected_score = '4' expected_score = '4'
expected_target = 'new_target' expected_src = 'new_src'
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
{INIT_VARS} {INIT_VARS}
alt_scores=(2) alt_scores=(2)
alt_filenames=("testfile") alt_targets=("testtgt")
alt_targets=("existing_target") alt_sources=("existing_src")
record_score "{score}" "testfile" "new_target" record_score "{score}" "testtgt" "new_src"
{REPORT_RESULTS} {REPORT_RESULTS}
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)
@ -88,21 +88,21 @@ def test_existing_scores(runner, yadm, difference):
assert run.err == '' assert run.err == ''
assert 'SIZE:1\n' in run.out assert 'SIZE:1\n' in run.out
assert f'SCORES:{expected_score}\n' in run.out assert f'SCORES:{expected_score}\n' in run.out
assert 'FILENAMES:testfile\n' in run.out assert 'TARGETS:testtgt\n' in run.out
assert f'TARGETS:{expected_target}\n' in run.out assert f'SOURCES:{expected_src}\n' in run.out
def test_existing_template(runner, yadm): def test_existing_template(runner, yadm):
"""Record nothing if a template command is registered for this file""" """Record nothing if a template command is registered for this target"""
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
{INIT_VARS} {INIT_VARS}
alt_scores=(1) alt_scores=(1)
alt_filenames=("testfile") alt_targets=("testtgt")
alt_targets=() alt_sources=()
alt_template_cmds=("existing_template") alt_template_cmds=("existing_template")
record_score "2" "testfile" "new_target" record_score "2" "testtgt" "new_src"
{REPORT_RESULTS} {REPORT_RESULTS}
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)
@ -110,5 +110,5 @@ def test_existing_template(runner, yadm):
assert run.err == '' assert run.err == ''
assert 'SIZE:1\n' in run.out assert 'SIZE:1\n' in run.out
assert 'SCORES:1\n' in run.out assert 'SCORES:1\n' in run.out
assert 'FILENAMES:testfile\n' in run.out assert 'TARGETS:testtgt\n' in run.out
assert 'TARGETS:\n' in run.out assert 'SOURCES:\n' in run.out

View File

@ -1,16 +1,16 @@
"""Unit tests: record_template""" """Unit tests: record_template"""
INIT_VARS = """ INIT_VARS = """
alt_filenames=()
alt_template_cmds=()
alt_targets=() alt_targets=()
alt_template_cmds=()
alt_sources=()
""" """
REPORT_RESULTS = """ REPORT_RESULTS = """
echo "SIZE:${#alt_filenames[@]}" echo "SIZE:${#alt_targets[@]}"
echo "FILENAMES:${alt_filenames[@]}" echo "TARGETS:${alt_targets[@]}"
echo "CMDS:${alt_template_cmds[@]}" echo "CMDS:${alt_template_cmds[@]}"
echo "TARGS:${alt_targets[@]}" echo "SOURCES:${alt_sources[@]}"
""" """
@ -20,18 +20,18 @@ def test_new_template(runner, yadm):
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
{INIT_VARS} {INIT_VARS}
record_template "file_one" "cmd_one" "targ_one" record_template "tgt_one" "cmd_one" "src_one"
record_template "file_two" "cmd_two" "targ_two" record_template "tgt_two" "cmd_two" "src_two"
record_template "file_three" "cmd_three" "targ_three" record_template "tgt_three" "cmd_three" "src_three"
{REPORT_RESULTS} {REPORT_RESULTS}
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)
assert run.success assert run.success
assert run.err == '' assert run.err == ''
assert 'SIZE:3\n' in run.out assert 'SIZE:3\n' in run.out
assert 'FILENAMES:file_one file_two file_three\n' in run.out assert 'TARGETS:tgt_one tgt_two tgt_three\n' in run.out
assert 'CMDS:cmd_one cmd_two cmd_three\n' in run.out assert 'CMDS:cmd_one cmd_two cmd_three\n' in run.out
assert 'TARGS:targ_one targ_two targ_three\n' in run.out assert 'SOURCES:src_one src_two src_three\n' in run.out
def test_existing_template(runner, yadm): def test_existing_template(runner, yadm):
@ -40,16 +40,16 @@ def test_existing_template(runner, yadm):
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
{INIT_VARS} {INIT_VARS}
alt_filenames=("testfile") alt_targets=("testtgt")
alt_template_cmds=("existing_cmd") alt_template_cmds=("existing_cmd")
alt_targets=("existing_targ") alt_sources=("existing_src")
record_template "testfile" "new_cmd" "new_targ" record_template "testtgt" "new_cmd" "new_src"
{REPORT_RESULTS} {REPORT_RESULTS}
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)
assert run.success assert run.success
assert run.err == '' assert run.err == ''
assert 'SIZE:1\n' in run.out assert 'SIZE:1\n' in run.out
assert 'FILENAMES:testfile\n' in run.out assert 'TARGETS:testtgt\n' in run.out
assert 'CMDS:new_cmd\n' in run.out assert 'CMDS:new_cmd\n' in run.out
assert 'TARGS:new_targ\n' in run.out assert 'SOURCES:new_src\n' in run.out

111
yadm
View File

@ -136,12 +136,12 @@ function main() {
# ****** Alternate Processing ****** # ****** Alternate Processing ******
function score_file() { function score_file() {
target="$1" src="$1"
filename="${target%%##*}" tgt="${src%%##*}"
conditions="${target#*##}" conditions="${src#*##}"
if [ "${filename#$YADM_ALT/}" != "${filename}" ]; then if [ "${tgt#$YADM_ALT/}" != "${tgt}" ]; then
filename="${YADM_WORK}/${filename#$YADM_ALT/}" tgt="${YADM_WORK}/${tgt#$YADM_ALT/}"
fi fi
score=0 score=0
@ -195,10 +195,10 @@ function score_file() {
score=0 score=0
cmd=$(choose_template_cmd "$value") cmd=$(choose_template_cmd "$value")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
record_template "$filename" "$cmd" "$target" record_template "$tgt" "$cmd" "$src"
else else
debug "No supported template processor for template $target" debug "No supported template processor for template $src"
[ -n "$loud" ] && echo "No supported template processor for template $target" [ -n "$loud" ] && echo "No supported template processor for template $src"
fi fi
return 0 return 0
# unsupported values # unsupported values
@ -208,30 +208,30 @@ function score_file() {
fi fi
done done
record_score "$score" "$filename" "$target" record_score "$score" "$tgt" "$src"
} }
function record_score() { function record_score() {
score="$1" score="$1"
filename="$2" tgt="$2"
target="$3" src="$3"
# record nothing if the score is zero # record nothing if the score is zero
[ "$score" -eq 0 ] && return [ "$score" -eq 0 ] && return
# search for the index of this filename, to see if we already are tracking it # search for the index of this target, to see if we already are tracking it
index=-1 index=-1
for search_index in "${!alt_filenames[@]}"; do for search_index in "${!alt_targets[@]}"; do
if [ "${alt_filenames[$search_index]}" = "$filename" ]; then if [ "${alt_targets[$search_index]}" = "$tgt" ]; then
index="$search_index" index="$search_index"
break break
fi fi
done done
# if we don't find an existing index, create one by appending to the array # if we don't find an existing index, create one by appending to the array
if [ "$index" -eq -1 ]; then if [ "$index" -eq -1 ]; then
alt_filenames+=("$filename") alt_targets+=("$tgt")
# set index to the last index (newly created one) # set index to the last index (newly created one)
for index in "${!alt_filenames[@]}"; do :; done for index in "${!alt_targets[@]}"; do :; done
# and set its initial score to zero # and set its initial score to zero
alt_scores[$index]=0 alt_scores[$index]=0
fi fi
@ -239,37 +239,37 @@ function record_score() {
# record nothing if a template command is registered for this file # record nothing if a template command is registered for this file
[ "${alt_template_cmds[$index]+isset}" ] && return [ "${alt_template_cmds[$index]+isset}" ] && return
# record higher scoring targets # record higher scoring sources
if [ "$score" -gt "${alt_scores[$index]}" ]; then if [ "$score" -gt "${alt_scores[$index]}" ]; then
alt_scores[$index]="$score" alt_scores[$index]="$score"
alt_targets[$index]="$target" alt_sources[$index]="$src"
fi fi
} }
function record_template() { function record_template() {
filename="$1" tgt="$1"
cmd="$2" cmd="$2"
target="$3" src="$3"
# search for the index of this filename, to see if we already are tracking it # search for the index of this target, to see if we already are tracking it
index=-1 index=-1
for search_index in "${!alt_filenames[@]}"; do for search_index in "${!alt_targets[@]}"; do
if [ "${alt_filenames[$search_index]}" = "$filename" ]; then if [ "${alt_targets[$search_index]}" = "$tgt" ]; then
index="$search_index" index="$search_index"
break break
fi fi
done done
# if we don't find an existing index, create one by appending to the array # if we don't find an existing index, create one by appending to the array
if [ "$index" -eq -1 ]; then if [ "$index" -eq -1 ]; then
alt_filenames+=("$filename") alt_targets+=("$tgt")
# set index to the last index (newly created one) # set index to the last index (newly created one)
for index in "${!alt_filenames[@]}"; do :; done for index in "${!alt_targets[@]}"; do :; done
fi fi
# record the template command, last one wins # record the template command, last one wins
alt_template_cmds[$index]="$cmd" alt_template_cmds[$index]="$cmd"
alt_targets[$index]="$target" alt_sources[$index]="$src"
} }
@ -445,15 +445,15 @@ function alt() {
function remove_stale_links() { function remove_stale_links() {
# review alternate candidates for stale links # review alternate candidates for stale links
# if a possible alt IS linked, but it's target is not part of alt_linked, # if a possible alt IS linked, but it's source is not part of alt_linked,
# remove it. # remove it.
if readlink_available; then if readlink_available; then
for stale_candidate in "${possible_alts[@]}"; do for stale_candidate in "${possible_alts[@]}"; do
if [ -L "$stale_candidate" ]; then if [ -L "$stale_candidate" ]; then
link_target=$(readlink "$stale_candidate" 2>/dev/null) src=$(readlink "$stale_candidate" 2>/dev/null)
if [ -n "$link_target" ]; then if [ -n "$src" ]; then
for review_link in "${alt_linked[@]}"; do for review_link in "${alt_linked[@]}"; do
[ "$link_target" = "$review_link" ] && continue 2 [ "$src" = "$review_link" ] && continue 2
done done
rm -f "$stale_candidate" rm -f "$stale_candidate"
fi fi
@ -489,12 +489,12 @@ function set_local_alt_values() {
function alt_future_linking() { function alt_future_linking() {
local alt_scores local alt_scores
local alt_filenames
local alt_targets local alt_targets
local alt_sources
local alt_template_cmds local alt_template_cmds
alt_scores=() alt_scores=()
alt_filenames=()
alt_targets=() alt_targets=()
alt_sources=()
alt_template_cmds=() alt_template_cmds=()
for alt_path in $(for tracked in "${tracked_files[@]}"; do printf "%s\n" "$tracked" "${tracked%/*}"; done | LC_ALL=C sort -u) "${ENCRYPT_INCLUDE_FILES[@]}"; do for alt_path in $(for tracked in "${tracked_files[@]}"; do printf "%s\n" "$tracked" "${tracked%/*}"; done | LC_ALL=C sort -u) "${ENCRYPT_INCLUDE_FILES[@]}"; do
@ -506,31 +506,31 @@ function alt_future_linking() {
fi fi
done done
for index in "${!alt_filenames[@]}"; do for index in "${!alt_targets[@]}"; do
filename="${alt_filenames[$index]}" tgt="${alt_targets[$index]}"
target="${alt_targets[$index]}" src="${alt_sources[$index]}"
template_cmd="${alt_template_cmds[$index]}" template_cmd="${alt_template_cmds[$index]}"
if [ -n "$template_cmd" ]; then if [ -n "$template_cmd" ]; then
# a template is defined, process the template # a template is defined, process the template
debug "Creating $filename from template $target" debug "Creating $tgt from template $src"
[ -n "$loud" ] && echo "Creating $filename from template $target" [ -n "$loud" ] && echo "Creating $tgt from template $src"
# ensure the destination path exists # ensure the destination path exists
assert_parent "$filename" assert_parent "$tgt"
# remove any existing symlink before processing template # remove any existing symlink before processing template
[ -L "$filename" ] && rm -f "$filename" [ -L "$tgt" ] && rm -f "$tgt"
"$template_cmd" "$target" "$filename" "$template_cmd" "$src" "$tgt"
elif [ -n "$target" ]; then elif [ -n "$src" ]; then
# a link target is defined, create symlink # a link source is defined, create symlink
debug "Linking $target to $filename" debug "Linking $src to $tgt"
[ -n "$loud" ] && echo "Linking $target to $filename" [ -n "$loud" ] && echo "Linking $src to $tgt"
# ensure the destination path exists # ensure the destination path exists
assert_parent "$filename" assert_parent "$tgt"
if [ "$do_copy" -eq 1 ]; then if [ "$do_copy" -eq 1 ]; then
# remove any existing symlink before copying # remove any existing symlink before copying
[ -L "$filename" ] && rm -f "$filename" [ -L "$tgt" ] && rm -f "$tgt"
cp -f "$target" "$filename" cp -f "$src" "$tgt"
else else
alt_ln "$target" "$filename" ln_relative "$src" "$tgt"
fi fi
fi fi
done done
@ -573,7 +573,7 @@ function alt_past_linking() {
fi fi
cp -f "$alt_path" "$new_link" cp -f "$alt_path" "$new_link"
else else
alt_ln "$alt_path" "$new_link" ln_relative "$alt_path" "$new_link"
fi fi
last_linked="$alt_path" last_linked="$alt_path"
fi fi
@ -609,12 +609,13 @@ function alt_past_linking() {
} }
function alt_ln() { function ln_relative() {
local full_link base local full_source full_target target_dir
full_link="$2" full_source="$1"
base="${full_link%/*}" full_target="$2"
rel_source=$(relative_path "$base" "$1") target_dir="${full_target%/*}"
ln -nfs "$rel_source" "$full_link" rel_source=$(relative_path "$target_dir" "$full_source")
ln -nfs "$rel_source" "$full_target"
alt_linked+=("$rel_source") alt_linked+=("$rel_source")
} }