Skip to content

Commit

Permalink
make Rscript a variable and re-use
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Nov 25, 2022
1 parent 7bd54d8 commit 2b8c580
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pre_commit/languages/r.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from pre_commit.util import win_exe

ENVIRONMENT_DIR = 'renv'
RSCRIPT_PROGRAM = 'Rscript'
RSCRIPT_OPTS = ('--no-save', '--no-restore', '--no-site-file', '--no-environ')
get_default_version = helpers.basic_get_default_version
health_check = helpers.basic_health_check
Expand Down Expand Up @@ -61,7 +62,7 @@ def _prefix_if_non_local_file_entry(

def _rscript_exec() -> str:
r_home = os.environ.get('R_HOME')
program = win_exe('Rscript')
program = win_exe(RSCRIPT_PROGRAM)
if r_home is None:
return program
else:
Expand All @@ -74,16 +75,16 @@ def _entry_validate(entry: Sequence[str]) -> None:
# Rscript -e expr
# Rscript path/to/file
"""
if entry[0] != 'Rscript':
raise ValueError('entry must start with `Rscript`.')
if entry[0] != RSCRIPT_PROGRAM:
raise ValueError(f'entry must start with `{RSCRIPT_PROGRAM}`.')

if entry[1] == '-e':
if len(entry) > 3:
raise ValueError('You can supply at most one expression.')
elif len(entry) > 2:
raise ValueError(
'The only valid syntax is `Rscript -e {expr}`',
'or `Rscript path/to/hook/script`',
f'The only valid syntax is `{RSCRIPT_PROGRAM} -e {{expr}}`',
f'or `{RSCRIPT_PROGRAM} path/to/hook/script`',
)


Expand Down

0 comments on commit 2b8c580

Please sign in to comment.