Merge pull request #203 from jameshclrk/arch-alternative

This commit is contained in:
Tim Byrne 2021-12-21 16:50:41 -06:00
commit 85e05d311a
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
6 changed files with 70 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

@ -82,6 +82,7 @@ def test_relative_link(runner, paths, yadm_alt):
@pytest.mark.parametrize('suffix', [
'##default',
'##default,e.txt', '##default,extension.txt',
'##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',
@ -90,7 +91,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, yadm_data = setup_standard_yadm_dir(paths)
@ -99,6 +100,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}
@ -221,6 +247,7 @@ def test_extensions(runner, yadm, ext):
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'
@ -239,6 +266,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}

26
yadm
View File

@ -189,37 +189,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
@ -424,6 +431,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" \
@ -442,6 +450,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" \
@ -458,6 +467,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" \
@ -509,6 +519,7 @@ function alt() {
# gather values for processing alternates
local local_class
local local_arch
local local_system
local local_host
local local_user
@ -609,6 +620,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"

5
yadm.1
View File

@ -490,6 +490,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