Skip to content

Commit

Permalink
Merge branch 'feat/1-export-manifests' into test-compare-action
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAxthelm committed Mar 13, 2024
2 parents f29ed6a + ef1f149 commit ed4db13
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 2 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
^LICENSE\.md$
.github/
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ Description: Provide utility functions to be called across RMI-PACTA's workflows
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
Imports:
digest,
jsonlite,
logger
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(export_manifest)
18 changes: 17 additions & 1 deletion R/export_manifest.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,33 @@ export_manifest <- function(
output_files
) {

manifest_list <- list()
manifest_list <- create_manifest(
input_files = input_files,
output_files = output_files

Check warning on line 18 in R/export_manifest.R

View check run for this annotation

Codecov / codecov/patch

R/export_manifest.R#L16-L18

Added lines #L16 - L18 were not covered by tests
)

manifest_json <- jsonlite::toJSON(
manifest_list,
pretty = TRUE,
auto_unbox = TRUE

Check warning on line 24 in R/export_manifest.R

View check run for this annotation

Codecov / codecov/patch

R/export_manifest.R#L21-L24

Added lines #L21 - L24 were not covered by tests
)

logger::log_debug("Writing metadata to file: ", manifest_path)
writeLines(
text = manifest_json,
con = manifest_path

Check warning on line 30 in R/export_manifest.R

View check run for this annotation

Codecov / codecov/patch

R/export_manifest.R#L27-L30

Added lines #L27 - L30 were not covered by tests
)
return(invisible(manifest_json))

Check warning on line 32 in R/export_manifest.R

View check run for this annotation

Codecov / codecov/patch

R/export_manifest.R#L32

Added line #L32 was not covered by tests
}

create_manifest <- function(
input_files,
output_files
) {
logger::log_debug("Creating metadata manifest")
manifest_list <- list(
input_files = get_file_metadata(input_files),
output_files = get_file_metadata(output_files)

Check warning on line 42 in R/export_manifest.R

View check run for this annotation

Codecov / codecov/patch

R/export_manifest.R#L39-L42

Added lines #L39 - L42 were not covered by tests
)
return(manifest_list)

Check warning on line 44 in R/export_manifest.R

View check run for this annotation

Codecov / codecov/patch

R/export_manifest.R#L44

Added line #L44 was not covered by tests
}
84 changes: 84 additions & 0 deletions R/get_file_metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#' Get Metadata for a vector of filepaths
#'
#' This function takes a vector of filepaths and returns a list of file
#' details, suitable for inclusion in manifest export.
#'
#' @param filepaths vector of filepaths
#'
#' @return nested list of file details, length the same as the input vector.
get_file_metadata <- function(filepaths) {
logger::log_trace("Getting metadata for files.")

Check warning on line 10 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L10

Added line #L10 was not covered by tests

file_metadata <- lapply(filepaths, get_single_file_metadata)

Check warning on line 12 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L12

Added line #L12 was not covered by tests

return(file_metadata)

Check warning on line 14 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L14

Added line #L14 was not covered by tests
}

#' Get Metadata for a file
#'
#' This function takes a single filepaths and returns a list of file
#' details, suitable for inclusion in manifest export.
#'
#' @param filepath vector of filepaths
#'
#' @return list of file details
get_single_file_metadata <- function(filepath) {
if (length(filepath) > 1L) {
logger::log_error("get_single_file_metadata only accepts single files.")
stop("Only one file path can be passed to get_single_file_metadata.")

Check warning on line 28 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L26-L28

Added lines #L26 - L28 were not covered by tests
}

logger::log_trace("Getting metadata for file: \"{filepath}\".")

Check warning on line 31 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L31

Added line #L31 was not covered by tests

file_name <- basename(filepath)
file_extension <- tools::file_ext(filepath)
file_path <- filepath
file_size <- file.info(filepath)[["size"]]
file_last_modified <- format(
as.POSIXlt(file.info(filepath)[["mtime"]], tz = "UTC"),
"%Y-%m-%dT%H:%M:%S+00:00"

Check warning on line 39 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L33-L39

Added lines #L33 - L39 were not covered by tests
)
file_md5 <- digest::digest(filepath, algo = "md5", file = TRUE)

Check warning on line 41 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L41

Added line #L41 was not covered by tests

file_metadata <- list(
file_name = file_name,
file_extension = file_extension,
file_path = file_path,
file_size = file_size,
file_last_modified = file_last_modified,
file_md5 = file_md5

Check warning on line 49 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L43-L49

Added lines #L43 - L49 were not covered by tests
)

logger::log_trace("Getting summary information for file: \"{filepath}\".")
if (tolower(tools::file_ext(filepath)) == "rds") {
contents <- readRDS(filepath)
} else if (tolower(tools::file_ext(filepath)) == "csv") {
contents <- utils::read.csv(filepath)

Check warning on line 56 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L52-L56

Added lines #L52 - L56 were not covered by tests
} else {
logger::log_trace(
"File not supported for summary information: \"{filepath}\"."

Check warning on line 59 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L58-L59

Added lines #L58 - L59 were not covered by tests
)
contents <- NULL

Check warning on line 61 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L61

Added line #L61 was not covered by tests
}
# expecting a data.frame for output files
if (inherits(contents, "data.frame")) {
summary_info <- list(
nrow = nrow(contents),
colnames = colnames(contents),
class = class(contents)

Check warning on line 68 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L64-L68

Added lines #L64 - L68 were not covered by tests
)
} else {
logger::log_trace(
"Only data.frame objects supported for summary information."

Check warning on line 72 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L71-L72

Added lines #L71 - L72 were not covered by tests
)
summary_info <- list(
class = class(contents)

Check warning on line 75 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L74-L75

Added lines #L74 - L75 were not covered by tests
)
}

if (exists("summary_info")) {
file_metadata[["summary_info"]] <- summary_info

Check warning on line 80 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L79-L80

Added lines #L79 - L80 were not covered by tests
}

return(file_metadata)

Check warning on line 83 in R/get_file_metadata.R

View check run for this annotation

Codecov / codecov/patch

R/get_file_metadata.R#L83

Added line #L83 was not covered by tests
}
21 changes: 21 additions & 0 deletions man/export_manifest.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/get_file_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/get_single_file_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed4db13

Please sign in to comment.