R/clearExternalDir.R

Defines functions .destroy_major_analogues clearObsoleteDir clearExternalDir

Documented in clearExternalDir clearObsoleteDir

#' Clear the external installation directory
#'
#' Clear the external installation directory by removing old Conda instances 
#' installed for different versions of \pkg{basilisk} with the same middle version number
#' (i.e., same Bioconductor release).
#'
#' @param path String containing the path to the latest version of the directory of interest.
#'
#' @details
#' \code{clearObsoleteDir} can also be applied to the directories for the individual Conda environments,
#' as the package version is also suffixed onto those directory paths.
#' This is useful for clearing out obsolete versions of package environments.
#'
#' @return 
#' For \code{clearExternalDir}, all conda instances (and associated environments) of the same Bioconductor release as the current \pkg{basilisk} installation are destroyed.
#'
#' The same applies for \code{clearObsoleteDir} except that the conda instance generated by the latest \pkg{basilisk} installation is retained.
#'
#' @author Aaron Lun
#'
#' @seealso
#' \code{\link{getExternalDir}}, which determines the location of the external directory.
#'
#' @seealso
#' \code{\link{installConda}}, for the motivation behind this function.
#'
#' @examples
#' # We can't actually run clearExternalDir() here, as it 
#' # relies on basilisk already being installed.
#' print("dummy test to pass BiocCheck")
#'
#' @export
clearExternalDir <- function() {
    host <- getExternalDir()
    .destroy_major_analogues(host, save=FALSE)
}

#' @export
#' @rdname clearExternalDir
clearObsoleteDir <- function(path=getExternalDir()) {
    .destroy_major_analogues(path, save=TRUE)
}

.destroy_major_analogues <- function(path, save) {
    pattern <- "([0-9]+\\.[0-9]+)\\.[0-9]+$" # Bioconductor version pattern.
    major.v <- sub(pattern, "\\1", basename(path))
    all.candidates <- list.files(dirname(path))
    all.major.v <- sub(pattern, "\\1", all.candidates)
    all.candidates <- all.candidates[all.major.v==major.v]

    if (save) {
        # Not using setdiff to avoid problems with path 
        # normalization on - you guessed it! - Windows.
        keep <- all.candidates != basename(path)
        all.candidates <- all.candidates[keep]
    }

    all.candidates <- file.path(dirname(path), all.candidates)
    unlink2(all.candidates)
}

Try the basilisk.utils package in your browser

Any scripts or data that you put into this service are public.

basilisk.utils documentation built on Jan. 29, 2021, 2 a.m.