Skip to content

Commit

Permalink
Merge pull request yadm-dev#304 from erijo/multiple-classes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLocehiliosan committed Jan 23, 2022
2 parents 3d34325 + 42c74ef commit 027c735
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ max-attributes=8
max-statements=65

[SIMILARITIES]
min-similarity-lines=7
min-similarity-lines=8

[MESSAGES CONTROL]
disable=redefined-outer-name
Expand Down
4 changes: 3 additions & 1 deletion test/test_alt.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def test_alt_conditions(

# set the class
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(
tst_arch=tst_arch,
Expand Down
1 change: 1 addition & 0 deletions test/test_unit_score_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def test_score_values(
YADM_TEST=1 source {yadm}
score=0
local_class={local_class}
local_classes=({local_class})
local_arch={local_arch}
local_system={local_system}
local_distro={local_distro}
Expand Down
5 changes: 4 additions & 1 deletion test/test_unit_set_local_alt_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def test_set_local_alt_values(
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')

run = runner(command=['bash'], inp=script)
Expand Down
6 changes: 6 additions & 0 deletions test/test_unit_template_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# these values are also testing the handling of bizarre characters
LOCAL_CLASS = "default_Test+@-!^Class"
LOCAL_CLASS2 = "default_Test+@-|^2nd_Class withSpace"
LOCAL_ARCH = "default_Test+@-!^Arch"
LOCAL_SYSTEM = "default_Test+@-!^System"
LOCAL_HOST = "default_Test+@-!^Host"
Expand Down Expand Up @@ -34,6 +35,9 @@
{{% else %}}
Should not be included...
{{% endif %}}
{{% if yadm.class == "{LOCAL_CLASS2}" %}}
Included section for second class
{{% endif %}}
{{% if yadm.class == "wrongclass2" %}}
wrong class 2
{{% endif %}}
Expand Down Expand Up @@ -106,6 +110,7 @@
Included section from else
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
Multiple lines
Included section for second class
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)
Expand Down Expand Up @@ -165,6 +170,7 @@ def test_template_default(runner, yadm, tmpdir):
YADM_TEST=1 source {yadm}
set_awk
local_class="{LOCAL_CLASS}"
local_classes=("{LOCAL_CLASS2}" "{LOCAL_CLASS}")
local_arch="{LOCAL_ARCH}"
local_system="{LOCAL_SYSTEM}"
local_host="{LOCAL_HOST}"
Expand Down
7 changes: 7 additions & 0 deletions test/test_unit_template_esh.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
FILE_MODE = 0o754

LOCAL_CLASS = "esh_Test+@-!^Class"
LOCAL_CLASS2 = "esh_Test+@-|^2nd_Class withSpace"
LOCAL_ARCH = "esh_Test+@-!^Arch"
LOCAL_SYSTEM = "esh_Test+@-!^System"
LOCAL_HOST = "esh_Test+@-!^Host"
Expand All @@ -28,6 +29,10 @@
<% if [ "$YADM_CLASS" = "wrongclass2" ]; then -%>
wrong class 2
<% 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 -%>
wrong arch 1
<% fi -%>
Expand Down Expand Up @@ -95,6 +100,7 @@
esh distro = >{LOCAL_DISTRO}<
esh distro_family = >{LOCAL_DISTRO_FAMILY}<
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 os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
Included esh section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
Expand Down Expand Up @@ -123,6 +129,7 @@ def test_template_esh(runner, yadm, tmpdir):
script = f"""
YADM_TEST=1 source {yadm}
local_class="{LOCAL_CLASS}"
local_classes=("{LOCAL_CLASS2}" "{LOCAL_CLASS}")
local_arch="{LOCAL_ARCH}"
local_system="{LOCAL_SYSTEM}"
local_host="{LOCAL_HOST}"
Expand Down
6 changes: 6 additions & 0 deletions test/test_unit_template_j2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
FILE_MODE = 0o754

LOCAL_CLASS = "j2_Test+@-!^Class"
LOCAL_CLASS2 = "j2_Test+@-|^2nd_Class withSpace"
LOCAL_ARCH = "j2_Test+@-!^Arch"
LOCAL_SYSTEM = "j2_Test+@-!^System"
LOCAL_HOST = "j2_Test+@-!^Host"
Expand All @@ -30,6 +31,9 @@
{{%- if YADM_CLASS == "wrongclass2" %}}
wrong class 2
{{%- endif %}}
{{%- if "{LOCAL_CLASS2}" in YADM_CLASSES.split("\\n") %}}
Included j2 section for second class
{{%- endif %}}
{{%- if YADM_ARCH == "wrongarch1" %}}
wrong arch 1
{{%- endif %}}
Expand Down Expand Up @@ -99,6 +103,7 @@
j2 distro = >{LOCAL_DISTRO}<
j2 distro_family = >{LOCAL_DISTRO_FAMILY}<
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 os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
Included j2 section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
Expand Down Expand Up @@ -128,6 +133,7 @@ def test_template_j2(runner, yadm, tmpdir, processor):
script = f"""
YADM_TEST=1 source {yadm}
local_class="{LOCAL_CLASS}"
local_classes=("{LOCAL_CLASS2}" "{LOCAL_CLASS}")
local_arch="{LOCAL_ARCH}"
local_system="{LOCAL_SYSTEM}"
local_host="{LOCAL_HOST}"
Expand Down
5 changes: 3 additions & 2 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
INCLUDE_CONTENT = '8780846c02e34c930d0afd127906668f'


def set_local(paths, variable, value):
def set_local(paths, variable, value, add=False):
"""Set local override"""
add = "--add" if add else ""
os.system(
f'GIT_DIR={str(paths.repo)} '
f'git config --local "local.{variable}" "{value}"'
f'git config --local {add} "local.{variable}" "{value}"'
)


Expand Down
40 changes: 35 additions & 5 deletions yadm
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ function score_file() {
return
fi
elif [[ "$label" =~ ^(c|class)$ ]]; then
if [ "$value" = "$local_class" ]; then
if in_list "$value" "${local_classes[@]}"; then
score=$((score + 16))
else
score=0
Expand Down Expand Up @@ -426,12 +426,22 @@ function replace_vars() {
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() {
pattern = ifs blank "+("
for (label in c) {
value = c[label]
gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value)
pattern = sprintf("%syadm\\.%s" blank "*==" blank "*\"%s\"|", pattern, label, value)
if (label != "class") {
value = c[label]
pattern = sprintf("%s%s|", pattern, condition_helper(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)
return pattern
Expand All @@ -448,6 +458,7 @@ EOF
-v distro_family="$local_distro_family" \
-v source="$input" \
-v source_dir="$(dirname "$input")" \
-v classes="$(join_string $'\n' "${local_classes[@]}")" \
"$awk_pgm" \
"$input" > "$temp_file" || rm -f "$temp_file"

Expand All @@ -467,6 +478,7 @@ function template_j2cli() {
YADM_DISTRO="$local_distro" \
YADM_DISTRO_FAMILY="$local_distro_family" \
YADM_SOURCE="$input" \
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
"$J2CLI_PROGRAM" "$input" -o "$temp_file"

move_file "$input" "$output" "$temp_file"
Expand All @@ -485,6 +497,7 @@ function template_envtpl() {
YADM_DISTRO="$local_distro" \
YADM_DISTRO_FAMILY="$local_distro_family" \
YADM_SOURCE="$input" \
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
"$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file"

move_file "$input" "$output" "$temp_file"
Expand All @@ -495,6 +508,7 @@ function template_esh() {
output="$2"
temp_file="${output}.$$.$RANDOM"

YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
"$ESH_PROGRAM" -o "$temp_file" "$input" \
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
Expand Down Expand Up @@ -533,6 +547,7 @@ function alt() {

# gather values for processing alternates
local local_class
local -a local_classes
local local_arch
local local_system
local local_host
Expand Down Expand Up @@ -633,7 +648,12 @@ function remove_stale_links() {

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)"
if [ -z "$local_arch" ] ; then
Expand Down Expand Up @@ -2062,6 +2082,16 @@ function join_string {
printf "%s" "${*:2}"
}

function in_list {
local element="$1"
shift

for e in "$@"; do
[[ "$e" = "$element" ]] && return 0
done
return 1
}

function get_mode {
local filename="$1"
local mode
Expand Down

0 comments on commit 027c735

Please sign in to comment.