Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple local classes #304

Merged
merged 1 commit into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -97,7 +97,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 @@ -201,6 +201,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 @@ -34,7 +34,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 @@ -32,6 +33,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 @@ -93,6 +97,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 @@ -150,6 +155,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 @@ -26,6 +27,10 @@
<% if [ "$YADM_CLASS" = "wrongclass2" ]; then -%>
wrong class 2
<% fi -%>
<% echo "$YADM_CLASSES" | while IFS='' read cls; do
if [ "$cls" = "{LOCAL_CLASS2}" ]; then -%>
Included section for second class
<% fi; done -%>
<% if [ "$YADM_ARCH" = "wrongarch1" ]; then -%>
wrong arch 1
<% fi -%>
Expand Down Expand Up @@ -82,6 +87,7 @@
esh user = >{LOCAL_USER}<
esh distro = >{LOCAL_DISTRO}<
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
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 All @@ -108,6 +114,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 @@ -27,6 +28,9 @@
{{%- if YADM_CLASS == "wrongclass2" %}}
wrong class 2
{{%- endif %}}
{{%- if "{LOCAL_CLASS2}" in YADM_CLASSES.split("\\n") %}}
Included section for second class
{{%- endif %}}
{{%- if YADM_ARCH == "wrongarch1" %}}
wrong arch 1
{{%- endif %}}
Expand Down Expand Up @@ -83,6 +87,7 @@
j2 user = >{LOCAL_USER}<
j2 distro = >{LOCAL_DISTRO}<
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
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 @@ -110,6 +115,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 @@ -211,7 +211,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 + 8))
else
score=0
Expand Down Expand Up @@ -418,12 +418,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 @@ -439,6 +449,7 @@ EOF
-v distro="$local_distro" \
-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 @@ -457,6 +468,7 @@ function template_j2cli() {
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
YADM_SOURCE="$input" \
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
"$J2CLI_PROGRAM" "$input" -o "$temp_file"

move_file "$input" "$output" "$temp_file"
Expand All @@ -474,6 +486,7 @@ function template_envtpl() {
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
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 @@ -484,6 +497,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 @@ -521,6 +535,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 @@ -620,7 +635,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 @@ -2032,6 +2052,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