This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR_front
442 lines (325 loc) · 10.3 KB
/
R_front
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
#!/bin/bash
# vi: se fdm=marker
# R front script for biodb extensions packages, version 1.1.2
SCRIPT_FILE=$(basename $0)
SCRIPT_DIR=$(dirname $0)
VERSION=1.0
DEBUG=0
R_BUILD_FOLDER=${R_BUILD_FOLDER:-$HOME/.local/opt/r}
VERSIONS_INFO_FILENAME=VERSION-INFO.dcf
VERSIONS_FILE="$R_BUILD_FOLDER/$VERSIONS_INFO_FILENAME"
R=R
MIRROR="https://cloud.r-project.org/"
R_VERSION=${R_VERSION:-current}
declare -a ACTIONS=()
# Print help {{{1
################################################################
function print_help {
cat <<END_HELP_MSG
$SCRIPT_FILE, version $VERSION, is a front end for R, allowing to choose between installed R or R beta or R devel versions.
Usage: $SCRIPT_FILE [options] [-- [options passed to R]]
Options:
--beta Install and use beta version of R.
--clean Remove everything from build folder.
--devel Install and use devel version of R.
-g, --debug Debug mode.
-h, --help Print this help message.
-n, --no-current
Do not use the R binary currently installed on system.
--print-bin Print path to R bin.
--print-home Print path to R folder.
--r-version <version>
Choose the version of R to run. Either a version number (4.0, 4.1, ...) or one of "current" (version installed), "release" (official release), "beta" (official prerelease) or "devel" (official development). Default is "current".
--run Run the R binary, passing any remaining arguments to it.
-u, --update-versions
Update the versions file, that lists downloadable versions of R.
END_HELP_MSG
}
# Error {{{1
################################################################
error() {
local msg="$*"
echo "[ERROR] $msg at ${BASH_SOURCE[1]}:${BASH_LINENO[0]}" >&2
quit 1
}
# Warning {{{1
################################################################
warning() {
local msg="$*"
[[ -n $QUIET ]] || echo "[WARNING] $msg at"\
"${BASH_SOURCE[1]}:${BASH_LINENO[0]}" >&2
}
# Debug {{{1
################################################################
debug() {
local lvl=$1
local msg="$*"
[[ $DEBUG -ge $lvl ]] && echo "[DEBUG] $msg" >&2
}
# Info {{{1
################################################################
info() {
local msg="$*"
[[ -n $QUIET ]] || echo "[INFO] $msg" >&2
}
# Quit {{{1
################################################################
function quit {
local mystatus=$1
[[ ${#ACTIONS[@]} -eq 0 ]] || warning "Some remaining actions where not run: ${ACTIONS[@]}."
exit $mystatus
}
# Read args {{{1
################################################################
function read_args {
local args="$*" # save arguments for debugging purpose
local print=
while true ; do
case $1 in
--beta) R_VERSION=beta ;;
--clean) ACTIONS+=('clean') ;;
--devel) R_VERSION=devel ;;
-g|--debug) DEBUG=$((DEBUG + 1)) ;;
-h|--help) print_help ; exit 0 ;;
-n|--no-current) NO_CURRENT=1 ;;
--print-bin) ACTIONS+=('select_r' 'print_bin') ;;
--print-home) ACTIONS+=('select_r' 'print_home') ;;
--r-version) R_VERSION="$2" ; shift ;;
--run) ACTIONS+=('select_r' 'run_r') ;;
-u|--update-versions) ACTIONS+=('download_versions_file') ;;
-[^-]*) split_opt=$(echo $1 | sed 's/^-//' | sed 's/\([a-zA-Z]\)/ -\1/g') ; set -- $1$split_opt "${@:2}" ;;
--) shift ; break ;;
*) break
esac
shift
done
# Read remaining arguments
R_ARGS=("$@")
# Debug
debug 1 "Arguments are : $args"
debug 1 "ACTIONS=${ACTIONS}"
debug 1 "DEBUG=$DEBUG"
debug 1 "NO_CURRENT=$NO_CURRENT"
debug 1 "R_ARGS=$R_ARGS"
debug 1 "R_VERSION=$R_VERSION"
}
# Download versions file {{{1
################################################################
function download_versions_file {
mkdir -p "$R_BUILD_FOLDER"
if [[ -f "$VERSIONS_FILE" ]] ; then
curl -o "$VERSIONS_FILE" -z "$VERSIONS_FILE" "$MIRROR/src/base/$VERSIONS_INFO_FILENAME"
else
curl -o "$VERSIONS_FILE" "$MIRROR/src/base/$VERSIONS_INFO_FILENAME"
fi
}
# Select version {{{1
################################################################
function select_version {
if [[ -z $SELECTED_VERSION ]] ; then
# Version name
if [[ $R_VERSION =~ ^(beta|devel|release|current)$ ]] ; then
SELECTED_VERSION=$R_VERSION
# Version number
elif [[ $R_VERSION =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]] ; then
local version_name=
# Try current version
if [[ -z $NO_CURRENT ]] ; then
local current_version=$(R --version | head -n 1 | sed 's/^.*version \([^ ]*\).*$/\1/')
debug 1 current_version=$current_version
if [[ $current_version =~ ^$R_VERSION(\.[0-9]+)?$ ]] ; then
version_name=current
fi
fi
# Try downloadable versions
if [[ -z $version_name ]] ; then
[[ -f $VERSIONS_FILE ]] || download_versions_file
version_name=$(grep ": $R_VERSION" "$VERSIONS_FILE" | sed 's/^\(.*\):.*$/\1/')
[[ -n $version_name ]] || error "Cannot find any version name"\
"in \"$VERSIONS_FILE\" for version \"$R_VERSION\"."
fi
# Set selected version
case $version_name in
Next-release) SELECTED_VERSION=beta ;;
Devel) SELECTED_VERSION=devel ;;
Release) SELECTED_VERSION=release ;;
current) SELECTED_VERSION=current ;;
*) error "Unknown version name \"$version_name\"." ;;
esac
# Unknown version
else
error "Unknown version $R_VERSION."
fi
fi
debug 1 "select_version SELECTED_VERSION=$SELECTED_VERSION"
}
# Extract real version {{{1
################################################################
function extract_real_version {
[[ -n $SELECTED_VERSION ]] || select_version
if [[ -z $REAL_VERSION ]] ; then
[[ -f $VERSIONS_FILE ]] || download_versions_file
case $SELECTED_VERSION in
beta) version_name=Next-release ;;
devel) version_name=Devel ;;
release) version_name=Release ;;
*) error "Unknown version $SELECTED_VERSION to download." ;;
esac
REAL_VERSION=$(grep "^$version_name:" "$VERSIONS_FILE" | sed 's/^[^:]*: *\(.*\)$/\1/')
fi
debug 1 "extract_real_version real_version=$REAL_VERSION"
}
# Get download URL {{{1
################################################################
function get_download_url {
local url=
[[ -n $SELECTED_VERSION ]] || select_version
case $SELECTED_VERSION in
beta) url=$MIRROR/src/base-prerelease/R-latest.tar.gz ;;
devel) url=$MIRROR/src/base-prerelease/R-devel.tar.gz ;;
release) url=$MIRROR/src/base/R-latest.tar.gz ;;
current) url= ;;
esac
debug 1 "get_download_url URL=$url"
echo "$url"
}
# Set R folder {{{1
################################################################
function set_r_folder {
if [[ -z $R_FOLDER ]] ; then
[[ -n $SELECTED_VERSION ]] || select_version
if [[ $SELECTED_VERSION == current ]] ; then
R_FOLDER=$(/usr/bin/env R --slave --no-restore RHOME)
else
[[ -n $REAL_VERSION ]] || extract_real_version
R_FOLDER=$R_BUILD_FOLDER/R-$REAL_VERSION
fi
fi
debug 1 "get_r_folder R_FOLDER=$R_FOLDER"
}
# Set R bin path {{{1
################################################################
function set_r_bin_path {
if [[ -z $R_BIN_PATH ]] ; then
R_BIN_PATH=R
[[ -n $SELECTED_VERSION ]] || select_version
if [[ $SELECTED_VERSION != current ]] ; then
set_r_folder
R_BIN_PATH="$R_FOLDER/bin/R"
fi
fi
debug 1 "set_r_bin_path R_BIN_PATH=$R_BIN_PATH"
}
# Install R {{{1
################################################################
function install_r {
[[ -d $R_BUILD_FOLDER ]] || mkdir -p "$R_BUILD_FOLDER"
set_r_folder
set_r_bin_path
debug 1 "install_r R_BUILD_FOLDER=$R_BUILD_FOLDER"
if [[ ! -x $R_BIN_PATH ]] ; then
debug 1 "install_r R_BIN_PATH=$R_BIN_PATH"
# Remove possible existing destination folder
rm -rf "$R_FOLDER"
# Make temporary folder for extraction
tmp="$R_BUILD_FOLDER/extract_folder"
mkdir -p "$tmp"
# Move to temporary folder
oldpwd=$(pwd)
cd "$tmp"
# Extract
curl "$(get_download_url)" | tar -xz
# Create destination folder
mkdir -p "$R_FOLDER"
# Move code
mv "$tmp"/*/* "$R_FOLDER"/.
# Exit temporary folder
cd "$oldpwd"
# Remove temporary folder
rm -r "$tmp"
# Build
cd "$R_FOLDER" && ./configure --prefix="$HOME/.local" >&2
make -C "$R_FOLDER" >&2
fi
}
# Select R {{{1
################################################################
function select_r {
set_r_folder
set_r_bin_path
R="$R_BIN_PATH"
R_HOME="$R_FOLDER"
[[ $R_VERSION == current ]] || install_r $R_VERSION
debug 1 "select_r R=$R"
debug 1 "select_r R_HOME=$R_HOME"
}
# Print bin path {{{1
################################################################
function print_bin {
echo "$R"
}
# Print path to home {{{1
################################################################
function print_home {
echo "$R_HOME"
}
# Run R {{{1
################################################################
function run_r {
export R_HOME
debug 1 "R_HOME=$R_HOME"
debug 1 "$R $R_ARGS"
$R "${R_ARGS[@]}"
}
# Clean build folder {{{1
################################################################
function clean {
[[ -n $R_BUILD_FOLDER ]] && rm -rf "$R_BUILD_FOLDER"
}
# Run actions {{{1
################################################################
function run_actions {
# Loop on all actions in the submitted order
while [[ ${#ACTIONS[@]} -gt 0 ]] ; do
# Pop first action
local action=${ACTIONS[0]} # Index starts at 1 in zsh, 0 in bash.
ACTIONS=("${ACTIONS[@]:1}") # Remove first element
debug 1 "action=$action"
# Run
case $action in
clean) ;&
print_bin) ;&
print_home) ;&
select_r) ;&
download_versions_file) ;&
run_r) $action ;;
*) error "Unknown action \"$action\"." ;;
esac
done
}
# Move action to start of array {{{1
################################################################
function move_action_to_start {
local action="$1"
if [[ " ${ACTIONS[@]} " == *" $action "* ]] ; then
declare -a new_actions=($action)
for a in ${ACTIONS[@]} ; do
[[ $a == $action ]] || new_actions+=($a)
done
ACTIONS=("${new_actions[@]}")
unset new_actions
fi
}
# Order actions {{{1
################################################################
function order_actions {
move_action_to_start "select_r"
move_action_to_start "download_versions_file"
move_action_to_start "clean"
}
# Main {{{1
################################################################
read_args "$@"
order_actions
run_actions
quit 0