Skip to content

Commit

Permalink
Replace usage of m4 by Jinja2
Browse files Browse the repository at this point in the history
  • Loading branch information
berland committed Jan 16, 2023
1 parent d1beced commit fb261c7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 89 deletions.
44 changes: 29 additions & 15 deletions komodo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
from pathlib import Path

import jinja2
import yaml as yml

import komodo.local as local
Expand All @@ -20,6 +21,33 @@
from komodo.yaml_file_type import YamlFile


def create_enable_scripts(komodo_prefix: str, komodo_release: str) -> None:
"""Render enable scripts (user facing) for bash and csh to an existing
directory komodo_release (in current working directory).
Args:
komodo_prefix: The filesystem path to where the release is to be
deployed.
komodo_release: The name of the release.
"""
jinja_env = jinja2.Environment(
loader=jinja2.PackageLoader(package_name="komodo", package_path="data"),
keep_trailing_newline=True,
)
for tmpl, target in [
("enable.jinja2", "enable"),
("enable.csh.jinja2", "enable.csh"),
]:

(Path(komodo_release) / target).write_text(
jinja_env.get_template(tmpl).render(
komodo_prefix=komodo_prefix,
komodo_release=komodo_release,
),
encoding="utf-8",
)


def _main(args):
abs_prefix = Path(args.prefix).resolve()

Expand Down Expand Up @@ -59,21 +87,7 @@ def _main(args):
if args.build and not args.install:
sys.exit(0)

# create the enable script
for tmpl, target in [("enable.in", "enable"), ("enable.csh.in", "enable.csh")]:
# TODO should args.release be release_path?
with open(f"{args.release}/{target}", mode="w", encoding="utf-8") as f_handle:
f_handle.write(
shell(
[
f"m4 {data.get('enable.m4')}",
f"-D komodo_prefix={tmp_prefix}",
f"-D komodo_pyver={args.pyver}",
f"-D komodo_release={args.release}",
data.get(tmpl),
]
).decode("utf-8")
)
create_enable_scripts(komodo_prefix=tmp_prefix, komodo_release=args.release)

