Skip to content

Commit 7740c98

Browse files
authored
Improve management of temporary files (#108)
* Improve management of temporary files * devtools::document()
1 parent 2d1e364 commit 7740c98

File tree

6 files changed

+46
-17
lines changed

6 files changed

+46
-17
lines changed

R/initGRASS.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
#' `set.ignore.stderrOption`; can be set to TRUE to silence
8989
#' `system()` output to standard error; does not apply on Windows
9090
#' platforms.
91+
#' @param tempdir a directory to use for temporary files. You may want to
92+
#' provide the same value for `home`.
9193
#'
9294
#' @return The function runs `gmeta6` before returning the current values
9395
#' of the running GRASS session that it provides.
@@ -163,7 +165,11 @@
163165
initGRASS <- function(
164166
gisBase = NULL, home, SG, gisDbase, addon_base, location,
165167
mapset, override = FALSE, use_g.dirseps.exe = TRUE, pid,
166-
remove_GISRC = FALSE, ignore.stderr = get.ignore.stderrOption()) {
168+
remove_GISRC = FALSE, ignore.stderr = get.ignore.stderrOption(),
169+
tempdir = base::tempdir()) {
170+
171+
Sys.setenv(RGRASS_TEMPDIR = tempdir)
172+
167173
# check for existing GRASS session from rc filename specified in GISRC
168174
if (nchar(Sys.getenv("GISRC")) > 0 && !override) {
169175
ask_override(
@@ -396,7 +402,7 @@ initGRASS <- function(
396402
# check if the working directory is writable otherwise use a tempfile
397403
if (file.access(".", 2) != 0) {
398404
warning("working directory not writable, using tempfile for GISRC")
399-
Sys.setenv(GISRC = paste(tempfile(), "junk", sep = "_"))
405+
Sys.setenv(GISRC = paste(tempfile(tmpdir = Sys.getenv("RGRASS_TEMPDIR")), "junk", sep = "_"))
400406
}
401407

402408
# write the GISRC file
@@ -416,7 +422,7 @@ initGRASS <- function(
416422
if (!missing(gisDbase)) {
417423
if (!file.exists(gisDbase)) dir.create(gisDbase)
418424
} else {
419-
gisDbase <- tempdir()
425+
gisDbase <- Sys.getenv("RGRASS_TEMPDIR")
420426
}
421427

422428
gisDbase <- ifelse(
@@ -496,7 +502,7 @@ initGRASS <- function(
496502
if (!missing(gisDbase)) {
497503
if (!file.exists(gisDbase)) dir.create(gisDbase)
498504
} else {
499-
gisDbase <- tempdir()
505+
gisDbase <- Sys.getenv("RGRASS_TEMPDIR")
500506
}
501507

502508
cat("GISDBASE:", gisDbase, "\n", file = Sys.getenv("GISRC"))
@@ -710,7 +716,7 @@ initGRASS <- function(
710716

711717
if (mSG) {
712718
if (nzchar(wkt_SG)) {
713-
tf <- tempfile()
719+
tf <- tempfile(tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
714720
writeLines(wkt_SG, con = tf)
715721

716722
MS <- execGRASS(

R/rast_link.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ read_RAST <- function(
362362
} else {
363363
NODATAi <- NODATA[i]
364364
}
365-
tmplist[[i]] <- tempfile(fileext = fxt)
365+
tmplist[[i]] <- tempfile(fileext = fxt, tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
366366
if (is.null(flags)) flags <- c("overwrite", "c", "m")
367367
if (!is.null(cat) && cat[i]) flags <- c(flags, "t")
368368
if (is.null(typei)) {
@@ -775,7 +775,8 @@ write_RAST <- function(
775775
drv <- "GTiff"
776776
fxt <- ".tif"
777777
}
778-
tf <- tempfile(fileext = fxt)
778+
tf <- tempfile(fileext = fxt, tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
779+
on.exit(unlink(tf))
779780
res <- getMethod("writeRaster", c("SpatRaster", "character"))(x,
780781
filename = tf, overwrite = TRUE, filetype = drv)
781782
tmpfl <- TRUE
@@ -794,7 +795,8 @@ write_RAST <- function(
794795
if (getMethod("nlyr", "SpatRaster")(x) == 1L) {
795796
xcats <- getMethod("cats", "SpatRaster")(x)[[1]]
796797
if (!is.null(xcats)) {
797-
tfc <- tempfile()
798+
tfc <- tempfile(tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
799+
on.exit(unlink(tfc), add = TRUE)
798800
write.table(xcats, tfc,
799801
sep = ":", row.names = FALSE,
800802
col.names = FALSE, quote = FALSE

R/vect_link_ng.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ read_VECT <- function(
212212

213213
} else {
214214
if (layer == "") layer <- "1"
215-
tf <- tempfile(fileext = ".gpkg")
215+
tf <- tempfile(fileext = ".gpkg", tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
216216
execGRASS("v.out.ogr",
217217
flags = flags, input = vname, type = type,
218218
layer = layer, output = tf, output_layer = vname,
@@ -274,7 +274,7 @@ write_VECT <- function(x, vname, flags = "overwrite",
274274
}
275275

276276
if (!file.exists(tf)) {
277-
tf <- tempfile(fileext = ".gpkg")
277+
tf <- tempfile(fileext = ".gpkg", tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
278278
getMethod("writeVector", c("SpatVector", "character"))(x, filename = tf,
279279
filetype = "GPKG", options = NULL, overwrite = TRUE)
280280
}

R/xml1.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ parseGRASS <- function(cmd, legacyExec = NULL) {
5151
tr <- try(system(cmd0, intern = TRUE))
5252
if (inherits(tr, "try-error")) stop(paste(cmd, "not found"))
5353
} else {
54-
errFile <- tempfile()
55-
outFile <- tempfile()
54+
errFile <- tempfile(tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
55+
outFile <- tempfile(tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
56+
on.exit({
57+
unlink(errFile)
58+
unlink(outFile)
59+
})
5660
command <- paste(prep, cmd, ext, sep = "")
5761
arguments <- "--interface-description"
5862
res <- system2(
@@ -716,8 +720,12 @@ execGRASS <- function(
716720
command <- attr(syscmd, "cmd")
717721
arguments <- substring(syscmd, (nchar(command) + 2), nchar(syscmd))
718722

719-
errFile <- tempfile(fileext = ".err")
720-
outFile <- tempfile(fileext = ".out")
723+
errFile <- tempfile(fileext = ".err", tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
724+
outFile <- tempfile(fileext = ".out", tmpdir = Sys.getenv("RGRASS_TEMPDIR"))
725+
on.exit({
726+
unlink(errFile)
727+
unlink(outFile)
728+
})
721729

722730
res <- system2(command, arguments,
723731
stderr = errFile,

man/initGRASS.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-execGRASS.R

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ test_that("testing basic doGRASS, execGRASS, stringexecGRASS", {
55
skip_if_not(!is.null(gisBase), "GRASS GIS not found on PATH")
66
skip_if(is.null(testdata), "GRASS GIS example dataset is not available")
77

8+
# This test case will also check the `tempdir` argument to `initGRASS`.
9+
tp = tempfile()
10+
dir.create(tp)
11+
previous.tempdir.contents = dir(tempdir())
12+
813
loc <- initGRASS(
9-
home = tempdir(),
14+
home = tp,
1015
gisBase = gisBase,
1116
gisDbase = testdata$gisDbase,
1217
location = "nc_basic_spm_grass7",
1318
mapset = "PERMANENT",
14-
override = TRUE
19+
override = TRUE,
20+
tempdir = tp
1521
)
1622

1723
# test assembling the command using arguments
@@ -85,6 +91,9 @@ test_that("testing basic doGRASS, execGRASS, stringexecGRASS", {
8591
"Invalid parameter name: silent"
8692
)
8793

94+
# Our parameters to `initGRASS` should've prevented creating any new files
95+
# at the root of `tempdir()`.
96+
expect_equal(dir(tempdir()), previous.tempdir.contents)
8897
})
8998

9099
test_that("testing options doGRASS, execGRASS, stringexecGRASS", {

0 commit comments

Comments
 (0)