summaryrefslogtreecommitdiffstats
path: root/pre_commit/languages/r.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-05-19 09:39:52 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-05-19 09:39:52 +0000
commitbcd2b7947fd4b94e2fbb12b861fee6b8fa12a8f7 (patch)
tree6df77839e91dbb56e97a6430db58ec5262d8dfc3 /pre_commit/languages/r.py
parentAdding upstream version 3.3.1. (diff)
downloadpre-commit-bcd2b7947fd4b94e2fbb12b861fee6b8fa12a8f7.tar.xz
pre-commit-bcd2b7947fd4b94e2fbb12b861fee6b8fa12a8f7.zip
Adding upstream version 3.3.2.upstream/3.3.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pre_commit/languages/r.py')
-rw-r--r--pre_commit/languages/r.py53
1 files changed, 37 insertions, 16 deletions
diff --git a/pre_commit/languages/r.py b/pre_commit/languages/r.py
index 138a26e..6feb065 100644
--- a/pre_commit/languages/r.py
+++ b/pre_commit/languages/r.py
@@ -4,6 +4,8 @@ import contextlib
import os
import shlex
import shutil
+import tempfile
+import textwrap
from typing import Generator
from typing import Sequence
@@ -21,6 +23,19 @@ get_default_version = lang_base.basic_get_default_version
health_check = lang_base.basic_health_check
+@contextlib.contextmanager
+def _r_code_in_tempfile(code: str) -> Generator[str, None, None]:
+ """
+ To avoid quoting and escaping issues, avoid `Rscript [options] -e {expr}`
+ but use `Rscript [options] path/to/file_with_expr.R`
+ """
+ with tempfile.TemporaryDirectory() as tmpdir:
+ fname = os.path.join(tmpdir, 'script.R')
+ with open(fname, 'w') as f:
+ f.write(_inline_r_setup(textwrap.dedent(code)))
+ yield fname
+
+
def get_env_patch(venv: str) -> PatchesT:
return (
('R_PROFILE_USER', os.path.join(venv, 'activate.R')),
@@ -93,6 +108,8 @@ def install_environment(
version: str,
additional_dependencies: Sequence[str],
) -> None:
+ lang_base.assert_version_default('r', version)
+
env_dir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
os.makedirs(env_dir, exist_ok=True)
shutil.copy(prefix.path('renv.lock'), env_dir)
@@ -127,20 +144,19 @@ def install_environment(
}}
"""
- cmd_output_b(
- _rscript_exec(), '--vanilla', '-e',
- _inline_r_setup(r_code_inst_environment),
- cwd=env_dir,
- )
+ with _r_code_in_tempfile(r_code_inst_environment) as f:
+ cmd_output_b(_rscript_exec(), '--vanilla', f, cwd=env_dir)
+
if additional_dependencies:
r_code_inst_add = 'renv::install(commandArgs(trailingOnly = TRUE))'
with in_env(prefix, version):
- cmd_output_b(
- _rscript_exec(), *RSCRIPT_OPTS, '-e',
- _inline_r_setup(r_code_inst_add),
- *additional_dependencies,
- cwd=env_dir,
- )
+ with _r_code_in_tempfile(r_code_inst_add) as f:
+ cmd_output_b(
+ _rscript_exec(), *RSCRIPT_OPTS,
+ f,
+ *additional_dependencies,
+ cwd=env_dir,
+ )
def _inline_r_setup(code: str) -> str:
@@ -148,11 +164,16 @@ def _inline_r_setup(code: str) -> str:
Some behaviour of R cannot be configured via env variables, but can
only be configured via R options once R has started. These are set here.
"""
- with_option = f"""\
- options(install.packages.compile.from.source = "never", pkgType = "binary")
- {code}
- """
- return with_option
+ with_option = [
+ textwrap.dedent("""\
+ options(
+ install.packages.compile.from.source = "never",
+ pkgType = "binary"
+ )
+ """),
+ code,
+ ]
+ return '\n'.join(with_option)
def run_hook(