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

Rename the alt variables local_* to YADM_* and export them

local_class -> YADM_CLASS
local_system -> YADM_OS
local_host -> YADM_HOSTNAME
local_user -> YADM_USER
local_distro -> YADM_DISTRO

The new names match the names used for j2/esh templates.
This commit is contained in:
Erik Flodin 2021-01-17 22:00:39 +01:00 committed by Erik Flodin
parent 2f00dabcdb
commit 650644cfb8
No known key found for this signature in database
GPG key ID: 420A7C865EE3F85F
7 changed files with 119 additions and 141 deletions

View file

@ -2,11 +2,6 @@
import pytest
INIT_VARS = """
score=0
local_class=testclass
local_system=testsystem
local_host=testhost
local_user=testuser
alt_scores=()
alt_targets=()
alt_sources=()

View file

@ -10,7 +10,7 @@ CONDITION = {
'labels': ['a', 'arch', 'architecture'],
'modifier': 1,
},
'system': {
'os': {
'labels': ['o', 'os'],
'modifier': 2,
},
@ -54,9 +54,9 @@ def calculate_score(filename):
else:
score = 0
break
elif label in CONDITION['system']['labels']:
if value == 'testsystem':
score += 1000 + CONDITION['system']['modifier']
elif label in CONDITION['os']['labels']:
if value == 'testos':
score += 1000 + CONDITION['os']['modifier']
else:
score = 0
break
@ -95,7 +95,7 @@ def calculate_score(filename):
@pytest.mark.parametrize(
'arch', ['arch', None], ids=['arch', 'no-arch'])
@pytest.mark.parametrize(
'system', ['system', None], ids=['system', 'no-system'])
'system', ['os', None], ids=['os', 'no-os'])
@pytest.mark.parametrize(
'distro', ['distro', None], ids=['distro', 'no-distro'])
@pytest.mark.parametrize(
@ -110,9 +110,9 @@ def test_score_values(
# pylint: disable=too-many-branches
local_class = 'testclass'
local_arch = 'testarch'
local_system = 'testsystem'
local_os = 'testos'
local_distro = 'testdistro'
local_host = 'testhost'
local_hostname = 'testhost'
local_user = 'testuser'
filenames = {'filename##': 0}
@ -145,7 +145,7 @@ def test_score_values(
newfile += ','
newfile += '.'.join([
label,
local_system if match else 'badsys'
local_os if match else 'bados'
])
filenames[newfile] = calculate_score(newfile)
if distro:
@ -181,7 +181,7 @@ def test_score_values(
newfile += ','
newfile += '.'.join([
label,
local_host if match else 'badhost'
local_hostname if match else 'badhost'
])
filenames[newfile] = calculate_score(newfile)
if user:
@ -200,12 +200,12 @@ def test_score_values(
script = f"""
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}
local_user={local_user}
YADM_CLASS={local_class}
YADM_ARCH={local_arch}
YADM_OS={local_os}
YADM_DISTRO={local_distro}
YADM_HOSTNAME={local_hostname}
YADM_USER={local_user}
"""
expected = ''
for filename in filenames:
@ -233,7 +233,7 @@ def test_extensions(runner, yadm, ext):
script = f"""
YADM_TEST=1 source {yadm}
score=0
local_user={local_user}
YADM_USER={local_user}
score_file "{filename}"
echo "$score"
"""
@ -248,9 +248,9 @@ def test_score_values_templates(runner, yadm):
"""Test score results"""
local_class = 'testclass'
local_arch = 'arch'
local_system = 'testsystem'
local_os = 'testos'
local_distro = 'testdistro'
local_host = 'testhost'
local_hostname = 'testhost'
local_user = 'testuser'
filenames = {'filename##': 0}
@ -265,12 +265,12 @@ def test_score_values_templates(runner, yadm):
script = f"""
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}
local_user={local_user}
YADM_CLASS={local_class}
YADM_ARCH={local_arch}
YADM_OS={local_os}
YADM_DISTRO={local_distro}
YADM_HOSTNAME={local_hostname}
YADM_USER={local_user}
"""
expected = ''
for filename in filenames:

View file

@ -1,4 +1,4 @@
"""Unit tests: set_local_alt_values"""
"""Unit tests: set_alt_values"""
import pytest
import utils
@ -20,18 +20,18 @@ import utils
]
)
@pytest.mark.usefixtures('ds1_copy')
def test_set_local_alt_values(
def test_set_alt_values(
runner, yadm, paths, tst_sys, tst_host, tst_user, override):
"""Use issue_legacy_path_warning"""
script = f"""
YADM_TEST=1 source {yadm} &&
set_operating_system &&
YADM_DIR={paths.yadm} YADM_DATA={paths.data} configure_paths &&
set_local_alt_values
echo "class='$local_class'"
echo "os='$local_system'"
echo "host='$local_host'"
echo "user='$local_user'"
set_alt_values
echo "class='$YADM_CLASS'"
echo "os='$YADM_OS'"
echo "host='$YADM_HOSTNAME'"
echo "user='$YADM_USER'"
"""
if override:
@ -69,8 +69,8 @@ def test_distro(runner, yadm):
YADM_TEST=1 source {yadm}
function config() {{ echo "$1"; }}
function query_distro() {{ echo "testdistro"; }}
set_local_alt_values
echo "distro='$local_distro'"
set_alt_values
echo "distro='$YADM_DISTRO'"
"""
run = runner(command=['bash'], inp=script)
assert run.success

