From 47a957070235ef86383446ef28eb91c6f75efce6 Mon Sep 17 00:00:00 2001 From: Elio Campitelli Date: Tue, 8 Oct 2024 17:50:25 +1100 Subject: [PATCH] Adds ephemeral files --- NEWS.md | 1 + R/cdo.R | 22 ++++++++++++++++++++-- pkg_build/extra-R/cdo.R | 22 ++++++++++++++++++++-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index a9fa42e..2f2d08f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,3 +5,4 @@ - Added options management. `cdo_options_use()` will use options for an operation. `cdo_option_set()` will set global options to use for all operations. `cdo_options_clear()` will clear the global options. - The new `cdo_operator()` allows the user to define an arbitrary operator in case the some operator is missing in the package. Operators are run with the `cdo()` function. - Added operators with zero outputs. +- By default, temporay files output are deleted when not accessible from the R session. diff --git a/R/cdo.R b/R/cdo.R index 81cba9d..c314f23 100644 --- a/R/cdo.R +++ b/R/cdo.R @@ -144,7 +144,7 @@ get_output_length <- function(x) { #' #' @param operation a CDO operation #' @param output an output file or base string for output files. Defaults to -#' temporary files. +#' temporary files that will be deleted when its bond variable is garbage collected. #' @param options character vector with CDO options. #' @param verbose whether to print the command being executed. #' @@ -186,6 +186,22 @@ cdo_execute <- function(operation, return(operation$output) } + +ephemeral_files <- R6::R6Class("ephemeral_files", public = list( + files = NA, + initialize = function(files) { + self$files <- files + return(self) + }, + print = function() { + cat("Will be deleted when garbage collected\n") + }, + + finalize = function() { + file.remove(self$files) + }) +) + temp_output <- function(operation) { if (operation$operator$n_output == Inf) { n <- 1 @@ -193,7 +209,9 @@ temp_output <- function(operation) { n <- operation$operator$n_output } - replicate(n, tempfile()) + files <- replicate(n, tempfile()) + attr(files, "ephemeral") <- ephemeral_files$new(files) + files } diff --git a/pkg_build/extra-R/cdo.R b/pkg_build/extra-R/cdo.R index 81cba9d..c314f23 100644 --- a/pkg_build/extra-R/cdo.R +++ b/pkg_build/extra-R/cdo.R @@ -144,7 +144,7 @@ get_output_length <- function(x) { #' #' @param operation a CDO operation #' @param output an output file or base string for output files. Defaults to -#' temporary files. +#' temporary files that will be deleted when its bond variable is garbage collected. #' @param options character vector with CDO options. #' @param verbose whether to print the command being executed. #' @@ -186,6 +186,22 @@ cdo_execute <- function(operation, return(operation$output) } + +ephemeral_files <- R6::R6Class("ephemeral_files", public = list( + files = NA, + initialize = function(files) { + self$files <- files + return(self) + }, + print = function() { + cat("Will be deleted when garbage collected\n") + }, + + finalize = function() { + file.remove(self$files) + }) +) + temp_output <- function(operation) { if (operation$operator$n_output == Inf) { n <- 1 @@ -193,7 +209,9 @@ temp_output <- function(operation) { n <- operation$operator$n_output } - replicate(n, tempfile()) + files <- replicate(n, tempfile()) + attr(files, "ephemeral") <- ephemeral_files$new(files) + files }