-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into consolidate-gitignores
- Loading branch information
Showing
30 changed files
with
668 additions
and
505 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,4 @@ | ||
import logging | ||
import os | ||
from ._metadata import __version__ # noqa | ||
from .api import here, read_params, set_stage, stage_here | ||
|
||
import rich_click as click | ||
|
||
from ._logging import log | ||
from ._metadata import __version__ | ||
from .compile_config import cli as compile_config_cli | ||
from .create import cli as create_cli | ||
from .exec import cli as exec_cli | ||
from .get_config import cli as get_config_cli | ||
from .init import cli as init_cli | ||
from .lint import cli as lint_cli | ||
from .repro import cli as repro_cli | ||
from .watermark import cli as watermark_cli | ||
|
||
click.rich_click.USE_MARKDOWN = True | ||
|
||
|
||
@click.group() | ||
@click.option( | ||
"-q", | ||
"--quiet", | ||
count=True, | ||
help=( | ||
"Reduce verbosity. `-q` disables info messages, `-qq` disables warnings. Errors messages cannot be disabled. " | ||
"The same can be achieved by setting the env var `DSO_QUIET=1` or `DSO_QUIET=2`, respectively." | ||
), | ||
default=int(os.environ.get("DSO_QUIET", 0)), | ||
) | ||
@click.option( | ||
"-v", | ||
"--verbose", | ||
help=( | ||
"Increase logging verbosity to include debug messages. " | ||
"The same can be achieved by setting the env var `DSO_VERBOSE=1`." | ||
), | ||
default=bool(int(os.environ.get("DSO_VERBOSE", 0))), | ||
is_flag=True, | ||
) | ||
@click.version_option(version=__version__, prog_name="dso") | ||
def cli(quiet: int, verbose: bool): | ||
"""Root command""" | ||
if quiet >= 2: | ||
log.setLevel(logging.ERROR) | ||
os.environ["DSO_QUIET"] = "2" | ||
elif quiet == 1: | ||
log.setLevel(logging.WARNING) | ||
os.environ["DSO_QUIET"] = "1" | ||
elif verbose: | ||
log.setLevel(logging.DEBUG) | ||
os.environ["DSO_VERBOSE"] = "1" | ||
|
||
|
||
cli.add_command(create_cli) | ||
cli.add_command(init_cli) | ||
cli.add_command(compile_config_cli) | ||
cli.add_command(repro_cli) | ||
cli.add_command(exec_cli) | ||
cli.add_command(lint_cli) | ||
cli.add_command(get_config_cli) | ||
cli.add_command(watermark_cli) | ||
__all__ = ["read_params", "here", "stage_here", "set_stage"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"""Helper functions for rendering quarto documents""" | ||
|
||
import os | ||
import stat | ||
import subprocess | ||
import sys | ||
import tempfile | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
from textwrap import dedent, indent | ||
|
||
from ruamel.yaml import YAML | ||
|
||
|
||
def render_quarto(quarto_dir: Path, report_dir: Path, before_script: str, cwd: Path, with_pandocfilter: bool = False): | ||
""" | ||
Render a quarto project | ||
Parameters | ||
---------- | ||
quarto_dir | ||
Path that contains the _quarto.yml document | ||
report_dir | ||
Output directory of the rendered document | ||
before_script | ||
Bash snippet to execute before running quarto (e.g. to setup the enviornment) | ||
""" | ||
before_script = indent(before_script, " " * 8) | ||
report_dir = report_dir.absolute() | ||
report_dir.mkdir(exist_ok=True) | ||
|
||
# clean up existing `.rmarkdown` files that may interfere with rendering | ||
# these are leftovers from a previous, failed `quarto render` attempt. If they still exist, the next attempt | ||
# fails. We remove them *before* the run instead of cleaning them up *after* the run, because they | ||
# may be usefule for debugging failures. | ||
# see https://github.com/Boehringer-Ingelheim/dso/issues/54 | ||
for f in quarto_dir.glob("*.rmarkdown"): | ||
if f.is_file(): | ||
f.unlink() | ||
|
||
# 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( | ||
f"""\ | ||
#!/bin/bash | ||
set -euo pipefail | ||
# this flags enables building larger reports with embedded resources | ||
export QUARTO_DENO_V8_OPTIONS=--max-old-space-size=8192 | ||
{before_script} | ||
quarto render "{quarto_dir}" --output-dir "{report_dir}" {quiet} {pandocfilter} | ||
""" | ||
) | ||
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) | ||
|
||
|
||
@contextmanager | ||
def quarto_config_yml(quarto_config: dict | None, quarto_dir: Path): | ||
"""Context manager that temporarily creates a _quarto.yml file and cleans up after itself""" | ||
if quarto_config is None: | ||
quarto_config = {} | ||
config_file = quarto_dir / "_quarto.yml" | ||
yaml = YAML(typ="safe") | ||
yaml.dump(quarto_config, config_file) | ||
try: | ||
yield | ||
finally: | ||
config_file.unlink() |
Oops, something went wrong.