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

Merge pull request #304 from erijo/multiple-classes

This commit is contained in:
Tim Byrne 2022-01-22 18:14:23 -06:00
commit 027c7359ac
No known key found for this signature in database
GPG key ID: 14DB4FC2465A4B12
9 changed files with 66 additions and 10 deletions

View file

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

View file

@ -99,7 +99,9 @@ def test_alt_conditions(
# set the class # set the class
tst_class = 'testclass' tst_class = 'testclass'
utils.set_local(paths, 'class', tst_class) utils.set_local(paths, 'class', tst_class + ".before")
utils.set_local(paths, 'class', tst_class, add=True)
utils.set_local(paths, 'class', tst_class + ".after", add=True)
suffix = string.Template(suffix).substitute( suffix = string.Template(suffix).substitute(
tst_arch=tst_arch, tst_arch=tst_arch,

View file

@ -205,6 +205,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_classes=({local_class})
local_arch={local_arch} local_arch={local_arch}
local_system={local_system} local_system={local_system}
local_distro={local_distro} local_distro={local_distro}

View file

@ -37,7 +37,10 @@ def test_set_local_alt_values(
echo "user='$local_user'" echo "user='$local_user'"
""" """
if override: if override == 'class':
utils.set_local(paths, override, 'first')
utils.set_local(paths, override, 'override', add=True)
elif override:
utils.set_local(paths, override, 'override') utils.set_local(paths, override, 'override')
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)

View file

@ -5,6 +5,7 @@ FILE_MODE = 0o754
# these values are also testing the handling of bizarre characters # these values are also testing the handling of bizarre characters
LOCAL_CLASS = "default_Test+@-!^Class" LOCAL_CLASS = "default_Test+@-!^Class"
LOCAL_CLASS2 = "default_Test+@-|^2nd_Class withSpace"
LOCAL_ARCH = "default_Test+@-!^Arch" LOCAL_ARCH = "default_Test+@-!^Arch"
LOCAL_SYSTEM = "default_Test+@-!^System" LOCAL_SYSTEM = "default_Test+@-!^System"
LOCAL_HOST = "default_Test+@-!^Host" LOCAL_HOST = "default_Test+@-!^Host"
@ -34,6 +35,9 @@ Multiple lines
{{% else %}} {{% else %}}
Should not be included... Should not be included...
{{% endif %}} {{% endif %}}
{{% if yadm.class == "{LOCAL_CLASS2}" %}}
Included section for second class
{{% endif %}}
{{% if yadm.class == "wrongclass2" %}} {{% if yadm.class == "wrongclass2" %}}
wrong class 2 wrong class 2
{{% endif %}} {{% endif %}}
@ -106,6 +110,7 @@ default distro_family = >{LOCAL_DISTRO_FAMILY}<
Included section from else Included section from else
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated) Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
Multiple lines Multiple lines
Included section for second class
Included section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated) Included section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated) Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again) Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
@ -165,6 +170,7 @@ def test_template_default(runner, yadm, tmpdir):
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
set_awk set_awk
local_class="{LOCAL_CLASS}" local_class="{LOCAL_CLASS}"
local_classes=("{LOCAL_CLASS2}" "{LOCAL_CLASS}")
local_arch="{LOCAL_ARCH}" local_arch="{LOCAL_ARCH}"
local_system="{LOCAL_SYSTEM}" local_system="{LOCAL_SYSTEM}"
local_host="{LOCAL_HOST}" local_host="{LOCAL_HOST}"

View file

@ -4,6 +4,7 @@ import os
FILE_MODE = 0o754 FILE_MODE = 0o754
LOCAL_CLASS = "esh_Test+@-!^Class" LOCAL_CLASS = "esh_Test+@-!^Class"
LOCAL_CLASS2 = "esh_Test+@-|^2nd_Class withSpace"
LOCAL_ARCH = "esh_Test+@-!^Arch" LOCAL_ARCH = "esh_Test+@-!^Arch"
LOCAL_SYSTEM = "esh_Test+@-!^System" LOCAL_SYSTEM = "esh_Test+@-!^System"
LOCAL_HOST = "esh_Test+@-!^Host" LOCAL_HOST = "esh_Test+@-!^Host"
@ -28,6 +29,10 @@ Included esh section for class = <%=$YADM_CLASS%> (<%=$YADM_CLASS%> repeated)
<% if [ "$YADM_CLASS" = "wrongclass2" ]; then -%> <% if [ "$YADM_CLASS" = "wrongclass2" ]; then -%>
wrong class 2 wrong class 2
<% fi -%> <% fi -%>
<% echo "$YADM_CLASSES" | while IFS='' read cls; do
if [ "$cls" = "{LOCAL_CLASS2}" ]; then -%>
Included esh section for second class
<% fi; done -%>
<% if [ "$YADM_ARCH" = "wrongarch1" ]; then -%> <% if [ "$YADM_ARCH" = "wrongarch1" ]; then -%>
wrong arch 1 wrong arch 1
<% fi -%> <% fi -%>
@ -95,6 +100,7 @@ esh user = >{LOCAL_USER}<
esh distro = >{LOCAL_DISTRO}< esh distro = >{LOCAL_DISTRO}<
esh distro_family = >{LOCAL_DISTRO_FAMILY}< esh distro_family = >{LOCAL_DISTRO_FAMILY}<
Included esh section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated) Included esh section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
Included esh section for second class
Included esh section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated) Included esh section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
Included esh section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated) Included esh section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
Included esh section for host = {LOCAL_HOST} ({LOCAL_HOST} again) Included esh section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
@ -123,6 +129,7 @@ def test_template_esh(runner, yadm, tmpdir):
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
local_class="{LOCAL_CLASS}" local_class="{LOCAL_CLASS}"
local_classes=("{LOCAL_CLASS2}" "{LOCAL_CLASS}")
local_arch="{LOCAL_ARCH}" local_arch="{LOCAL_ARCH}"
local_system="{LOCAL_SYSTEM}" local_system="{LOCAL_SYSTEM}"
local_host="{LOCAL_HOST}" local_host="{LOCAL_HOST}"

View file

@ -5,6 +5,7 @@ import pytest
FILE_MODE = 0o754 FILE_MODE = 0o754
LOCAL_CLASS = "j2_Test+@-!^Class" LOCAL_CLASS = "j2_Test+@-!^Class"
LOCAL_CLASS2 = "j2_Test+@-|^2nd_Class withSpace"
LOCAL_ARCH = "j2_Test+@-!^Arch" LOCAL_ARCH = "j2_Test+@-!^Arch"
LOCAL_SYSTEM = "j2_Test+@-!^System" LOCAL_SYSTEM = "j2_Test+@-!^System"
LOCAL_HOST = "j2_Test+@-!^Host" LOCAL_HOST = "j2_Test+@-!^Host"
@ -30,6 +31,9 @@ Included j2 section for class = \
{{%- if YADM_CLASS == "wrongclass2" %}} {{%- if YADM_CLASS == "wrongclass2" %}}
wrong class 2 wrong class 2
{{%- endif %}} {{%- endif %}}
{{%- if "{LOCAL_CLASS2}" in YADM_CLASSES.split("\\n") %}}
Included j2 section for second class
{{%- endif %}}
{{%- if YADM_ARCH == "wrongarch1" %}} {{%- if YADM_ARCH == "wrongarch1" %}}
wrong arch 1 wrong arch 1
{{%- endif %}} {{%- endif %}}
@ -99,6 +103,7 @@ j2 user = >{LOCAL_USER}<
j2 distro = >{LOCAL_DISTRO}< j2 distro = >{LOCAL_DISTRO}<
j2 distro_family = >{LOCAL_DISTRO_FAMILY}< j2 distro_family = >{LOCAL_DISTRO_FAMILY}<
Included j2 section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated) Included j2 section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
Included j2 section for second class
Included j2 section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated) Included j2 section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
Included j2 section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated) Included j2 section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
Included j2 section for host = {LOCAL_HOST} ({LOCAL_HOST} again) Included j2 section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
@ -128,6 +133,7 @@ def test_template_j2(runner, yadm, tmpdir, processor):
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
local_class="{LOCAL_CLASS}" local_class="{LOCAL_CLASS}"
local_classes=("{LOCAL_CLASS2}" "{LOCAL_CLASS}")
local_arch="{LOCAL_ARCH}" local_arch="{LOCAL_ARCH}"
local_system="{LOCAL_SYSTEM}" local_system="{LOCAL_SYSTEM}"
local_host="{LOCAL_HOST}" local_host="{LOCAL_HOST}"

View file

@ -21,11 +21,12 @@ INCLUDE_DIRS = ['', 'test alt']
INCLUDE_CONTENT = '8780846c02e34c930d0afd127906668f' INCLUDE_CONTENT = '8780846c02e34c930d0afd127906668f'
def set_local(paths, variable, value): def set_local(paths, variable, value, add=False):
"""Set local override""" """Set local override"""
add = "--add" if add else ""
os.system( os.system(
f'GIT_DIR={str(paths.repo)} ' f'GIT_DIR={str(paths.repo)} '
f'git config --local "local.{variable}" "{value}"' f'git config --local {add} "local.{variable}" "{value}"'
) )

