Skip to content

Commit

Permalink
Make sure dso_pandocfilter version matches parent dso command version (
Browse files Browse the repository at this point in the history
…#61)

* Make pandocfilter independent of binary

* Update CHANGELOG

* Autoformat

* fix traversable import
  • Loading branch information
grst authored Nov 19, 2024
1 parent d51ec25 commit 0861958
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning][].
requirement introduced by the R API package, see [#50](https://github.com/Boehringer-Ingelheim/dso/issues/50) ([#58](https://github.com/Boehringer-Ingelheim/dso/pull/58)).
- Improve logging for "missing path" warning during `compile-config` ([#59](https://github.com/Boehringer-Ingelheim/dso/pull/59)).
- Improve logging for missing parameters in `dvc.yaml` during `get-config` ([#59](https://github.com/Boehringer-Ingelheim/dso/pull/59)).
- Make sure internal calls to the dso pandocfilter use the same python and dso version as the parent command. This is important for the upcoming `dso-mgr` feature ([#61](https://github.com/Boehringer-Ingelheim/dso/pull/61)).

## v0.9.0

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ urls.Documentation = "https://github.com/Boehringer-Ingelheim/dso"
urls.Home-page = "https://github.com/Boehringer-Ingelheim/dso"
urls.Source = "https://github.com/Boehringer-Ingelheim/dso"
scripts.dso = "dso:cli"
scripts.dso_pandocfilter = "dso.pandocfilter:main"

[tool.hatch.version]
source = "vcs"
Expand Down
8 changes: 7 additions & 1 deletion src/dso/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
from collections.abc import Sequence
from functools import cache
from importlib import resources
from importlib.abc import Traversable

try:
# has been added in Python 3.11
from importlib.resources.abc import Traversable
except ImportError:
# will be removed in Python 3.14
from importlib.abc import Traversable
from os import environ
from pathlib import Path
from typing import Literal
Expand Down
27 changes: 26 additions & 1 deletion src/dso/exec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import stat
import subprocess
import sys
import tempfile
from contextlib import contextmanager
from pathlib import Path
from textwrap import dedent, indent
Expand Down Expand Up @@ -39,7 +41,25 @@ def _render_quarto(quarto_dir: Path, report_dir: Path, before_script: str, cwd:
if f.is_file():
f.unlink()

pandocfilter = "--filter dso_pandocfilter" if with_pandocfilter else ""
# Enable pandocfilter if requested.
# We create a temporary script that then calls the current python binary with the dso.pandocfilter module
# This may seem cumbersome, but we do it this way because
# * pandoc only supports a single binary for `--filter`, referring to subcommands or `-m` is not possible here
# * we want to ensure that exactly the same python/dso version is used for the pandocfilter as for the
# parent command (important when running through dso-mgr)
filter_script = None
if with_pandocfilter:
with tempfile.NamedTemporaryFile(delete=False, mode="w") as f:
f.write("#!/bin/bash\n")
f.write(f'{sys.executable} -m dso.pandocfilter "$@"\n')
filter_script = Path(f.name)

filter_script.chmod(filter_script.stat().st_mode | stat.S_IEXEC)

pandocfilter = f"--filter {filter_script}"
else:
pandocfilter = ""

# propagate quiet setting to quarto
quiet = "--quiet" if bool(int(os.environ.get("DSO_QUIET", 0))) else ""
script = dedent(
Expand All @@ -56,6 +76,11 @@ def _render_quarto(quarto_dir: Path, report_dir: Path, before_script: str, cwd:
"""
)
res = subprocess.run(script, shell=True, executable="/bin/bash", cwd=cwd)

# clean up
if filter_script is not None:
filter_script.unlink()

if res.returncode:
sys.exit(res.returncode)

Expand Down
11 changes: 4 additions & 7 deletions src/dso/pandocfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* warning box at the top
* watermark to all PNG images
Called internally by `dso exec quarto`.
"""

import sys
Expand Down Expand Up @@ -89,10 +91,5 @@ def action(elem, doc):
return elem


def main(doc=None):
"""
A pandoc filter to add warning boxes to documents and watermarks to plots
Called internally by `dso exec quarto`.
"""
return run_filter(action, prepare=prepare, doc=doc)
if __name__ == "__main__":
run_filter(action, prepare=prepare, doc=None)

0 comments on commit 0861958

Please sign in to comment.