Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Package info #6

Merged
merged 90 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
d3c9ced
Add manifest creation datetime to output
AlexAxthelm Mar 18, 2024
f50be58
add function to get environment information
AlexAxthelm Mar 19, 2024
c43ca87
Add test for session info
AlexAxthelm Mar 19, 2024
bc3e60e
eliminate extra comma
AlexAxthelm Mar 19, 2024
d7297c3
Reindent
AlexAxthelm Mar 19, 2024
7a25821
Add tests for local package info
AlexAxthelm Mar 19, 2024
ac5a6f3
Add test for package info from GH source
AlexAxthelm Mar 19, 2024
b58e156
Add dependency on `pak`
AlexAxthelm Mar 19, 2024
85e46ec
linting
AlexAxthelm Mar 19, 2024
8c4d51c
Change testing flow for local packages
AlexAxthelm Mar 20, 2024
b6d28fe
add local R_USER_CACHE_DIR
AlexAxthelm Mar 20, 2024
c1a40a7
Change from pak to pkgdepends fo package info
AlexAxthelm Mar 20, 2024
506af48
Capture PAK output to make tests easier to read
AlexAxthelm Mar 20, 2024
2df69db
Sanitize package details before exporting
AlexAxthelm Mar 20, 2024
6e86e29
Add pak to testing deps
AlexAxthelm Mar 20, 2024
6281f19
copy pak functions before messing with libpaths
AlexAxthelm Mar 20, 2024
bf63e19
Fix madness with libpaths
AlexAxthelm Mar 20, 2024
0239085
bump dev version
AlexAxthelm Mar 20, 2024
0317d14
Add debugging informtaion to test ouput
AlexAxthelm Mar 20, 2024
0cad051
prefix libpath, not replace
AlexAxthelm Mar 20, 2024
738aeb6
Update Docs
AlexAxthelm Mar 20, 2024
05753b8
refactor local installs
AlexAxthelm Mar 20, 2024
24bbd96
refactor package info expectations
AlexAxthelm Mar 20, 2024
9788431
Allow matching for repo
AlexAxthelm Mar 20, 2024
1a5233b
Change from `git2r` to `gert`
AlexAxthelm Mar 20, 2024
4096ad8
Chagne dependency in DESCRIPTION
AlexAxthelm Mar 20, 2024
408825e
Change argument name
AlexAxthelm Mar 20, 2024
21b1b4f
explicitly define pak sources
AlexAxthelm Mar 20, 2024
e932448
better define matching regex
AlexAxthelm Mar 20, 2024
cd0fcb0
add libPaths to session info
AlexAxthelm Mar 20, 2024
09d54c8
Convert filepath sep in expectations
AlexAxthelm Mar 20, 2024
bfb3f7a
Add updated libpaths to tests
AlexAxthelm Mar 20, 2024
5079cc8
Add fix for remotepkgref on Windows
AlexAxthelm Mar 20, 2024
ef590c6
nolint
AlexAxthelm Mar 20, 2024
8825b76
Add test for missing package
AlexAxthelm Mar 20, 2024
41f40b5
Switch pattern and replacement in file path fix
AlexAxthelm Mar 20, 2024
930a8ad
add lintr exclusion
AlexAxthelm Mar 21, 2024
2249851
Add check for singularity
AlexAxthelm Mar 21, 2024
80f1c23
Add test for singularity
AlexAxthelm Mar 21, 2024
17d3782
Add check for package existence
AlexAxthelm Mar 21, 2024
df2f296
clean up unneeded nolints
AlexAxthelm Mar 21, 2024
286e770
add check for multiple installations of package
AlexAxthelm Mar 21, 2024
941044a
add test for libpaths down the search tree
AlexAxthelm Mar 21, 2024
5cab781
Fix behavior for down-search-path libraries
AlexAxthelm Mar 21, 2024
eafbc85
namespace `utils::installed.packages()`
AlexAxthelm Mar 21, 2024
4a8f591
fix IMCOMPLETE_STRING lintr error
AlexAxthelm Mar 21, 2024
daf3526
Linting
AlexAxthelm Mar 21, 2024
9aee81e
Fix failing test
AlexAxthelm Mar 21, 2024
631b926
Check version as part of multiple installs
AlexAxthelm Mar 21, 2024
21d7660
Attempt fixing windows paths
AlexAxthelm Mar 21, 2024
8367fb0
reduce strictness for platform check
AlexAxthelm Mar 21, 2024
f48d550
reflow tests when altering libpaths against current
AlexAxthelm Mar 21, 2024
aee5cc1
Reindent
AlexAxthelm Mar 21, 2024
72da85f
Fix paths on windows (again)
AlexAxthelm Mar 21, 2024
5b5cb77
Change placeholder return value
AlexAxthelm Mar 21, 2024
23a1c34
Add placeholder test
AlexAxthelm Mar 21, 2024
447532b
move functions into own file
AlexAxthelm Mar 21, 2024
fe0f9d2
Document session info
AlexAxthelm Mar 21, 2024
39d9716
update docstrings
AlexAxthelm Mar 21, 2024
cd67cc1
Update rendered docs
AlexAxthelm Mar 21, 2024
01f82b9
Rename test file
AlexAxthelm Mar 21, 2024
567a88d
Name testthat args
AlexAxthelm Mar 21, 2024
2bd9954
Allow NA fgor pkg ref (base pkgs)
AlexAxthelm Mar 21, 2024
ca3a8eb
Reflow docstrings
AlexAxthelm Mar 21, 2024
73f1773
improve tests
AlexAxthelm Mar 21, 2024
d9f2430
Add test for empty arguments
AlexAxthelm Mar 21, 2024
41bbce0
Add tests for `get_package_info()`
AlexAxthelm Mar 21, 2024
181edbd
Update docstrings
AlexAxthelm Mar 21, 2024
52470c2
update rendered docs
AlexAxthelm Mar 21, 2024
d0228c2
Reflow docstrings
AlexAxthelm Mar 21, 2024
092c957
Update rendered docs
AlexAxthelm Mar 21, 2024
26afb9c
Add tests for NULL arguments
AlexAxthelm Mar 22, 2024
9a7847e
change structure of outputs
AlexAxthelm Mar 22, 2024
c1d1f72
Update signature for get_package_info
AlexAxthelm Mar 25, 2024
ebc6517
update rendered docs
AlexAxthelm Mar 25, 2024
f2c816f
re-enable tests
AlexAxthelm Mar 25, 2024
86b0db7
Add test for mixed nesting
AlexAxthelm Mar 25, 2024
cc7f45d
lintr exclusion
AlexAxthelm Mar 25, 2024
c558112
Add handling for packages loaded with pkgload
AlexAxthelm Mar 25, 2024
ed66d62
Add test for `devtools::load_all()`
AlexAxthelm Mar 25, 2024
9addb26
Add pkgload dependency
AlexAxthelm Mar 25, 2024
eb69f3a
Add skips for missing packages
AlexAxthelm Mar 25, 2024
f6a54f2
Add warning handling
AlexAxthelm Mar 25, 2024
cb5a36f
Reindent
AlexAxthelm Mar 25, 2024
f54db91
Add devtools to suggests
AlexAxthelm Mar 25, 2024
f54e709
Check for pkgload, not CRAN for warning handler
AlexAxthelm Mar 25, 2024
a9b856a
Do not test for silence
AlexAxthelm Mar 25, 2024
69ac9bb
update rendered docs
AlexAxthelm Mar 25, 2024
876b4fb
Normalize path
AlexAxthelm Mar 25, 2024
d94cc6a
change output key name
AlexAxthelm Mar 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pacta.workflow.utils
Title: Utility functions for PACTA workflows
Version: 0.0.0.9001
Version: 0.0.0.9002
Authors@R:
c(person(given = "Alex",
family = "Axthelm",
Expand All @@ -18,8 +18,13 @@ RoxygenNote: 7.3.1
Imports:
digest,
jsonlite,
logger
logger,
pkgdepends,
pkgload
Suggests:
devtools,
gert,
pak,
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
8 changes: 7 additions & 1 deletion R/export_manifest.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@
logger::log_debug("Creating metadata manifest")
manifest_list <- list(
input_files = get_file_metadata(input_files),
output_files = get_file_metadata(output_files)
output_files = get_file_metadata(output_files),
manifest_creation_datetime = format.POSIXct(
x = Sys.time(),
format = "%F %R",
tz = "UTC",
usetz = TRUE

Check warning on line 47 in R/export_manifest.R

View check run for this annotation

Codecov / codecov/patch

R/export_manifest.R#L42-L47

Added lines #L42 - L47 were not covered by tests
)
)
return(manifest_list)
}
30 changes: 30 additions & 0 deletions R/get_environment.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' Get Environment information for manifest
#'
#' This function takes no arguments and returns a nested list, suitable for
#' inclusion in manifest export.
#'
#' @return nested list of file details, length the same as the input vector.
get_manifest_envirionment_info <- function() {
#: Envvars
return(invisible(NULL))
}

#' Get session information for manifest
#'
#' This function takes no arguments and returns a list, suitable for
#' inclusion in manifest export.
#'
#' @return list of session details, including R Version, platform, OS
#' (`running`), locale, timezone, and library paths.
get_r_session_info <- function() {
return(
list(
R.version = utils::sessionInfo()[["R.version"]],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$R.version$nickname
[1] "Eye Holes"

🤣

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL

platform = utils::sessionInfo()[["platform"]],
running = utils::sessionInfo()[["running"]],
locale = utils::sessionInfo()[["locale"]],
tzone = utils::sessionInfo()[["tzone"]],
libPaths = .libPaths() # nolint: undesirable_function_linter
)
)
}
147 changes: 147 additions & 0 deletions R/get_package_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#' Get package information for active packages
#'
#' This function takes a vector or (possibly nested) list of package names and
#' returns a nested list of package details, suitable for inclusion in manifest
#' export.
#'
#' @param packagelist vector or list of package names. Best left as default,
#' which includes the currently loaded and attached namespaces separated into
#' useful categories.
#'
#' @return nested list of file details, length 3, with top level keys being
#' `base`, `attached`, and `loaded`. Underneath those keys are lists of package
#' details, with the package names as keys, and further details as returned by
#' [get_individual_package_info()].
#' @seealso [get_individual_package_info()]
get_package_info <- function(
packagelist = list(
base = utils::sessionInfo()[["basePkgs"]],
attached = names(utils::sessionInfo()[["otherPkgs"]]),
loaded = names(utils::sessionInfo()[["loadedOnly"]])
)
) {
log_debug("Getting package info.")
if (inherits(packagelist, "character")) {
out <- vapply(
X = packagelist,
FUN = function(x) {
list(x = get_individual_package_info(x))
},
FUN.VALUE = list(1L),
USE.NAMES = TRUE
)
} else {
out <- vapply(
X = packagelist,
FUN = function(x) {
list(
x = get_package_info(x)
)
},
FUN.VALUE = list(1L),
USE.NAMES = TRUE
)
}
return(out)
}

#' Get package information for a package
#'
#' This function takes a single package name and returns a list of package
#' details, suitable for inclusion in manifest export.
#'
#' @param packagename Singular charater string of package name
#'
#' @return nested list of file details, length 11, with keys:
#' - `package`: The name of the package
#' - `version`: The version of the package
#' - `loaded_with_pkgload`: Is this package loaded with `pkgload`? (logical).
#' Useful for identifying local development versions
#' - `library`: The path of the library the package is installed in
#' - `library_index`: The index of the library in the `.libPaths()` vector
#' - `repository`: The repository the package was pulled from
#' - `platform`: The platform the package was built for
#' - `built`: Information about package build (relevant for binary packages)
#' - `remotetype`: The type of remote repository the package was pulled from
#' - `remotepkgref`: The reference used by `pak` to install the package
#' - `remoteref`: The reference of the package when it was pulled from REPO
#' - `remotesha`: the SHA-1 hash of the reference (if applicable)
get_individual_package_info <- function(packagename) {
if (length(packagename) != 1L || !is.character(packagename)) {
log_error("packagename must be a single string.")
# Early return
stop("packagename must be a single string.")
}
dev_package <- pkgload::is_dev_package(packagename)
if (dev_package) {
log_warn("Package \"{packagename}\" is a development package.")
log_warn("Package information may not be accurate.")
warning("Identifying development packages may not be accurate.")
package_dev_dir <- pkgload::pkg_path(
path = dirname(system.file("DESCRIPTION", package = packagename))
)
pkg_details <- list(
package = pkgload::pkg_name(package_dev_dir),
version = paste("DEV", pkgload::pkg_version(package_dev_dir)),
library = NA_character_,
library_index = NA_integer_,
repository = NA_character_,
platform = NA_character_,
built = NA_character_,
remotetype = "pkgload",
remotepkgref = normalizePath(package_dev_dir),
remoteref = NA_character_,
remotesha = NA_character_
)
} else {
if (packagename %in% utils::installed.packages()[, "Package"]) {
installed_index <- which(
utils::installed.packages()[, "Package"] == packagename
)
installed_path <- utils::installed.packages()[installed_index, "LibPath"]
if (length(installed_path) > 1L) {
log_warn(
"Multiple installations of package \"{packagename}\" found: ",
"{installed_path}"
)
log_warn("Using installation first on the search path.")
warning("Multiple installations of package found.")
}
lib_index <- min(which(.libPaths() == installed_path)) #nolint: undesirable_function_linter
lib <- .libPaths()[lib_index] #nolint: undesirable_function_linter
log_trace("Package \"{packagename}\" is installed at {lib}")
} else {
log_error("Package \"{packagename}\" is not installed.")
stop("Package is not installed.")
}
log_trace("Getting package info for {packagename}.")
pkg_details <- as.list(
pkgdepends::lib_status(
library = lib,
packages = packagename
)
)
pkg_details[["library_index"]] <- lib_index
}
details_list <- list(
package = pkg_details[["package"]],
version = pkg_details[["version"]],
loaded_with_pkgload = dev_package,
library = pkg_details[["library"]],
library_index = pkg_details[["library_index"]],
repository = pkg_details[["repository"]],
platform = pkg_details[["platform"]],
built = pkg_details[["built"]],
remotetype = pkg_details[["remotetype"]],
remotepkgref = pkg_details[["remotepkgref"]],
remoteref = pkg_details[["remoteref"]],
remotesha = pkg_details[["remotesha"]]
)
clean_details_list <- lapply(
X = details_list,
FUN = function(x) {
ifelse(is.null(x), NA_character_, x)
}
)
return(clean_details_list)
}
33 changes: 33 additions & 0 deletions man/get_individual_package_info.Rd

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

15 changes: 15 additions & 0 deletions man/get_manifest_envirionment_info.Rd

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

31 changes: 31 additions & 0 deletions man/get_package_info.Rd

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

16 changes: 16 additions & 0 deletions man/get_r_session_info.Rd

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

32 changes: 32 additions & 0 deletions tests/testthat/test-get_environment.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## save current settings so that we can reset later
threshold <- logger::log_threshold()
appender <- logger::log_appender()
layout <- logger::log_layout()
on.exit({
## reset logger settings
logger::log_threshold(threshold)
logger::log_layout(layout)
logger::log_appender(appender)
})

logger::log_appender(logger::appender_stdout)
logger::log_threshold(logger::FATAL)
logger::log_layout(logger::layout_simple)
AlexAxthelm marked this conversation as resolved.
Show resolved Hide resolved

test_that("get_single_file_metadata processes CSV tables correctly", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like this test name/message is out of sync with reality

expect_identical(
get_r_session_info(),
list(
R.version = utils::sessionInfo()[["R.version"]],
platform = utils::sessionInfo()[["platform"]],
running = utils::sessionInfo()[["running"]],
locale = utils::sessionInfo()[["locale"]],
tzone = utils::sessionInfo()[["tzone"]],
libPaths = .libPaths() # nolint: undesirable_function_linter
)
)
})

testthat::expect_null(
get_manifest_envirionment_info()
)
Loading
Loading