38
yadm
View file

@ -218,7 +218,7 @@ function score_file() {
return return
fi fi
elif [[ "$label" =~ ^(c|class)$ ]]; then elif [[ "$label" =~ ^(c|class)$ ]]; then
if [ "$value" = "$local_class" ]; then if in_list "$value" "${local_classes[@]}"; then
score=$((score + 16)) score=$((score + 16))
else else
score=0 score=0
@ -426,12 +426,22 @@ function replace_vars() {
gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label]) gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label])
} }
} }
function condition_helper(label, value) {
gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value)
return sprintf("yadm\\.%s" blank "*==" blank "*\"%s\"", label, value)
}
function conditions() { function conditions() {
pattern = ifs blank "+(" pattern = ifs blank "+("
for (label in c) { for (label in c) {
if (label != "class") {
value = c[label] value = c[label]
gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value) pattern = sprintf("%s%s|", pattern, condition_helper(label, value));
pattern = sprintf("%syadm\\.%s" blank "*==" blank "*\"%s\"|", pattern, label, value) }
}
split(classes, cls_array, "\n")
for (idx in cls_array) {
value = cls_array[idx]
pattern = sprintf("%s%s|", pattern, condition_helper("class", value));
} }
sub(/\|$/, ")" blank "*%}$", pattern) sub(/\|$/, ")" blank "*%}$", pattern)
return pattern return pattern
@ -448,6 +458,7 @@ EOF
-v distro_family="$local_distro_family" \ -v distro_family="$local_distro_family" \
-v source="$input" \ -v source="$input" \
-v source_dir="$(dirname "$input")" \ -v source_dir="$(dirname "$input")" \
-v classes="$(join_string $'\n' "${local_classes[@]}")" \
"$awk_pgm" \ "$awk_pgm" \
"$input" > "$temp_file" || rm -f "$temp_file" "$input" > "$temp_file" || rm -f "$temp_file"
@ -467,6 +478,7 @@ function template_j2cli() {
YADM_DISTRO="$local_distro" \ YADM_DISTRO="$local_distro" \
YADM_DISTRO_FAMILY="$local_distro_family" \ YADM_DISTRO_FAMILY="$local_distro_family" \
YADM_SOURCE="$input" \ YADM_SOURCE="$input" \
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
"$J2CLI_PROGRAM" "$input" -o "$temp_file" "$J2CLI_PROGRAM" "$input" -o "$temp_file"
move_file "$input" "$output" "$temp_file" move_file "$input" "$output" "$temp_file"
@ -485,6 +497,7 @@ function template_envtpl() {
YADM_DISTRO="$local_distro" \ YADM_DISTRO="$local_distro" \
YADM_DISTRO_FAMILY="$local_distro_family" \ YADM_DISTRO_FAMILY="$local_distro_family" \
YADM_SOURCE="$input" \ YADM_SOURCE="$input" \
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
"$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file" "$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file"
move_file "$input" "$output" "$temp_file" move_file "$input" "$output" "$temp_file"
@ -495,6 +508,7 @@ function template_esh() {
output="$2" output="$2"
temp_file="${output}.$$.$RANDOM" temp_file="${output}.$$.$RANDOM"
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
"$ESH_PROGRAM" -o "$temp_file" "$input" \ "$ESH_PROGRAM" -o "$temp_file" "$input" \
YADM_CLASS="$local_class" \ YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \ YADM_ARCH="$local_arch" \
@ -533,6 +547,7 @@ function alt() {
# gather values for processing alternates # gather values for processing alternates
local local_class local local_class
local -a local_classes
local local_arch local local_arch
local local_system local local_system
local local_host local local_host
@ -633,7 +648,12 @@ function remove_stale_links() {
function set_local_alt_values() { function set_local_alt_values() {
local_class="$(config local.class)" local -a all_classes
all_classes=$(config --get-all local.class)
while IFS='' read -r local_class; do
local_classes+=("$local_class")
done <<< "$all_classes"
local_class="${local_classes[-1]:-}"
local_arch="$(config local.arch)" local_arch="$(config local.arch)"
if [ -z "$local_arch" ] ; then if [ -z "$local_arch" ] ; then
@ -2062,6 +2082,16 @@ function join_string {
printf "%s" "${*:2}" printf "%s" "${*:2}"
} }
function in_list {
local element="$1"
shift
for e in "$@"; do
[[ "$e" = "$element" ]] && return 0
done
return 1
}
function get_mode { function get_mode {
local filename="$1" local filename="$1"
local mode local mode