with open(args.locations_config, mode="r", encoding="utf-8") as defs, open(
Path(args.release) / "local", mode="w", encoding="utf-8"
Expand Down
27 changes: 12 additions & 15 deletions komodo/data/enable.csh.in → komodo/data/enable.csh.jinja2
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
changequote(<!,!>)

alias disable_komodo '\\
test $?_PRE_KOMODO_PATH != 0 && setenv PATH "$_PRE_KOMODO_PATH" && unsetenv _PRE_KOMODO_PATH;\\
test $?KOMODO_RELEASE != 0 && test $?_PRE_KOMODO_MANPATH == 0 && unsetenv MANPATH;\\
Expand All @@ -18,29 +16,29 @@ disable_komodo preserve_disable_komodo

if $?PATH then
setenv _PRE_KOMODO_PATH "$PATH"
setenv PATH komodo_prefix/bin:$PATH
setenv PATH {{komodo_prefix}}/bin:$PATH
else
setenv PATH komodo_prefix/bin
setenv PATH {{komodo_prefix}}/bin
endif

if $?MANPATH then
setenv _PRE_KOMODO_MANPATH "$MANPATH"
setenv MANPATH komodo_prefix/share/man:$MANPATH
setenv MANPATH {{komodo_prefix}}/share/man:$MANPATH
else
setenv MANPATH komodo_prefix/share/man:
setenv MANPATH {{komodo_prefix}}/share/man:
endif


if $?LD_LIBRARY_PATH then
setenv _PRE_KOMODO_LD_PATH "$LD_LIBRARY_PATH"
setenv LD_LIBRARY_PATH komodo_prefix/lib:komodo_prefix/lib64:$LD_LIBRARY_PATH
setenv LD_LIBRARY_PATH {{komodo_prefix}}/lib:{{komodo_prefix}}/lib64:$LD_LIBRARY_PATH
else
setenv LD_LIBRARY_PATH komodo_prefix/lib:komodo_prefix/lib64
setenv LD_LIBRARY_PATH {{komodo_prefix}}/lib:{{komodo_prefix}}/lib64
endif

setenv KOMODO_RELEASE komodo_release
setenv KOMODO_RELEASE {{komodo_release}}

set local_script=komodo_prefix/../local.csh
set local_script={{komodo_prefix}}/../local.csh
if ( -r $local_script) then
source $local_script
endif
Expand All @@ -54,14 +52,13 @@ if ( $?prompt ) then
endif

rehash
changequote()

if ( -d komodo_prefix/../motd/scripts ) then
foreach f (komodo_prefix/../motd/scripts/*)
if ( -d {{komodo_prefix}}/../motd/scripts ) then
foreach f ({{komodo_prefix}}/../motd/scripts/*)
$f
end
endif

if ( -d komodo_prefix/../motd/messages ) then
cat komodo_prefix/../motd/messages/*
if ( -d {{komodo_prefix}}/../motd/messages ) then
cat {{komodo_prefix}}/../motd/messages/*
endif
18 changes: 9 additions & 9 deletions komodo/data/enable.in → komodo/data/enable.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ disable_komodo () {
# unset irrelevant variables
disable_komodo preserve_disable_komodo

export KOMODO_RELEASE=komodo_release
export KOMODO_RELEASE={{komodo_release}}

export _PRE_KOMODO_PATH="${PATH:-}"
export PATH=komodo_prefix/bin${PATH:+:${PATH}}
export PATH={{komodo_prefix}}/bin${PATH:+:${PATH}}

export _PRE_KOMODO_MANPATH="${MANPATH:-}"
export MANPATH=komodo_prefix/share/man:${MANPATH:-}
export MANPATH={{komodo_prefix}}/share/man:${MANPATH:-}

export _PRE_KOMODO_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH=komodo_prefix/lib:komodo_prefix/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export LD_LIBRARY_PATH={{komodo_prefix}}/lib:{{komodo_prefix}}/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

export _PRE_KOMODO_PS1="${PS1:-}"
export PS1="(${KOMODO_RELEASE}) ${PS1:-}"

local_script=komodo_prefix/../local
local_script={{komodo_prefix}}/../local
if [ -e $local_script ]; then
source $local_script
fi
Expand All @@ -87,15 +87,15 @@ if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r
fi

if [ -d komodo_prefix/../motd/scripts ]
if [ -d {{komodo_prefix}}/../motd/scripts ]
then
for f in komodo_prefix/../motd/scripts/*
for f in {{komodo_prefix}}/../motd/scripts/*
do
$f
done
fi

if [ -d komodo_prefix/../motd/messages ]
if [ -d {{komodo_prefix}}/../motd/messages ]
then
cat komodo_prefix/../motd/messages/*
cat {{komodo_prefix}}/../motd/messages/*
fi
71 changes: 21 additions & 50 deletions tests/test_enable_scipts.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import json
import os
from pathlib import Path

from komodo.data import Data
from komodo.cli import create_enable_scripts
from komodo.shell import shell


def _create_script(kmd_prefix, kmd_pyver, kmd_release, target, template):
data = Data(extra_data_dirs=None)
os.mkdir(kmd_release)
with open("{}/{}".format(kmd_release, target), "w") as f:
f.write(
shell(
[
"m4 {}".format(data.get("enable.m4")),
"-D komodo_prefix={}".format(kmd_prefix),
"-D komodo_pyver={}".format(kmd_pyver),
"-D komodo_release={}".format(kmd_release),
data.get(template),
]
).decode("utf-8")
)


def _load_envs():
with open("pre_source.env") as pre:
pre_env = json.loads(pre.read())
Expand Down Expand Up @@ -51,10 +34,8 @@ def _load_envs():

def test_enable_bash_nopresets(tmpdir):
with tmpdir.as_cwd():
kmd_release = "bleeding"
kmd_pyver = "3.6"
kmd_prefix = "prefix"
_create_script(kmd_prefix, kmd_pyver, kmd_release, "enable", "enable.in")
Path("bleeding").mkdir()
create_enable_scripts(komodo_prefix="prefix", komodo_release="bleeding")
with open("test_enable.sh", "w") as test_file:
test_file.write(
TEST_SCRIPT_SIMPLE.format(
Expand All @@ -80,12 +61,8 @@ def test_enable_bash_nopresets(tmpdir):

def test_enable_csh_no_presets(tmpdir):
with tmpdir.as_cwd():
kmd_release = "bleeding"
kmd_pyver = "3.6"
kmd_prefix = "prefix"
_create_script(
kmd_prefix, kmd_pyver, kmd_release, "enable.csh", "enable.csh.in"
)
Path("bleeding").mkdir()
create_enable_scripts(komodo_prefix="prefix", komodo_release="bleeding")
with open("test_enable.sh", "w") as test_file:
test_file.write(
TEST_SCRIPT_SIMPLE.format(
Expand All @@ -111,16 +88,14 @@ def test_enable_csh_no_presets(tmpdir):

def test_enable_bash_with_presets(tmpdir):
with tmpdir.as_cwd():
kmd_release = "bleeding"
kmd_pyver = "3.6"
kmd_prefix = "prefix"
_create_script(kmd_prefix, kmd_pyver, kmd_release, "enable", "enable.in")
with open("test_enable.sh", "w") as test_file:
test_file.write(
TEST_SCRIPT_SIMPLE.format(
set_envs=BASH_ENVS, enable_path="bleeding/enable"
)
)
Path("bleeding").mkdir()
create_enable_scripts(komodo_prefix="prefix", komodo_release="bleeding")
Path("test_enable.sh").write_text(
TEST_SCRIPT_SIMPLE.format(
set_envs=BASH_ENVS, enable_path="bleeding/enable"
),
encoding="utf-8",
)

shell(["bash test_enable.sh"])
pre_env, sourced_env, post_env = _load_envs()
Expand All @@ -139,18 +114,14 @@ def test_enable_bash_with_presets(tmpdir):

def test_enable_csh_with_presets(tmpdir):
with tmpdir.as_cwd():
kmd_release = "bleeding"
kmd_pyver = "3.6"
kmd_prefix = "prefix"
_create_script(
kmd_prefix, kmd_pyver, kmd_release, "enable.csh", "enable.csh.in"
Path("bleeding").mkdir()
create_enable_scripts(komodo_prefix="prefix", komodo_release="bleeding")
Path("test_enable.sh").write_text(
TEST_SCRIPT_SIMPLE.format(
set_envs=CSH_ENVS, enable_path="bleeding/enable.csh"
),
encoding="utf-8",
)
with open("test_enable.sh", "w") as test_file:
test_file.write(
TEST_SCRIPT_SIMPLE.format(
set_envs=CSH_ENVS, enable_path="bleeding/enable.csh"
)
)

shell(["csh test_enable.sh"])
pre_env, sourced_env, post_env = _load_envs()
Expand Down

0 comments on commit fb261c7

Please sign in to comment.