Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseomartelli committed Jul 26, 2024
0 parents commit 2ca6810
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^Makefile
^readme.md
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.Rcheck
*.tar.gz
man
NAMESPACE
12 changes: 12 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Package: normalizepath
Title: What the Package Does (One Line, Title Case)
Version: 0.0.1
Authors@R:
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Imports: jsonlite (>= 1.8.0)
RoxygenNote: 7.3.2
Empty file added LICENSE
Empty file.
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
default: build

.PHONY: document test clean check build clean

PKG_NAME := normalizepath
PKG_VERS := 0.0.1
PKG_TAR := $(PKG_NAME)_$(PKG_VERS).tar.gz

R := R

document:
$(R) -e "devtools::document()"

test:
true # Placeholder.

clean:
rm -rf $(PKG_TAR) $(PKG_NAME).Rcheck

check: build
R CMD check $(PKG_TAR)

build: clean document test
$(R) CMD build .

clean:
rm -rf $(PKG_NAME).Rcheck $(PKG_TAR)
73 changes: 73 additions & 0 deletions R/normalize_path.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' Check if the script is running in a container.
#'
#' @returns A truthy value indicating the state.
is_running_in_docker <- function() {
dockerenv_exists <- file.exists("/.dockerenv")
cgroup_exists <- file.exists("/proc/1/cgroup")
in_container_runtime <- FALSE
if (cgroup_exists) {
in_container_runtime <- any(
grepl("docker", readLines("/proc/1/cgroup", warn = FALSE))
)
}
return(dockerenv_exists || in_container_runtime)
}

#' Gets the absolute path of a file.
#'
#' @param path a normalized, absolute path.
#' @return The absolute host path, if it exists.
#' @export
absolute_path_mapper <- function(path) {
return(normalizePath(path, mustWork = FALSE))
}

#' Normalizes a path
#'
#' @param path The path to normalize.
#' @param path_mappers The mappers to be utilized to normalize the path.
#' @returns The normalized path.
#' @export
normalize_path <- function(path, path_mappers = c()) {
path_mappers <- c(
absolute_path_mapper, # Adds absolute_path_mapper at the beginning.
path_mappers
)
for (mapper in path_mappers) {
path <- mapper(path)
}
return(path)
}

#' Maps a path to host volumes.
#'
#' @param path a normalized, absolute path.
#' @return The absolute host path, if it exists.
#' @export
docker_mount_mapper <- function(path) {
if (!is_running_in_docker()) {
return(path)
}

# Since we are running in docker, we can use our hostname as an heuristic
# to obtain our id.
# TODO: Check if this assumption is right for other container engines and
# generalize this implementation.
hostname <- Sys.info()["nodename"]
output <- system2("docker",
args = paste("inspect -f '{{ json .Mounts }}'", hostname),
stdout = TRUE,
)
parsed_output <- jsonlite::fromJSON(output)

# Iterate over mounts, return first match.
for (i in seq_len(nrow(parsed_output))) {
destination <- parsed_output[i, ]$Destination
if (startsWith(path, destination)) {
source <- parsed_output[i, ]$Source
path <- sub(destination, source, path)
return(path)
}
}
return(path)
}
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# normalizepath

Normalizes path using one (or more) heuristics.

---

Get the latest [release](https://github.com/Reproducible-Bioinformatics/normalizepath/releases/latest).

Usage example:

```R
normalized <- normalize_path(
path = "../the/path/to_normalize",
path_mappers = c(docker_mount_mapper)
)
```

0 comments on commit 2ca6810

Please sign in to comment.