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

Conversation

hjoliver
Copy link
Member

@hjoliver hjoliver commented Jun 19, 2023

Allow the template processor to access workflow files - in the source dir for a source workflow, or the run dir for installed workflows.

This is the most straightforward way to cater to this kind of use case

(I can't think of any downside...)

Check List

  • I have read CONTRIBUTING.md and added my name as a Code Contributor.
  • Contains logically grouped changes (else tidy your branch by rebase).
  • Does not contain off-topic changes (use other PRs for other changes).
  • Applied any dependency changes to both setup.cfg (and conda-environment.yml if present).
  • Tests are included (or explain why tests are not needed).
  • CHANGES.md entry included if this is a change that can affect users
  • Cylc-Doc issue opened if required at Document Jinja2 access to workflow dir (source or installed) cylc-doc#622
  • If this is a bug fix, PR should be raised against the relevant ?.?.x branch.

@@ -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.

Copy link
Member

@wxtim wxtim left a comment

Choose a reason for hiding this comment

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

  • Read code
  • Tried code

I'm generally happy, but I've made some suggestions about the test.

@hjoliver
Copy link
Member Author

@wxtim - I tweaked the new test as you suggested. A final commit just cleans up the file to pass flake8.

@wxtim wxtim self-requested a review July 10, 2023 09:04
@wxtim
Copy link
Member

wxtim commented Jul 10, 2023

I'll hit merge as soon as the tests finish./

@wxtim wxtim merged commit 6b4055d into cylc:master Jul 10, 2023
25 checks passed
@hjoliver hjoliver deleted the workflow-parsing-cwd branch July 10, 2023 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants