summaryrefslogtreecommitdiffstats
path: root/pre_commit/languages/r.py
diff options
context:
space:
mode:
Diffstat (limited to 'pre_commit/languages/r.py')
-rw-r--r--pre_commit/languages/r.py53
1 files changed, 31 insertions, 22 deletions
diff --git a/pre_commit/languages/r.py b/pre_commit/languages/r.py
index 83e6000..d573775 100644
--- a/pre_commit/languages/r.py
+++ b/pre_commit/languages/r.py
@@ -15,6 +15,7 @@ from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output_b
ENVIRONMENT_DIR = 'renv'
+RSCRIPT_OPTS = ('--no-save', '--no-restore', '--no-site-file', '--no-environ')
get_default_version = helpers.basic_get_default_version
healthy = helpers.basic_healthy
@@ -39,14 +40,19 @@ def _get_env_dir(prefix: Prefix, version: str) -> str:
return prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
-def _prefix_if_file_entry(
+def _prefix_if_non_local_file_entry(
entry: Sequence[str],
prefix: Prefix,
+ src: str,
) -> Sequence[str]:
if entry[1] == '-e':
return entry[1:]
else:
- return (prefix.path(entry[1]),)
+ if src == 'local':
+ path = entry[1]
+ else:
+ path = prefix.path(entry[1])
+ return (path,)
def _entry_validate(entry: Sequence[str]) -> None:
@@ -69,13 +75,12 @@ def _entry_validate(entry: Sequence[str]) -> None:
def _cmd_from_hook(hook: Hook) -> Tuple[str, ...]:
- opts = ('--no-save', '--no-restore', '--no-site-file', '--no-environ')
entry = shlex.split(hook.entry)
_entry_validate(entry)
return (
- *entry[:1], *opts,
- *_prefix_if_file_entry(entry, hook.prefix),
+ *entry[:1], *RSCRIPT_OPTS,
+ *_prefix_if_non_local_file_entry(entry, hook.prefix, hook.src),
*hook.args,
)
@@ -89,29 +94,32 @@ def install_environment(
with clean_path_on_failure(env_dir):
os.makedirs(env_dir, exist_ok=True)
shutil.copy(prefix.path('renv.lock'), env_dir)
+ shutil.copytree(prefix.path('renv'), os.path.join(env_dir, 'renv'))
cmd_output_b(
'Rscript', '--vanilla', '-e',
f"""\
prefix_dir <- {prefix.prefix_dir!r}
- missing_pkgs <- setdiff(
- "renv", unname(installed.packages()[, "Package"])
- )
options(
repos = c(CRAN = "https://cran.rstudio.com"),
renv.consent = TRUE
)
- install.packages(missing_pkgs)
- renv::activate()
+ source("renv/activate.R")
renv::restore()
activate_statement <- paste0(
- 'renv::activate("', file.path(getwd()), '"); '
+ 'suppressWarnings({{',
+ 'old <- setwd("', getwd(), '"); ',
+ 'source("renv/activate.R"); ',
+ 'setwd(old); ',
+ 'renv::load("', getwd(), '");}})'
)
writeLines(activate_statement, 'activate.R')
- is_package <- tryCatch({{
- content_desc <- read.dcf(file.path(prefix_dir, 'DESCRIPTION'))
- suppressWarnings(unname(content_desc[,'Type']) == "Package")
- }},
- error = function(...) FALSE
+ is_package <- tryCatch(
+ {{
+ path_desc <- file.path(prefix_dir, 'DESCRIPTION')
+ suppressWarnings(desc <- read.dcf(path_desc))
+ "Package" %in% colnames(desc)
+ }},
+ error = function(...) FALSE
)
if (is_package) {{
renv::install(prefix_dir)
@@ -120,12 +128,13 @@ def install_environment(
cwd=env_dir,
)
if additional_dependencies:
- cmd_output_b(
- 'Rscript', '-e',
- 'renv::install(commandArgs(trailingOnly = TRUE))',
- *additional_dependencies,
- cwd=env_dir,
- )
+ with in_env(prefix, version):
+ cmd_output_b(
+ 'Rscript', *RSCRIPT_OPTS, '-e',
+ 'renv::install(commandArgs(trailingOnly = TRUE))',
+ *additional_dependencies,
+ cwd=env_dir,
+ )
def run_hook(