Debian Codegraph @

R/system.R

raw
1
2 renv_system_exec <- function(command,
3 args = NULL,
4 action = "executing command",
5 success = 0L,
6 stream = FALSE,
7 quiet = NULL)
8 {
9 # be quiet when running tests by default
10 quiet <- quiet %||% renv_tests_running()
11
12 # handle 'stream' specially
13 if (stream) {
14
15 # form stdout, stderr
16 stdout <- stderr <- if (quiet) FALSE else ""
17
18 # execute command
19 status <- suppressWarnings(
20 if (is.null(args))
21 system(command, ignore.stdout = quiet, ignore.stderr = quiet)
22 else
23 system2(command, args, stdout = stdout, stderr = stderr)
24 )
25
26 # check for error
27 status <- status %||% 0L
28 if (!is.null(success) && !status %in% success) {
29 fmt <- "error %s [error code %i]"
30 stopf(fmt, action, status)
31 }
32
33 # return status code
34 return(status)
35
36 }
37
38 # suppress warnings as some successful commands may return a non-zero exit
39 # code, whereas R will always warn on such error codes
40 output <- suppressWarnings(
41 if (is.null(args))
42 system(command, intern = TRUE)
43 else
44 system2(command, args, stdout = TRUE, stderr = TRUE)
45 )
46
47 # extract status code from result
48 status <- attr(output, "status") %||% 0L
49
50 # if this status matches an expected 'success' code, return output
51 if (is.null(success) || status %in% success)
52 return(output)
53
54 # otherwise, notify the user that things went wrong
55 abort(
56 sprintf("error %s [error code %i]", action, status),
57 body = renv_system_exec_details(command, args, output)
58 )
59
60 }
61
62 renv_system_exec_details <- function(command, args, output) {
63
64 # get header, giving the command that was run
65 cmdline <- paste(command, paste(args, collapse = " "))
66 underline <- paste(rep.int("=", min(80L, nchar(cmdline))), collapse = "")
67 header <- c(cmdline, underline)
68
69 # truncate output (avoid overwhelming console)
70 body <- if (length(output) > 200L)
71 c(head(output, n = 100L), "< ... >", tail(output, n = 100L))
72 else
73 output
74
75 c(header, "", body)
76
77 }