1515from pre_commit .envcontext import UNSET
1616from pre_commit .prefix import Prefix
1717from pre_commit .util import cmd_output
18- from pre_commit .util import cmd_output_b
1918from pre_commit .util import win_exe
2019
2120ENVIRONMENT_DIR = 'renv'
22- RSCRIPT_OPTS = ('--no-save' , '--no-restore' , '--no-site-file' , '--no-environ' )
2321get_default_version = lang_base .basic_get_default_version
2422
23+ _RENV_ACTIVATED_OPTS = (
24+ '--no-save' , '--no-restore' , '--no-site-file' , '--no-environ' ,
25+ )
2526
26- def _execute_vanilla_r_code_as_script (
27+
28+ def _execute_r (
2729 code : str , * ,
2830 prefix : Prefix , version : str , args : Sequence [str ] = (), cwd : str ,
31+ cli_opts : Sequence [str ],
2932) -> str :
3033 with in_env (prefix , version ), _r_code_in_tempfile (code ) as f :
3134 _ , out , _ = cmd_output (
32- _rscript_exec (), * RSCRIPT_OPTS , f , * args , cwd = cwd ,
35+ _rscript_exec (), * cli_opts , f , * args , cwd = cwd ,
3336 )
3437 return out .rstrip ('\n ' )
3538
3639
40+ def _execute_r_in_renv (
41+ code : str , * ,
42+ prefix : Prefix , version : str , args : Sequence [str ] = (), cwd : str ,
43+ ) -> str :
44+ return _execute_r (
45+ code = code , prefix = prefix , version = version , args = args , cwd = cwd ,
46+ cli_opts = _RENV_ACTIVATED_OPTS ,
47+ )
48+
49+
50+ def _execute_vanilla_r (
51+ code : str , * ,
52+ prefix : Prefix , version : str , args : Sequence [str ] = (), cwd : str ,
53+ ) -> str :
54+ return _execute_r (
55+ code = code , prefix = prefix , version = version , args = args , cwd = cwd ,
56+ cli_opts = ('--vanilla' ,),
57+ )
58+
59+
3760def _read_installed_version (envdir : str , prefix : Prefix , version : str ) -> str :
38- return _execute_vanilla_r_code_as_script (
61+ return _execute_r_in_renv (
3962 'cat(renv::settings$r.version())' ,
4063 prefix = prefix , version = version ,
4164 cwd = envdir ,
4265 )
4366
4467
4568def _read_executable_version (envdir : str , prefix : Prefix , version : str ) -> str :
46- return _execute_vanilla_r_code_as_script (
69+ return _execute_r_in_renv (
4770 'cat(as.character(getRversion()))' ,
4871 prefix = prefix , version = version ,
4972 cwd = envdir ,
@@ -53,7 +76,7 @@ def _read_executable_version(envdir: str, prefix: Prefix, version: str) -> str:
5376def _write_current_r_version (
5477 envdir : str , prefix : Prefix , version : str ,
5578) -> None :
56- _execute_vanilla_r_code_as_script (
79+ _execute_r_in_renv (
5780 'renv::settings$r.version(as.character(getRversion()))' ,
5881 prefix = prefix , version = version ,
5982 cwd = envdir ,
@@ -161,7 +184,7 @@ def _cmd_from_hook(
161184 _entry_validate (cmd )
162185
163186 cmd_part = _prefix_if_file_entry (cmd , prefix , is_local = is_local )
164- return (cmd [0 ], * RSCRIPT_OPTS , * cmd_part , * args )
187+ return (cmd [0 ], * _RENV_ACTIVATED_OPTS , * cmd_part , * args )
165188
166189
167190def install_environment (
@@ -204,14 +227,15 @@ def install_environment(
204227 renv::install(prefix_dir)
205228 }}
206229 """
207-
208- with _r_code_in_tempfile (r_code_inst_environment ) as f :
209- cmd_output_b (_rscript_exec (), '--vanilla' , f , cwd = env_dir )
230+ _execute_vanilla_r (
231+ r_code_inst_environment ,
232+ prefix = prefix , version = version , cwd = env_dir ,
233+ )
210234
211235 _write_current_r_version (envdir = env_dir , prefix = prefix , version = version )
212236 if additional_dependencies :
213237 r_code_inst_add = 'renv::install(commandArgs(trailingOnly = TRUE))'
214- _execute_vanilla_r_code_as_script (
238+ _execute_r_in_renv (
215239 code = r_code_inst_add , prefix = prefix , version = version ,
216240 args = additional_dependencies ,
217241 cwd = env_dir ,
0 commit comments