Skip to content

Commit

Permalink
use fs for filesys ops
Browse files Browse the repository at this point in the history
  • Loading branch information
mllg committed Jan 29, 2018
1 parent a1c7123 commit c4c2fe7
Show file tree
Hide file tree
Showing 58 changed files with 278 additions and 309 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Imports:
brew,
checkmate (>= 1.8.5),
digest (>= 0.6.9),
fs (>= 1.1.0),
parallel,
progress (>= 1.1.1),
R6,
Expand Down
4 changes: 2 additions & 2 deletions R/Algorithm.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ removeAlgorithms = function(name, reg = getDefaultRegistry()) {
job.ids = filter(def.ids, reg$status, "job.id")

info("Removing Algorithm '%s' and %i corresponding jobs ...", nn, nrow(job.ids))
file.remove.safely(getAlgorithmURI(reg, nn))
file_remove(getAlgorithmURI(reg, nn))
reg$defs = reg$defs[!def.ids]
reg$status = reg$status[!job.ids]
reg$algorithms = chsetdiff(reg$algorithms, nn)
Expand All @@ -65,5 +65,5 @@ removeAlgorithms = function(name, reg = getDefaultRegistry()) {
}

getAlgorithmURI = function(reg, name) {
fp(dir(reg, "algorithms"), mangle(name))
fs::path(dir(reg, "algorithms"), mangle(name))
}
3 changes: 1 addition & 2 deletions R/ExperimentRegistry.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ makeExperimentRegistry = function(file.dir = "registry", work.dir = getwd(), con
reg = makeRegistry(file.dir = file.dir, work.dir = work.dir, conf.file = conf.file,
packages = packages, namespaces = namespaces, source = source, load = load, seed = seed, make.default = make.default)

dir.create(fp(reg$file.dir, "problems"))
dir.create(fp(reg$file.dir, "algorithms"))
fs::dir_create(fs::path(reg$file.dir, c("problems", "algorithms")))

reg$problems = character(0L)
reg$algorithms = character(0L)
Expand Down
14 changes: 7 additions & 7 deletions R/Export.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ batchExport = function(export = list(), unexport = character(0L), reg = getDefau
assertList(export, names = "named")
assertCharacter(unexport, any.missing = FALSE, min.chars = 1L)

path = fp(reg$file.dir, "exports")
path = fs::path(reg$file.dir, "exports")

if (length(export) > 0L) {
nn = names(export)
fn = fp(path, mangle(nn))
found = file.exists(fn)
fn = fs::path(path, mangle(nn))
found = fs::file_exists(fn)
if (any(!found))
info("Exporting new objects: '%s' ...", stri_flatten(nn[!found], "','"))
if (any(found))
Expand All @@ -51,13 +51,13 @@ batchExport = function(export = list(), unexport = character(0L), reg = getDefau
}

if (length(unexport) > 0L) {
fn = fp(path, mangle(unexport))
found = file.exists(fn)
fn = fs::path(path, mangle(unexport))
found = fs::file_exists(fn)
if (any(found))
info("Un-exporting exported objects: '%s' ...", stri_flatten(unexport[found], "','"))
file.remove.safely(fn[found])
file_remove(fn[found])
}

fns = list.files(path, pattern = "\\.rds")
invisible(data.table(name = unmangle(fns), uri = fp(path, fns)))
invisible(data.table(name = unmangle(fns), uri = fs::path(path, fns)))
}
10 changes: 4 additions & 6 deletions R/Job.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ BaseJob = R6Class("BaseJob", cloneable = FALSE,
},

external.dir = function() {
path = fp(self$file.dir, "external", self$id)
dir.create(path, recursive = TRUE, showWarnings = FALSE)
path
fs::dir_create(fs::path(self$file.dir, "external", self$id))
}
)
)
Expand All @@ -38,10 +36,10 @@ Job = R6Class("Job", cloneable = FALSE, inherit = BaseJob,
),
active = list(
fun = function() {
self$reader$get(fp(self$file.dir, "user.function.rds"))
self$reader$get(fs::path(self$file.dir, "user.function.rds"))
},
pars = function() {
c(self$job.pars, self$reader$get(fp(self$file.dir, "more.args.rds")))
c(self$job.pars, self$reader$get(fs::path(self$file.dir, "more.args.rds")))
}
)
)
Expand Down Expand Up @@ -80,7 +78,7 @@ Experiment = R6Class("Experiment", cloneable = FALSE, inherit = BaseJob,
p = self$problem
if (p$cache) {
cache.file = getProblemCacheURI(self)
if (file.exists(cache.file)) {
if (fs::file_exists(cache.file)) {
result = try(readRDS(cache.file))
if (!inherits(result, "try-error"))
return(result)
Expand Down
2 changes: 1 addition & 1 deletion R/JobCollection.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ createCollection = function(jobs, resources = list(), reg = getDefaultRegistry()
jc$work.dir = reg$work.dir
jc$seed = reg$seed
jc$uri = getJobFiles(reg, hash = jc$job.hash)
jc$log.file = fp(reg$file.dir, "logs", sprintf("%s.log", jc$job.hash))
jc$log.file = fs::path(reg$file.dir, "logs", sprintf("%s.log", jc$job.hash))
jc$packages = reg$packages
jc$namespaces = reg$namespaces
jc$source = reg$source
Expand Down
4 changes: 2 additions & 2 deletions R/Logs.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @useDynLib batchtools fill_gaps
readLog = function(id, missing.as.empty = FALSE, reg = getDefaultRegistry()) {
log.file = getLogFiles(reg, id)
if (is.na(log.file) || !file.exists(log.file)) {
if (is.na(log.file) || !fs::file_exists(log.file)) {
if (missing.as.empty)
return(data.table(job.id = integer(0L), lines = character(0L)))
stopf("Log file for job with id %i not available", id$job.id)
Expand Down Expand Up @@ -108,7 +108,7 @@ showLog = function(id, reg = getDefaultRegistry()) {
assertRegistry(reg, sync = TRUE)
id = convertId(reg, id)
lines = extractLog(readLog(id, reg = reg), id)
log.file = fp(tempdir(), sprintf("%i.log", id$job.id))
log.file = fs::path(fs::path_temp(), sprintf("%i.log", id$job.id))
writeLines(text = lines, con = log.file)
file.show(log.file, delete.file = TRUE)
}
Expand Down
18 changes: 10 additions & 8 deletions R/Problem.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ addProblem = function(name, data = NULL, fun = NULL, seed = NULL, cache = FALSE,
writeRDS(prob, file = getProblemURI(reg, name))
reg$problems = union(reg$problems, name)
cache.dir = getProblemCacheDir(reg, name)
if (dir.exists(cache.dir))
unlink(cache.dir, recursive = TRUE)
if (fs::dir_exists(cache.dir))
fs::dir_delete(cache.dir)
if (cache)
dir.create(cache.dir, recursive = TRUE)
fs::dir_create(cache.dir)
saveRegistry(reg)
invisible(prob)
}
Expand All @@ -106,25 +106,27 @@ removeProblems = function(name, reg = getDefaultRegistry()) {
job.ids = filter(def.ids, reg$status, "job.id")

info("Removing Problem '%s' and %i corresponding jobs ...", nn, nrow(job.ids))
file.remove.safely(getProblemURI(reg, nn))
file_remove(getProblemURI(reg, nn))
reg$defs = reg$defs[!def.ids]
reg$status = reg$status[!job.ids]
reg$problems = chsetdiff(reg$problems, nn)
unlink(getProblemCacheDir(reg, nn), recursive = TRUE)
cache = getProblemCacheDir(reg, nn)
if (fs::dir_exists(cache))
fs::dir_delete(cache)
}

sweepRegistry(reg)
invisible(TRUE)
}

getProblemURI = function(reg, name) {
fp(dir(reg, "problems"), mangle(name))
fs::path(dir(reg, "problems"), mangle(name))
}

getProblemCacheDir = function(reg, name) {
fp(dir(reg, "cache"), "problems", base32_encode(name, use.padding = FALSE))
fs::path(dir(reg, "cache"), "problems", base32_encode(name, use.padding = FALSE))
}

getProblemCacheURI = function(job) {
fp(getProblemCacheDir(job, job$prob.name), sprintf("%s.rds", digest(list(job$prob.name, job$prob.pars, job$repl))))
fs::path(getProblemCacheDir(job, job$prob.name), sprintf("%s.rds", digest(list(job$prob.name, job$prob.pars, job$repl))))
}
2 changes: 1 addition & 1 deletion R/RDSReader.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RDSReader = R6Class("RDSReader",
},

get = function(uri, slot = NA_character_) {
read = function(uri) if (file.exists(uri)) readRDS(uri) else NULL
read = function(uri) if (fs::file_exists(uri)) readRDS(uri) else NULL

# no cache used, read object from disk and return
if (!self$use.cache)
Expand Down
17 changes: 8 additions & 9 deletions R/Registry.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ makeRegistry = function(file.dir = "registry", work.dir = getwd(), conf.file = f

reg = new.env(parent = asNamespace("batchtools"))
reg$file.dir = file.dir
reg$work.dir = npath(work.dir)
reg$work.dir = fs::path_norm(work.dir)
reg$packages = packages
reg$namespaces = namespaces
reg$source = source
Expand Down Expand Up @@ -177,17 +177,16 @@ makeRegistry = function(file.dir = "registry", work.dir = getwd(), conf.file = f
setSystemConf(reg, conf.file)

if (is.na(file.dir))
reg$file.dir = tempfile("registry", tmpdir = reg$temp.dir)
reg$file.dir = fs::file_temp("registry", tmp_dir = reg$temp.dir)
"!DEBUG [makeRegistry]: Creating directories in '`reg$file.dir`'"
for (d in fp(reg$file.dir, c("jobs", "results", "updates", "logs", "exports", "external")))
dir.create(d, recursive = TRUE)
reg$file.dir = npath(reg$file.dir)
reg$file.dir = fs::path_norm(reg$file.dir)
fs::dir_create(fs::path(reg$file.dir, c("jobs", "results", "updates", "logs", "exports", "external")))

with_dir(reg$work.dir, loadRegistryDependencies(reg))

class(reg) = "Registry"
saveRegistry(reg)
reg$mtime = file.mtime(fp(reg$file.dir, "registry.rds"))
reg$mtime = file_mtime(fs::path(reg$file.dir, "registry.rds"))
reg$hash = rnd_hash()
info("Created registry in '%s' using cluster functions '%s'", reg$file.dir, reg$cluster.functions$name)
if (make.default)
Expand Down Expand Up @@ -247,7 +246,7 @@ assertRegistry = function(reg, class = NULL, writeable = FALSE, sync = FALSE, ru
assertFlag(sync)
assertFlag(running.ok)

if (reg$writeable && !identical(reg$mtime, file.mtime(fp(reg$file.dir, "registry.rds")))) {
if (reg$writeable && !identical(reg$mtime, file_mtime(fs::path(reg$file.dir, "registry.rds")))) {
warning("Registry has been altered since last read. Switching to read-only mode in this session.")
reg$writeable = FALSE
}
Expand Down Expand Up @@ -294,13 +293,13 @@ loadRegistryDependencies = function(x, must.work = FALSE) {
}
}

path = fp(x$file.dir, "exports")
path = fs::path(x$file.dir, "exports")
fns = list.files(path, pattern = "\\.rds$")
if (length(fns) > 0L) {
ee = .GlobalEnv
Map(function(name, fn) {
assign(x = name, value = readRDS(fn), envir = ee)
}, name = unmangle(fns), fn = fp(path, fns))
}, name = unmangle(fns), fn = fs::path(path, fns))
}

invisible(TRUE)
Expand Down
4 changes: 2 additions & 2 deletions R/batchMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ batchMap = function(fun, ..., args = list(), more.args = list(), reg = getDefaul
return(noIds())
info("Adding %i jobs ...", nrow(ddd))

writeRDS(fun, file = fp(reg$file.dir, "user.function.rds"))
writeRDS(fun, file = fs::path(reg$file.dir, "user.function.rds"))
if (length(more.args) > 0L)
writeRDS(more.args, file = fp(reg$file.dir, "more.args.rds"))
writeRDS(more.args, file = fs::path(reg$file.dir, "more.args.rds"))
ids = seq_row(ddd)

reg$defs = data.table(
Expand Down
6 changes: 3 additions & 3 deletions R/clearRegistry.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ clearRegistry = function(reg = getDefaultRegistry()) {
reg$status = reg$status[FALSE]
reg$defs = reg$defs[FALSE]
reg$resources = reg$resources[FALSE]
user.fun = fp(reg$file.dir, "user.function.rds")
if (file.exists(user.fun)) {
user.fun = fs::path(reg$file.dir, "user.function.rds")
if (fs::file_exists(user.fun)) {
info("Removing user function ...")
file.remove.safely(user.fun)
file_remove(user.fun)
}
sweepRegistry(reg = reg)
}
26 changes: 13 additions & 13 deletions R/clusterFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ cfReadBrewTemplate = function(template, comment.string = NA_character_) {
cfBrewTemplate = function(reg, text, jc) {
assertString(text)

path = if (batchtools$debug || reg$cluster.functions$store.job.files) fp(reg$file.dir, "jobs") else tempdir()
path = if (batchtools$debug || reg$cluster.functions$store.job.files) fs::path(reg$file.dir, "jobs") else fs::path_temp()
fn = sprintf("%s.job", jc$job.hash)
outfile = fp(path, fn)
outfile = fs::path(path, fn)

parent.env(jc) = asNamespace("batchtools")
on.exit(parent.env(jc) <- emptyenv())
Expand Down Expand Up @@ -301,25 +301,25 @@ findTemplateFile = function(name) {

x = Sys.getenv("R_BATCHTOOLS_SEARCH_PATH")
if (nzchar(x)) {
x = fp(x, sprintf("batchtools.%s.tmpl", name))
if (file.exists(x))
return(normalizePath(x, winslash = "/"))
x = fs::path(x, sprintf("batchtools.%s.tmpl", name))
if (fs::file_exists(x))
return(fs::path_real(x))
}

x = sprintf("batchtools.%s.tmpl", name)
if (file.exists(x))
return(normalizePath(x, winslash = "/"))
if (fs::file_exists(x))
return(fs::path_real(x))

x = fp(user_config_dir("batchtools", expand = FALSE), sprintf("%s.tmpl", name))
if (file.exists(x))
x = fs::path(user_config_dir("batchtools", expand = FALSE), sprintf("%s.tmpl", name))
if (fs::file_exists(x))
return(x)

x = fp("~", sprintf(".batchtools.%s.tmpl", name))
if (file.exists(x))
return(normalizePath(x, winslash = "/"))
x = fs::path("~", sprintf(".batchtools.%s.tmpl", name))
if (fs::file_exists(x))
return(fs::path_real(x))

x = system.file("templates", sprintf("%s.tmpl", name), package = "batchtools")
if (file.exists(x))
if (fs::file_exists(x))
return(x)

stopf("Argument 'template' (=\"%s\") must point to a template file or contain the template itself as string (containing at least one newline)", name)
Expand Down
2 changes: 1 addition & 1 deletion R/clusterFunctionsSlurm.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ makeClusterFunctionsSlurm = function(template = "slurm", clusters = NULL, array.

jc$clusters = clusters
if (jc$array.jobs) {
logs = sprintf("%s_%i", basename(jc$log.file), seq_row(jc$jobs))
logs = sprintf("%s_%i", fs::path_file(jc$log.file), seq_row(jc$jobs))
jc$log.file = stri_join(jc$log.file, "_%a")
}
outfile = cfBrewTemplate(reg, template, jc)
Expand Down
2 changes: 1 addition & 1 deletion R/clusterFunctionsTORQUE.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ makeClusterFunctionsTORQUE = function(template = "torque", scheduler.latency = 1
}

if (jc$array.jobs) {
logs = sprintf("%s-%i", basename(jc$log.file), seq_row(jc$jobs))
logs = sprintf("%s-%i", fs::path_file(jc$log.file), seq_row(jc$jobs))
makeSubmitJobResult(status = 0L, batch.id = stri_replace_first_fixed(output, "[]", stri_paste("[", seq_row(jc$jobs), "]")), log.file = logs)
} else {
makeSubmitJobResult(status = 0L, batch.id = output)
Expand Down
25 changes: 12 additions & 13 deletions R/config.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
findConfFile = function() {
x = Sys.getenv("R_BATCHTOOLS_SEARCH_PATH")
if (nzchar(x)) {
x = fp(x, "batchtools.conf.R")
if (file.exists(x))
return(normalizePath(x, winslash = "/"))
x = fs::path(x, "batchtools.conf.R")
if (fs::file_exists(x))
return(fs::path_real(x))
}

x = "batchtools.conf.R"
if (file.exists(x))
return(normalizePath(x, winslash = "/"))
if (fs::file_exists(x))
return(fs::path_real(x))

x = fp(user_config_dir("batchtools", expand = FALSE), "config.R")
if (file.exists(x))
x = fs::path(user_config_dir("batchtools", expand = FALSE), "config.R")
if (fs::file_exists(x))
return(x)

x = normalizePath(fp("~", ".batchtools.conf.R"), winslash = "/", mustWork = FALSE)
if (file.exists(x))
return(x)
x = fs::path("~", ".batchtools.conf.R")
if (fs::file_exists(x))
return(fs::path_real(x))

return(character(0L))
}

setSystemConf = function(reg, conf.file) {
reg$cluster.functions = makeClusterFunctionsInteractive()
reg$default.resources = list()
reg$temp.dir = tempdir()
reg$temp.dir = fs::path_temp()

if (length(conf.file) > 0L) {
assertString(conf.file)
Expand All @@ -33,8 +33,7 @@ setSystemConf = function(reg, conf.file) {

assertClass(reg$cluster.functions, "ClusterFunctions")
assertList(reg$default.resources, names = "unique")
if (!dir.exists(reg$temp.dir))
dir.create(reg$temp.dir, recursive = TRUE)
fs::dir_create(reg$temp.dir)
} else {
info("No configuration file found")
}
Expand Down
Loading

0 comments on commit c4c2fe7

Please sign in to comment.