Skip to content

Commit 4cbe3de

Browse files
authored
Merge pull request #6944 from wxtim/feat.WORKFLOW_SRC_DIR-template-var
Make a new standard template variable "CYLC_SOURCE_DIRECTORY"
1 parent cf9b13c commit 4cbe3de

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

changes.d/6944.feat.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make CYLC_WORKFLOW_SRC_DIR available as a template variable.

cylc/flow/parsec/fileparse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,11 @@ def read_and_proc(
471471
template_vars = merge_template_vars(template_vars, extra_vars)
472472
template_vars['CYLC_TEMPLATE_VARS'] = template_vars
473473

474+
# Add CYLC_WORKFLOW_SRC_DIR to template variables.
475+
sourcepath = get_workflow_source_dir(Path(fpath).parent)[1]
476+
template_vars['CYLC_WORKFLOW_SRC_DIR'] = (
477+
sourcepath.readlink() if sourcepath else Path(fpath).parent)
478+
474479
# Fail if templating_detected ≠ hashbang
475480
process_with = hashbang_and_plugin_templating_clash(
476481
extra_vars[TEMPLATING_DETECTED], flines
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env bash
2+
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
3+
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
#------------------------------------------------------------------------------
19+
# Test `CYLC_WORKFLOW_SRC_DIR.t` is set
20+
21+
. "$(dirname "$0")/test_header"
22+
23+
set_test_number 6
24+
25+
WORKFLOW_NAME="cylctb-x$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6)"
26+
27+
cat > flow.cylc <<__HERE__
28+
#!jinja2
29+
# TEST: {{ CYLC_WORKFLOW_SRC_DIR }}
30+
[scheduler]
31+
allow implicit tasks = true
32+
[scheduling]
33+
[[graph]]
34+
R1 = foo
35+
__HERE__
36+
37+
run_ok "${TEST_NAME_BASE}-view" cylc view -p .
38+
39+
named_grep_ok "src-path-in-view-p" "TEST: $PWD" \
40+
"${TEST_NAME_BASE}-view.stdout"
41+
42+
# It starts playing:
43+
run_ok "${TEST_NAME_BASE}-vip" \
44+
cylc vip \
45+
--pause \
46+
--no-run-name \
47+
--workflow-name "${WORKFLOW_NAME}"
48+
49+
# It can get CYLC_WORKFLOW_SRC_DIR
50+
named_grep_ok "src-path-available" \
51+
"TEST: $PWD" "${RUN_DIR}/${WORKFLOW_NAME}/log/config/flow-processed.cylc"
52+
53+
# It can be updated with Cylc VR
54+
echo "[meta]" >> flow.cylc
55+
run_ok "${TEST_NAME_BASE}-vr" \
56+
cylc vr "${WORKFLOW_NAME}"
57+
poll_grep "meta" "${RUN_DIR}/${WORKFLOW_NAME}/log/config/flow-processed.cylc"
58+
59+
# It can get CYLC_WORKFLOW_SRC_DIR
60+
named_grep_ok "src-path-available" \
61+
"TEST: $PWD" "${RUN_DIR}/${WORKFLOW_NAME}/log/config/flow-processed.cylc"
62+
63+
cylc stop "${WORKFLOW_NAME}"
64+
65+
purge "${WORKFLOW_NAME}"

tests/functional/validate/75-CYLC_TEMPLATE_VARS.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ cat > 'flow.cylc' <<__HEREDOC__
3333
__HEREDOC__
3434

3535
run_ok "${TEST_NAME_BASE}-validate" cylc validate . --debug
36-
grep_ok "CYLC_TEMPLATE_VARS={'CYLC_VERSION': '.*', 'CYLC_TEMPLATE_VARS': {...}}" \
36+
grep_ok "CYLC_TEMPLATE_VARS={'CYLC_VERSION': '.*', 'CYLC_TEMPLATE_VARS': {...}" \
3737
"${TEST_NAME_BASE}-validate.stderr"

tests/integration/test_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
XtriggerConfigError,
3232
)
3333
from cylc.flow.parsec.exceptions import ListValueError
34+
from cylc.flow.parsec.fileparse import read_and_proc
3435
from cylc.flow.pathutil import get_workflow_run_pub_db_path
3536

3637
Fixture = Any
@@ -673,6 +674,47 @@ async def test_invalid_starttask(one_conf, flow, scheduler, start):
673674
pass
674675

675676

677+
async def test_CYLC_WORKFLOW_SRC_DIR_correctly_set(tmp_path, install, run_dir):
678+
"""CYLC_WORKFLOW_SRC_DIR is set correctly:
679+
680+
* In source dir
681+
* In installed dir (Not testing different permutations of installed
682+
dir as these are covered by testing of `get_workflow_source_dir`)
683+
* Created directly in the run dir.
684+
685+
"""
686+
def process_file(target):
687+
"""Run config through read_and_proc (~= cylc view --process)
688+
"""
689+
return read_and_proc(
690+
target,
691+
viewcfg={
692+
'mark': False,
693+
'single': False,
694+
'label': False,
695+
'jinja2': True,
696+
'contin': True,
697+
'inline': True,
698+
},
699+
)
700+
701+
# Setup a source directory:
702+
(tmp_path / 'flow.cylc').write_text(
703+
'#!jinja2\n{{ CYLC_WORKFLOW_SRC_DIR }}'
704+
)
705+
706+
# Check that the CYLC_SRC_DIRECTORY
707+
# points to the source directory (tmp_path):
708+
processed = process_file(tmp_path / 'flow.cylc')
709+
assert processed[0] == str(tmp_path)
710+
711+
# After installation the CYLC_WORKFLOW_SRC_DIR
712+
# *still* points back to tmp_path:
713+
wid = await install(tmp_path)
714+
processed = process_file(run_dir / wid / 'flow.cylc')
715+
assert processed[0] == str(tmp_path)
716+
717+
676718
async def test_task_event_bad_custom_template(
677719
flow, validate, scheduler, start, log_filter
678720
):

0 commit comments

Comments
 (0)