-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrewrapper_shim.sh
More file actions
129 lines (106 loc) · 2.52 KB
/
rewrapper_shim.sh
File metadata and controls
129 lines (106 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
set -euo pipefail
find_real_rewrapper() {
if [[ -n "${RBE_WRAPPER_REAL:-}" ]]; then
printf '%s\n' "${RBE_WRAPPER_REAL}"
return
fi
if [[ -n "${RBE_DIR:-}" ]]; then
printf '%s\n' "${RBE_DIR}/rewrapper"
return
fi
printf '%s\n' "prebuilts/remoteexecution-client/live/rewrapper"
}
find_exec_root() {
local arg
for arg in "$@"; do
case "${arg}" in
-exec_root=*)
printf '%s\n' "${arg#-exec_root=}"
return
;;
esac
done
if [[ -n "${RBE_exec_root:-}" ]]; then
printf '%s\n' "${RBE_exec_root}"
return
fi
pwd
}
rewrite_args() {
local workdir="$1"
shift
local -a args=("$@")
local keep_input="${workdir}/.keep"
local -a rewritten=()
local saw_inputs=0
local cmd_start=-1
local i arg
for ((i = 0; i < ${#args[@]}; i++)); do
arg="${args[i]}"
if [[ "${arg}" == "--" ]]; then
cmd_start=$((i + 1))
break
fi
done
if (( cmd_start < 0 )); then
for ((i = 0; i < ${#args[@]}; i++)); do
arg="${args[i]}"
if [[ "${arg}" != -* ]] || [[ "${arg}" == "-" ]]; then
cmd_start="${i}"
break
fi
done
fi
if (( cmd_start < 0 )); then
printf '%s\0' "${args[@]}"
return 0
fi
for ((i = 0; i < cmd_start; i++)); do
arg="${args[i]}"
if [[ "${arg}" == "--" ]]; then
continue
fi
case "${arg}" in
-inputs=*)
saw_inputs=1
rewritten+=("-inputs=${keep_input},${arg#-inputs=}")
;;
*)
rewritten+=("${arg}")
;;
esac
done
if (( saw_inputs == 0 )); then
rewritten+=("-inputs=${keep_input}")
fi
for ((i = cmd_start; i < ${#args[@]}; i++)); do
rewritten+=("${args[i]}")
done
printf '%s\0' "${rewritten[@]}"
}
main() {
local exec_root real_rewrapper shim_dir workdir
exec_root="$(find_exec_root "$@")"
real_rewrapper="$(find_real_rewrapper)"
if [[ "${real_rewrapper}" != /* ]]; then
real_rewrapper="${exec_root}/${real_rewrapper}"
fi
shim_dir="${exec_root}/.rbe_shim"
workdir="${shim_dir}/wd"
mkdir -p "${workdir}"
: > "${workdir}/.keep"
local entry name target
for entry in "${exec_root}"/* "${exec_root}"/.[!.]* "${exec_root}"/..?*; do
[[ -e "${entry}" ]] || continue
name="${entry##*/}"
[[ "${name}" == ".rbe_shim" ]] && continue
target="${workdir}/${name}"
ln -sfnT "../../${name}" "${target}"
done
local -a rewritten
mapfile -d '' -t rewritten < <(rewrite_args ".rbe_shim/wd" "$@")
cd "${workdir}"
exec "${real_rewrapper}" "${rewritten[@]}"
}
main "$@"