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

cd to workflow dir during file parsing. #5589

Merged
merged 5 commits into from
Jul 10, 2023
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ After successful reload the scheduler will unpause the workflow.
[#5537](https://github.com/cylc/cylc-flow/pull/5537) - Allow parameters
in family names to be split, e.g. `<foo>FAM<bar>`.

[#5589](https://github.com/cylc/cylc-flow/pull/5589) - Move to workflow
directory during file parsing, to give the template processor access to
workflow files.

[#5405](https://github.com/cylc/cylc-flow/pull/5405) - Improve scan command
help, and add scheduler PID to the output.

Expand Down
8 changes: 8 additions & 0 deletions cylc/flow/parsec/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ def read_and_proc(
fpath = _get_fpath_for_source(fpath, opts)
fdir = os.path.dirname(fpath)

odir = os.getcwd()
Copy link
Member

@wxtim wxtim Jul 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be safer

try:
  os.chdir(fdir)
finally:
  os.chdir(odir)

or even:

@contextmanager
def work_in(path):
  before = os.getcwd()
  try:
    os.chdir(path)
  finally:
    os.chdir(before)

with work_in(fdir):
  # ... stuff ...

Kind of surprised that the latter isn't part of Pathlib.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't actually matter if we don't os.chdir back afterwards, this only applies to the Python session itself not the parent shell.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note my change does os.chdir back afterwards. I agree it should not matter, but to explicitly avoid any side-effects of the dir change that I might not be aware of.


# Move to the file location to give the template processor easy access to
# other files in the workflow directory (whether source or installed).
os.chdir(fdir)

# Allow Python modules in lib/python/ (e.g. for use by Jinja2 filters).
workflow_lib_python = os.path.join(fdir, "lib", "python")
if (
Expand Down Expand Up @@ -494,6 +500,8 @@ def read_and_proc(
if do_contin:
flines = _concatenate(flines)

os.chdir(odir)

# return rstripped lines
return [fl.rstrip() for fl in flines]

Expand Down
Loading