Skip to content
This repository has been archived by the owner on Jul 31, 2019. It is now read-only.

Commit

Permalink
helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
hrbrmstr committed Aug 23, 2018
1 parent cf0133c commit 3a7412a
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ export(gep_render_har)
export(gep_render_html)
export(gep_render_magick)
export(gep_render_pdf)
export(gep_stop)
export(gepetto)
export(get_chrome_env)
export(install_gepetto)
export(set_chrome_env)
export(start_gepetto)
export(stop_gepetto)
import(httr)
import(magick)
import(processx)
Expand Down
141 changes: 141 additions & 0 deletions R/gepetto-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#' Install gepetto
#'
#' This function verifies you have what you need to install `gepetto` then
#' does so (globally). Ideally, you'll have `Node.js` and `npm` already on your
#' system (since this function will not install them).
#'
#' While you _can_ try to install gepetto this way, it is highly recommended that
#' you follow the instructions (<https://gitlab.com/hrbrmstr/gepetto>) manually
#' since many Node.js/npm installations have permissions issues due to errant
#' historical use of `sudo`.
#'
#' @md
#' @export
install_gepetto <- function() {

node <- Sys.which("node")
npm <- Sys.which("npm")

if (node == "") {
message(
"Could not locate Node.js. Please visit <https://nodejs.org/en/download/package-manager/> ",
"and obtain it via one of the methods described there then try installing gepetto from ",
"a fresh R session.", sep=""
)
return()
}

if (npm == "") {
message(
"Could not local npm. Please visit <https://www.npmjs.com/get-npm> to ensure ",
"you have installed both Node.js and npm correctly, then try installing gepetto ",
"from a fresh R session.", sep=""
)
return()
}

res <- processx::run(node, "--version", error_on_status = FALSE)
if (res$status != 0) {
message(
"An error occurred while trying to determine the version of Node.js that is ",
"installed on the system. Please ensure you have it installed correctly and ",
"try installing gepetto again from a fresh R session.", sep=""
)
return()
}

node_vers <- unlist(strsplit(res$stdout, "\\."))[1]
if (!(node_vers %in% c("v10", "v9"))) {
message(
"You have a very old installation of Node.js. Please update it and try installing ",
"gepetto again from a fresh R session.", sep=""
)
return()
}

res <- processx::run(npm, "--version", error_on_status = FALSE)
if (res$status != 0) {
message(
"An error occurred while trying to determine the version of npm that is ",
"installed on the system. Please ensure you have it installed correctly and ",
"try installing gepetto again from a fresh R session.", sep=""
)
return()
}

npm_vers <- unlist(strsplit(res$stdout, "\\."))[1]
if (!(npm_vers %in% c("6"))) {
message(
"You have an old installation of npm. Please update it and try installing ",
"gepetto again from a fresh R session.", sep=""
)
return()
}

message("Attemping to install gepetto")

processx::run(
command = "npm",
args = c("install", "https://gitlab.com/hrbrmstr/gepetto.git", "--global"),
error_on_status = FALSE,
echo_cmd = TRUE,
echo = TRUE
) -> res

if (res$status == 0) {
message("gepetto has been installed.")
} else {
message(
"There was an error installing gepetto. Check the error log and make sure ",
"permissions are set correctly or for other errors that occurred which ",
"prevented the installation frombeing successful and try again or follow the ",
"manual instructions at <https://gitlab.com/hrbrmstr/gepetto>.", sep=""
)
}

}

#' Start/stop gepetto
#'
#' These functions can help start/stop gepetto instances. You must remember to
#' save the object returned by [start_gepetto()].
#'
#' Note that [stop_gepetto()] kills the process. You can/should consider using
#' [gep_stop()] instead.
#'
#' @md
#' @param host IP/host to use instead of `localhost`
#' @param port port to use besides the default (`8080`)
#' @param pxobj the `processx` object of a running gepetto instance generated by [start_gepetto()]
#' @return [start_gepetto()] returns a `processx` object
#' @export
#' @examples \dontrun{
#' pid <- start_gepetto()
#' # ... do stuff
#' stop_gepetto(pid)
#' }
start_gepetto <- function(host = "localhost", port = 8080) {

gep <- Sys.which("gepetto")
if (gep == "") stop("gepetto not found.", call.=FALSE)

HOST <- Sys.getenv("HOST")
on.exit(Sys.setenv(HOST=HOST), add=TRUE)
PORT <- Sys.getenv("PORT")
on.exit(Sys.setenv(PORT=PORT), add=TRUE)

Sys.setenv(HOST=host)
Sys.setenv(PORT=port)
processx::process$new(
command = "gepetto", stdout = "|", stderr = "|"
) -> res

res

}

#' @rdname start_gepetto
#' @export
stop_gepetto <- function(pxobj) {
pxobj$kill()
}
25 changes: 25 additions & 0 deletions R/gepetto.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,31 @@ gep_active <- function(gep) {

}

#' Gracefully stop a gepetto instance
#'
#' @md
#' @param gep a gepetto connection object
#' @export
#' @examples \dontrun{
#' gepetto() %>%
#' gep_stop()
#' }
gep_stop <- function(gep) {

s_GET(
url = sprintf("http://%s:%s/_stop", gep$host, gep$port)
) -> res

res <- stop_for_problem(res)

httr::stop_for_status(res)

out <- httr::content(res, as="text")
out <- jsonlite::fromJSON(out)

out$status == "ok"

}

#' #' Execute Puppeteer commands
#' #'
Expand Down
8 changes: 8 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ The following functions are implemented:

### `gepetto`-based ops

Helpers to get gepetto installed:

- `install_gepetto`: Install gepetto
- `start_gepetto`: Start/stop gepetto
- `stop_gepetto`: Start/stop gepetto

API interface functions:

- `gepetto`: Create a connection to a Gepetto API server
- `gep_active`: Get test whether the gepetto server is active
- `gep_debug`: Get "debug-level" information of a running gepetto server
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ The following functions are implemented:

### `gepetto`-based ops

Helpers to get gepetto installed:

- `install_gepetto`: Install gepetto
- `start_gepetto`: Start/stop gepetto
- `stop_gepetto`: Start/stop gepetto

API interface functions:

- `gepetto`: Create a connection to a Gepetto API server
- `gep_active`: Get test whether the gepetto server is active
- `gep_debug`: Get "debug-level" information of a running gepetto server
Expand Down
20 changes: 20 additions & 0 deletions man/gep_stop.Rd

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

19 changes: 19 additions & 0 deletions man/install_gepetto.Rd

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

36 changes: 36 additions & 0 deletions man/start_gepetto.Rd

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

0 comments on commit 3a7412a

Please sign in to comment.