Support architecture in alternates (#202)

This commit is contained in:
James Clark 2020-02-22 01:10:39 +00:00
parent 550a6b4340
commit 77d2da4e9b
No known key found for this signature in database
GPG Key ID: 980CD71DA927E089
6 changed files with 71 additions and 13 deletions

View File

@ -8,7 +8,7 @@ max-attributes=8
max-statements=65
[SIMILARITIES]
min-similarity-lines=6
min-similarity-lines=7
[MESSAGES CONTROL]
disable=redefined-outer-name

View File

@ -74,6 +74,12 @@ def tst_sys():
return platform.system()
@pytest.fixture(scope='session')
def tst_arch():
"""Test session's uname value"""
return platform.machine()
@pytest.fixture(scope='session')
def supported_commands():
"""List of supported commands

View File

@ -81,6 +81,7 @@ def test_relative_link(runner, paths, yadm_alt):
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.parametrize('suffix', [
'##default',
'##a.$tst_arch', '##arch.$tst_arch', '##architecture.$tst_arch',
'##o.$tst_sys', '##os.$tst_sys',
'##d.$tst_distro', '##distro.$tst_distro',
'##c.$tst_class', '##class.$tst_class',
@ -89,7 +90,7 @@ def test_relative_link(runner, paths, yadm_alt):
])
def test_alt_conditions(
runner, paths,
tst_sys, tst_distro, tst_host, tst_user, suffix):
tst_arch, tst_sys, tst_distro, tst_host, tst_user, suffix):
"""Test conditions supported by yadm alt"""
yadm_dir = setup_standard_yadm_dir(paths)
@ -98,6 +99,7 @@ def test_alt_conditions(
utils.set_local(paths, 'class', tst_class)
suffix = string.Template(suffix).substitute(
tst_arch=tst_arch,
tst_sys=tst_sys,
tst_distro=tst_distro,
tst_class=tst_class,

View File

@ -6,25 +6,29 @@ CONDITION = {
'labels': ['default'],
'modifier': 0,
},
'arch': {
'labels': ['a', 'arch', 'architecture'],
'modifier': 1,
},
'system': {
'labels': ['o', 'os'],
'modifier': 1,
'modifier': 2,
},
'distro': {
'labels': ['d', 'distro'],
'modifier': 2,
'modifier': 4,
},
'class': {
'labels': ['c', 'class'],
'modifier': 4,
'modifier': 8,
},
'hostname': {
'labels': ['h', 'hostname'],
'modifier': 8,
'modifier': 16,
},
'user': {
'labels': ['u', 'user'],
'modifier': 16,
'modifier': 32,
},
}
TEMPLATE_LABELS = ['t', 'template', 'yadm']
@ -44,6 +48,12 @@ def calculate_score(filename):
label, value = condition.split('.', 1)
if label in CONDITION['default']['labels']:
score += 1000
elif label in CONDITION['arch']['labels']:
if value == 'testarch':
score += 1000 + CONDITION['arch']['modifier']
else:
score = 0
break
elif label in CONDITION['system']['labels']:
if value == 'testsystem':
score += 1000 + CONDITION['system']['modifier']
@ -82,6 +92,8 @@ def calculate_score(filename):
@pytest.mark.parametrize(
'default', ['default', None], ids=['default', 'no-default'])
@pytest.mark.parametrize(
'arch', ['arch', None], ids=['arch', 'no-arch'])
@pytest.mark.parametrize(
'system', ['system', None], ids=['system', 'no-system'])
@pytest.mark.parametrize(
@ -93,10 +105,11 @@ def calculate_score(filename):
@pytest.mark.parametrize(
'user', ['user', None], ids=['user', 'no-user'])
def test_score_values(
runner, yadm, default, system, distro, cla, host, user):
runner, yadm, default, arch, system, distro, cla, host, user):
"""Test score results"""
# pylint: disable=too-many-branches
local_class = 'testclass'
local_arch = 'testarch'
local_system = 'testsystem'
local_distro = 'testdistro'
local_host = 'testhost'
@ -111,6 +124,18 @@ def test_score_values(
newfile += ','
newfile += label
filenames[newfile] = calculate_score(newfile)
if arch:
for filename in list(filenames):
for match in [True, False]:
for label in CONDITION[arch]['labels']:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
newfile += '.'.join([
label,
local_arch if match else 'badarch'
])
filenames[newfile] = calculate_score(newfile)
if system:
for filename in list(filenames):
for match in [True, False]:
@ -176,6 +201,7 @@ def test_score_values(
YADM_TEST=1 source {yadm}
score=0
local_class={local_class}
local_arch={local_arch}
local_system={local_system}
local_distro={local_distro}
local_host={local_host}
@ -199,6 +225,7 @@ def test_score_values(
def test_score_values_templates(runner, yadm):
"""Test score results"""
local_class = 'testclass'
local_arch = 'arch'
local_system = 'testsystem'
local_distro = 'testdistro'
local_host = 'testhost'
@ -217,6 +244,7 @@ def test_score_values_templates(runner, yadm):
YADM_TEST=1 source {yadm}
score=0
local_class={local_class}
local_arch={local_arch}
local_system={local_system}
local_distro={local_distro}
local_host={local_host}

27
yadm
View File

@ -175,37 +175,44 @@ function score_file() {
if [[ "$label" =~ ^(default)$ ]]; then
score=$((score + 0))
# variable conditions
elif [[ "$label" =~ ^(a|arch|architecture)$ ]]; then
if [ "$value" = "$local_arch" ]; then
score=$((score + 1))
else
score=0
return
fi
elif [[ "$label" =~ ^(o|os)$ ]]; then
if [ "$value" = "$local_system" ]; then
score=$((score + 1))
score=$((score + 2))
else
score=0
return
fi
elif [[ "$label" =~ ^(d|distro)$ ]]; then
if [ "$value" = "$local_distro" ]; then
score=$((score + 2))
score=$((score + 4))
else
score=0
return
fi
elif [[ "$label" =~ ^(c|class)$ ]]; then
if [ "$value" = "$local_class" ]; then
score=$((score + 4))
score=$((score + 8))
else
score=0
return
fi
elif [[ "$label" =~ ^(h|hostname)$ ]]; then
if [ "$value" = "$local_host" ]; then
score=$((score + 8))
score=$((score + 16))
else
score=0
return
fi
elif [[ "$label" =~ ^(u|user)$ ]]; then
if [ "$value" = "$local_user" ]; then
score=$((score + 16))
score=$((score + 32))
else
score=0
return
@ -367,6 +374,7 @@ EOF
"${AWK_PROGRAM[0]}" \
-v class="$local_class" \
-v arch="$local_arch" \
-v os="$local_system" \
-v host="$local_host" \
-v user="$local_user" \
@ -383,6 +391,7 @@ function template_j2cli() {
temp_file="${output}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
@ -398,6 +407,7 @@ function template_envtpl() {
temp_file="${output}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
@ -416,6 +426,7 @@ function alt() {
# gather values for processing alternates
local local_class
local local_arch
local local_system
local local_host
local local_user
@ -527,6 +538,11 @@ function set_local_alt_values() {
local_class="$(config local.class)"
local_arch="$(config local.arch)"
if [ -z "$local_arch" ] ; then
local_arch=$(uname -m)
fi
local_system="$(config local.os)"
if [ -z "$local_system" ] ; then
local_system="$OPERATING_SYSTEM"
@ -656,6 +672,7 @@ function alt_past_linking() {
[ -n "$loud" ] && echo "Creating $real_file from template $tracked_file"
temp_file="${real_file}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \

5
yadm.1
View File

@ -513,6 +513,11 @@ Valid if the value matches the OS.
OS is calculated by running
.BR "uname -s" .
.TP
.BR architecture , " arch" , " a
Valid if the value matches the architecture.
Architecture is calculated by running
.BR "uname -m" .
.TP
.BR class , " c
Valid if the value matches the
.B local.class