|
| 1 | +import contextlib |
| 2 | +import os |
| 3 | +import shlex |
| 4 | +import shutil |
| 5 | +from typing import Generator |
| 6 | +from typing import Sequence |
| 7 | +from typing import Tuple |
| 8 | + |
| 9 | +from pre_commit.envcontext import envcontext |
| 10 | +from pre_commit.envcontext import PatchesT |
| 11 | +from pre_commit.hook import Hook |
| 12 | +from pre_commit.languages import helpers |
| 13 | +from pre_commit.prefix import Prefix |
| 14 | +from pre_commit.util import clean_path_on_failure |
| 15 | +from pre_commit.util import cmd_output_b |
| 16 | + |
| 17 | +ENVIRONMENT_DIR = 'renv' |
| 18 | +get_default_version = helpers.basic_get_default_version |
| 19 | +healthy = helpers.basic_healthy |
| 20 | + |
| 21 | + |
| 22 | +def get_env_patch(venv: str) -> PatchesT: |
| 23 | + return ( |
| 24 | + ('R_PROFILE_USER', os.path.join(venv, 'activate.R')), |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +@contextlib.contextmanager |
| 29 | +def in_env( |
| 30 | + prefix: Prefix, |
| 31 | + language_version: str, |
| 32 | +) -> Generator[None, None, None]: |
| 33 | + envdir = _get_env_dir(prefix, language_version) |
| 34 | + with envcontext(get_env_patch(envdir)): |
| 35 | + yield |
| 36 | + |
| 37 | + |
| 38 | +def _get_env_dir(prefix: Prefix, version: str) -> str: |
| 39 | + return prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version)) |
| 40 | + |
| 41 | + |
| 42 | +def _prefix_if_file_entry( |
| 43 | + entry: Sequence[str], |
| 44 | + prefix: Prefix, |
| 45 | +) -> Sequence[str]: |
| 46 | + if entry[1] == '-e': |
| 47 | + return entry[1:] |
| 48 | + else: |
| 49 | + return (prefix.path(entry[1]),) |
| 50 | + |
| 51 | + |
| 52 | +def _entry_validate(entry: Sequence[str]) -> None: |
| 53 | + """ |
| 54 | + Allowed entries: |
| 55 | + # Rscript -e expr |
| 56 | + # Rscript path/to/file |
| 57 | + """ |
| 58 | + if entry[0] != 'Rscript': |
| 59 | + raise ValueError('entry must start with `Rscript`.') |
| 60 | + |
| 61 | + if entry[1] == '-e': |
| 62 | + if len(entry) > 3: |
| 63 | + raise ValueError('You can supply at most one expression.') |
| 64 | + elif len(entry) > 2: |
| 65 | + raise ValueError( |
| 66 | + 'The only valid syntax is `Rscript -e {expr}`', |
| 67 | + 'or `Rscript path/to/hook/script`', |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def _cmd_from_hook(hook: Hook) -> Tuple[str, ...]: |
| 72 | + opts = ('--no-save', '--no-restore', '--no-site-file', '--no-environ') |
| 73 | + entry = shlex.split(hook.entry) |
| 74 | + _entry_validate(entry) |
| 75 | + |
| 76 | + return ( |
| 77 | + *entry[:1], *opts, |
| 78 | + *_prefix_if_file_entry(entry, hook.prefix), |
| 79 | + *hook.args, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def install_environment( |
| 84 | + prefix: Prefix, |
| 85 | + version: str, |
| 86 | + additional_dependencies: Sequence[str], |
| 87 | +) -> None: |
| 88 | + env_dir = _get_env_dir(prefix, version) |
| 89 | + with clean_path_on_failure(env_dir): |
| 90 | + os.makedirs(env_dir, exist_ok=True) |
| 91 | + path_desc_source = prefix.path('DESCRIPTION') |
| 92 | + if os.path.exists(path_desc_source): |
| 93 | + shutil.copy(path_desc_source, env_dir) |
| 94 | + shutil.copy(prefix.path('renv.lock'), env_dir) |
| 95 | + cmd_output_b( |
| 96 | + 'Rscript', '--vanilla', '-e', |
| 97 | + """\ |
| 98 | + missing_pkgs <- setdiff( |
| 99 | + "renv", unname(installed.packages()[, "Package"]) |
| 100 | + ) |
| 101 | + options( |
| 102 | + repos = c(CRAN = "https://cran.rstudio.com"), |
| 103 | + renv.consent = TRUE |
| 104 | + ) |
| 105 | + install.packages(missing_pkgs) |
| 106 | + renv::activate() |
| 107 | + renv::restore() |
| 108 | + activate_statement <- paste0( |
| 109 | + 'renv::activate("', file.path(getwd()), '"); ' |
| 110 | + ) |
| 111 | + writeLines(activate_statement, 'activate.R') |
| 112 | + is_package <- tryCatch( |
| 113 | + suppressWarnings( |
| 114 | + unname(read.dcf('DESCRIPTION')[,'Type'] == "Package") |
| 115 | + ), |
| 116 | + error = function(...) FALSE |
| 117 | + ) |
| 118 | + if (is_package) { |
| 119 | + renv::install(normalizePath('.')) |
| 120 | + } |
| 121 | + """, |
| 122 | + cwd=env_dir, |
| 123 | + ) |
| 124 | + if additional_dependencies: |
| 125 | + cmd_output_b( |
| 126 | + 'Rscript', '-e', |
| 127 | + 'renv::install(commandArgs(trailingOnly = TRUE))', |
| 128 | + *additional_dependencies, |
| 129 | + cwd=env_dir, |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +def run_hook( |
| 134 | + hook: Hook, |
| 135 | + file_args: Sequence[str], |
| 136 | + color: bool, |
| 137 | +) -> Tuple[int, bytes]: |
| 138 | + with in_env(hook.prefix, hook.language_version): |
| 139 | + return helpers.run_xargs( |
| 140 | + hook, _cmd_from_hook(hook), file_args, color=color, |
| 141 | + ) |
0 commit comments