1
0
Fork 0
mirror of synced 2024-12-22 06:31:07 -05:00

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 max-statements=65
[SIMILARITIES] [SIMILARITIES]
min-similarity-lines=6 min-similarity-lines=7
[MESSAGES CONTROL] [MESSAGES CONTROL]
disable=redefined-outer-name disable=redefined-outer-name

View file

@ -74,6 +74,12 @@ def tst_sys():
return platform.system() return platform.system()
@pytest.fixture(scope='session')
def tst_arch():
"""Test session's uname value"""
return platform.machine()
@pytest.fixture(scope='session') @pytest.fixture(scope='session')
def supported_commands(): def supported_commands():
"""List of 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.usefixtures('ds1_copy')
@pytest.mark.parametrize('suffix', [ @pytest.mark.parametrize('suffix', [
'##default', '##default',
'##a.$tst_arch', '##arch.$tst_arch', '##architecture.$tst_arch',
'##o.$tst_sys', '##os.$tst_sys', '##o.$tst_sys', '##os.$tst_sys',
'##d.$tst_distro', '##distro.$tst_distro', '##d.$tst_distro', '##distro.$tst_distro',
'##c.$tst_class', '##class.$tst_class', '##c.$tst_class', '##class.$tst_class',
@ -89,7 +90,7 @@ def test_relative_link(runner, paths, yadm_alt):
]) ])
def test_alt_conditions( def test_alt_conditions(
runner, paths, 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""" """Test conditions supported by yadm alt"""
yadm_dir = setup_standard_yadm_dir(paths) yadm_dir = setup_standard_yadm_dir(paths)
@ -98,6 +99,7 @@ def test_alt_conditions(
utils.set_local(paths, 'class', tst_class) utils.set_local(paths, 'class', tst_class)
suffix = string.Template(suffix).substitute( suffix = string.Template(suffix).substitute(
tst_arch=tst_arch,
tst_sys=tst_sys, tst_sys=tst_sys,
tst_distro=tst_distro, tst_distro=tst_distro,
tst_class=tst_class, tst_class=tst_class,

View file

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

27
yadm
View file

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