Browse Source

helpers

master
boB Rudis 4 years ago
parent
commit
3a7412aadb
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 4
      NAMESPACE
  2. 141
      R/gepetto-helpers.R
  3. 25
      R/gepetto.R
  4. 8
      README.Rmd
  5. 8
      README.md
  6. 20
      man/gep_stop.Rd
  7. 19
      man/install_gepetto.Rd
  8. 36
      man/start_gepetto.Rd

4
NAMESPACE

@ -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)

141
R/gepetto-helpers.R

@ -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
R/gepetto.R

@ -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
#' #'

8
README.Rmd

@ -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

8
README.md

@ -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

20
man/gep_stop.Rd

@ -0,0 +1,20 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/gepetto.R
\name{gep_stop}
\alias{gep_stop}
\title{Gracefully stop a gepetto instance}
\usage{
gep_stop(gep)
}
\arguments{
\item{gep}{a gepetto connection object}
}
\description{
Gracefully stop a gepetto instance
}
\examples{
\dontrun{
gepetto() \%>\%
gep_stop()
}
}

19
man/install_gepetto.Rd

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/gepetto-helpers.R
\name{install_gepetto}
\alias{install_gepetto}
\title{Install gepetto}
\usage{
install_gepetto()
}
\description{
This function verifies you have what you need to install \code{gepetto} then
does so (globally). Ideally, you'll have \code{Node.js} and \code{npm} already on your
system (since this function will not install them).
}
\details{
While you \emph{can} try to install gepetto this way, it is highly recommended that
you follow the instructions (\url{https://gitlab.com/hrbrmstr/gepetto}) manually
since many Node.js/npm installations have permissions issues due to errant
historical use of \code{sudo}.
}

36
man/start_gepetto.Rd

@ -0,0 +1,36 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/gepetto-helpers.R
\name{start_gepetto}
\alias{start_gepetto}
\alias{stop_gepetto}
\title{Start/stop gepetto}
\usage{
start_gepetto(host = "localhost", port = 8080)
stop_gepetto(pxobj)
}
\arguments{
\item{host}{IP/host to use instead of \code{localhost}}
\item{port}{port to use besides the default (\code{8080})}
\item{pxobj}{the \code{processx} object of a running gepetto instance generated by \code{\link[=start_gepetto]{start_gepetto()}}}
}
\value{
\code{\link[=start_gepetto]{start_gepetto()}} returns a \code{processx} object
}
\description{
These functions can help start/stop gepetto instances. You must remember to
save the object returned by \code{\link[=start_gepetto]{start_gepetto()}}.
}
\details{
Note that \code{\link[=stop_gepetto]{stop_gepetto()}} kills the process. You can/should consider using
\code{\link[=gep_stop]{gep_stop()}} instead.
}
\examples{
\dontrun{
pid <- start_gepetto()
# ... do stuff
stop_gepetto(pid)
}
}
Loading…
Cancel
Save