| 1 |
#! /usr/bin/env bash |
| 2 |
# |
| 3 |
# Executes a command and formats the command and its stdout in reST. |
| 4 |
# |
| 5 |
|
| 6 |
trap cleanup INT TERM EXIT |
| 7 |
|
| 8 |
function usage() { |
| 9 |
echo |
| 10 |
echo "$(basename "$0") [options] <command line>" |
| 11 |
echo |
| 12 |
echo " -d Do not actually execute command; just format the command line." |
| 13 |
echo " -h Show this help." |
| 14 |
echo " -r <file> Insert <file> into output, rather than stdout." |
| 15 |
echo " -o Do not include command into output." |
| 16 |
echo " -c <cmd> Show <cmd> in output instead of the one actually executed." |
| 17 |
echo " -f <filter> Run <filter> command on command output (or file) before including." |
| 18 |
echo " -n <n> Include only n lines of output, adding a [...] marker if there's more." |
| 19 |
echo |
| 20 |
exit 1 |
| 21 |
} |
| 22 |
|
| 23 |
function apply_filter() { |
| 24 |
eval "$filter_env" | eval "$filter_opt" |
| 25 |
} |
| 26 |
|
| 27 |
# Strip leading white-space and then indent to 6 space. |
| 28 |
function indent() { |
| 29 |
python3 -c " |
| 30 |
from __future__ import print_function |
| 31 |
import sys |
| 32 |
input = sys.stdin.readlines() |
| 33 |
n = 1e10 |
| 34 |
for i in input: |
| 35 |
n = min(n, len(i) - len(i.lstrip())) |
| 36 |
|
| 37 |
for i in input: |
| 38 |
print(' ' + i[n:], end='') |
| 39 |
" |
| 40 |
} |
| 41 |
|
| 42 |
function cleanup() { |
| 43 |
# shellcheck disable=SC2086 |
| 44 |
rm -f $tmps |
| 45 |
exit |
| 46 |
} |
| 47 |
|
| 48 |
stdout=$(mktemp -t "$(basename "$0")".XXXXXX) |
| 49 |
cmd_out=$(mktemp -t "$(basename "$0")".XXXXXX) |
| 50 |
filter_out=$(mktemp -t "$(basename "$0")".XXXXXX) |
| 51 |
tmps="$stdout $cmd_out $filter_out" |
| 52 |
|
| 53 |
include=$cmd_out |
| 54 |
show_command=1 |
| 55 |
cmd_display="" |
| 56 |
dry=0 |
| 57 |
lines=0 |
| 58 |
|
| 59 |
filter_env=${BTEST_RST_FILTER} |
| 60 |
|
| 61 |
while getopts "odhr:f:c:n:" opt; do |
| 62 |
case $opt in |
| 63 |
h) usage ;; |
| 64 |
o) show_command=0 ;; |
| 65 |
r) include=$OPTARG ;; |
| 66 |
d) |
| 67 |
dry=1 |
| 68 |
include="" |
| 69 |
;; |
| 70 |
c) cmd_display=$OPTARG ;; |
| 71 |
f) filter_opt=$OPTARG ;; |
| 72 |
n) lines=$OPTARG ;; |
| 73 |
*) exit 1 ;; |
| 74 |
esac |
| 75 |
done |
| 76 |
|
| 77 |
shift $((OPTIND - 1)) |
| 78 |
|
| 79 |
cmd=$* |
| 80 |
test "$cmd_display" == "" && cmd_display=$cmd |
| 81 |
test "$filter_opt" == "" && filter_opt="cat" |
| 82 |
test "$filter_env" == "" && filter_env="cat" |
| 83 |
|
| 84 |
test "$cmd" == "" && usage |
| 85 |
|
| 86 |
if [ "$dry" != "1" ]; then |
| 87 |
if ! eval "$cmd" >"$cmd_out"; then |
| 88 |
exit 1 |
| 89 |
fi |
| 90 |
fi |
| 91 |
|
| 92 |
# Generate reST output. |
| 93 |
|
| 94 |
if [ "$show_command" == "1" ]; then |
| 95 |
{ |
| 96 |
echo ".. rst-class:: btest-cmd" |
| 97 |
echo |
| 98 |
echo " .. code-block:: none" |
| 99 |
echo " :linenos:" |
| 100 |
echo " :emphasize-lines: 1,1" |
| 101 |
echo |
| 102 |
echo " # $cmd_display" | apply_filter |
| 103 |
} >>"$stdout" |
| 104 |
else |
| 105 |
{ |
| 106 |
echo ".. rst-class:: btest-include" |
| 107 |
echo |
| 108 |
echo " .. code-block:: guess" |
| 109 |
echo " :linenos:" |
| 110 |
echo |
| 111 |
} >>"$stdout" |
| 112 |
fi |
| 113 |
|
| 114 |
for i in $include; do |
| 115 |
echo " $(basename "$i")" >>"$filter_out" |
| 116 |
echo "" >>"$filter_out" |
| 117 |
cat "$i" | apply_filter | indent >"$filter_out" |
| 118 |
|
| 119 |
if [ "$lines" = 0 ]; then |
| 120 |
cat "$filter_out" >>"$stdout" |
| 121 |
else |
| 122 |
cat "$filter_out" | head -n "$lines" >>"$stdout" |
| 123 |
if [ "$(wc -l <"$filter_out")" -gt "$lines" ]; then |
| 124 |
echo ' [...]' >>"$stdout" |
| 125 |
fi |
| 126 |
fi |
| 127 |
|
| 128 |
rm -f "$filter_out" |
| 129 |
done |
| 130 |
|
| 131 |
echo >>"$stdout" |
| 132 |
|
| 133 |
# Branch depending on where this script was started from. |
| 134 |
|
| 135 |
if [ "$BTEST_RST_OUTPUT" != "" ]; then |
| 136 |
# Running from inside Sphinx, just output to where it tells us. |
| 137 |
cat "$stdout" >>"${BTEST_RST_OUTPUT}#${TEST_PART}" |
| 138 |
|
| 139 |
elif [ "$TEST_NAME" ]; then |
| 140 |
# Running from inside BTest, output into file that btest-diff-rst will pickup. |
| 141 |
cat "$stdout" >>"btest-${TEST_NAME}#${TEST_PART}" |
| 142 |
|
| 143 |
else |
| 144 |
# Running from command line, just print out. |
| 145 |
cat "$stdout" |
| 146 |
fi |