-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
550 lines (475 loc) · 17.9 KB
/
run.sh
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#!/bin/bash
# -*- sh-basic-offset: 4; sh-indentation: 4 -*-
# Bootstrap a CI environment for R
set -e
# Comment out this line for quieter output:
# Or ratherm set it for a lot noisier output
# set -x
# Where do we run? OS can be 'Linux' or 'Darwin', ARCH can be 'x86_64' or 'arm64'
OS=$(uname -s)
ARCH=$(uname -m)
# Default CRAN repo (use the CDN) and R verssion
CRAN=${CRAN:-"https://cloud.r-project.org"}
RVER=${RVER:-"4.3.1"}
## Optional drat repos, unset by default
DRAT_REPOS=${DRAT_REPOS:-""}
## Optional BSPM use, defaults to true for r2u
USE_BSPM=${USE_BSPM:-"TRUE"}
## Optional additional PPAs, unset by default
ADDED_PPAS=${ADDED_PPAS:-""}
## Optional trimming of extra apt source list entry, defaults to true
TRIM_APT_SOURCES=${TRIM_APT_SOURCES:-"TRUE"}
## Optional setting of type argument in covr::coverage() call below, defaults to "tests"
COVERAGE_TYPE=${COVERAGE_TYPE:-"tests"}
R_BUILD_ARGS=${R_BUILD_ARGS-"--no-build-vignettes --no-manual"}
R_CHECK_ARGS=${R_CHECK_ARGS-"--no-vignettes --no-manual --as-cran"}
R_CHECK_INSTALL_ARGS=${R_CHECK_INSTALL_ARGS-"--install-args=--install-tests"}
_R_CHECK_TESTS_NLINES_=0
ShowBanner() {
echo ""
echo "r-ci: Portable CI for R at Travis, GitHub Actions, Azure, ..."
echo ""
echo "Current Ubuntu distribution per 'lsb_release': '$(lsb_release -ds)' aka '$(lsb_release -cs)'"
echo ""
}
Bootstrap() {
SetRepos
if [[ "Darwin" == "${OS}" ]]; then
BootstrapMac
elif [[ "Linux" == "${OS}" ]]; then
BootstrapLinux
else
echo "Unknown OS: ${OS}"
exit 1
fi
#if ! (test -e .Rbuildignore && grep -q 'travis-tool' .Rbuildignore); then
# echo '^travis-tool\.sh$' >>.Rbuildignore
#fi
if ! (test -e .Rbuildignore && grep -q 'run.sh' .Rbuildignore); then
echo '^run\.sh$' >> .Rbuildignore
fi
#if ! (test -e .Rbuildignore && grep -q 'travis_wait' .Rbuildignore); then
# echo '^travis_wait_.*\.log$' >> .Rbuildignore
#fi
# Make sure unit test package (among testthat, tinytest, RUnit) installed
EnsureUnittestRunner
# Report version
Rscript -e 'sessionInfo()'
}
SetRepos() {
echo "local({" >> ~/.Rprofile
echo " r <- getOption(\"repos\");" >> ~/.Rprofile
echo " r[\"CRAN\"] <- \"${CRAN}\"" >> ~/.Rprofile
for d in ${DRAT_REPOS}; do
echo " r[\"${d}\"] <- \"https://${d}.github.io/drat\"" >> ~/.Rprofile
done
echo " options(repos=r)" >> ~/.Rprofile
echo "})" >> ~/.Rprofile
}
InstallPandoc() {
## deprecated 2020-Sep
echo "Deprecated"
}
BootstrapLinux() {
## Check for sudo_release and install if needed
test -x /usr/bin/sudo || apt-get install -y --no-install-recommends sudo
## Hotfix for key issue
echo 'Acquire::AllowInsecureRepositories "true";' | sudo tee /etc/apt/apt.conf.d/90local-secure >/dev/null
## Check for lsb_release and install if needed
test -x /usr/bin/lsb_release || sudo apt-get install -y --no-install-recommends lsb-release
## Check for add-apt-repository and install if needed, using a fudge around the (manual) tz config dialog
test -x /usr/bin/add-apt-repository || \
(echo 12 > /tmp/input.txt; echo 5 >> /tmp/input.txt; sudo apt-get install -y tzdata < /tmp/input.txt; sudo apt-get install -y --no-install-recommends software-properties-common)
ShowBanner
## If opted in, trim apt sources
if [[ "${TRIM_APT_SOURCES}" != "FALSE" ]]; then
sudo rm -vf /etc/apt/sources.list.d/*.list
fi
## from r2u setup script
sudo apt update -qq && sudo apt install --yes --no-install-recommends wget ca-certificates dirmngr gnupg gpg-agent
wget -q -O- https://eddelbuettel.github.io/r2u/assets/dirk_eddelbuettel_key.asc | sudo tee -a /etc/apt/trusted.gpg.d/cranapt_key.asc
echo "deb [arch=amd64] https://r2u.stat.illinois.edu/ubuntu $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/cranapt.list
wget -q -O- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
echo "deb [arch=amd64] https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/" | sudo tee -a /etc/apt/sources.list.d/cran_r.list
echo "Package: *" | sudo tee -a /etc/apt/preferences.d/99cranapt
echo "Pin: release o=CRAN-Apt Project" | sudo tee -a /etc/apt/preferences.d/99cranapt
echo "Pin: release l=CRAN-Apt Packages" | sudo tee -a /etc/apt/preferences.d/99cranapt
echo "Pin-Priority: 700" | sudo tee -a /etc/apt/preferences.d/99cranapt
## Set up our CRAN mirror.
# No longer need c2d4u when using r2u
## Get the key if it is missing
#if ! test -f /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc; then
# wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
#fi
## Add the repo
## need pinning to ensure repo sorts higher, note we also pin r2u
#echo "Package: *" | sudo tee /etc/apt/preferences.d/c2d4u-pin >/dev/null
#echo "Pin: release o=LP-PPA-c2d4u.team-c2d4u4.0+" | sudo tee -a /etc/apt/preferences.d/c2d4u-pin >/dev/null
#echo "Pin-Priority: 600" | sudo tee -a /etc/apt/preferences.d/c2d4u-pin >/dev/null
## now add repo (and update index)
#sudo add-apt-repository -y "deb ${CRAN}/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
# Add marutter's c2d4u repository.
# R 4.0 (not needed as CRAN current) and c2d4u/4.0 variant as backup
#sudo add-apt-repository -y "ppa:marutter/rrutter4.0"
# No longer need c2d4u when using r2u
#sudo add-apt-repository -y "ppa:c2d4u.team/c2d4u4.0+"
## Added PPAs, if given
if [[ "${ADDED_PPAS}" != "" ]]; then
for ppa in "${ADDED_PPAS}"; do
sudo add-apt-repository -y "${ppa}"
done
fi
# Update after adding all repositories. Retry several times to work around
# flaky connection to Launchpad PPAs.
Retry sudo apt-get update -qq
# Install an R development environment. qpdf is also needed for
# --as-cran checks:
# https://stat.ethz.ch/pipermail/r-help//2012-September/335676.html
# May 2020: we also need devscripts for checkbashism
# Sep 2020: add bspm and remotes
Retry sudo apt-get install -y --no-install-recommends r-base-dev r-recommended qpdf devscripts r-cran-bspm r-cran-remotes
#sudo cp -ax /usr/lib/R/site-library/littler/examples/{build.r,check.r,install*.r,update.r} /usr/local/bin
## for now also from littler from GH
#sudo install.r remotes
#sudo installGithub.r eddelbuettel/littler
#sudo cp -ax /usr/local/lib/R/site-library/littler/examples/{check.r,install*.r} /usr/local/bin
# Default to no recommends
echo 'APT::Install-Recommends "false";' | sudo tee /etc/apt/apt.conf.d/90local-no-recommends >/dev/null
# Change permissions for /usr/local/lib/R/site-library
# This should really be via 'staff adduser travis staff'
# but that may affect only the next shell
sudo chmod 2777 /usr/local/lib/R /usr/local/lib/R/site-library
# Process options
BootstrapLinuxOptions
}
BootstrapLinuxOptions() {
if [[ -n "$BOOTSTRAP_LATEX" ]]; then
# We add a backports PPA for more recent TeX packages.
# sudo add-apt-repository -y "ppa:texlive-backports/ppa"
Retry sudo apt-get install -y --no-install-recommends \
texlive-base texlive-latex-base \
texlive-fonts-recommended texlive-fonts-extra \
texlive-extra-utils texlive-latex-recommended texlive-latex-extra \
texinfo lmodern
# no longer exists: texlive-generic-recommended
fi
if [[ "${USE_BSPM}" != "FALSE" ]]; then
## sudo Rscript --vanilla -e 'install.packages("bspm", repos="https://cran.r-project.org")'
## sudo Rscript --vanilla -e 'remotes::install_github("Enchufa2/bspm")'
## for now to get 0.4.0.1 with type="binary-source"
## curl -OLs https://eddelbuettel.github.io/r-ci/bspm_0.4.0.1.tar.gz && sudo R CMD INSTALL bspm_0.4.0.1.tar.gz
## 2023-02-20 for now stick with 0.3.10
## sudo Rscript --vanilla -e 'remotes::install_url("https://cloud.r-project.org/src/contrib/Archive/bspm/bspm_0.3.10.tar.gz")'
## 2023-03-17 back bspm now at 0.5.1
echo "options(bspm.sudo = TRUE)" | sudo tee --append /etc/R/Rprofile.site >/dev/null
echo "suppressMessages(bspm::enable())" | sudo tee --append /etc/R/Rprofile.site >/dev/null
echo "options(bspm.version.check=FALSE)" | sudo tee --append /etc/R/Rprofile.site >/dev/null
#echo "options(bspm.sudo=TRUE)" | sudo tee --append /etc/R/Rprofile.site >/dev/null
fi
}
BootstrapMac() {
# Install from latest CRAN binary build for OS X (given ${ARCH} from 'uname -m')
wget ${CRAN}/bin/macosx/big-sur-${ARCH}/base/R-${RVER}-${ARCH}.pkg -O /tmp/R-latest.pkg
echo "Installing macOS binary package for R on ${ARCH}"
sudo installer -pkg "/tmp/R-latest.pkg" -target /
rm "/tmp/R-latest.pkg"
# Process options
BootstrapMacOptions
# Default packages
sudo Rscript -e 'install.packages(c("remotes"))'
}
BootstrapMacOptions() {
if [[ -n "$BOOTSTRAP_LATEX" ]]; then
# TODO: Install MacTeX.pkg once there's enough disk space
MACTEX=BasicTeX.pkg
wget http://ctan.math.utah.edu/ctan/tex-archive/systems/mac/mactex/$MACTEX -O "/tmp/$MACTEX"
echo "Installing OS X binary package for MacTeX"
sudo installer -pkg "/tmp/$MACTEX" -target /
rm "/tmp/$MACTEX"
# We need a few more packages than the basic package provides; this
# post saved me so much pain:
# https://stat.ethz.ch/pipermail/r-sig-mac/2010-May/007399.html
sudo tlmgr update --self
sudo tlmgr install inconsolata upquote courier courier-scaled helvetic
fi
}
EnsureDevtools() {
## deprecated 2020-Sep
echo "Deprecated"
}
EnsureUnittestRunner() {
if test -f DESCRIPTION; then
if [[ "Linux" == "${OS}" ]]; then
sudo Rscript -e 'dcf <- read.dcf(file="DESCRIPTION")[1,]; if ("Suggests" %in% names(dcf)) { sug <- dcf[["Suggests"]]; pkg <- do.call(c, sapply(c("testthat", "tinytest", "RUnit"), function(p, sug) if (grepl(p, sug)) p else NULL, sug, USE.NAMES=FALSE)); if (!is.null(pkg)) install.packages(pkg, type="binary-source") }'
else
sudo Rscript -e 'dcf <- read.dcf(file="DESCRIPTION")[1,]; if ("Suggests" %in% names(dcf)) { sug <- dcf[["Suggests"]]; pkg <- do.call(c, sapply(c("testthat", "tinytest", "RUnit"), function(p, sug) if (grepl(p, sug)) p else NULL, sug, USE.NAMES=FALSE)); if (!is.null(pkg)) install.packages(pkg) }'
fi
fi
}
InstallIfNotYetInstalled() {
res=$(Rscript -e 'if (requireNamespace(commandArgs(TRUE), quietly=TRUE)) cat("YES") else cat("NO")' "$1")
if [[ "${res}" != "YES" ]]; then
if [[ "Linux" == "${OS}" ]]; then
sudo Rscript -e 'install.packages(commandArgs(TRUE), type="binary-source")' "$1"
else
sudo Rscript -e 'install.packages(commandArgs(TRUE))' "$1"
fi
fi
}
AptGetInstall() {
if [[ "Linux" != "${OS}" ]]; then
echo "Wrong OS: ${OS}"
exit 1
fi
if [[ "" == "$*" ]]; then
echo "No arguments to aptget_install"
exit 1
fi
echo "Installing apt package(s) $@"
Retry sudo apt-get -y --no-install-recommends --allow-unauthenticated install "$@"
}
DpkgCurlInstall() {
if [[ "Linux" != "${OS}" ]]; then
echo "Wrong OS: ${OS}"
exit 1
fi
if [[ "" == "$*" ]]; then
echo "No arguments to dpkgcurl_install"
exit 1
fi
echo "Installing remote package(s) $@"
for rf in "$@"; do
curl -OL ${rf}
f=$(basename ${rf})
sudo dpkg -i ${f}
rm -v ${f}
done
}
RInstall() {
if [[ "" == "$*" ]]; then
echo "No arguments to r_install"
exit 1
fi
echo "Installing R package(s): $@"
if [[ "Linux" == "${OS}" ]]; then
sudo Rscript -e 'install.packages(commandArgs(TRUE), type="binary-source")' "$@"
else
sudo Rscript -e 'install.packages(commandArgs(TRUE))' "$@"
fi
}
BiocInstall() {
if [[ "" == "$*" ]]; then
echo "No arguments to bioc_install"
exit 1
fi
echo "Installing R Bioconductor package(s): $@"
Rscript -e 'if (!requireNamespace("BiocManager", quietly=TRUE)) install.packages("BiocManager")'
Rscript -e "BiocManager::install(commandArgs(TRUE), update=FALSE)" "$@"
}
RBinaryInstall() {
if [[ -z "$#" ]]; then
echo "No arguments to r_binary_install"
exit 1
fi
if [[ "Linux" != "${OS}" ]] || [[ -n "${FORCE_SOURCE_INSTALL}" ]]; then
echo "Fallback: Installing from source"
RInstall "$@"
return
fi
echo "Installing *binary* R packages: $*"
r_packages=$(echo $* | tr '[:upper:]' '[:lower:]')
r_debs=$(for r_package in ${r_packages}; do echo -n "r-cran-${r_package} "; done)
AptGetInstall ${r_debs}
}
InstallGithub() {
#EnsureDevtools
sudo Rscript -e 'remotes::install_github(commandArgs(TRUE))' "$@"
}
InstallDeps() {
#EnsureDevtools
sudo Rscript -e 'remotes::install_deps(".")'
}
InstallDepsAndSuggests() {
sudo Rscript -e 'remotes::install_deps(".", dependencies=TRUE)'
}
InstallBiocDeps() {
## deprecated 2020-Sep
echo "Deprecated"
}
DumpSysinfo() {
echo "Dumping system information."
R -e '.libPaths(); sessionInfo(); installed.packages()'
}
DumpLogsByExtension() {
if [[ -z "$1" ]]; then
echo "dump_logs_by_extension requires exactly one argument, got: $@"
exit 1
fi
extension=$1
shift
package=$(find . -maxdepth 1 -name "*.Rcheck" -type d)
if [[ ${#package[@]} -ne 1 ]]; then
echo "Could not find package Rcheck directory, skipping log dump."
exit 0
fi
for name in $(find "${package}" -type f -name "*${extension}"); do
echo ">>> Filename: ${name} <<<"
cat ${name}
done
}
DumpLogs() {
echo "Dumping test execution logs."
DumpLogsByExtension "out"
DumpLogsByExtension "log"
DumpLogsByExtension "fail"
}
Coverage() {
echo "Running Code Coverage analysis via the covr package"
## assumes that the Rutter PPAs are in fact known, which is a given here
AptGetInstall r-cran-covr
Rscript -e "covr::codecov(type = '${COVERAGE_TYPE}', quiet = FALSE)"
}
UpdatePackages() {
echo "Updating Packages"
Rscript -e 'update.packages(ask=FALSE)'
}
RunTests() {
echo "Building with: R CMD build ${R_BUILD_ARGS}"
R CMD build ${R_BUILD_ARGS} .
# We want to grab the version we just built.
FILE=$(ls -1t *.tar.gz | head -n 1)
# Create binary package (currently Windows only)
if [[ "${OS:0:5}" == "MINGW" ]]; then
R_CHECK_INSTALL_ARGS=${R_CHECK_INSTALL_ARGS-"--install-args=\"--build --install-tests\""}
fi
echo "Testing with: R CMD check \"${FILE}\" ${R_CHECK_ARGS} ${R_CHECK_INSTALL_ARGS}"
_R_CHECK_CRAN_INCOMING_=${_R_CHECK_CRAN_INCOMING_:-FALSE}
if [[ "$_R_CHECK_CRAN_INCOMING_" == "FALSE" ]]; then
echo "(CRAN incoming checks are off)"
fi
_R_CHECK_CRAN_INCOMING_=${_R_CHECK_CRAN_INCOMING_} R CMD check "${FILE}" ${R_CHECK_ARGS} ${R_CHECK_INSTALL_ARGS}
if [[ -n "${WARNINGS_ARE_ERRORS}" ]]; then
if DumpLogsByExtension "00check.log" | grep -q WARNING; then
echo "Found warnings, treated as errors."
echo "Clear or unset the WARNINGS_ARE_ERRORS environment variable to ignore warnings."
exit 1
fi
fi
}
Retry() {
if "$@"; then
return 0
fi
for wait_time in 5 20 30 60; do
echo "Command failed, retrying in ${wait_time} ..."
sleep ${wait_time}
if "$@"; then
return 0
fi
done
echo "Failed all retries!"
exit 1
}
ShowHelpAndExit() {
echo "Usage: run.sh COMMAND"
echo "Derived from the venerable r-travis project, and still maintained lovingly by @eddelbuettel."
echo "See https://eddelbuettel.github.io/r-ci for more."
exit 0
}
COMMAND=$1
#echo "Running command: ${COMMAND}"
shift
case $COMMAND in
##
## Bootstrap a new core system
"bootstrap")
Bootstrap
;;
## Code coverage via covr.io
"coverage")
Coverage
;;
##
## Ensure devtools is loaded (implicitly called)
## (Deprecated Sep 2020)
"install_devtools"|"devtools_install")
EnsureDevtools
;;
##
## Install a binary deb package via apt-get
"install_aptget"|"aptget_install")
AptGetInstall "$@"
;;
##
## Install a binary deb package via a curl call and local dpkg -i
"install_dpkgcurl"|"dpkgcurl_install")
DpkgCurlInstall "$@"
;;
##
## Install an R dependency from CRAN
"install_r"|"r_install")
RInstall "$@"
;;
##
## Install an R dependency from Bioconductor
"install_bioc"|"bioc_install")
BiocInstall "$@"
;;
##
## Install an R dependency as a binary (via c2d4u PPA)
"install_r_binary"|"r_binary_install")
RBinaryInstall "$@"
;;
##
## Install a package from github sources
"install_github"|"github_package")
InstallGithub "$@"
;;
##
## Install package dependencies from CRAN
"install_deps")
InstallDeps
;;
##
## Install package dependencies and suggests from CRAN
"install_all")
InstallDepsAndSuggests
;;
##
## Install package dependencies from Bioconductor and CRAN (needs devtools)
## deprecated 2020-Sep
"install_bioc_deps")
InstallBiocDeps
;;
##
## Update packages
"update_packages")
UpdatePackages
;;
##
##
## Run the actual tests, ie R CMD check
"run_tests")
RunTests
;;
##
## Dump information about installed packages
"dump_sysinfo")
DumpSysinfo
;;
##
## Dump build or check logs
"dump_logs")
DumpLogs
;;
##
## Dump selected build or check logs
"dump_logs_by_extension")
DumpLogsByExtension "$@"
;;
##
## Help
"help")
ShowHelpAndExit
;;
esac