From 2d74ed67af73e54d78251cdaf471ce41d14b5b70 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Thu, 21 Sep 2023 17:49:25 +0100 Subject: [PATCH] Start running separately --- R/run.R | 44 +++++++++++++++++++++++++++--- man/orderly_run.Rd | 35 +++++++++++++++++++++++- tests/testthat/helper-orderly.R | 42 ++++++++++++++++++++++------ tests/testthat/test-run-separate.R | 11 ++++++++ 4 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 tests/testthat/test-run-separate.R diff --git a/R/run.R b/R/run.R index 604e7690..7acca951 100644 --- a/R/run.R +++ b/R/run.R @@ -81,6 +81,29 @@ ##' functionality was never available in orderly version 1, though ##' we had intended to support it. ##' +##' @section Running with a source tree separate from outpack root: +##' +##' Sometimes it is useful to run things from a different place on +##' disk to your outpack root. We know of two cases where this has +##' come up: +##' +##' * when running reports within a runner on a server, we make a +##' clean clone of the source tree at a particular git reference +##' into a new temporary directory and then run the report there, +##' but have it insert into an orderly repo at a fixed and +##' non-temporary location. +##' * we have a user for whom it is more convenient torun their report +##' on a hard drive but store the archive and metadata on a (larger) +##' shared drive. +##' +##' In the first instance, we have a source path at `` which +##' contains the file `orderly_config.yml` and the directory `src/` +##' with our source reports, and a separate path `` which +##' contains the directory `.outpack/` with all the metadata - it +##' may also have an unpacked archive, and a `.git/` directory +##' depending on the configuration. (Later this will make more sense +##' once we support a "bare" outpack layout.) +##' ##' @title Run a report ##' ##' @param name Name of the report to run. Any leading `./` `src/` or @@ -113,6 +136,12 @@ ##' then orderly looks in the working directory and up through its ##' parents until it finds an `.outpack` directory ##' +##' @param root_src Separately, the root of the orderly source tree, +##' if separate from the outpack root (given as `root`). This is +##' intended for running reports in situations where the source tree +##' is kept in a different place to the outpack root; see Details +##' for more information. +##' ##' @return The id of the created report (a string) ##' ##' @export @@ -129,10 +158,17 @@ ##' # and we can query the metadata: ##' orderly2::orderly_metadata_extract(name = "data", root = path) orderly_run <- function(name, parameters = NULL, envir = NULL, echo = TRUE, - search_options = NULL, root = NULL, locate = TRUE) { - root <- root_open(root, locate, require_orderly = TRUE, - call = environment()) - root_src <- root$path + search_options = NULL, root = NULL, locate = TRUE, + root_src = NULL) { + if (is.null(root_src)) { + root <- root_open(root, locate, require_orderly = TRUE, + call = environment()) + root_src <- root$path + } else { + root <- root_open(root, locate, require_orderly = FALSE, + call = environment()) + root_src <- orderly_src_root(root_src, locate, call = environment()) + } name <- validate_orderly_directory(name, root_src, environment()) diff --git a/man/orderly_run.Rd b/man/orderly_run.Rd index 360bf5c0..c5a40319 100644 --- a/man/orderly_run.Rd +++ b/man/orderly_run.Rd @@ -11,7 +11,8 @@ orderly_run( echo = TRUE, search_options = NULL, root = NULL, - locate = TRUE + locate = TRUE, + root_src = NULL ) } \arguments{ @@ -44,6 +45,12 @@ directory is configured for orderly, and not just outpack (see searched for. If \code{TRUE} and \code{config} is not given, then orderly looks in the working directory and up through its parents until it finds an \code{.outpack} directory} + +\item{root_src}{Separately, the root of the orderly source tree, +if separate from the outpack root (given as \code{root}). This is +intended for running reports in situations where the source tree +is kept in a different place to the outpack root; see Details +for more information.} } \value{ The id of the created report (a string) @@ -137,6 +144,32 @@ functionality was never available in orderly version 1, though we had intended to support it. } +\section{Running with a source tree separate from outpack root}{ + + +Sometimes it is useful to run things from a different place on +disk to your outpack root. We know of two cases where this has +come up: +\itemize{ +\item when running reports within a runner on a server, we make a +clean clone of the source tree at a particular git reference +into a new temporary directory and then run the report there, +but have it insert into an orderly repo at a fixed and +non-temporary location. +\item we have a user for whom it is more convenient torun their report +on a hard drive but store the archive and metadata on a (larger) +shared drive. +} + +In the first instance, we have a source path at \verb{} which +contains the file \code{orderly_config.yml} and the directory \verb{src/} +with our source reports, and a separate path \verb{} which +contains the directory \verb{.outpack/} with all the metadata - it +may also have an unpacked archive, and a \verb{.git/} directory +depending on the configuration. (Later this will make more sense +once we support a "bare" outpack layout.) +} + \examples{ # Create a simple example: path <- orderly2::orderly_example("default") diff --git a/tests/testthat/helper-orderly.R b/tests/testthat/helper-orderly.R index 2cba992f..39a1d8be 100644 --- a/tests/testthat/helper-orderly.R +++ b/tests/testthat/helper-orderly.R @@ -6,17 +6,42 @@ options(outpack.schema_validate = test_prepare_orderly_example <- function(examples, ...) { tmp <- tempfile() withr::defer_parent(unlink(tmp, recursive = TRUE)) - suppressMessages(orderly_init(tmp)) - config <- readLines(file.path(tmp, "orderly_config.yml")) + suppressMessages(orderly_init(tmp, ...)) + copy_examples(examples, tmp) + tmp +} + + +test_prepare_orderly_example_separate <- function(examples, ...) { + tmp <- tempfile() + withr::defer_parent(unlink(tmp, recursive = TRUE)) + + path_outpack <- file.path(tmp, "outpack") + suppressMessages(orderly_init(path_outpack, ...)) + + path_src <- file.path(tmp, "src") + copy_examples(examples, path_src) + list(src = path_src, outpack = path_outpack) +} + + +copy_examples <- function(examples, path_src) { + if (file.exists(path_src)) { + config <- readLines(file.path(path_src, "orderly_config.yml")) + } else { + config <- empty_config_contents() + } + + fs::dir_create(path_src) if (any(c("shared", "shared-dir") %in% examples)) { - fs::dir_create(file.path(tmp, "shared")) + fs::dir_create(file.path(path_src, "shared")) if ("shared" %in% examples) { fs::file_copy(test_path("examples/explicit/data.csv"), - file.path(tmp, "shared")) + file.path(path_src, "shared")) } if ("shared-dir" %in% examples) { - fs::dir_create(file.path(tmp, "shared", "data")) + fs::dir_create(file.path(path_src, "shared", "data")) } } @@ -28,13 +53,12 @@ test_prepare_orderly_example <- function(examples, ...) { " distribution:", " normal") } + writeLines(config, file.path(path_src, "orderly_config.yml")) - writeLines(config, file.path(tmp, "orderly_config.yml")) - fs::dir_create(file.path(tmp, "src")) + fs::dir_create(file.path(path_src, "src")) for (i in examples) { - fs::dir_copy(test_path("examples", i), file.path(tmp, "src")) + fs::dir_copy(test_path("examples", i), file.path(path_src, "src")) } - tmp } diff --git a/tests/testthat/test-run-separate.R b/tests/testthat/test-run-separate.R new file mode 100644 index 00000000..3c32b20a --- /dev/null +++ b/tests/testthat/test-run-separate.R @@ -0,0 +1,11 @@ +test_that("can run simple case in separate directory", { + info <- test_prepare_orderly_example_separate("explicit") + id <- orderly_run_quietly("explicit", envir = new.env(), + root = info$outpack, root_src = info$src) + expect_type(id, "character") + expect_true(file.exists(file.path(info$src, "draft"))) + expect_false(file.exists(file.path(info$src, "archive"))) + expect_false(file.exists(file.path(info$outpack, "draft"))) + expect_true(file.exists(file.path(info$outpack, "archive"))) + expect_true(file.exists(file.path(info$outpack, "archive", "explicit", id))) +})