Skip to content

Commit

Permalink
Read from package db, not from DESCRIPTIONS
Browse files Browse the repository at this point in the history
  • Loading branch information
hsonne committed Apr 30, 2024
1 parent 062cdce commit 4531ea3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ importFrom(kwb.utils,getAttribute)
importFrom(kwb.utils,get_homedir)
importFrom(kwb.utils,hsRestoreAttributes)
importFrom(kwb.utils,lastElement)
importFrom(kwb.utils,moveColumnsToFront)
importFrom(kwb.utils,noFactorDataFrame)
importFrom(kwb.utils,noSuchElements)
importFrom(kwb.utils,orderBy)
Expand Down
50 changes: 34 additions & 16 deletions R/getPackageLicences.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,41 @@
#' Which Licences are Specified for the Packages?
#'
#' @param packages names of (installed) packages
#' @param db optional. Package database, similar to what is returned by
#' \code{\link[utils]{installed.packages}}. Default:
#' \code{as.data.frame(installed.packages())}
#' @return data frame
#' @importFrom kwb.utils moveColumnsToFront rbindAll
#' @importFrom utils installed.packages
#' @export
getPackageLicences <- function(packages, stop.on.error = FALSE)
getPackageLicences <- function(
packages,
db = as.data.frame(utils::installed.packages())
)
{
lapply(packages, function(package) {
description <- readDescription(package, stop.on.error = stop.on.error)
if (is.null(description)) {
data.frame(licence = "<not_found>")
} else {
columns <- intersect(colnames(description), c("licence", "license"))
description[, columns, drop = FALSE] %>%
as.data.frame() %>%
renameColumns(list(license = "licence"))
} # else NULL
}) %>%
stats::setNames(packages) %>%
rbindAll(nameColumn = "package", namesAsFactor = FALSE) %>%
moveColumnsToFront("package")
#kwb.utils::assignPackageObjects("kwb.package");stop.on.error = FALSE
#`%>%` <- magrittr::`%>%`
#db <- kwb.utils:::get_cached("package_db")
#packages <- db$Package

colnames(db) <- tolower(colnames(db))
licence_fields <- intersect(colnames(db), c("licence", "license"))
stopifnot(length(licence_fields) == 1L)

backbone <- data.frame(
package = packages,
stringsAsFactors = FALSE
)

result <- backbone %>%
merge(
y = db[, c("package", licence_fields)],
by = "package",
all.x = TRUE
) %>%
renameColumns(list(license = "licence")) %>%
orderBy("package")

result[["licence"]] <- defaultIfNa(result[["licence"]], "<not_found>")

result
}
11 changes: 7 additions & 4 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,25 @@ readDescription <- function(package, stop.on.error = TRUE)
return(NULL)
}

file %>%
read.dcf() %>%
`colnames<-`(tolower(colnames(.)))
result <- read.dcf(file)

colnames(result) <- tolower(colnames(result))

result
}

# stopIfNotInstalled -----------------------------------------------------------

#' Is a Package Installed?
#'
#' @param package package name (character vector of length one)
#' @importFrom utils installed.packages
#' @export
stopIfNotInstalled <- function(package)
{
stopifnot(is.character(package), length(package) == 1L)

available <- rownames(installed.packages())
available <- rownames(utils::installed.packages())

if (!package %in% available) {
stopFormatted("The package '%s' is not installed.", package)
Expand Down
2 changes: 1 addition & 1 deletion R/packageDependenciesByType.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ packageDependenciesByType <- function(
type = rep(type, length(dependencies))
) %>%
cbind(
getPackageLicences(dependencies) %>%
getPackageLicences(dependencies, db = db) %>%
renameColumns(list(package = "dependency"))
)
} # else NULL
Expand Down
6 changes: 5 additions & 1 deletion man/getPackageLicences.Rd

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

0 comments on commit 4531ea3

Please sign in to comment.