QC Report for Quartet RNA-Seq
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

350 行
9.8KB

  1. local({
  2. # the requested version of renv
  3. version <- "0.12.0"
  4. # the project directory
  5. project <- getwd()
  6. # avoid recursion
  7. if (!is.na(Sys.getenv("RENV_R_INITIALIZING", unset = NA)))
  8. return(invisible(TRUE))
  9. # signal that we're loading renv during R startup
  10. Sys.setenv("RENV_R_INITIALIZING" = "true")
  11. on.exit(Sys.unsetenv("RENV_R_INITIALIZING"), add = TRUE)
  12. # signal that we've consented to use renv
  13. options(renv.consent = TRUE)
  14. # load the 'utils' package eagerly -- this ensures that renv shims, which
  15. # mask 'utils' packages, will come first on the search path
  16. library(utils, lib.loc = .Library)
  17. # check to see if renv has already been loaded
  18. if ("renv" %in% loadedNamespaces()) {
  19. # if renv has already been loaded, and it's the requested version of renv,
  20. # nothing to do
  21. spec <- .getNamespaceInfo(.getNamespace("renv"), "spec")
  22. if (identical(spec[["version"]], version))
  23. return(invisible(TRUE))
  24. # otherwise, unload and attempt to load the correct version of renv
  25. unloadNamespace("renv")
  26. }
  27. # load bootstrap tools
  28. bootstrap <- function(version, library) {
  29. # read repos (respecting override if set)
  30. repos <- Sys.getenv("RENV_CONFIG_REPOS_OVERRIDE", unset = NA)
  31. if (is.na(repos))
  32. repos <- getOption("repos")
  33. # fix up repos
  34. on.exit(options(repos = repos), add = TRUE)
  35. repos[repos == "@CRAN@"] <- "https://cloud.r-project.org"
  36. options(repos = repos)
  37. # attempt to download renv
  38. tarball <- tryCatch(renv_bootstrap_download(version), error = identity)
  39. if (inherits(tarball, "error"))
  40. stop("failed to download renv ", version)
  41. # now attempt to install
  42. status <- tryCatch(renv_bootstrap_install(version, tarball, library), error = identity)
  43. if (inherits(status, "error"))
  44. stop("failed to install renv ", version)
  45. }
  46. renv_bootstrap_download_impl <- function(url, destfile) {
  47. mode <- "wb"
  48. # https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17715
  49. fixup <-
  50. Sys.info()[["sysname"]] == "Windows" &&
  51. substring(url, 1L, 5L) == "file:"
  52. if (fixup)
  53. mode <- "w+b"
  54. download.file(
  55. url = url,
  56. destfile = destfile,
  57. mode = mode,
  58. quiet = TRUE
  59. )
  60. }
  61. renv_bootstrap_download <- function(version) {
  62. methods <- list(
  63. renv_bootstrap_download_cran_latest,
  64. renv_bootstrap_download_cran_archive,
  65. renv_bootstrap_download_github
  66. )
  67. for (method in methods) {
  68. path <- tryCatch(method(version), error = identity)
  69. if (is.character(path) && file.exists(path))
  70. return(path)
  71. }
  72. stop("failed to download renv ", version)
  73. }
  74. renv_bootstrap_download_cran_latest <- function(version) {
  75. # check for renv on CRAN matching this version
  76. db <- as.data.frame(available.packages(), stringsAsFactors = FALSE)
  77. entry <- db[db$Package %in% "renv" & db$Version %in% version, ]
  78. if (nrow(entry) == 0) {
  79. fmt <- "renv %s is not available from your declared package repositories"
  80. stop(sprintf(fmt, version))
  81. }
  82. message("* Downloading renv ", version, " from CRAN ... ", appendLF = FALSE)
  83. info <- tryCatch(
  84. download.packages("renv", destdir = tempdir()),
  85. condition = identity
  86. )
  87. if (inherits(info, "condition")) {
  88. message("FAILED")
  89. return(FALSE)
  90. }
  91. message("OK")
  92. info[1, 2]
  93. }
  94. renv_bootstrap_download_cran_archive <- function(version) {
  95. name <- sprintf("renv_%s.tar.gz", version)
  96. repos <- getOption("repos")
  97. urls <- file.path(repos, "src/contrib/Archive/renv", name)
  98. destfile <- file.path(tempdir(), name)
  99. message("* Downloading renv ", version, " from CRAN archive ... ", appendLF = FALSE)
  100. for (url in urls) {
  101. status <- tryCatch(
  102. renv_bootstrap_download_impl(url, destfile),
  103. condition = identity
  104. )
  105. if (identical(status, 0L)) {
  106. message("OK")
  107. return(destfile)
  108. }
  109. }
  110. message("FAILED")
  111. return(FALSE)
  112. }
  113. renv_bootstrap_download_github <- function(version) {
  114. enabled <- Sys.getenv("RENV_BOOTSTRAP_FROM_GITHUB", unset = "TRUE")
  115. if (!identical(enabled, "TRUE"))
  116. return(FALSE)
  117. # prepare download options
  118. pat <- Sys.getenv("GITHUB_PAT")
  119. if (nzchar(Sys.which("curl")) && nzchar(pat)) {
  120. fmt <- "--location --fail --header \"Authorization: token %s\""
  121. extra <- sprintf(fmt, pat)
  122. saved <- options("download.file.method", "download.file.extra")
  123. options(download.file.method = "curl", download.file.extra = extra)
  124. on.exit(do.call(base::options, saved), add = TRUE)
  125. } else if (nzchar(Sys.which("wget")) && nzchar(pat)) {
  126. fmt <- "--header=\"Authorization: token %s\""
  127. extra <- sprintf(fmt, pat)
  128. saved <- options("download.file.method", "download.file.extra")
  129. options(download.file.method = "wget", download.file.extra = extra)
  130. on.exit(do.call(base::options, saved), add = TRUE)
  131. }
  132. message("* Downloading renv ", version, " from GitHub ... ", appendLF = FALSE)
  133. url <- file.path("https://api.github.com/repos/rstudio/renv/tarball", version)
  134. name <- sprintf("renv_%s.tar.gz", version)
  135. destfile <- file.path(tempdir(), name)
  136. status <- tryCatch(
  137. renv_bootstrap_download_impl(url, destfile),
  138. condition = identity
  139. )
  140. if (!identical(status, 0L)) {
  141. message("FAILED")
  142. return(FALSE)
  143. }
  144. message("Done!")
  145. return(destfile)
  146. }
  147. renv_bootstrap_install <- function(version, tarball, library) {
  148. # attempt to install it into project library
  149. message("* Installing renv ", version, " ... ", appendLF = FALSE)
  150. dir.create(library, showWarnings = FALSE, recursive = TRUE)
  151. # invoke using system2 so we can capture and report output
  152. bin <- R.home("bin")
  153. exe <- if (Sys.info()[["sysname"]] == "Windows") "R.exe" else "R"
  154. r <- file.path(bin, exe)
  155. args <- c("--vanilla", "CMD", "INSTALL", "-l", shQuote(library), shQuote(tarball))
  156. output <- system2(r, args, stdout = TRUE, stderr = TRUE)
  157. message("Done!")
  158. # check for successful install
  159. status <- attr(output, "status")
  160. if (is.numeric(status) && !identical(status, 0L)) {
  161. header <- "Error installing renv:"
  162. lines <- paste(rep.int("=", nchar(header)), collapse = "")
  163. text <- c(header, lines, output)
  164. writeLines(text, con = stderr())
  165. }
  166. status
  167. }
  168. renv_bootstrap_prefix <- function() {
  169. # construct version prefix
  170. version <- paste(R.version$major, R.version$minor, sep = ".")
  171. prefix <- paste("R", numeric_version(version)[1, 1:2], sep = "-")
  172. # include SVN revision for development versions of R
  173. # (to avoid sharing platform-specific artefacts with released versions of R)
  174. devel <-
  175. identical(R.version[["status"]], "Under development (unstable)") ||
  176. identical(R.version[["nickname"]], "Unsuffered Consequences")
  177. if (devel)
  178. prefix <- paste(prefix, R.version[["svn rev"]], sep = "-r")
  179. # build list of path components
  180. components <- c(prefix, R.version$platform)
  181. # include prefix if provided by user
  182. prefix <- Sys.getenv("RENV_PATHS_PREFIX")
  183. if (nzchar(prefix))
  184. components <- c(prefix, components)
  185. # build prefix
  186. paste(components, collapse = "/")
  187. }
  188. renv_bootstrap_library_root <- function(project) {
  189. path <- Sys.getenv("RENV_PATHS_LIBRARY", unset = NA)
  190. if (!is.na(path))
  191. return(path)
  192. path <- Sys.getenv("RENV_PATHS_LIBRARY_ROOT", unset = NA)
  193. if (!is.na(path))
  194. return(file.path(path, basename(project)))
  195. file.path(project, "renv/library")
  196. }
  197. renv_bootstrap_validate_version <- function(version) {
  198. loadedversion <- utils::packageDescription("renv", fields = "Version")
  199. if (version == loadedversion)
  200. return(TRUE)
  201. # assume four-component versions are from GitHub; three-component
  202. # versions are from CRAN
  203. components <- strsplit(loadedversion, "[.-]")[[1]]
  204. remote <- if (length(components) == 4L)
  205. paste("rstudio/renv", loadedversion, sep = "@")
  206. else
  207. paste("renv", loadedversion, sep = "@")
  208. fmt <- paste(
  209. "renv %1$s was loaded from project library, but renv %2$s is recorded in lockfile.",
  210. "Use `renv::record(\"%3$s\")` to record this version in the lockfile.",
  211. "Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.",
  212. sep = "\n"
  213. )
  214. msg <- sprintf(fmt, loadedversion, version, remote)
  215. warning(msg, call. = FALSE)
  216. FALSE
  217. }
  218. renv_bootstrap_load <- function(project, libpath, version) {
  219. # try to load renv from the project library
  220. if (!requireNamespace("renv", lib.loc = libpath, quietly = TRUE))
  221. return(FALSE)
  222. # warn if the version of renv loaded does not match
  223. renv_bootstrap_validate_version(version)
  224. # load the project
  225. renv::load(project)
  226. TRUE
  227. }
  228. # construct path to library root
  229. root <- renv_bootstrap_library_root(project)
  230. # construct library prefix for platform
  231. prefix <- renv_bootstrap_prefix()
  232. # construct full libpath
  233. libpath <- file.path(root, prefix)
  234. # attempt to load
  235. if (renv_bootstrap_load(project, libpath, version))
  236. return(TRUE)
  237. # load failed; attempt to bootstrap
  238. bootstrap(version, libpath)
  239. # exit early if we're just testing bootstrap
  240. if (!is.na(Sys.getenv("RENV_BOOTSTRAP_INSTALL_ONLY", unset = NA)))
  241. return(TRUE)
  242. # try again to load
  243. if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {
  244. message("Successfully installed and loaded renv ", version, ".")
  245. return(renv::load())
  246. }
  247. # failed to download or load renv; warn the user
  248. msg <- c(
  249. "Failed to find an renv installation: the project will not be loaded.",
  250. "Use `renv::activate()` to re-initialize the project."
  251. )
  252. warning(paste(msg, collapse = "\n"), call. = FALSE)
  253. })