OILS / test / tsv-lib.sh View on Github | oilshell.org

111 lines, 69 significant
1# TSV utilities
2#
3# Usage:
4# source test/tsv-lib.sh
5
6test -n "${__TEST_TSV_LIB_SH:-}" && return
7readonly __TEST_TSV_LIB_SH=1
8
9if test -z "${REPO_ROOT:-}"; then
10 echo "${BASH_SOURCE[0]}: \$REPO_ROOT must be set before sourcing" >&2
11 exit 2
12fi
13
14readonly TAB=$'\t'
15
16time-tsv() {
17 ### Run a task and output TSV
18 $REPO_ROOT/benchmarks/time_.py --tsv "$@"
19}
20
21time-tsv3() {
22 ### Maybe run with python3, our CI VMs don't have python2
23
24 # Some Soil tasks like dev-setup-* and raw-vm run on the VM, with python3, not python2
25 # Others run in a container with python2, not python3 (this may go away)
26
27 if command -v python3 >/dev/null; then
28 python3 $REPO_ROOT/benchmarks/time_.py --tsv "$@"
29 else
30 $REPO_ROOT/benchmarks/time_.py --tsv "$@"
31 fi
32}
33
34tsv2html() {
35 ### Convert TSV to an HTML table
36 $REPO_ROOT/web/table/csv2html.py --tsv "$@"
37}
38
39tsv2html3() {
40 ### Convert TSV to an HTML table
41 python3 $REPO_ROOT/web/table/csv2html.py --tsv "$@"
42}
43
44tsv-row() {
45 ### Usage: tsv-row a b c
46 local i=0
47 for cell in "$@"; do
48 if test $i -ne 0; then
49 echo -n $'\t'
50 fi
51
52 # note: if this were QTT, then it would be quoted
53 echo -n "$cell"
54
55 i=$((i + 1))
56 done
57
58 echo # newline
59}
60
61here-schema-tsv() {
62 ### Read a legible text format on stdin, and write TSV on stdout
63
64 while read -r one two; do
65 echo "${one}${TAB}${two}"
66 done
67}
68
69here-schema-tsv-3col() {
70 ### As above, with 3 cols
71 while read -r one two three; do
72 echo "${one}${TAB}${two}${TAB}${three}"
73 done
74}
75
76here-schema-tsv-4col() {
77 ### As above, with 4 cols
78 while read -r one two three four; do
79 echo "${one}${TAB}${two}${TAB}${three}${TAB}${four}"
80 done
81}
82
83
84tsv-concat() {
85 devtools/tsv_concat.py "$@"
86}
87
88# TSV and CSV concatenation are actually the same. Just use the same script.
89csv-concat() {
90 devtools/tsv_concat.py "$@"
91}
92
93tsv-add-const-column() {
94 ### Used to add a host name to GC stats
95
96 local col_name=$1
97 local const_value=$2
98
99 local i=0
100 while read -r line; do
101 if test $i = 0; then
102 echo -n "$col_name$TAB"
103 else
104 echo -n "$const_value$TAB"
105 fi
106 # Print the other columns
107 printf '%s\n' "$line"
108
109 i=$((i+1))
110 done
111}