From fb261c763910ccef72e7e79644f47499b90a033e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Berland?= Date: Mon, 9 Jan 2023 16:05:26 +0100 Subject: [PATCH] Replace usage of m4 by Jinja2 --- komodo/cli.py | 44 ++++++++---- .../data/{enable.csh.in => enable.csh.jinja2} | 27 ++++--- komodo/data/{enable.in => enable.jinja2} | 18 ++--- tests/test_enable_scipts.py | 71 ++++++------------- 4 files changed, 71 insertions(+), 89 deletions(-) rename komodo/data/{enable.csh.in => enable.csh.jinja2} (69%) rename komodo/data/{enable.in => enable.jinja2} (81%) diff --git a/komodo/cli.py b/komodo/cli.py index 670e9b897..15873b5f8 100755 --- a/komodo/cli.py +++ b/komodo/cli.py @@ -3,6 +3,7 @@ import sys from pathlib import Path +import jinja2 import yaml as yml import komodo.local as local @@ -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() @@ -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" diff --git a/komodo/data/enable.csh.in b/komodo/data/enable.csh.jinja2 similarity index 69% rename from komodo/data/enable.csh.in rename to komodo/data/enable.csh.jinja2 index ce838656c..2bac33fcc 100644 --- a/komodo/data/enable.csh.in +++ b/komodo/data/enable.csh.jinja2 @@ -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;\\ @@ -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 @@ -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 diff --git a/komodo/data/enable.in b/komodo/data/enable.jinja2 similarity index 81% rename from komodo/data/enable.in rename to komodo/data/enable.jinja2 index aaf3ef52d..a38592947 100755 --- a/komodo/data/enable.in +++ b/komodo/data/enable.jinja2 @@ -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 @@ -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 diff --git a/tests/test_enable_scipts.py b/tests/test_enable_scipts.py index 7d373b40a..1cf7f64af 100644 --- a/tests/test_enable_scipts.py +++ b/tests/test_enable_scipts.py @@ -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()) @@ -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( @@ -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( @@ -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() @@ -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()