View file

@ -6,8 +6,8 @@ FILE_MODE = 0o754
# these values are also testing the handling of bizarre characters
LOCAL_CLASS = "default_Test+@-!^Class"
LOCAL_ARCH = "default_Test+@-!^Arch"
LOCAL_SYSTEM = "default_Test+@-!^System"
LOCAL_HOST = "default_Test+@-!^Host"
LOCAL_OS = "default_Test+@-!^System"
LOCAL_HOSTNAME = "default_Test+@-!^Host"
LOCAL_USER = "default_Test+@-!^User"
LOCAL_DISTRO = "default_Test+@-!^Distro"
TEMPLATE = f'''
@ -47,7 +47,7 @@ wrong arch 2
{{% if yadm.os == "wrongos1" %}}
wrong os 1
{{% endif %}}
{{% if yadm.os == "{LOCAL_SYSTEM}" %}}
{{% if yadm.os == "{LOCAL_OS}" %}}
Included section for os = {{{{yadm.os}}}} ({{{{yadm.os}}}} repeated)
{{% endif %}}
{{% if yadm.os == "wrongos2" %}}
@ -56,7 +56,7 @@ wrong os 2
{{% if yadm.hostname == "wronghost1" %}}
wrong host 1
{{% endif %}}
{{% if yadm.hostname == "{LOCAL_HOST}" %}}
{{% if yadm.hostname == "{LOCAL_HOSTNAME}" %}}
Included section for host = {{{{yadm.hostname}}}} ({{{{yadm.hostname}}}} again)
{{% endif %}}
{{% if yadm.hostname == "wronghost2" %}}
@ -86,16 +86,16 @@ EXPECTED = f'''
start of template
default class = >{LOCAL_CLASS}<
default arch = >{LOCAL_ARCH}<
default os = >{LOCAL_SYSTEM}<
default host = >{LOCAL_HOST}<
default os = >{LOCAL_OS}<
default host = >{LOCAL_HOSTNAME}<
default user = >{LOCAL_USER}<
default distro = >{LOCAL_DISTRO}<
Included section from else
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
Multiple lines
Included section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
Included section for os = {LOCAL_OS} ({LOCAL_OS} repeated)
Included section for host = {LOCAL_HOSTNAME} ({LOCAL_HOSTNAME} again)
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
end of template
@ -149,12 +149,12 @@ def test_template_default(runner, yadm, tmpdir):
script = f"""
YADM_TEST=1 source {yadm}
set_awk
local_class="{LOCAL_CLASS}"
local_arch="{LOCAL_ARCH}"
local_system="{LOCAL_SYSTEM}"
local_host="{LOCAL_HOST}"
local_user="{LOCAL_USER}"
local_distro="{LOCAL_DISTRO}"
YADM_CLASS="{LOCAL_CLASS}"
YADM_ARCH="{LOCAL_ARCH}"
YADM_OS="{LOCAL_OS}"
YADM_HOSTNAME="{LOCAL_HOSTNAME}"
YADM_USER="{LOCAL_USER}"
YADM_DISTRO="{LOCAL_DISTRO}"
template_default "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
@ -193,7 +193,7 @@ def test_include(runner, yadm, tmpdir):
basic_file = tmpdir.join('basic')
basic_file.write(INCLUDE_BASIC)
variables_file = tmpdir.join(f'variables.{LOCAL_SYSTEM}')
variables_file = tmpdir.join(f'variables.{LOCAL_OS}')
variables_file.write(INCLUDE_VARIABLES)
nested_file = tmpdir.join('dir').join('nested')
@ -207,8 +207,8 @@ def test_include(runner, yadm, tmpdir):
script = f"""
YADM_TEST=1 source {yadm}
set_awk
local_class="{LOCAL_CLASS}"
local_system="{LOCAL_SYSTEM}"
YADM_CLASS="{LOCAL_CLASS}"
YADM_OS="{LOCAL_OS}"
template_default "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)

