-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2ca6810
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
^Makefile | ||
^readme.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.Rcheck | ||
*.tar.gz | ||
man | ||
NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
``` |