Skip to content

Commit

Permalink
Merge pull request #9 from markheckmann/issue_2_ps_exec_policy
Browse files Browse the repository at this point in the history
Internals: Fail with clear error message if scripts not allowed by PS execuction policy (#2)
  • Loading branch information
davidgohel authored Oct 6, 2024
2 parents e0bd568 + 196ca84 commit bc7ca94
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 19 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^revdep$
^Dockerfile$
^inst/medias$
^dev_local
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.Ruserdata
revdep
Dockerfile
dev_local
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: doconv
Type: Package
Title: Document Conversion to 'PDF' or 'PNG'
Version: 0.3.2
Version: 0.3.3.0001
Authors@R: c(
person("David", "Gohel", role = c("aut", "cre"),
email = "david.gohel@ardata.fr"),
Expand All @@ -21,7 +21,7 @@ Description: It provides the ability to generate images from documents
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Imports: magick, pdftools, locatexec, processx, tools
Depends: R (>= 4.0.0)
Suggests: tinytest, testthat, webshot2
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# doconv 0.3.3 (dev version)

## Issues

- Fail with informative error message if PowerShell (PS) execution strategy does not allow
running PS scripts. PS scripts are required for certain actions on Windows (#2).

# doconv 0.3.2

## Features
Expand Down
44 changes: 28 additions & 16 deletions R/docx2pdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ docx2pdf <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", input))
}
}


#' @importFrom locatexec is_osx
#' @importFrom processx run
docx2pdf_osx <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", input)){

if (!is_osx()) {
stop("docx2pdf_osx() should only be used on 'macOS' systems.", call. = FALSE)
}
Expand Down Expand Up @@ -101,6 +101,7 @@ docx2pdf_osx <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", inpu
output
}


#' @importFrom locatexec is_windows
docx2pdf_win <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", input)){

Expand All @@ -120,18 +121,22 @@ docx2pdf_win <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", inpu

output_name <- file.path(default_root, gsub("\\.(docx|doc|rtf)$", ".pdf", basename(input)))

script_sourcefile <- system.file(
package = "doconv", "scripts", "powershell", "docx2pdf.ps1")
script_sourcefile <- system.file(package = "doconv", "scripts", "powershell", "docx2pdf.ps1")
script_path <- tempfile(fileext = ".ps1")
script_str <- readLines(script_sourcefile, encoding = "UTF-8")
script_str[1] <- sprintf(script_str[1], input)
script_str[2] <- sprintf(script_str[2], output_name)
writeLines(script_str, script_path, useBytes = TRUE)

res <- run("powershell", args = c("-file", script_path), error_on_status = FALSE)

success <- res$status == 0

if(success) {
# fail with informative error if conversion fails due to PS Execution Policy
if (!success && grepl("UnauthorizedAccess", res$stderr)) {
stop_on_wrong_ps_exec_policy()
}

if (success) {
success <- file.copy(from = output_name, to = output, overwrite = TRUE)
}

Expand All @@ -140,10 +145,9 @@ docx2pdf_win <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", inpu
if(!success) stop("could not convert ", input, res$stderr, call. = FALSE)

output


}


#' @export
#' @title Update docx fields
#' @description Update all fields and table of contents of a
Expand Down Expand Up @@ -178,6 +182,7 @@ docx_update <- function(input) {
invisible(x)
}


docx_update_osx <- function(input){

if (!is_osx()) {
Expand All @@ -203,6 +208,8 @@ docx_update_osx <- function(input){

success
}


docx_update_win <- function(input){

if (!is_windows()) {
Expand All @@ -227,16 +234,17 @@ docx_update_win <- function(input){
res <- run("powershell", args = c("-file", script_path), error_on_status = FALSE)
success <- res$status == 0

if(!success) stop("could not update ", input, call. = FALSE)
# fail with informative error if conversion fails due to PS Execution Policy
if (!success && grepl("UnauthorizedAccess", res$stderr)) {
stop_on_wrong_ps_exec_policy()
}

if (!success) stop("could not update ", input, call. = FALSE)

success
}






#' @export
#' @title Convert pptx to pdf
#' @description Convert pptx to pdf directly using "Microsoft PowerPoint".
Expand Down Expand Up @@ -290,6 +298,7 @@ pptx2pdf <- function(input, output = gsub("\\.pptx$", ".pdf", input)) {
}
}


#' @importFrom locatexec is_osx
#' @importFrom processx run
pptx2pdf_osx <- function(input, output = gsub("\\.pptx$", ".pdf", input)){
Expand Down Expand Up @@ -332,6 +341,7 @@ pptx2pdf_osx <- function(input, output = gsub("\\.pptx$", ".pdf", input)){
output
}


#' @importFrom locatexec is_windows
pptx2pdf_win <- function(input, output = gsub("\\.pptx$", ".pdf", input)){

Expand All @@ -357,10 +367,12 @@ pptx2pdf_win <- function(input, output = gsub("\\.pptx$", ".pdf", input)){
res <- run("powershell", args = c("-file", script_path), error_on_status = FALSE)
success <- res$status == 0

if(!success) stop("could not convert ", input, call. = FALSE)

success
# fail with informative error if conversion fails due to PS Execution Policy
if (!success && grepl("UnauthorizedAccess", res$stderr)) {
stop_on_wrong_ps_exec_policy()
}

if(!success) stop("could not convert ", input, call. = FALSE)

output
}

1 change: 1 addition & 0 deletions R/expect_snapshot_doc.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ expect_snapshot_doc <- function(

}


#' @title Visual test for an HTML document
#' @description This expectation can be used with 'tinytest' and 'testthat'
#' to check if a current document of type HTML
Expand Down
2 changes: 1 addition & 1 deletion R/to_pdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ check_libreoffice_export <- function(UserInstallation = NULL) {
"--outdir", default_root,
input),
error_on_status = FALSE,
timeout = 15
timeout = 45
)
success <- res$status == 0

Expand Down
19 changes: 19 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,22 @@ images_to_miniature <- function(img_list, row = NULL, width = 650,
img_stack
}

# get PS exec policy
ps_execution_policy <- function() {
res <- run("powershell", args = c("Get-ExecutionPolicy"), error_on_status = FALSE)
policy <- gsub("\\s+", "", res$stdout)
policy
}


# check if policy allows PS script execution. If not advise user.
stop_on_wrong_ps_exec_policy <- function() {
policy <- ps_execution_policy()
stop("Conversion failed.\n",
"Your PowerShell execution policy '", policy, "' prevents script execution.\n",
"Run one of the following commands in PS as admin to enable script execution: \n",
" `Set-ExecutionPolicy Bypass`\n",
" `Set-ExecutionPolicy Unrestricted`\n",
call. = FALSE
)
}

0 comments on commit bc7ca94

Please sign in to comment.