View file

@ -5,8 +5,8 @@ FILE_MODE = 0o754
LOCAL_CLASS = "esh_Test+@-!^Class"
LOCAL_ARCH = "esh_Test+@-!^Arch"
LOCAL_SYSTEM = "esh_Test+@-!^System"
LOCAL_HOST = "esh_Test+@-!^Host"
LOCAL_OS = "esh_Test+@-!^System"
LOCAL_HOSTNAME = "esh_Test+@-!^Host"
LOCAL_USER = "esh_Test+@-!^User"
LOCAL_DISTRO = "esh_Test+@-!^Distro"
TEMPLATE = f'''
@ -38,7 +38,7 @@ wrong arch 2
<% if [ "$YADM_OS" = "wrongos1" ]; then -%>
wrong os 1
<% fi -%>
<% if [ "$YADM_OS" = "{LOCAL_SYSTEM}" ]; then -%>
<% if [ "$YADM_OS" = "{LOCAL_OS}" ]; then -%>
Included section for os = <%=$YADM_OS%> (<%=$YADM_OS%> repeated)
<% fi -%>
<% if [ "$YADM_OS" = "wrongos2" ]; then -%>
@ -47,7 +47,7 @@ wrong os 2
<% if [ "$YADM_HOSTNAME" = "wronghost1" ]; then -%>
wrong host 1
<% fi -%>
<% if [ "$YADM_HOSTNAME" = "{LOCAL_HOST}" ]; then -%>
<% if [ "$YADM_HOSTNAME" = "{LOCAL_HOSTNAME}" ]; then -%>
Included section for host = <%=$YADM_HOSTNAME%> (<%=$YADM_HOSTNAME%> again)
<% fi -%>
<% if [ "$YADM_HOSTNAME" = "wronghost2" ]; then -%>
@ -77,14 +77,14 @@ EXPECTED = f'''
start of template
esh class = >{LOCAL_CLASS}<
esh arch = >{LOCAL_ARCH}<
esh os = >{LOCAL_SYSTEM}<
esh host = >{LOCAL_HOST}<
esh os = >{LOCAL_OS}<
esh host = >{LOCAL_HOSTNAME}<
esh user = >{LOCAL_USER}<
esh distro = >{LOCAL_DISTRO}<
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
Included section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
Included section for os = {LOCAL_OS} ({LOCAL_OS} repeated)
Included section for host = {LOCAL_HOSTNAME} ({LOCAL_HOSTNAME} again)
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
end of template
@ -107,12 +107,12 @@ def test_template_esh(runner, yadm, tmpdir):
script = f"""
YADM_TEST=1 source {yadm}
local_class="{LOCAL_CLASS}"
local_arch="{LOCAL_ARCH}"
local_system="{LOCAL_SYSTEM}"
local_host="{LOCAL_HOST}"
local_user="{LOCAL_USER}"
local_distro="{LOCAL_DISTRO}"
export YADM_CLASS="{LOCAL_CLASS}"
export YADM_ARCH="{LOCAL_ARCH}"
export YADM_OS="{LOCAL_OS}"
export YADM_HOSTNAME="{LOCAL_HOSTNAME}"
export YADM_USER="{LOCAL_USER}"
export YADM_DISTRO="{LOCAL_DISTRO}"
template_esh "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)

View file

@ -6,8 +6,8 @@ FILE_MODE = 0o754
LOCAL_CLASS = "j2_Test+@-!^Class"
LOCAL_ARCH = "j2_Test+@-!^Arch"
LOCAL_SYSTEM = "j2_Test+@-!^System"
LOCAL_HOST = "j2_Test+@-!^Host"
LOCAL_OS = "j2_Test+@-!^System"
LOCAL_HOSTNAME = "j2_Test+@-!^Host"
LOCAL_USER = "j2_Test+@-!^User"
LOCAL_DISTRO = "j2_Test+@-!^Distro"
TEMPLATE = f'''
@ -39,7 +39,7 @@ wrong arch 2
{{%- if YADM_OS == "wrongos1" %}}
wrong os 1
{{%- endif %}}
{{%- if YADM_OS == "{LOCAL_SYSTEM}" %}}
{{%- if YADM_OS == "{LOCAL_OS}" %}}
Included section for os = {{{{YADM_OS}}}} ({{{{YADM_OS}}}} repeated)
{{%- endif %}}
{{%- if YADM_OS == "wrongos2" %}}
@ -48,7 +48,7 @@ wrong os 2
{{%- if YADM_HOSTNAME == "wronghost1" %}}
wrong host 1
{{%- endif %}}
{{%- if YADM_HOSTNAME == "{LOCAL_HOST}" %}}
{{%- if YADM_HOSTNAME == "{LOCAL_HOSTNAME}" %}}
Included section for host = {{{{YADM_HOSTNAME}}}} ({{{{YADM_HOSTNAME}}}} again)
{{%- endif %}}
{{%- if YADM_HOSTNAME == "wronghost2" %}}
@ -78,14 +78,14 @@ EXPECTED = f'''
start of template
j2 class = >{LOCAL_CLASS}<
j2 arch = >{LOCAL_ARCH}<
j2 os = >{LOCAL_SYSTEM}<
j2 host = >{LOCAL_HOST}<
j2 os = >{LOCAL_OS}<
j2 host = >{LOCAL_HOSTNAME}<
j2 user = >{LOCAL_USER}<
j2 distro = >{LOCAL_DISTRO}<
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
Included section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
Included section for os = {LOCAL_OS} ({LOCAL_OS} repeated)
Included section for host = {LOCAL_HOSTNAME} ({LOCAL_HOSTNAME} again)
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
end of template
@ -109,12 +109,12 @@ def test_template_j2(runner, yadm, tmpdir, processor):
script = f"""
YADM_TEST=1 source {yadm}
local_class="{LOCAL_CLASS}"
local_arch="{LOCAL_ARCH}"
local_system="{LOCAL_SYSTEM}"
local_host="{LOCAL_HOST}"
local_user="{LOCAL_USER}"
local_distro="{LOCAL_DISTRO}"
export YADM_CLASS="{LOCAL_CLASS}"
export YADM_ARCH="{LOCAL_ARCH}"
export YADM_OS="{LOCAL_OS}"
export YADM_HOSTNAME="{LOCAL_HOSTNAME}"
export YADM_USER="{LOCAL_USER}"
export YADM_DISTRO="{LOCAL_DISTRO}"
template_{processor} "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)

