Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
pre-commit/pre_commit/languages/r.py at main · JunaidLoonat/pre-commit · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
JunaidLoonat
/
pre-commit
Public
forked from
pre-commit/pre-commit
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
pre-commit
/
pre_commit
/
languages
/
r.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
195 lines (166 loc) · 5.42 KB
main
Breadcrumbs
pre-commit
/
pre_commit
/
languages
/
r.py
Copy path
Top
File metadata and controls
Code
Blame
195 lines (166 loc) · 5.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from
__future__
import
annotations
import
contextlib
import
os
import
shlex
import
shutil
import
tempfile
import
textwrap
from
collections
.
abc
import
Generator
from
collections
.
abc
import
Sequence
from
pre_commit
import
lang_base
from
pre_commit
.
envcontext
import
envcontext
from
pre_commit
.
envcontext
import
PatchesT
from
pre_commit
.
envcontext
import
UNSET
from
pre_commit
.
prefix
import
Prefix
from
pre_commit
.
util
import
cmd_output_b
from
pre_commit
.
util
import
win_exe
ENVIRONMENT_DIR
=
'renv'
RSCRIPT_OPTS
=
(
'--no-save'
,
'--no-restore'
,
'--no-site-file'
,
'--no-environ'
)
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'
)),
(
'RENV_PROJECT'
,
UNSET
),
)
@
contextlib
.
contextmanager
def
in_env
(
prefix
:
Prefix
,
version
:
str
)
->
Generator
[
None
,
None
,
None
]:
envdir
=
lang_base
.
environment_dir
(
prefix
,
ENVIRONMENT_DIR
,
version
)
with
envcontext
(
get_env_patch
(
envdir
)):
yield
def
_prefix_if_file_entry
(
entry
:
list
[
str
],
prefix
:
Prefix
,
*
,
is_local
:
bool
,
)
->
Sequence
[
str
]:
if
entry
[
1
]
==
'-e'
or
is_local
:
return
entry
[
1
:]
else
:
return
(
prefix
.
path
(
entry
[
1
]),)
def
_rscript_exec
()
->
str
:
r_home
=
os
.
environ
.
get
(
'R_HOME'
)
if
r_home
is
None
:
return
'Rscript'
else
:
return
os
.
path
.
join
(
r_home
,
'bin'
,
win_exe
(
'Rscript'
))
def
_entry_validate
(
entry
:
list
[
str
])
->
None
:
"""
Allowed entries:
# Rscript -e expr
# Rscript path/to/file
"""
if
entry
[
0
]
!=
'Rscript'
:
raise
ValueError
(
'entry must start with `Rscript`.'
)
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`'
,
)
def
_cmd_from_hook
(
prefix
:
Prefix
,
entry
:
str
,
args
:
Sequence
[
str
],
*
,
is_local
:
bool
,
)
->
tuple
[
str
, ...]:
cmd
=
shlex
.
split
(
entry
)
_entry_validate
(
cmd
)
cmd_part
=
_prefix_if_file_entry
(
cmd
,
prefix
,
is_local
=
is_local
)
return
(
cmd
[
0
],
*
RSCRIPT_OPTS
,
*
cmd_part
,
*
args
)
def
install_environment
(
prefix
:
Prefix
,
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
)
shutil
.
copytree
(
prefix
.
path
(
'renv'
),
os
.
path
.
join
(
env_dir
,
'renv'
))
r_code_inst_environment
=
f"""
\
prefix_dir <-
{
prefix
.
prefix_dir
!r
}
options(
repos = c(CRAN = "https://cran.rstudio.com"),
renv.consent = TRUE
)
source("renv/activate.R")
renv::restore()
activate_statement <- paste0(
'suppressWarnings({{',
'old <- setwd("', getwd(), '"); ',
'source("renv/activate.R"); ',
'setwd(old); ',
'renv::load("', getwd(), '");}})'
)
writeLines(activate_statement, 'activate.R')
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)
}}
"""
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
):
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
:
"""
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
=
[
textwrap
.
dedent
(
"""
\
options(
install.packages.compile.from.source = "never",
pkgType = "binary"
)
"""
),
code
,
]
return
'
\n
'
.
join
(
with_option
)
def
run_hook
(
prefix
:
Prefix
,
entry
:
str
,
args
:
Sequence
[
str
],
file_args
:
Sequence
[
str
],
*
,
is_local
:
bool
,
require_serial
:
bool
,
color
:
bool
,
)
->
tuple
[
int
,
bytes
]:
cmd
=
_cmd_from_hook
(
prefix
,
entry
,
args
,
is_local
=
is_local
)
return
lang_base
.
run_xargs
(
cmd
,
file_args
,
require_serial
=
require_serial
,
color
=
color
,
)
You can’t perform that action at this time.