-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathretrieve.R
1263 lines (951 loc) · 33.3 KB
/
retrieve.R
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
`_renv_repos_archive` <- new.env(parent = emptyenv())
# this routine retrieves a package + its dependencies, and as a side
# effect populates the restore state's `retrieved` member with a
# list of package records which can later be used for install
retrieve <- function(packages) {
# confirm that we have restore state set up
state <- renv_restore_state()
if (is.null(state))
stopf("renv_restore_begin() must be called first")
# normalize repositories (ensure @CRAN@ is resolved)
options(repos = renv_repos_normalize())
# transform repository URLs for PPM
if (renv_ppm_enabled()) {
repos <- getOption("repos")
renv_scope_options(repos = renv_ppm_transform(repos))
}
# ensure HTTPUserAgent is set (required for PPM binaries)
agent <- renv_http_useragent()
if (!grepl("renv", agent)) {
renv <- sprintf("renv (%s)", renv_metadata_version())
agent <- paste(renv, agent, sep = "; ")
}
renv_scope_options(HTTPUserAgent = agent)
writef(header("Downloading packages"))
# TODO: parallel?
handler <- state$handler
for (package in packages)
handler(package, renv_retrieve_impl(package))
if (is.null(state$downloaded)) {
writef("[no downloads required]")
}
writef("")
state <- renv_restore_state()
data <- state$install$data()
names(data) <- extract_chr(data, "Package")
data
}
renv_retrieve_impl <- function(package) {
# skip packages with 'base' priority
if (package %in% renv_packages_base())
return()
# if we've already attempted retrieval of this package, skip
state <- renv_restore_state()
if (visited(package, envir = state$retrieved))
return()
# extract record for package
records <- state$records
record <- records[[package]] %||% renv_retrieve_missing_record(package)
# normalize the record source
source <- renv_record_source(record, normalize = TRUE)
# don't install packages from incompatible OS
ostype <- tolower(record[["OS_type"]] %||% "")
skip <-
renv_platform_unix() && identical(ostype, "windows") ||
renv_platform_windows() && identical(ostype, "unix")
if (skip)
return()
# if this is a package from Bioconductor, activate those repositories now
if (source %in% c("bioconductor")) {
project <- renv_restore_state(key = "project")
renv_scope_bioconductor(project = project)
}
# if this is a package from R-Forge, activate its repository
if (source %in% c("repository")) {
repository <- record$Repository %||% ""
if (tolower(repository) %in% c("rforge", "r-forge")) {
repos <- getOption("repos")
if (!"R-Forge" %in% names(repos)) {
repos[["R-Forge"]] <- "https://R-Forge.R-project.org"
renv_scope_options(repos = repos)
}
}
}
# if the record doesn't declare the package version,
# treat it as a request for the latest version on CRAN
# TODO: should make this behavior configurable
uselatest <-
source %in% c("repository", "bioconductor") &&
is.null(record$Version)
if (uselatest) {
record <- renv_available_packages_latest(package)
if (is.null(record)) {
stopf("package '%s' is not available", package)
return()
}
}
# if the requested record is incompatible with the set
# of requested package versions thus far, request the
# latest version on the R package repositories
#
# TODO: handle more explicit dependency requirements
# TODO: report to the user if they have explicitly requested
# installation of this package version despite it being incompatible
compat <- renv_retrieve_incompatible(package, record)
if (NROW(compat)) {
# get the latest available package version
replacement <- renv_available_packages_latest(package)
if (is.null(replacement))
stopf("package '%s' is not available", package)
# if it's not compatible, then we might need to try again with
# a source version (assuming type = "both")
pkgtype <- getOption("pkgType")
if (identical(pkgtype, "both")) {
iscompat <- renv_retrieve_incompatible(package, replacement)
if (NROW(iscompat)) {
replacement <- renv_available_packages_latest(package, type = "source")
}
}
# report if we couldn't find a compatible package
renv_retrieve_incompatible_report(package, record, replacement, compat)
record <- replacement
}
if (!renv_restore_rebuild_required(record)) {
# if we have an installed package matching the requested record, finish early
path <- renv_restore_find(package, record)
if (file.exists(path))
return(renv_retrieve_successful(record, path, install = FALSE))
# if the requested record already exists in the cache,
# we'll use that package for install
cacheable <-
renv_cache_config_enabled(project = state$project) &&
renv_record_cacheable(record)
if (cacheable) {
# try to find the record in the cache
path <- renv_cache_find(record)
if (nzchar(path) && renv_cache_package_validate(path))
return(renv_retrieve_successful(record, path))
}
}
# if this is a URL source, then it should already have a local path
# check for the Path and Source fields and see if they resolve
fields <- c("Path", "Source")
for (field in fields) {
# check for a valid field
path <- record[[field]]
if (is.null(path))
next
# check whether it looks like an explicit source
isurl <-
is.character(path) &&
nzchar(path) &&
grepl("[/\\]|[.](?:zip|tgz|gz)$", path)
if (!isurl)
next
# error if the field is declared but doesn't exist
if (!file.exists(path)) {
fmt <- "record for package '%s' declares local source '%s', but that file does not exist"
stopf(fmt, record$Package, path)
}
# otherwise, success
path <- renv_path_normalize(path, mustWork = TRUE)
return(renv_retrieve_successful(record, path))
}
if (!renv_restore_rebuild_required(record)) {
# try some early shortcut methods
shortcuts <- c(
renv_retrieve_explicit,
renv_retrieve_cellar,
if (!renv_tests_running() && config$install.shortcuts())
renv_retrieve_libpaths
)
for (shortcut in shortcuts) {
retrieved <- catch(shortcut(record))
if (identical(retrieved, TRUE))
return(TRUE)
}
}
state$downloaded <- TRUE
# time to retrieve -- delegate based on previously-determined source
switch(source,
bioconductor = renv_retrieve_bioconductor(record),
bitbucket = renv_retrieve_bitbucket(record),
git = renv_retrieve_git(record),
github = renv_retrieve_github(record),
gitlab = renv_retrieve_gitlab(record),
repository = renv_retrieve_repos(record),
url = renv_retrieve_url(record),
renv_retrieve_unknown_source(record)
)
}
renv_retrieve_name <- function(record, type = "source", ext = NULL) {
package <- record$Package
version <- record$RemoteSha %||% record$Version
ext <- ext %||% renv_package_ext(type)
sprintf("%s_%s%s", package, version, ext)
}
renv_retrieve_path <- function(record, type = "source", ext = NULL) {
# extract relevant record information
package <- record$Package
name <- renv_retrieve_name(record, type, ext)
source <- renv_record_source(record)
# check for packages from an PPM binary URL, and
# update the package type if known
if (renv_ppm_enabled()) {
url <- attr(record, "url")
if (is.character(url) && grepl("/__[^_]+__/", url))
type <- "binary"
}
# form path for package to be downloaded
if (type == "source")
renv_paths_source(source, package, name)
else if (type == "binary")
renv_paths_binary(source, package, name)
else
stopf("unrecognized type '%s'", type)
}
renv_retrieve_bioconductor <- function(record) {
# try to read the bioconductor version from the record
version <- renv_retrieve_bioconductor_version(record)
# activate Bioconductor repositories in this context
project <- renv_restore_state(key = "project")
renv_scope_bioconductor(project = project, version = version)
# retrieve record using updated repositories
renv_retrieve_repos(record)
}
renv_retrieve_bioconductor_version <- function(record) {
# read git branch
branch <- record[["git_branch"]]
if (is.null(branch))
return(NULL)
# try and parse version
parts <- strsplit(branch, "_", fixed = TRUE)[[1L]]
ok <-
length(parts) == 3L &&
tolower(parts[[1L]]) == "release"
if (!ok)
return(NULL)
# we have a version; use it
paste(tail(parts, n = -1L), collapse = ".")
}
renv_retrieve_bitbucket <- function(record) {
# query repositories endpoint to find download URL
host <- record$RemoteHost %||% config$bitbucket.host()
origin <- renv_retrieve_origin(host)
username <- record$RemoteUsername
repo <- record$RemoteRepo
# scope authentication
renv_scope_auth(repo)
fmt <- "%s/repositories/%s/%s"
url <- sprintf(fmt, origin, username, repo)
destfile <- renv_scope_tempfile("renv-bitbucket-")
download(url, destfile = destfile, quiet = TRUE)
json <- renv_json_read(destfile)
# now build URL to tarball
base <- json$links$html$href
ref <- record$RemoteSha %||% record$RemoteRef
fmt <- "%s/get/%s.tar.gz"
url <- sprintf(fmt, base, ref)
path <- renv_retrieve_path(record)
renv_retrieve_package(record, url, path)
}
renv_retrieve_github <- function(record) {
host <- record$RemoteHost %||% config$github.host()
origin <- renv_retrieve_origin(host)
username <- record$RemoteUsername
repo <- record$RemoteRepo
ref <- record$RemoteSha %||% record$RemoteRef
if (is.null(ref)) {
fmt <- "GitHub record for package '%s' has no recorded 'RemoteSha' / 'RemoteRef'"
stopf(fmt, record$Package)
}
fmt <- "%s/repos/%s/%s/tarball/%s"
url <- with(record, sprintf(fmt, origin, username, repo, ref))
path <- renv_retrieve_path(record)
renv_retrieve_package(record, url, path)
}
renv_retrieve_gitlab <- function(record) {
host <- record$RemoteHost %||% config$gitlab.host()
origin <- renv_retrieve_origin(host)
user <- record$RemoteUsername
repo <- record$RemoteRepo
id <- URLencode(paste(user, repo, sep = "/"), reserved = TRUE)
fmt <- "%s/api/v4/projects/%s/repository/archive.tar.gz"
url <- sprintf(fmt, origin, id)
path <- renv_retrieve_path(record)
sha <- record$RemoteSha %||% record$RemoteRef
if (!is.null(sha))
url <- paste(url, paste("sha", sha, sep = "="), sep = "?")
renv_retrieve_package(record, url, path)
}
renv_retrieve_git <- function(record) {
path <- renv_scope_tempfile("renv-git-")
ensure_directory(path)
renv_retrieve_git_impl(record, path)
renv_retrieve_successful(record, path)
}
renv_retrieve_git_impl <- function(record, path) {
renv_git_preflight()
package <- record$Package
url <- record$RemoteUrl
ref <- record$RemoteRef
sha <- record$RemoteSha
# figure out the default ref
gitref <- case(
nzchar(sha %||% "") ~ sha,
nzchar(ref %||% "") ~ ref,
"HEAD"
)
# be quiet if requested
quiet <- getOption("renv.git.quiet", default = TRUE)
quiet <- if (quiet) "--quiet" else ""
template <- heredoc('
cd "${DIR}"
git init ${QUIET}
git remote add origin "${ORIGIN}"
git fetch ${QUIET} --depth=1 origin "${REF}"
git reset ${QUIET} --hard FETCH_HEAD
')
data <- list(
DIR = renv_path_normalize(path),
ORIGIN = url,
REF = gitref,
QUIET = quiet
)
commands <- renv_template_replace(template, data)
command <- gsub("\n", " && ", commands, fixed = TRUE)
if (renv_platform_windows())
command <- paste(comspec(), "/C", command)
writef("Cloning '%s' ...", url)
before <- Sys.time()
status <- local({
renv_scope_auth(record)
renv_scope_git_auth()
system(command)
})
after <- Sys.time()
if (status != 0L) {
fmt <- "error cloning '%s' from '%s' [status code %i]"
stopf(fmt, package, url, status)
}
fmt <- "\tOK [cloned repository in %s]"
elapsed <- difftime(after, before, units = "auto")
writef(fmt, renv_difftime_format(elapsed))
TRUE
}
renv_retrieve_cellar_find <- function(record, project = NULL) {
project <- renv_project_resolve(project)
# packages installed with 'remotes::install_local()' will
# have a RemoteUrl entry that we can use
url <- record$RemoteUrl %||% ""
if (file.exists(url)) {
path <- renv_path_normalize(url, mustWork = TRUE)
type <- if (fileext(path) %in% c(".tgz", ".zip")) "binary" else "source"
return(named(path, type))
}
# otherwise, look in the cellar
roots <- renv_cellar_roots(project)
for (type in c("binary", "source")) {
name <- renv_retrieve_name(record, type = type)
for (root in roots) {
package <- record$Package
paths <- c(
file.path(root, package, name),
file.path(root, name)
)
for (path in paths)
if (file.exists(path))
return(named(path, type))
}
}
fmt <- "%s [%s] is not available locally"
stopf(fmt, record$Package, record$Version)
}
renv_retrieve_cellar_report <- function(record) {
source <- renv_record_source(record)
if (source == "cellar")
return(record)
fmt <- "* Package %s [%s] will be installed from the cellar."
with(record, writef(fmt, Package, Version))
record
}
renv_retrieve_cellar <- function(record) {
source <- renv_retrieve_cellar_find(record)
record <- renv_retrieve_cellar_report(record)
renv_retrieve_successful(record, source)
}
renv_retrieve_libpaths <- function(record) {
libpaths <- c(renv_libpaths_user(), renv_libpaths_site())
for (libpath in libpaths)
if (renv_retrieve_libpaths_impl(record, libpath))
return(TRUE)
}
renv_retrieve_libpaths_impl <- function(record, libpath) {
# form path to installed package's DESCRIPTION
path <- file.path(libpath, record$Package)
if (!file.exists(path))
return(FALSE)
# read DESCRIPTION
desc <- renv_description_read(path = path)
# check if it's compatible with the requested record
fields <- c("Package", "Version", grep("^Remote", names(record), value = TRUE))
compatible <- identical(record[fields], desc[fields])
if (!compatible)
return(FALSE)
# check that it was built for a compatible version of R
built <- desc[["Built"]]
if (is.null(built))
return(FALSE)
ok <- catch(renv_description_built_version(desc))
if (!identical(ok, TRUE))
return(FALSE)
# check that this package has a known source
source <- renv_snapshot_description_source(desc)
if (identical(source$Source, "unknown"))
return(FALSE)
# OK: copy this package as-is
renv_retrieve_successful(record, path)
}
renv_retrieve_explicit <- function(record) {
# try parsing as a local remote
source <- record$Path %||% record$RemoteUrl %||% ""
if (nzchar(source)) {
resolved <- catch(renv_remotes_resolve_path(source))
if (inherits(resolved, "error"))
return(FALSE)
}
# treat as 'local' source but extract path
normalized <- renv_path_normalize(source, mustWork = TRUE)
resolved$Source <- "Local"
renv_retrieve_successful(resolved, normalized)
}
renv_retrieve_repos <- function(record) {
# if this record is tagged with a type + url, we can
# use that directly for retrieval
if (all(c("type", "url") %in% names(attributes(record))))
return(renv_retrieve_repos_impl(record))
# figure out what package sources are okay to use here
pkgtype <- getOption("pkgType", default = "source")
srcok <- pkgtype %in% c("both", "source") ||
getOption("install.packages.check.source", default = "yes") %in% "yes"
binok <- pkgtype %in% c("both") || grepl("binary", pkgtype, fixed = TRUE)
# collect list of 'methods' for retrieval
methods <- stack(mode = "list")
# add binary package methods
if (binok) {
# prefer repository binaries if available
methods$push(renv_retrieve_repos_binary)
# also try fallback binary locations (for Nexus)
methods$push(renv_retrieve_repos_binary_fallback)
# if MRAN is enabled, check those binaries as well
if (renv_mran_enabled())
methods$push(renv_retrieve_repos_mran)
}
# next, try to retrieve from sources
if (srcok) {
# retrieve from source repositories
methods$push(renv_retrieve_repos_source)
# also try fallback source locations (for Nexus)
methods$push(renv_retrieve_repos_source_fallback)
# if this is a package from r-universe, try restoring from github
# (currently inferred from presence for RemoteUrl field)
unifields <- c("RemoteUrl", "RemoteRef", "RemoteSha")
if (all(unifields %in% names(record)))
methods$push(renv_retrieve_git)
else
methods$push(renv_retrieve_repos_archive)
}
# capture errors for reporting
errors <- stack()
for (method in methods$data()) {
status <- catch(
withCallingHandlers(
method(record),
renv.retrieve.error = function(error) {
errors$push(error$data)
}
)
)
if (inherits(status, "error")) {
errors$push(status)
next
}
if (identical(status, TRUE))
return(TRUE)
if (!is.logical(status)) {
fmt <- "internal error: unexpected status code '%s'"
warningf(fmt, stringify(status))
}
}
# if we couldn't download the package, report the errors we saw
local({
renv_scope_options(warn = 1)
for (error in errors$data())
warning(error)
})
stopf("failed to retrieve package '%s'", renv_record_format_remote(record))
}
renv_retrieve_repos_error_report <- function(record, errors) {
if (empty(errors))
return()
messages <- extract(errors, "message")
if (empty(messages))
return()
messages <- unlist(messages, recursive = TRUE, use.names = FALSE)
if (empty(messages))
return()
fmt <- "The following error(s) occurred while retrieving '%s':"
preamble <- sprintf(fmt, record$Package)
renv_pretty_print(
values = paste("-", messages),
preamble = preamble
)
if (renv_verbose())
str(errors)
}
renv_retrieve_url <- function(record) {
if (is.null(record$RemoteUrl)) {
fmt <- "package '%s' has no recorded RemoteUrl"
stopf(fmt, record$Package)
}
resolved <- renv_remotes_resolve_url(record$RemoteUrl, quiet = FALSE)
renv_retrieve_successful(record, resolved$Path)
}
renv_retrieve_repos_archive_name <- function(record, type = "source") {
file <- record$File
if (length(file) && !is.na(file))
return(file)
ext <- renv_package_ext(type)
paste0(record$Package, "_", record$Version, ext)
}
renv_retrieve_repos_mran <- function(record) {
# MRAN does not make binaries available on Linux
if (renv_platform_linux())
return(FALSE)
# ensure local MRAN database is up-to-date
renv_mran_database_refresh(explicit = FALSE)
# check that we have an available database
path <- renv_mran_database_path()
if (!file.exists(path))
return(FALSE)
# attempt to read it
database <- catch(renv_mran_database_load())
if (inherits(database, "error")) {
warning(database)
return(FALSE)
}
# get entry for this version of R + platform
suffix <- contrib.url("", type = "binary")
entry <- database[[suffix]]
if (is.null(entry))
return(FALSE)
# check for known entry for this package + version
key <- paste(record$Package, record$Version)
idate <- entry[[key]]
if (is.null(idate))
return(FALSE)
# convert from integer to date
date <- as.Date(idate, origin = "1970-01-01")
# form url to binary package
base <- renv_mran_url(date, suffix)
name <- renv_retrieve_name(record, type = "binary")
url <- file.path(base, name)
# form path to saved file
path <- renv_retrieve_path(record, "binary")
# attempt to retrieve
renv_retrieve_package(record, url, path)
}
renv_retrieve_repos_binary <- function(record) {
renv_retrieve_repos_impl(record, "binary")
}
renv_retrieve_repos_binary_fallback <- function(record) {
for (repo in getOption("repos")) {
if (renv_nexus_enabled(repo)) {
repourl <- contrib.url(repo, type = "binary")
status <- catch(renv_retrieve_repos_impl(record, "binary", repo = repourl))
if (!inherits(status, "error"))
return(status)
}
}
FALSE
}
renv_retrieve_repos_source <- function(record) {
renv_retrieve_repos_impl(record, "source")
}
renv_retrieve_repos_source_fallback <- function(record, repo) {
for (repo in getOption("repos")) {
if (renv_nexus_enabled(repo)) {
repourl <- contrib.url(repo, type = "source")
status <- catch(renv_retrieve_repos_impl(record, "source", repo = repourl))
if (!inherits(status, "error"))
return(status)
}
}
FALSE
}
renv_retrieve_repos_archive <- function(record) {
for (repo in getOption("repos")) {
# try to determine path to package in archive
url <- renv_retrieve_repos_archive_path(repo, record)
if (is.null(url))
next
# attempt download
name <- renv_retrieve_repos_archive_name(record, type = "source")
status <- catch(renv_retrieve_repos_impl(record, "source", name, url))
if (identical(status, TRUE))
return(TRUE)
}
return(FALSE)
}
renv_retrieve_repos_archive_path <- function(repo, record) {
# allow users to provide a custom archive path for a record,
# in case they're using a repository that happens to archive
# packages with a different format than regular CRAN network
# https://github.com/rstudio/renv/issues/602
override <- getOption("renv.retrieve.repos.archive.path")
if (is.function(override)) {
result <- override(repo, record)
if (!is.null(result))
return(result)
}
# if we already know the format of the repository, use that
if (exists(repo, envir = `_renv_repos_archive`)) {
formatter <- get(repo, envir = `_renv_repos_archive`)
root <- formatter(repo, record)
return(root)
}
# otherwise, try determining the archive paths with a couple
# custom locations, and cache the version that works for the
# associated repository
formatters <- list(
# default CRAN format
function(repo, record) {
with(record, file.path(repo, "src/contrib/Archive", Package))
},
# format used by Artifactory
# https://github.com/rstudio/renv/issues/602
function(repo, record) {
with(record, file.path(repo, "src/contrib/Archive", Package, Version))
},
# format used by Nexus
# https://github.com/rstudio/renv/issues/595
function(repo, record) {
with(record, file.path(repo, "src/contrib"))
}
)
name <- renv_retrieve_repos_archive_name(record, "source")
for (formatter in formatters) {
root <- formatter(repo, record)
url <- file.path(root, name)
if (renv_download_available(url)) {
assign(repo, formatter, envir = `_renv_repos_archive`)
return(root)
}
}
}
# NOTE: If 'repo' is provided, it should be the path to the appropriate 'arm'
# of a repository, which is normally generated from the repository URL via
# 'contrib.url()'.
renv_retrieve_repos_impl <- function(record,
type = NULL,
name = NULL,
repo = NULL)
{
package <- record$Package
version <- record$Version
type <- type %||% attr(record, "type", exact = TRUE)
name <- name %||% renv_retrieve_repos_archive_name(record, type)
repo <- repo %||% attr(record, "url", exact = TRUE)
# if we weren't provided a repository for this package, try to find it
if (is.null(repo)) {
entry <- catch(
renv_available_packages_entry(
package = package,
type = type,
filter = version,
prefer = record[["Repository"]]
)
)
if (inherits(entry, "error")) {
attr(entry, "record") <- record
renv_condition_signal("renv.retrieve.error", entry)
return(FALSE)
}
# get repository path
repo <- entry$Repository
# add in the path if available
path <- entry$Path
if (length(path) && !is.na(path))
repo <- file.path(repo, path)
# update the tarball name if it was declared
file <- entry$File
if (length(file) && !is.na(file))
name <- file
}
url <- file.path(repo, name)
path <- renv_retrieve_path(record, type)
renv_retrieve_package(record, url, path)
}
renv_retrieve_package <- function(record, url, path) {
ensure_parent_directory(path)
type <- renv_record_source(record)
status <- local({
renv_scope_auth(record)
catch(download(url, destfile = path, type = type))
})
# report error for logging upstream
if (inherits(status, "error")) {
attr(status, "record") <- record
renv_condition_signal("renv.retrieve.error", status)
}
# handle FALSE returns (shouldn't normally happen?)
if (identical(status, FALSE)) {
fmt <- "an unknown error occurred installing '%s' (%s)"
msg <- sprintf(fmt, record$Package, renv_record_format_remote(record))
status <- simpleError(msg)
}
# handle errors
if (inherits(status, "error"))
stop(status)
# handle success
renv_retrieve_successful(record, path)
}
renv_retrieve_successful_subdir <- function(record, path) {
# if it's a file, assume RemoteSubdir needs to be honored
info <- file.info(path, extra_cols = FALSE)
if (identical(info$isdir, FALSE))
return(record$RemoteSubdir)
# otherwise, respect RemoteSubdir only if it seems to
# point at a valid DESCRPITION file
if (!is.null(record$RemoteSubdir)) {
parts <- c(path, record$RemoteSubdir, "DESCRIPTION")
descpath <- paste(parts, collapse = "/")
if (file.exists(descpath))
return(record$RemoteSubdir)
}
}
renv_retrieve_successful <- function(record, path, install = TRUE) {
# if we downloaded an archive, adjust its permissions here
mode <- Sys.getenv("RENV_CACHE_MODE", unset = NA)
if (!is.na(mode)) {
info <- file.info(path, extra_cols = FALSE)
if (identical(info$isdir, FALSE)) {
parent <- dirname(path)
renv_system_exec(
command = "chmod",
args = c("-Rf", renv_shell_quote(mode), renv_shell_path(parent)),
action = "chmoding cached package",
quiet = TRUE,
success = NULL
)
}
}
# the handling of 'subdir' here is a little awkward, as this function
# can receive:
#
# - archives, whose package might live within a sub-directory;
# - folders, whose package might live within a sub-directory;
# - cache paths, for which the subdir is no longer relevant
#
# this warrants a proper cleanup, but for now we we use a hack
subdir <- renv_retrieve_successful_subdir(record, path)
# augment record with information from DESCRIPTION file
desc <- renv_description_read(path, subdir = subdir)
# update the record's package name, version
# TODO: should we warn if they didn't match for some reason?
record$Package <- desc$Package