99
yadm
View file

@ -190,42 +190,42 @@ function score_file() {
score=$((score + 0))
# variable conditions
elif [[ "$label" =~ ^(a|arch|architecture)$ ]]; then
if [ "$value" = "$local_arch" ]; then
if [ "$value" = "$YADM_ARCH" ]; then
score=$((score + 1))
else
score=0
return
fi
elif [[ "$label" =~ ^(o|os)$ ]]; then
if [ "$value" = "$local_system" ]; then
if [ "$value" = "$YADM_OS" ]; then
score=$((score + 2))
else
score=0
return
fi
elif [[ "$label" =~ ^(d|distro)$ ]]; then
if [ "$value" = "$local_distro" ]; then
if [ "$value" = "$YADM_DISTRO" ]; then
score=$((score + 4))
else
score=0
return
fi
elif [[ "$label" =~ ^(c|class)$ ]]; then
if [ "$value" = "$local_class" ]; then
if [ "$value" = "$YADM_CLASS" ]; then
score=$((score + 8))
else
score=0
return
fi
elif [[ "$label" =~ ^(h|hostname)$ ]]; then
if [ "$value" = "$local_host" ]; then
if [ "$value" = "$YADM_HOSTNAME" ]; then
score=$((score + 16))
else
score=0
return
fi
elif [[ "$label" =~ ^(u|user)$ ]]; then
if [ "$value" = "$local_user" ]; then
if [ "$value" = "$YADM_USER" ]; then
score=$((score + 32))
else
score=0
@ -367,7 +367,7 @@ BEGIN {
c["class"] = class
c["arch"] = arch
c["os"] = os
c["hostname"] = host
c["hostname"] = hostname
c["user"] = user
c["distro"] = distro
c["source"] = source
@ -431,12 +431,12 @@ function conditions() {
EOF
"${AWK_PROGRAM[0]}" \
-v class="$local_class" \
-v arch="$local_arch" \
-v os="$local_system" \
-v host="$local_host" \
-v user="$local_user" \
-v distro="$local_distro" \
-v class="$YADM_CLASS" \
-v arch="$YADM_ARCH" \
-v os="$YADM_OS" \
-v hostname="$YADM_HOSTNAME" \
-v user="$YADM_USER" \
-v distro="$YADM_DISTRO" \
-v source="$input" \
-v source_dir="$(dirname "$input")" \
"$awk_pgm" \
@ -450,13 +450,7 @@ function template_j2cli() {
output="$2"
temp_file="${output}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
YADM_SOURCE="$input" \
YADM_SOURCE="$input" \
"$J2CLI_PROGRAM" "$input" -o "$temp_file"
move_file "$input" "$output" "$temp_file"
@ -467,13 +461,7 @@ function template_envtpl() {
output="$2"
temp_file="${output}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
YADM_SOURCE="$input" \
YADM_SOURCE="$input" \
"$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file"
move_file "$input" "$output" "$temp_file"
@ -484,14 +472,8 @@ function template_esh() {
output="$2"
temp_file="${output}.$$.$RANDOM"
"$ESH_PROGRAM" -o "$temp_file" "$input" \
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
YADM_SOURCE="$input"
YADM_SOURCE="$input" \
"$ESH_PROGRAM" -o "$temp_file" "$input"
move_file "$input" "$output" "$temp_file"
}
@ -520,13 +502,7 @@ function alt() {
parse_encrypt
# gather values for processing alternates
local local_class
local local_arch
local local_system
local local_host
local local_user
local local_distro
set_local_alt_values
set_alt_values
# only be noisy if the "alt" command was run directly
local loud=
@ -618,32 +594,39 @@ function remove_stale_links() {
fi
}
function set_local_alt_values() {
function set_alt_values() {
local_class="$(config local.class)"
export YADM_CLASS
export YADM_ARCH
export YADM_OS
export YADM_HOSTNAME
export YADM_USER
export YADM_DISTRO
local_arch="$(config local.arch)"
if [ -z "$local_arch" ] ; then
local_arch=$(uname -m)
YADM_CLASS="$(config local.class)"
YADM_ARCH="$(config local.arch)"
if [ -z "$YADM_ARCH" ] ; then
YADM_ARCH=$(uname -m)
fi
local_system="$(config local.os)"
if [ -z "$local_system" ] ; then
local_system="$OPERATING_SYSTEM"
YADM_OS="$(config local.os)"
if [ -z "$YADM_OS" ] ; then
YADM_OS="$OPERATING_SYSTEM"
fi
local_host="$(config local.hostname)"
if [ -z "$local_host" ] ; then
local_host=$(uname -n)
local_host=${local_host%%.*} # trim any domain from hostname
YADM_HOSTNAME="$(config local.hostname)"
if [ -z "$YADM_HOSTNAME" ] ; then
YADM_HOSTNAME=$(uname -n)
YADM_HOSTNAME=${YADM_HOSTNAME%%.*} # trim any domain from hostname
fi
local_user="$(config local.user)"
if [ -z "$local_user" ] ; then
local_user=$(id -u -n)
YADM_USER="$(config local.user)"
if [ -z "$YADM_USER" ] ; then
YADM_USER=$(id -u -n)
fi
local_distro="$(query_distro)"
YADM_DISTRO="$(query_distro)"
}