#!/bin/bash # # Usage: # ./common.sh set -o nounset set -o pipefail set -o errexitsetconst global FOO = "bar" # For xargs -P in spec-runner.sh, wild-runner.sh. readonly JOBS = $( $(nproc) - 1 ) proc log { echo @Argv !1 > !2 } proc die { log @Argv exit 1 } proc run-task-with-status { var out_file = $1 shift # --quiet suppresses a warning message /usr/bin/env time \ --output $out_file \ --format '%x %e' \ -- @Argv || true # suppress failure # Hack to get around the fact that --quiet is Debian-specific: # http://lists.oilshell.org/pipermail/oil-dev-oilshell.org/2017-March/000012.html # # Long-term solution: our xargs should have --format. sed -i '/Command exited with non-zero status/d' $out_file # TODO: Use rows like this with oil # '{"status": %x, "wall_secs": %e, "user_secs": %U, "kernel_secs": %S}' \ } proc run-task-with-status-test { run-task-with-status _tmp/status.txt sh -c 'sleep 0.1; exit 1' || true cat _tmp/status.txt test $[wc -l < _tmp/status.txt] = '1' || die "Expected only one line" } if test $[basename $0] = 'common.sh' { @Argv } (CommandList children: [ (C {(set)} {(-o)} {(nounset)}) (C {(set)} {(-o)} {(pipefail)}) (C {(set)} {(-o)} {(errexit)}) (Assignment keyword: Assign_Readonly pairs: [ (assign_pair lhs: (LhsName name:JOBS) op: Equal rhs: { (ArithSubPart anode: (ArithBinary op_id: Arith_Minus left: (ArithWord w: { (CommandSubPart command_list: (CommandList children:[(C {(nproc)})]) left_token: spids: [41 43] ) } ) right: (ArithWord w:{(Lit_Digits 1)}) ) spids: [39 50] ) } spids: [38] ) ] spids: [36] ) (FuncDef name: log body: (BraceGroup children: [ (SimpleCommand words: [{(echo)} {(DQ ($ VSub_At "$@"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:1 arg_word:{(2)} spids:[66])] ) ] spids: [57] ) spids: [53 56] ) (FuncDef name: die body: (BraceGroup children:[(C {(log)} {(DQ ($ VSub_At "$@"))})(C {(exit)} {(1)})] spids:[76]) spids: [72 75] ) (FuncDef name: run-task-with-status body: (BraceGroup children: [ (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:out_file) op: Equal rhs: {($ VSub_Number "$1")} spids: [102] ) ] spids: [100] ) (C {(shift)}) (AndOr children: [ (C {(/usr/bin/env)} {(KW_Time time)} {(--output)} {($ VSub_Name "$out_file")} {(--format)} {(SQ <"%x %e">)} {(--)} {(DQ ($ VSub_At "$@"))} ) (C {(true)}) ] op_id: Op_DPipe ) (C {(sed)} {(-i)} {(SQ <"/Command exited with non-zero status/d">)} {($ VSub_Name "$out_file")} ) ] spids: [97] ) spids: [93 96] ) (FuncDef name: run-task-with-status-test body: (BraceGroup children: [ (AndOr children: [ (C {(run-task-with-status)} {(_tmp/status.txt)} {(sh)} {(-c)} {(SQ <"sleep 0.1; exit 1">)}) (C {(true)}) ] op_id: Op_DPipe ) (C {(cat)} {(_tmp/status.txt)}) (AndOr children: [ (C {(test)} { (DQ (CommandSubPart command_list: (CommandList children: [ (SimpleCommand words: [{(wc)} {(-l)}] redirects: [ (Redir op_id: Redir_Less fd: -1 arg_word: {(_tmp/status.txt)} spids: [224] ) ] ) ] ) left_token: spids: [219 227] ) ) } {(Lit_Other "=")} {(SQ <1>)} ) (C {(die)} {(DQ ("Expected only one line"))}) ] op_id: Op_DPipe ) ] spids: [191] ) spids: [187 190] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(test)} { (DQ (CommandSubPart command_list: (CommandList children: [(C {(basename)} {($ VSub_Number "$0")})] ) left_token: spids: [252 256] ) ) } {(Lit_Other "=")} {(SQ )} ) terminator: ) ] action: [(C {(DQ ($ VSub_At "$@"))})] spids: [-1 266] ) ] spids: [-1 273] ) ] )