2018-02-02 06:08:08 +00:00
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import datetime
|
2020-02-15 16:07:48 +00:00
|
|
|
import requests
|
2018-02-02 06:08:08 +00:00
|
|
|
|
2019-09-08 12:54:51 +00:00
|
|
|
|
2022-01-26 08:29:21 +00:00
|
|
|
from ..semver import parse_version as V
|
2018-02-02 06:08:08 +00:00
|
|
|
from .python import PythonBuildPack
|
2021-12-17 10:14:49 +00:00
|
|
|
from ._r_base import rstudio_base_scripts
|
2018-02-02 06:08:08 +00:00
|
|
|
|
2018-05-23 13:52:24 +00:00
|
|
|
|
2018-02-02 06:08:08 +00:00
|
|
|
class RBuildPack(PythonBuildPack):
|
|
|
|
"""
|
|
|
|
Setup R for use with a repository
|
|
|
|
|
|
|
|
This sets up R + RStudio + IRKernel for a repository that contains:
|
|
|
|
|
2018-02-03 01:38:36 +00:00
|
|
|
1. A `runtime.txt` file with the text:
|
2018-02-02 06:08:08 +00:00
|
|
|
|
2018-02-03 01:38:36 +00:00
|
|
|
r-<year>-<month>-<date>
|
2018-02-02 06:08:08 +00:00
|
|
|
|
2018-02-03 01:38:36 +00:00
|
|
|
Where 'year', 'month' and 'date' refer to a specific
|
2021-12-17 09:47:44 +00:00
|
|
|
date whose CRAN snapshot we will use to fetch packages.
|
|
|
|
Uses https://packagemanager.rstudio.com, or MRAN if no snapshot
|
|
|
|
is found on packagemanager.rstudio.com
|
2018-02-02 06:08:08 +00:00
|
|
|
|
2018-10-30 15:03:38 +00:00
|
|
|
2. A `DESCRIPTION` file signaling an R package
|
2018-10-30 09:32:42 +00:00
|
|
|
|
2021-12-17 09:47:44 +00:00
|
|
|
If there is no `runtime.txt`, then the CRAN snapshot is set to latest
|
2018-10-30 15:03:38 +00:00
|
|
|
date that is guaranteed to exist across timezones.
|
|
|
|
|
2018-11-07 17:52:23 +00:00
|
|
|
Additional R packages are installed if specified either
|
2018-10-30 15:03:38 +00:00
|
|
|
|
|
|
|
- in a file `install.R`, that will be executed at build time,
|
2021-12-17 09:47:44 +00:00
|
|
|
and can be used for installing packages from both CRAN and GitHub
|
2018-10-30 15:03:38 +00:00
|
|
|
|
|
|
|
- as dependencies in a `DESCRIPTION` file
|
|
|
|
|
2020-11-11 09:57:04 +00:00
|
|
|
- are needed by a specific tool
|
2018-02-02 06:08:08 +00:00
|
|
|
|
2022-01-25 08:45:20 +00:00
|
|
|
The `r-base-core` package from Ubuntu or "Ubuntu packages for R"
|
|
|
|
apt repositories is used to install R itself,
|
|
|
|
rather than any of the methods from https://cran.r-project.org/.
|
2019-10-05 18:13:55 +00:00
|
|
|
|
|
|
|
The `r-base-dev` package is installed as advised in RStudio instructions.
|
2018-02-02 06:08:08 +00:00
|
|
|
"""
|
2019-05-31 09:10:17 +00:00
|
|
|
|
2018-02-02 06:08:08 +00:00
|
|
|
@property
|
|
|
|
def runtime(self):
|
|
|
|
"""
|
|
|
|
Return contents of runtime.txt if it exists, '' otherwise
|
|
|
|
"""
|
2019-05-31 09:10:17 +00:00
|
|
|
if not hasattr(self, "_runtime"):
|
|
|
|
runtime_path = self.binder_path("runtime.txt")
|
2018-02-02 06:08:08 +00:00
|
|
|
try:
|
|
|
|
with open(runtime_path) as f:
|
|
|
|
self._runtime = f.read().strip()
|
|
|
|
except FileNotFoundError:
|
2019-05-31 09:10:17 +00:00
|
|
|
self._runtime = ""
|
2018-02-02 06:08:08 +00:00
|
|
|
|
|
|
|
return self._runtime
|
|
|
|
|
2019-09-03 05:20:38 +00:00
|
|
|
@property
|
|
|
|
def r_version(self):
|
|
|
|
"""Detect the R version for a given `runtime.txt`
|
|
|
|
|
2019-09-06 11:52:51 +00:00
|
|
|
Will return the version specified by the user or the current default
|
|
|
|
version.
|
2019-09-03 05:20:38 +00:00
|
|
|
"""
|
2022-06-03 17:46:31 +00:00
|
|
|
version_map = {
|
|
|
|
"4.2": "4.2.0",
|
|
|
|
"4.1": "4.1.3",
|
|
|
|
"4.0": "4.0.5",
|
|
|
|
"3.6": "3.6.3",
|
|
|
|
"3.5": "3.5.3",
|
|
|
|
"3.4": "3.4.0",
|
|
|
|
"3.3": "3.3.3",
|
|
|
|
}
|
|
|
|
|
2019-09-06 11:52:51 +00:00
|
|
|
# the default if nothing is specified
|
2022-06-03 17:22:10 +00:00
|
|
|
r_version = "4.1.0"
|
2019-09-06 11:52:51 +00:00
|
|
|
|
2019-09-03 05:20:38 +00:00
|
|
|
if not hasattr(self, "_r_version"):
|
2019-09-06 10:12:06 +00:00
|
|
|
parts = self.runtime.split("-")
|
|
|
|
if len(parts) == 5:
|
2019-09-06 11:52:51 +00:00
|
|
|
r_version = parts[1]
|
2022-06-03 17:46:31 +00:00
|
|
|
# For versions of form x.y, we want to explicitly provide x.y.z - latest patchlevel
|
|
|
|
# available. Users can howver explicitly specify the full version to get something specific
|
|
|
|
if r_version in version_map:
|
|
|
|
r_version = version_map[r_version]
|
2019-09-06 11:52:51 +00:00
|
|
|
|
2019-09-08 14:02:53 +00:00
|
|
|
# translate to the full version string
|
2022-06-03 17:22:10 +00:00
|
|
|
self._r_version = r_version
|
2019-09-03 05:20:38 +00:00
|
|
|
|
|
|
|
return self._r_version
|
|
|
|
|
2018-02-02 06:08:08 +00:00
|
|
|
@property
|
|
|
|
def checkpoint_date(self):
|
|
|
|
"""
|
2021-12-17 09:47:44 +00:00
|
|
|
Return the date of CRAN checkpoint to use for this repo
|
2018-02-02 06:08:08 +00:00
|
|
|
|
|
|
|
Returns '' if no date is specified
|
|
|
|
"""
|
2019-05-31 09:10:17 +00:00
|
|
|
if not hasattr(self, "_checkpoint_date"):
|
2019-09-06 11:52:51 +00:00
|
|
|
match = re.match(r"r-(\d.\d(.\d)?-)?(\d\d\d\d)-(\d\d)-(\d\d)", self.runtime)
|
2018-02-02 06:08:08 +00:00
|
|
|
if not match:
|
|
|
|
self._checkpoint_date = False
|
2018-02-02 17:37:56 +00:00
|
|
|
else:
|
2019-09-03 05:20:38 +00:00
|
|
|
# turn the last three groups of the match into a date
|
|
|
|
self._checkpoint_date = datetime.date(
|
|
|
|
*[int(s) for s in match.groups()[-3:]]
|
|
|
|
)
|
2018-02-02 06:08:08 +00:00
|
|
|
|
|
|
|
return self._checkpoint_date
|
|
|
|
|
|
|
|
def detect(self):
|
|
|
|
"""
|
|
|
|
Check if current repo should be built with the R Build pack
|
|
|
|
|
2018-11-09 06:40:39 +00:00
|
|
|
super().detect() is not called in this function - it would return
|
|
|
|
false unless a `requirements.txt` is present and we do not want
|
|
|
|
to require the presence of a `requirements.txt` to use R.
|
2018-02-02 06:08:08 +00:00
|
|
|
"""
|
2018-02-03 01:38:36 +00:00
|
|
|
# If no date is found, then self.checkpoint_date will be False
|
|
|
|
# Otherwise, it'll be a date object, which will evaluate to True
|
2018-09-25 08:53:46 +00:00
|
|
|
if self.checkpoint_date:
|
2018-09-20 17:58:45 +00:00
|
|
|
return True
|
2018-10-30 09:32:42 +00:00
|
|
|
|
2019-05-31 09:10:17 +00:00
|
|
|
description_R = "DESCRIPTION"
|
2020-11-11 09:57:04 +00:00
|
|
|
if not self.binder_dir and os.path.exists(description_R):
|
2018-10-30 15:02:40 +00:00
|
|
|
if not self.checkpoint_date:
|
|
|
|
# no R snapshot date set through runtime.txt
|
2021-12-17 08:30:12 +00:00
|
|
|
# Set it to two days ago from today
|
|
|
|
self._checkpoint_date = datetime.date.today() - datetime.timedelta(
|
|
|
|
days=2
|
2019-05-31 09:10:17 +00:00
|
|
|
)
|
2018-10-30 15:02:40 +00:00
|
|
|
self._runtime = "r-{}".format(str(self._checkpoint_date))
|
|
|
|
return True
|
2018-02-02 06:08:08 +00:00
|
|
|
|
|
|
|
def get_path(self):
|
2018-02-03 01:38:36 +00:00
|
|
|
"""
|
|
|
|
Return paths to be added to the PATH environment variable.
|
|
|
|
|
|
|
|
The RStudio package installs its binaries in a non-standard path,
|
|
|
|
so we explicitly add that path to PATH.
|
|
|
|
"""
|
2019-05-31 09:10:17 +00:00
|
|
|
return super().get_path() + ["/usr/lib/rstudio-server/bin/"]
|
2018-02-02 06:08:08 +00:00
|
|
|
|
2018-05-11 12:54:17 +00:00
|
|
|
def get_build_env(self):
|
2018-02-03 01:38:36 +00:00
|
|
|
"""
|
|
|
|
Return environment variables to be set.
|
|
|
|
|
|
|
|
We want libraries to be installed in a path that users can write to
|
|
|
|
without needing root. This is set via the `R_LIBS_USER` environment
|
|
|
|
variable, so we set that here.
|
|
|
|
"""
|
2018-05-11 12:54:17 +00:00
|
|
|
return super().get_build_env() + [
|
2018-02-02 06:08:08 +00:00
|
|
|
# This is the path where user libraries are installed
|
2019-05-31 09:10:17 +00:00
|
|
|
("R_LIBS_USER", "${APP_BASE}/rlibs")
|
2018-02-02 06:08:08 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_packages(self):
|
2018-02-03 01:38:36 +00:00
|
|
|
"""
|
|
|
|
Return list of packages to be installed.
|
|
|
|
|
|
|
|
We install a base version of R, and packages required for RStudio to
|
|
|
|
be installed.
|
|
|
|
"""
|
2019-09-03 05:20:38 +00:00
|
|
|
packages = [
|
|
|
|
# For rstudio
|
|
|
|
"psmisc",
|
|
|
|
"libapparmor1",
|
|
|
|
"sudo",
|
|
|
|
"lsb-release",
|
|
|
|
]
|
|
|
|
|
|
|
|
return super().get_packages().union(packages)
|
2018-02-02 06:08:08 +00:00
|
|
|
|
2022-01-04 13:00:39 +00:00
|
|
|
def get_rspm_snapshot_url(self, snapshot_date, max_days_prior=7):
|
|
|
|
for i in range(max_days_prior):
|
|
|
|
snapshots = requests.post(
|
|
|
|
"https://packagemanager.rstudio.com/__api__/url",
|
|
|
|
# Ask for midnight UTC snapshot
|
|
|
|
json={
|
|
|
|
"repo": "all",
|
2022-01-04 13:04:33 +00:00
|
|
|
"snapshot": (snapshot_date - datetime.timedelta(days=i)).strftime(
|
|
|
|
"%Y-%m-%dT00:00:00Z"
|
|
|
|
),
|
2022-01-04 13:00:39 +00:00
|
|
|
},
|
|
|
|
).json()
|
|
|
|
# Construct a snapshot URL that will give us binary packages for Ubuntu Bionic (18.04)
|
|
|
|
if "upsi" in snapshots:
|
|
|
|
return (
|
|
|
|
"https://packagemanager.rstudio.com/all/__linux__/bionic/"
|
|
|
|
+ snapshots["upsi"]
|
|
|
|
)
|
2022-01-04 13:04:33 +00:00
|
|
|
raise ValueError(
|
|
|
|
"No snapshot found for {} or {} days prior in packagemanager.rstudio.com".format(
|
|
|
|
snapshot_date.strftime("%Y-%m-%d"), max_days_prior
|
|
|
|
)
|
|
|
|
)
|
2022-01-04 13:00:39 +00:00
|
|
|
|
|
|
|
def get_mran_snapshot_url(self, snapshot_date, max_days_prior=7):
|
|
|
|
for i in range(max_days_prior):
|
|
|
|
try_date = snapshot_date - datetime.timedelta(days=i)
|
|
|
|
# Fall back to MRAN if packagemanager.rstudio.com doesn't have it
|
2022-01-04 13:04:33 +00:00
|
|
|
url = "https://mran.microsoft.com/snapshot/{}".format(try_date.isoformat())
|
2022-01-04 13:00:39 +00:00
|
|
|
r = requests.head(url)
|
|
|
|
if r.ok:
|
|
|
|
return url
|
2022-01-05 16:05:07 +00:00
|
|
|
raise ValueError(
|
|
|
|
"No snapshot found for {} or {} days prior in mran.microsoft.com".format(
|
|
|
|
snapshot_date.strftime("%Y-%m-%d"), max_days_prior
|
2022-01-04 13:04:33 +00:00
|
|
|
)
|
2022-01-05 16:05:07 +00:00
|
|
|
)
|
2022-01-04 13:00:39 +00:00
|
|
|
|
|
|
|
def get_cran_mirror_url(self, snapshot_date):
|
|
|
|
# Date after which we will use rspm + binary packages instead of MRAN + source packages
|
|
|
|
rspm_cutoff_date = datetime.date(2022, 1, 1)
|
|
|
|
|
2022-01-04 13:04:33 +00:00
|
|
|
if snapshot_date >= rspm_cutoff_date or self.r_version >= V("4.1"):
|
2022-01-04 13:00:39 +00:00
|
|
|
return self.get_rspm_snapshot_url(snapshot_date)
|
|
|
|
else:
|
|
|
|
return self.get_mran_snapshot_url(snapshot_date)
|
2021-12-17 08:30:12 +00:00
|
|
|
|
2022-01-04 18:42:24 +00:00
|
|
|
def get_devtools_snapshot_url(self):
|
2021-12-17 10:14:49 +00:00
|
|
|
"""
|
2022-01-04 18:42:24 +00:00
|
|
|
Return url of snapshot to use for getting devtools install
|
2021-12-17 10:14:49 +00:00
|
|
|
|
|
|
|
devtools is part of our 'core' base install, so we should have some
|
|
|
|
control over what version we install here.
|
|
|
|
"""
|
2022-01-07 05:47:46 +00:00
|
|
|
# Picked from https://packagemanager.rstudio.com/client/#/repos/1/overview
|
|
|
|
# Hardcoded rather than dynamically determined from a date to avoid extra API calls
|
|
|
|
# Plus, we can always use packagemanager.rstudio.com here as we always install the
|
|
|
|
# necessary apt packages.
|
|
|
|
return "https://packagemanager.rstudio.com/all/__linux__/bionic/2022-01-04+Y3JhbiwyOjQ1MjYyMTU7NzlBRkJEMzg"
|
2021-12-17 10:14:49 +00:00
|
|
|
|
2018-02-02 06:08:08 +00:00
|
|
|
def get_build_scripts(self):
|
2018-02-03 01:38:36 +00:00
|
|
|
"""
|
|
|
|
Return series of build-steps common to all R repositories
|
|
|
|
|
|
|
|
All scripts here should be independent of contents of the repository.
|
|
|
|
|
|
|
|
This sets up:
|
|
|
|
|
2018-11-09 06:40:39 +00:00
|
|
|
- A directory owned by non-root in ${R_LIBS_USER}
|
|
|
|
for installing R packages into
|
2018-02-03 01:38:36 +00:00
|
|
|
- RStudio
|
2018-11-09 06:40:39 +00:00
|
|
|
- R's devtools package, at a particular frozen version
|
2021-12-17 09:47:44 +00:00
|
|
|
(determined by CRAN)
|
2018-02-03 01:38:36 +00:00
|
|
|
- IRKernel
|
|
|
|
- nbrsessionproxy (to access RStudio via Jupyter Notebook)
|
2019-06-25 11:15:45 +00:00
|
|
|
|
|
|
|
We set the snapshot date used to install R libraries from based on the
|
|
|
|
contents of runtime.txt.
|
2018-02-03 01:38:36 +00:00
|
|
|
"""
|
2019-10-07 07:14:26 +00:00
|
|
|
|
2021-12-17 08:30:12 +00:00
|
|
|
cran_mirror_url = self.get_cran_mirror_url(self.checkpoint_date)
|
2019-06-25 11:15:45 +00:00
|
|
|
|
2021-12-20 12:39:24 +00:00
|
|
|
# Determine which R apt repository should be enabled
|
2019-09-08 12:54:51 +00:00
|
|
|
if V(self.r_version) >= V("3.5"):
|
2020-09-14 09:55:52 +00:00
|
|
|
if V(self.r_version) >= V("4"):
|
|
|
|
vs = "40"
|
|
|
|
else:
|
|
|
|
vs = "35"
|
2021-12-20 12:39:24 +00:00
|
|
|
|
|
|
|
scripts = [
|
|
|
|
(
|
|
|
|
"root",
|
|
|
|
rf"""
|
2022-06-03 17:28:21 +00:00
|
|
|
wget --quiet -o /tmp/r-{self.r_version}.deb https://cdn.rstudio.com/r/ubuntu-1804/pkgs/r-{self.r_version}_1_amd64.deb && \
|
|
|
|
dpkg -i /tmp/r-{self.r_version}.deb
|
2021-12-20 12:39:24 +00:00
|
|
|
""",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"root",
|
2022-01-25 12:29:15 +00:00
|
|
|
# we should have --no-install-recommends on all our apt-get install commands,
|
|
|
|
# but here it's important because it will pull in CRAN packages
|
|
|
|
# via r-recommends, which is only guaranteed to be compatible with the latest r-base-core
|
2021-12-20 12:39:24 +00:00
|
|
|
r"""
|
|
|
|
apt-get update > /dev/null && \
|
2022-01-25 08:45:20 +00:00
|
|
|
apt-get install --yes --no-install-recommends \
|
2021-12-20 12:39:24 +00:00
|
|
|
libclang-dev \
|
|
|
|
libzmq3-dev > /dev/null && \
|
|
|
|
apt-get -qq purge && \
|
|
|
|
apt-get -qq clean && \
|
|
|
|
rm -rf /var/lib/apt/lists/*
|
2022-06-03 17:22:10 +00:00
|
|
|
""",
|
2021-12-20 12:39:24 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
scripts += rstudio_base_scripts(self.r_version)
|
2019-09-03 05:20:38 +00:00
|
|
|
|
2021-12-20 12:39:24 +00:00
|
|
|
scripts += [
|
2018-02-02 06:08:08 +00:00
|
|
|
(
|
|
|
|
"root",
|
|
|
|
r"""
|
|
|
|
mkdir -p ${R_LIBS_USER} && \
|
|
|
|
chown -R ${NB_USER}:${NB_USER} ${R_LIBS_USER}
|
2019-05-31 09:10:17 +00:00
|
|
|
""",
|
2021-12-20 12:39:24 +00:00
|
|
|
),
|
2018-02-14 01:56:42 +00:00
|
|
|
(
|
|
|
|
"root",
|
2018-03-13 20:13:03 +00:00
|
|
|
# Set paths so that RStudio shares libraries with base R
|
|
|
|
# install. This first comments out any R_LIBS_USER that
|
|
|
|
# might be set in /etc/R/Renviron and then sets it.
|
2018-02-14 01:56:42 +00:00
|
|
|
r"""
|
2018-03-13 20:13:03 +00:00
|
|
|
sed -i -e '/^R_LIBS_USER=/s/^/#/' /etc/R/Renviron && \
|
|
|
|
echo "R_LIBS_USER=${R_LIBS_USER}" >> /etc/R/Renviron
|
2019-05-31 09:10:17 +00:00
|
|
|
""",
|
2018-02-14 01:56:42 +00:00
|
|
|
),
|
2018-02-03 01:38:36 +00:00
|
|
|
(
|
2021-12-17 08:30:12 +00:00
|
|
|
"root",
|
|
|
|
# RStudio's CRAN mirror needs this to figure out which binary package to serve.
|
|
|
|
# If not set properly, it will just serve up source packages
|
|
|
|
# Quite hilarious, IMO.
|
2022-01-07 05:48:02 +00:00
|
|
|
# See https://docs.rstudio.com/rspm/1.0.12/admin/binaries.html
|
|
|
|
# Set mirror for RStudio too, by modifying rsession.conf
|
2018-02-03 01:38:36 +00:00
|
|
|
r"""
|
2021-12-17 08:30:12 +00:00
|
|
|
R RHOME && \
|
|
|
|
mkdir -p /usr/lib/R/etc /etc/rstudio && \
|
|
|
|
echo 'options(repos = c(CRAN = "{cran_mirror_url}"))' > /usr/lib/R/etc/Rprofile.site && \
|
|
|
|
echo 'options(HTTPUserAgent = sprintf("R/%s R (%s)", getRversion(), paste(getRversion(), R.version$platform, R.version$arch, R.version$os)))' >> /usr/lib/R/etc/Rprofile.site && \
|
|
|
|
echo 'r-cran-repos={cran_mirror_url}' > /etc/rstudio/rsession.conf
|
2018-02-03 01:38:36 +00:00
|
|
|
""".format(
|
2021-12-17 08:30:12 +00:00
|
|
|
cran_mirror_url=cran_mirror_url
|
2019-05-31 09:10:17 +00:00
|
|
|
),
|
2018-05-24 16:48:17 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"${NB_USER}",
|
2022-01-07 05:48:02 +00:00
|
|
|
# Install a pinned version of devtools, IRKernel and shiny
|
2018-05-24 16:48:17 +00:00
|
|
|
r"""
|
2021-12-17 10:14:49 +00:00
|
|
|
R --quiet -e "install.packages(c('devtools', 'IRkernel', 'shiny'), repos='{devtools_cran_mirror_url}')" && \
|
2021-12-17 08:30:12 +00:00
|
|
|
R --quiet -e "IRkernel::installspec(prefix='$NB_PYTHON_PREFIX')"
|
2018-05-25 06:14:14 +00:00
|
|
|
""".format(
|
2022-01-04 18:42:24 +00:00
|
|
|
devtools_cran_mirror_url=self.get_devtools_snapshot_url()
|
2019-05-31 09:10:17 +00:00
|
|
|
),
|
2021-12-17 10:26:00 +00:00
|
|
|
),
|
2018-02-03 01:38:36 +00:00
|
|
|
]
|
|
|
|
|
2019-06-25 11:15:45 +00:00
|
|
|
return super().get_build_scripts() + scripts
|
|
|
|
|
2019-06-29 09:52:01 +00:00
|
|
|
def get_preassemble_script_files(self):
|
2019-07-23 05:33:06 +00:00
|
|
|
files = super().get_preassemble_script_files()
|
2019-06-29 09:52:01 +00:00
|
|
|
installR_path = self.binder_path("install.R")
|
|
|
|
if os.path.exists(installR_path):
|
|
|
|
files[installR_path] = installR_path
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
2019-06-25 11:42:46 +00:00
|
|
|
def get_preassemble_scripts(self):
|
2019-06-29 09:52:01 +00:00
|
|
|
"""Install contents of install.R
|
|
|
|
|
|
|
|
Attempt to execute `install.R` before copying the contents of the
|
|
|
|
repository. We speculate that most of the time we do not need access.
|
|
|
|
In case this fails we re-run it after copying the repository contents.
|
|
|
|
|
|
|
|
The advantage of executing it before copying is that minor edits to the
|
|
|
|
repository content will not trigger a re-install making things faster.
|
|
|
|
"""
|
2019-06-25 11:42:46 +00:00
|
|
|
scripts = []
|
2019-06-25 11:15:45 +00:00
|
|
|
|
2019-05-31 09:10:17 +00:00
|
|
|
installR_path = self.binder_path("install.R")
|
2018-03-15 15:22:01 +00:00
|
|
|
if os.path.exists(installR_path):
|
2019-06-29 09:52:01 +00:00
|
|
|
scripts += [
|
|
|
|
(
|
|
|
|
"${NB_USER}",
|
2022-01-25 15:56:16 +00:00
|
|
|
# Delete /tmp/downloaded_packages only if install.R fails, as the second
|
|
|
|
# invocation of install.R might be able to reuse them
|
|
|
|
"Rscript %s && touch /tmp/.preassembled || true && rm -rf /tmp/downloaded_packages"
|
|
|
|
% installR_path,
|
2019-06-29 09:52:01 +00:00
|
|
|
)
|
|
|
|
]
|
2019-06-25 11:42:46 +00:00
|
|
|
|
|
|
|
return super().get_preassemble_scripts() + scripts
|
|
|
|
|
|
|
|
def get_assemble_scripts(self):
|
2019-06-29 09:52:01 +00:00
|
|
|
"""Install the dependencies of or the repository itself"""
|
2019-06-25 11:42:46 +00:00
|
|
|
assemble_scripts = super().get_assemble_scripts()
|
2018-02-02 06:08:08 +00:00
|
|
|
|
2019-06-29 09:52:01 +00:00
|
|
|
installR_path = self.binder_path("install.R")
|
|
|
|
if os.path.exists(installR_path):
|
|
|
|
assemble_scripts += [
|
|
|
|
(
|
|
|
|
"${NB_USER}",
|
|
|
|
# only run install.R if the pre-assembly failed
|
2022-01-25 15:56:16 +00:00
|
|
|
# Delete any downloaded packages in /tmp, as they aren't reused by R
|
|
|
|
"""if [ ! -f /tmp/.preassembled ]; then Rscript {}; rm -rf /tmp/downloaded_packages; fi""".format(
|
2019-06-29 09:52:01 +00:00
|
|
|
installR_path
|
|
|
|
),
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2019-05-31 09:10:17 +00:00
|
|
|
description_R = "DESCRIPTION"
|
2019-04-27 21:28:50 +00:00
|
|
|
if not self.binder_dir and os.path.exists(description_R):
|
2018-09-17 11:18:33 +00:00
|
|
|
assemble_scripts += [
|
2019-05-31 09:10:17 +00:00
|
|
|
("${NB_USER}", 'R --quiet -e "devtools::install_local(getwd())"')
|
2018-09-17 11:18:33 +00:00
|
|
|
]
|
|
|
|
|
2018-02-02 06:08:08 +00:00
|
|
|
return assemble_scripts
|