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

Add tempdir() to avoid unwanted package file writing #239

Merged
merged 9 commits into from
Aug 4, 2019
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ Authors@R:
person(given = "Hauke",
family = "Sonnenberg",
role = "ctb",
comment = c(ORCID = "0000-0001-9134-2871"))
comment = c(ORCID = "0000-0001-9134-2871")),
person(given = "Sebastian",
family = "Kreutzer",
role = "ctb",
comment = c(ORCID = "0000-0002-0734-2199"))
)
Description: The 'Codemeta' Project defines a 'JSON-LD' format for describing
software metadata, as detailed at <https://codemeta.github.io>. This package
Expand All @@ -66,7 +70,6 @@ Depends: R (>= 3.0.0)
Imports:
jsonlite (>= 1.6),
git2r,
pkgbuild,
memoise,
methods,
stats,
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* address internet timeout issues
* tidy source code
* update test suite to reflect newly available metadata.
* `write_codemeta()` and `create_codemeta()`: `use_filesize = FALSE` is now the default and the estimation of the file size does not leave any more unwanted files behind [PR #239](https://github.com/ropensci/codemetar/pull/239). Furthermore, the way the file size is calculated changed: Before we used the size of the package built with `pkgbuild::build()`, which took rather long. Now the size is calculated based on the source files minus files excluded via
`.Rbuildignore` (if such a file exists).
* `write_codemeta()`: the default of argument `use_git_hook` is now `FALSE` to avoid an
unwanted alteration of the user's git environment [issue #240](https://github.com/ropensci/codemetar/issues/240).
* Package dependency to 'pkgbuild' has been dropped.

# codemetar 0.1.7 2018-12

Expand Down
2 changes: 1 addition & 1 deletion R/create_codemeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ create_codemeta <- function(
pkg = ".",
root = ".",
id = NULL,
use_filesize = TRUE,
use_filesize = FALSE,
force_update =
getOption("codemeta_force_update", TRUE),
verbose = TRUE,
Expand Down
62 changes: 41 additions & 21 deletions R/guess_other_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,54 @@ guess_releaseNotes <- function(root = ".") {
}

# guess_fileSize ---------------------------------------------------------------
guess_fileSize <- function(root = ".") {

## No root, no file size
if (is.null(root)) {

#' @title Estimate the File Size of the Software
#'
#' @description The function makes a rough estimation of the size of the
#' R package using the base R function [file.size]. Files in `.Rbuildignore` are
#' exclude. Please note that this estimation does not necessarily reflect the installed size
#' of the package.
#'
#' @param root the root file path
#'
#' @param .ignore optional vector of regular expresssions that will be ignore when the file
#' size is guessed
#'
#' @seealso [base::file.size]
#'
#' @examples
#'
#' guess_fileSize()
#'
#' @md
#' @noRd
guess_fileSize <- function(root = ".", .ignore = NULL) {
## no root, no file size
if (is.null(root))
return(NULL)
}

## Look for files 1. Meta, 2. DESCRIPTION
file_exists <- file.exists(file.path(root, c("Meta", "DESCRIPTION")))
## check for .Rbuildignore, everything listed should be excluded since
## it will not become part of the final package
if (file.exists(".Rbuildignore") && is.null(.ignore)){
.ignore <- readLines(normalizePath(paste0(root,"Rbuildignore")), warn = FALSE)

## Meta exists -> cannot build (or read file size?) on an already installed
## package
if (file_exists[1] || ! file_exists[2]) {
}else{
.ignore <- " "

return(NULL)
}

file_path <- try(silent = TRUE, pkgbuild::build(
root, vignettes = FALSE, manual = FALSE, quiet = TRUE
## grep all files of interest (exclude hidden files)
files <- normalizePath(list.files(
path = normalizePath(root),
recursive = TRUE,
full.names = TRUE,
all.files = FALSE
))

if (inherits(file_path, "try-error")) {
## kick-out all files that do not belong to the R package
files <-
files[!grepl(paste(.ignore, collapse = "|"), files, perl = TRUE)]

NULL

} else {

paste0(file.size(file_path) / 1e3, "KB")
}
## estimate total size
paste0(sum(file.size(files)) / 1e3, "KB")
}

11 changes: 6 additions & 5 deletions R/write_codemeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
#' @param root if pkg is a codemeta object, optionally give the path to package
#' root. Default guess is current dir.
#' @param id identifier for the package, e.g. a DOI (or other resolvable URL)
#' @param use_filesize whether to try adding a filesize by using
#' \code{pkgbuild::build()}.
#' @param use_filesize whether to try to estimating and adding a filesize by using
#' \code{base::files.ize()}. Files in \code{.Rbuildignore} are ignored.
#' @param force_update Update guessed fields even if they are defined in an
#' existing codemeta.json file
#' @param use_git_hook Whether to create a pre-commit hook requiring
#' codemeta.json to be updated when DESCRIPTION is changed. Defaults to TRUE.
#' codemeta.json to be updated when DESCRIPTION is changed. Default is \code{FALSE} to avoid
#' an unwanted alteration of the user's git environment.
#' @param verbose Whether to print messages indicating opinions e.g. when
#' DESCRIPTION has no URL. See \code{\link{give_opinions}}.
#' @param ... additional arguments to \code{\link{write_json}}
Expand All @@ -39,8 +40,8 @@
#' write_codemeta("codemetar", path = "example_codemetar_codemeta.json")
#' }
write_codemeta <- function(
pkg = ".", path = "codemeta.json", root = ".", id = NULL, use_filesize = TRUE,
force_update = getOption("codemeta_force_update", TRUE), use_git_hook = TRUE,
pkg = ".", path = "codemeta.json", root = ".", id = NULL, use_filesize = FALSE,
force_update = getOption("codemeta_force_update", TRUE), use_git_hook = FALSE,
verbose = TRUE, ...
) {

Expand Down
1 change: 1 addition & 0 deletions man/codemetar-package.Rd

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

6 changes: 3 additions & 3 deletions man/create_codemeta.Rd

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

11 changes: 6 additions & 5 deletions man/write_codemeta.Rd

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

12 changes: 8 additions & 4 deletions tests/testthat/test-guess_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@ test_that("fileSize", {
skip_on_cran()
skip_if_offline()

## should be NULL if root == NULL
expect_null(guess_fileSize(NULL))
expect_null(guess_fileSize("."))
## expect_null?
f <- system.file(".", package="codemetar")
expect_null(guess_fileSize(f))

## this should just work fine
expect_is(guess_fileSize("."), "character")

## test argument .ignore
expect_is(guess_fileSize(".", .ignore = " "), "character")

})

test_that("add_github_topics",{
Expand Down