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:
parent
61576a6ae1
commit
5634c09a8a
3 changed files with 93 additions and 92 deletions
|
@ -8,16 +8,16 @@ INIT_VARS = """
|
|||
local_host=testhost
|
||||
local_user=testuser
|
||||
alt_scores=()
|
||||
alt_filenames=()
|
||||
alt_targets=()
|
||||
alt_sources=()
|
||||
alt_template_cmds=()
|
||||
"""
|
||||
|
||||
REPORT_RESULTS = """
|
||||
echo "SIZE:${#alt_scores[@]}"
|
||||
echo "SCORES:${alt_scores[@]}"
|
||||
echo "FILENAMES:${alt_filenames[@]}"
|
||||
echo "TARGETS:${alt_targets[@]}"
|
||||
echo "SOURCES:${alt_sources[@]}"
|
||||
"""
|
||||
|
||||
|
||||
|
@ -27,7 +27,7 @@ def test_dont_record_zeros(runner, yadm):
|
|||
script = f"""
|
||||
YADM_TEST=1 source {yadm}
|
||||
{INIT_VARS}
|
||||
record_score "0" "testfile" "testtarget"
|
||||
record_score "0" "testtgt" "testsrc"
|
||||
{REPORT_RESULTS}
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
|
@ -35,8 +35,8 @@ def test_dont_record_zeros(runner, yadm):
|
|||
assert run.err == ''
|
||||
assert 'SIZE:0\n' in run.out
|
||||
assert 'SCORES:\n' in run.out
|
||||
assert 'FILENAMES:\n' in run.out
|
||||
assert 'TARGETS:\n' in run.out
|
||||
assert 'SOURCES:\n' in run.out
|
||||
|
||||
|
||||
def test_new_scores(runner, yadm):
|
||||
|
@ -45,9 +45,9 @@ def test_new_scores(runner, yadm):
|
|||
script = f"""
|
||||
YADM_TEST=1 source {yadm}
|
||||
{INIT_VARS}
|
||||
record_score "1" "file_one" "targ_one"
|
||||
record_score "2" "file_two" "targ_two"
|
||||
record_score "4" "file_three" "targ_three"
|
||||
record_score "1" "tgt_one" "src_one"
|
||||
record_score "2" "tgt_two" "src_two"
|
||||
record_score "4" "tgt_three" "src_three"
|
||||
{REPORT_RESULTS}
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
|
@ -55,8 +55,8 @@ def test_new_scores(runner, yadm):
|
|||
assert run.err == ''
|
||||
assert 'SIZE:3\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:targ_one targ_two targ_three\n' in run.out
|
||||
assert 'TARGETS:tgt_one tgt_two tgt_three\n' in run.out
|
||||
assert 'SOURCES:src_one src_two src_three\n' in run.out
|
||||
|
||||
|
||||
@pytest.mark.parametrize('difference', ['lower', 'equal', 'higher'])
|
||||
|
@ -64,7 +64,7 @@ def test_existing_scores(runner, yadm, difference):
|
|||
"""Test existing scores"""
|
||||
|
||||
expected_score = '2'
|
||||
expected_target = 'existing_target'
|
||||
expected_src = 'existing_src'
|
||||
if difference == 'lower':
|
||||
score = '1'
|
||||
elif difference == 'equal':
|
||||
|
@ -72,15 +72,15 @@ def test_existing_scores(runner, yadm, difference):
|
|||
else:
|
||||
score = '4'
|
||||
expected_score = '4'
|
||||
expected_target = 'new_target'
|
||||
expected_src = 'new_src'
|
||||
|
||||
script = f"""
|
||||
YADM_TEST=1 source {yadm}
|
||||
{INIT_VARS}
|
||||
alt_scores=(2)
|
||||
alt_filenames=("testfile")
|
||||
alt_targets=("existing_target")
|
||||
record_score "{score}" "testfile" "new_target"
|
||||
alt_targets=("testtgt")
|
||||
alt_sources=("existing_src")
|
||||
record_score "{score}" "testtgt" "new_src"
|
||||
{REPORT_RESULTS}
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
|
@ -88,21 +88,21 @@ def test_existing_scores(runner, yadm, difference):
|
|||
assert run.err == ''
|
||||
assert 'SIZE:1\n' in run.out
|
||||
assert f'SCORES:{expected_score}\n' in run.out
|
||||
assert 'FILENAMES:testfile\n' in run.out
|
||||
assert f'TARGETS:{expected_target}\n' in run.out
|
||||
assert 'TARGETS:testtgt\n' in run.out
|
||||
assert f'SOURCES:{expected_src}\n' in run.out
|
||||
|
||||
|
||||
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"""
|
||||
YADM_TEST=1 source {yadm}
|
||||
{INIT_VARS}
|
||||
alt_scores=(1)
|
||||
alt_filenames=("testfile")
|
||||
alt_targets=()
|
||||
alt_targets=("testtgt")
|
||||
alt_sources=()
|
||||
alt_template_cmds=("existing_template")
|
||||
record_score "2" "testfile" "new_target"
|
||||
record_score "2" "testtgt" "new_src"
|
||||
{REPORT_RESULTS}
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
|
@ -110,5 +110,5 @@ def test_existing_template(runner, yadm):
|
|||
assert run.err == ''
|
||||
assert 'SIZE:1\n' in run.out
|
||||
assert 'SCORES:1\n' in run.out
|
||||
assert 'FILENAMES:testfile\n' in run.out
|
||||
assert 'TARGETS:\n' in run.out
|
||||
assert 'TARGETS:testtgt\n' in run.out
|
||||
assert 'SOURCES:\n' in run.out
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
"""Unit tests: record_template"""
|
||||
|
||||
INIT_VARS = """
|
||||
alt_filenames=()
|
||||
alt_template_cmds=()
|
||||
alt_targets=()
|
||||
alt_template_cmds=()
|
||||
alt_sources=()
|
||||
"""
|
||||
|
||||
REPORT_RESULTS = """
|
||||
echo "SIZE:${#alt_filenames[@]}"
|
||||
echo "FILENAMES:${alt_filenames[@]}"
|
||||
echo "SIZE:${#alt_targets[@]}"
|
||||
echo "TARGETS:${alt_targets[@]}"
|
||||
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"""
|
||||
YADM_TEST=1 source {yadm}
|
||||
{INIT_VARS}
|
||||
record_template "file_one" "cmd_one" "targ_one"
|
||||
record_template "file_two" "cmd_two" "targ_two"
|
||||
record_template "file_three" "cmd_three" "targ_three"
|
||||
record_template "tgt_one" "cmd_one" "src_one"
|
||||
record_template "tgt_two" "cmd_two" "src_two"
|
||||
record_template "tgt_three" "cmd_three" "src_three"
|
||||
{REPORT_RESULTS}
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
assert run.success
|
||||
assert run.err == ''
|
||||
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 '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):
|
||||
|
@ -40,16 +40,16 @@ def test_existing_template(runner, yadm):
|
|||
script = f"""
|
||||
YADM_TEST=1 source {yadm}
|
||||
{INIT_VARS}
|
||||
alt_filenames=("testfile")
|
||||
alt_targets=("testtgt")
|
||||
alt_template_cmds=("existing_cmd")
|
||||
alt_targets=("existing_targ")
|
||||
record_template "testfile" "new_cmd" "new_targ"
|
||||
alt_sources=("existing_src")
|
||||
record_template "testtgt" "new_cmd" "new_src"
|
||||
{REPORT_RESULTS}
|
||||
"""
|
||||
run = runner(command=['bash'], inp=script)
|
||||
assert run.success
|
||||
assert run.err == ''
|
||||
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 'TARGS:new_targ\n' in run.out
|
||||
assert 'SOURCES:new_src\n' in run.out
|
||||
|
|
111
yadm
111
yadm
|
@ -136,12 +136,12 @@ function main() {
|
|||
# ****** Alternate Processing ******
|
||||
|
||||
function score_file() {
|
||||
target="$1"
|
||||
filename="${target%%##*}"
|
||||
conditions="${target#*##}"
|
||||
src="$1"
|
||||
tgt="${src%%##*}"
|
||||
conditions="${src#*##}"
|
||||
|
||||
if [ "${filename#$YADM_ALT/}" != "${filename}" ]; then
|
||||
filename="${YADM_WORK}/${filename#$YADM_ALT/}"
|
||||
if [ "${tgt#$YADM_ALT/}" != "${tgt}" ]; then
|
||||
tgt="${YADM_WORK}/${tgt#$YADM_ALT/}"
|
||||
fi
|
||||
|
||||
score=0
|
||||
|
@ -195,10 +195,10 @@ function score_file() {
|
|||
score=0
|
||||
cmd=$(choose_template_cmd "$value")
|
||||
if [ -n "$cmd" ]; then
|
||||
record_template "$filename" "$cmd" "$target"
|
||||
record_template "$tgt" "$cmd" "$src"
|
||||
else
|
||||
debug "No supported template processor for template $target"
|
||||
[ -n "$loud" ] && echo "No supported template processor for template $target"
|
||||
debug "No supported template processor for template $src"
|
||||
[ -n "$loud" ] && echo "No supported template processor for template $src"
|
||||
fi
|
||||
return 0
|
||||
# unsupported values
|
||||
|
@ -208,30 +208,30 @@ function score_file() {
|
|||
fi
|
||||
done
|
||||
|
||||
record_score "$score" "$filename" "$target"
|
||||
record_score "$score" "$tgt" "$src"
|
||||
}
|
||||
|
||||
function record_score() {
|
||||
score="$1"
|
||||
filename="$2"
|
||||
target="$3"
|
||||
tgt="$2"
|
||||
src="$3"
|
||||
|
||||
# record nothing if the score is zero
|
||||
[ "$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
|
||||
for search_index in "${!alt_filenames[@]}"; do
|
||||
if [ "${alt_filenames[$search_index]}" = "$filename" ]; then
|
||||
for search_index in "${!alt_targets[@]}"; do
|
||||
if [ "${alt_targets[$search_index]}" = "$tgt" ]; then
|
||||
index="$search_index"
|
||||
break
|
||||
fi
|
||||
done
|
||||
# if we don't find an existing index, create one by appending to the array
|
||||
if [ "$index" -eq -1 ]; then
|
||||
alt_filenames+=("$filename")
|
||||
alt_targets+=("$tgt")
|
||||
# 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
|
||||
alt_scores[$index]=0
|
||||
fi
|
||||
|
@ -239,37 +239,37 @@ function record_score() {
|
|||
# record nothing if a template command is registered for this file
|
||||
[ "${alt_template_cmds[$index]+isset}" ] && return
|
||||
|
||||
# record higher scoring targets
|
||||
# record higher scoring sources
|
||||
if [ "$score" -gt "${alt_scores[$index]}" ]; then
|
||||
alt_scores[$index]="$score"
|
||||
alt_targets[$index]="$target"
|
||||
alt_sources[$index]="$src"
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
function record_template() {
|
||||
filename="$1"
|
||||
tgt="$1"
|
||||
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
|
||||
for search_index in "${!alt_filenames[@]}"; do
|
||||
if [ "${alt_filenames[$search_index]}" = "$filename" ]; then
|
||||
for search_index in "${!alt_targets[@]}"; do
|
||||
if [ "${alt_targets[$search_index]}" = "$tgt" ]; then
|
||||
index="$search_index"
|
||||
break
|
||||
fi
|
||||
done
|
||||
# if we don't find an existing index, create one by appending to the array
|
||||
if [ "$index" -eq -1 ]; then
|
||||
alt_filenames+=("$filename")
|
||||
alt_targets+=("$tgt")
|
||||
# set index to the last index (newly created one)
|
||||
for index in "${!alt_filenames[@]}"; do :; done
|
||||
for index in "${!alt_targets[@]}"; do :; done
|
||||
fi
|
||||
|
||||
# record the template command, last one wins
|
||||
alt_template_cmds[$index]="$cmd"
|
||||
alt_targets[$index]="$target"
|
||||
alt_sources[$index]="$src"
|
||||
|
||||
}
|
||||
|
||||
|
@ -445,15 +445,15 @@ function alt() {
|
|||
|
||||
function remove_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.
|
||||
if readlink_available; then
|
||||
for stale_candidate in "${possible_alts[@]}"; do
|
||||
if [ -L "$stale_candidate" ]; then
|
||||
link_target=$(readlink "$stale_candidate" 2>/dev/null)
|
||||
if [ -n "$link_target" ]; then
|
||||
src=$(readlink "$stale_candidate" 2>/dev/null)
|
||||
if [ -n "$src" ]; then
|
||||
for review_link in "${alt_linked[@]}"; do
|
||||
[ "$link_target" = "$review_link" ] && continue 2
|
||||
[ "$src" = "$review_link" ] && continue 2
|
||||
done
|
||||
rm -f "$stale_candidate"
|
||||
fi
|
||||
|
@ -489,12 +489,12 @@ function set_local_alt_values() {
|
|||
function alt_future_linking() {
|
||||
|
||||
local alt_scores
|
||||
local alt_filenames
|
||||
local alt_targets
|
||||
local alt_sources
|
||||
local alt_template_cmds
|
||||
alt_scores=()
|
||||
alt_filenames=()
|
||||
alt_targets=()
|
||||
alt_sources=()
|
||||
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
|
||||
|
@ -506,31 +506,31 @@ function alt_future_linking() {
|
|||
fi
|
||||
done
|
||||
|
||||
for index in "${!alt_filenames[@]}"; do
|
||||
filename="${alt_filenames[$index]}"
|
||||
target="${alt_targets[$index]}"
|
||||
for index in "${!alt_targets[@]}"; do
|
||||
tgt="${alt_targets[$index]}"
|
||||
src="${alt_sources[$index]}"
|
||||
template_cmd="${alt_template_cmds[$index]}"
|
||||
if [ -n "$template_cmd" ]; then
|
||||
# a template is defined, process the template
|
||||
debug "Creating $filename from template $target"
|
||||
[ -n "$loud" ] && echo "Creating $filename from template $target"
|
||||
debug "Creating $tgt from template $src"
|
||||
[ -n "$loud" ] && echo "Creating $tgt from template $src"
|
||||
# ensure the destination path exists
|
||||
assert_parent "$filename"
|
||||
assert_parent "$tgt"
|
||||
# remove any existing symlink before processing template
|
||||
[ -L "$filename" ] && rm -f "$filename"
|
||||
"$template_cmd" "$target" "$filename"
|
||||
elif [ -n "$target" ]; then
|
||||
# a link target is defined, create symlink
|
||||
debug "Linking $target to $filename"
|
||||
[ -n "$loud" ] && echo "Linking $target to $filename"
|
||||
[ -L "$tgt" ] && rm -f "$tgt"
|
||||
"$template_cmd" "$src" "$tgt"
|
||||
elif [ -n "$src" ]; then
|
||||
# a link source is defined, create symlink
|
||||
debug "Linking $src to $tgt"
|
||||
[ -n "$loud" ] && echo "Linking $src to $tgt"
|
||||
# ensure the destination path exists
|
||||
assert_parent "$filename"
|
||||
assert_parent "$tgt"
|
||||
if [ "$do_copy" -eq 1 ]; then
|
||||
# remove any existing symlink before copying
|
||||
[ -L "$filename" ] && rm -f "$filename"
|
||||
cp -f "$target" "$filename"
|
||||
[ -L "$tgt" ] && rm -f "$tgt"
|
||||
cp -f "$src" "$tgt"
|
||||
else
|
||||
alt_ln "$target" "$filename"
|
||||
ln_relative "$src" "$tgt"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
@ -573,7 +573,7 @@ function alt_past_linking() {
|
|||
fi
|
||||
cp -f "$alt_path" "$new_link"
|
||||
else
|
||||
alt_ln "$alt_path" "$new_link"
|
||||
ln_relative "$alt_path" "$new_link"
|
||||
fi
|
||||
last_linked="$alt_path"
|
||||
fi
|
||||
|
@ -609,12 +609,13 @@ function alt_past_linking() {
|
|||
|
||||
}
|
||||
|
||||
function alt_ln() {
|
||||
local full_link base
|
||||
full_link="$2"
|
||||
base="${full_link%/*}"
|
||||
rel_source=$(relative_path "$base" "$1")
|
||||
ln -nfs "$rel_source" "$full_link"
|
||||
function ln_relative() {
|
||||
local full_source full_target target_dir
|
||||
full_source="$1"
|
||||
full_target="$2"
|
||||
target_dir="${full_target%/*}"
|
||||
rel_source=$(relative_path "$target_dir" "$full_source")
|
||||
ln -nfs "$rel_source" "$full_target"
|
||||
alt_linked+=("$rel_source")
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue