-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.helper_bash_functions
More file actions
315 lines (298 loc) · 14.1 KB
/
.helper_bash_functions
File metadata and controls
315 lines (298 loc) · 14.1 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/bash
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Color_Off='\033[0m' # Text Reset
CATKIN_WS_PATH="/cortex/.catkin_ws"
THIS_SCRIPT_BRANCH="main"
THIS_SCRIPT_URL="https://raw.githubusercontent.com/Extend-Robotics/er_build_tools/refs/heads/${THIS_SCRIPT_BRANCH}/.helper_bash_functions"
# ROS helpers
export ROSCONSOLE_FORMAT='[${severity}](${node}): [${time}] ${message}'
colcon_build() {
local COLCON_BUILD_COMMAND="colcon build --cmake-args -DSETUPTOOLS_DEB_LAYOUT=OFF"
[ -n "$1" ] && COLCON_BUILD_COMMAND+=" --packages-up-to $1"
_colcon_build "$COLCON_BUILD_COMMAND"
return $?
}
colcon_build_no_deps() {
local COLCON_BUILD_COMMAND="colcon build --cmake-args -DSETUPTOOLS_DEB_LAYOUT=OFF"
[ -n "$1" ] && COLCON_BUILD_COMMAND+=" --packages-select $1"
_colcon_build "$COLCON_BUILD_COMMAND"
return $?
}
colcon_build_sym() {
local COLCON_BUILD_COMMAND="colcon build --packages-skip-cache-valid --symlink-install --cmake-args -DSETUPTOOLS_DEB_LAYOUT=OFF"
[ -n "$1" ] && COLCON_BUILD_COMMAND+=" --packages-up-to $1"
_colcon_build "$COLCON_BUILD_COMMAND"
return $?
}
_colcon_build() {
local START_DIR="$(pwd)"
local COLCON_BUILD_COMMAND="$1"
cd "$CATKIN_WS_PATH" || return 1
source /opt/ros/noetic/setup.bash
echo -e "${Yellow}Running build command:\n${Green}${COLCON_BUILD_COMMAND}${Color_Off}"
eval "$COLCON_BUILD_COMMAND"
local RET_VAL=$?
source "$CATKIN_WS_PATH/install/setup.bash"
cd "$START_DIR"
return $RET_VAL
}
colcon_build_this() { colcon_build "$(find_cmake_project_names_from_dir .)"; }
colcon_build_er_interface() { colcon_build_no_deps "$(find_cmake_project_names_from_dir ${CATKIN_WS_PATH}/src/er_interface)"; }
colcon_test_er_interface() { colcon_test_this_package "$(find_cmake_project_names_from_dir ${CATKIN_WS_PATH}/src/er_interface)"; }
rosdep_install() { rosdep install --from-paths src --ignore-src -r -y ; }
_show_test_failures() {
python3 - "$@" <<'PYEOF'
import sys, xml.etree.ElementTree as ET
from pathlib import Path
for d in sys.argv[1:]:
for p in sorted(Path(d).rglob("*.xml")):
for tc in ET.parse(p).iter("testcase"):
for f in list(tc.iter("failure")) + list(tc.iter("error")):
tag = "FAIL" if f.tag == "failure" else "ERROR"
print(f"\n {tag}: {tc.get('classname', '')}.{tc.get('name', '')}")
if f.text:
lines = f.text.strip().splitlines()
for l in lines[:20]:
print(f" {l}")
if len(lines) > 20:
print(f" ... ({len(lines) - 20} more lines)")
PYEOF
}
colcon_test_these_packages() { local THIS_DIR=$(pwd)
local VERBOSE_FLAG=""
if [[ "$1" == "-v" || "$1" == "--verbose" ]]; then
VERBOSE_FLAG="--event-handlers console_direct+"
shift
fi
cd ${CATKIN_WS_PATH}
colcon_build_no_deps "$1"
source install/setup.bash && \
colcon test --packages-select $1 ${VERBOSE_FLAG}
for pkg in $1; do
if ! colcon test-result --test-result-base "build/$pkg/test_results"; then
_show_test_failures "build/$pkg/test_results"
fi
done
cd $THIS_DIR
}
colcon_test_this_package() { colcon_test_these_packages "$@"; }
find_cmake_project_names_from_dir() { if [[ -z $1 ]]; then DIR_TO_SEARCH="."; else DIR_TO_SEARCH=$1; fi; wget -qO- https://raw.githubusercontent.com/Extend-Robotics/er_build_tools/refs/heads/main/bin/find_cmake_project_names.py | python3 - $DIR_TO_SEARCH; }
if [ -f /.dockerenv ]; then
# Git autocomplete
source /etc/bash_completion.d/git-prompt
source /usr/share/bash-completion/completions/git
# Git pretty terminal
function be_get_branch {
local dir="$PWD"
local vcs
local nick
while [[ "$dir" != "/" ]]; do
for vcs in git hg svn bzr; do
if [[ -d "$dir/.$vcs" ]] && hash "$vcs" &>/dev/null; then
case "$vcs" in
git) __git_ps1 "${1:-(%s) }"; return;;
hg) nick=$(hg branch 2>/dev/null);;
svn) nick=$(svn info 2>/dev/null\
| grep -e '^Repository Root:'\
| sed -e 's#.*/##');;
bzr)
local conf="${dir}/.bzr/branch/branch.conf" # normal branch
[[ -f "$conf" ]] && nick=$(grep -E '^nickname =' "$conf" | cut -d' ' -f 3)
conf="${dir}/.bzr/branch/location" # colo/lightweight branch
[[ -z "$nick" ]] && [[ -f "$conf" ]] && nick="$(basename "$(< $conf)")"
[[ -z "$nick" ]] && nick="$(basename "$(readlink -f "$dir")")";;
esac
[[ -n "$nick" ]] && printf "${1:-(%s) }" "$nick"
return 0
fi
done
dir="$(dirname "$dir")"
done
}
# Add branch to PS1 (based on $PS1 or $1), formatted as $2
export GIT_PS1_SHOWDIRTYSTATE=yes
#pretty PS1:
export PS1="\$([[ \$? != 0 ]] && echo \"[\[\033[0;31m\]\342\234\227\[\033[0;37m\]]\342\224\200\")[$(if [[ ${EUID} == 0 ]]; then echo '\[\033[0;31m\]\h'; else echo '\[\033[0;33m\]\u\[\033[0;37m\]@\[\033[0;95m\]\h'; fi)\[\033[0;37m\]]\342\224\200[\[\033[0;32m\]\w\[\033[0;37m\]]\n\[\033[0;37m\]\342\224\224\342\224\200\342\224\200\342\225\274 \[\033[0m\]"
export PS1="\[\033[0;37m\]\342\224\214\342\224\200\$(be_get_branch "$2")${PS1}";
fi
# Git helpers
git config --global alias.sshify '!f() { git remote set-url origin $(git remote get-url origin | sed -En "s/https:\/\/github.com\//git@github.com:/p") ; }; f'
git config --global alias.unsshify '!f() { git remote set-url origin $(git remote get-url origin | sed -En "s/git@github.com:/https:\/\/github.com\//p") ; }; f'
git_add_ssh() { eval "$(ssh-agent -s)"; ssh-add ~/.ssh/id_rsa ; }
git_print_log() { git log --graph --oneline --decorate --all ; }
git_push_empty_commit() { git commit --allow-empty -m "trigger_checks" && git push origin $(git branch | grep "\* " | awk '{print $NF}'); }
# General bash helpers
function confirm() {
# call with a prompt string or use a default
read -r -p "${1:-[y/N]} " response
case "$response" in
[yY][eE][sS]|[yY])
echo "y"
return 1
;;
*)
echo "n"
return 0
;;
esac
}
FIND_PYTHON_FILES_SCRIPT="$HOME/.find_python_files.sh"
check_find_python_files() {
if ! [ -x "$FIND_PYTHON_FILES_SCRIPT" ]; then
wget -qO "$FIND_PYTHON_FILES_SCRIPT" \
https://raw.githubusercontent.com/Extend-Robotics/er_build_tools/refs/heads/main/find_python_files.sh
chmod +x "$FIND_PYTHON_FILES_SCRIPT"
fi
}
alias cgrep="grep --color=always"
ps_aux() { ps aux | cgrep $1 | grep -v grep ; }
ps_aux_command() { ps -e -o command | cgrep $1 | grep -v grep ; }
grep_all() { grep -rn '.' -e "$1"; }
find_all() { find '.' -name "*$1*"; }
cd_ltr(){ cd $1$(ls $1 -ltr -d */ | tail -n 1 | awk '{print $9}'); }
cats() { for input_file in $@; do echo -e "${input_file}: \n"; highlight -O ansi --force $input_file; echo -e "\n"; done; }
catsn() { for input_file in $@; do echo -e "${input_file}: \n"; highlight -O ansi --force -n $input_file; echo -e "\n"; done; }
ps_aux() { ps aux | cgrep $1 | grep -v grep ; }
kill_any_process() { ps_aux_command $1; conf="$(confirm "kill these processes? [Y/n]")"; if [[ $conf == "y" ]]; then echo "killing..."; sudo kill -9 $(ps_aux $1 | awk {'print $2}'); sleep 1; echo "remaining: "; ps_aux_command $1 else echo "not killing"; fi ; }
docker_exec () { if [[ $(docker container ls -q | wc -l) -eq 1 ]]; then docker exec -it $(docker container ls -q) bash; else echo "wrong number of containers running"; fi; }
awk_line_length() { if [[ -z $2 ]]; then MAX_LINE_LENGTH=200; else MAX_LINE_LENGTH=$2; fi; cat $1 | awk 'length($0) < '"$MAX_LINE_LENGTH"''; }
MERGE_VARS_URL="https://raw.githubusercontent.com/Extend-Robotics/er_build_tools/refs/heads/${THIS_SCRIPT_BRANCH}/bin/merge_helper_vars.py"
update_helper_bash_functions() {
if [ ! -f ~/.helper_bash_functions ]; then
echo -e "${Red}ERROR: Tried to replace this file but couldn't find it, something has gone wrong!${Color_Off}\n"
return 1
fi
if [ -f "${FIND_PYTHON_FILES_SCRIPT}" ]; then
rm "${FIND_PYTHON_FILES_SCRIPT}"
fi
check_find_python_files
local tmp_new
tmp_new=$(mktemp)
if ! wget -q -O "${tmp_new}" "${THIS_SCRIPT_URL}"; then
echo -e "${Red}Error: Failed to download updated helper_bash_functions${Color_Off}"
rm -f "${tmp_new}"
return 1
fi
local merge_script
merge_script=$(curl -fSL "${MERGE_VARS_URL}") || {
echo -e "${Red}Error: Failed to fetch merge_helper_vars.py${Color_Off}"
rm -f "${tmp_new}"
return 1
}
if ! python3 <(echo "${merge_script}") ~/.helper_bash_functions "${tmp_new}"; then
echo -e "${Red}Error: merge_helper_vars.py failed; aborting update to preserve local customizations${Color_Off}"
rm -f "${tmp_new}"
return 1
fi
cp "${tmp_new}" ~/.helper_bash_functions
rm -f "${tmp_new}"
echo ""
echo -e "${Green}helper_bash_functions updated. Run 'source ~/.helper_bash_functions' to reload.${Color_Off}"
}
# python linters
LINTER_VERSIONS_URL="https://raw.githubusercontent.com/Extend-Robotics/er_build_tools/refs/heads/${THIS_SCRIPT_BRANCH}/linter_versions.env"
LINTER_MAX_LINE_LENGTH="140"
check_linter_versions() {
if ! [ -s /tmp/linter_versions.env ]; then
echo -e "${Yellow}Fetching ${LINTER_VERSIONS_URL}...${Color_Off}" >&2
local tmp=/tmp/linter_versions.env.tmp
if ! wget -qO "$tmp" "$LINTER_VERSIONS_URL" || ! [ -s "$tmp" ]; then
echo -e "${Red}Failed to fetch ${LINTER_VERSIONS_URL}${Color_Off}" >&2
rm -f "$tmp"
return 1
fi
mv "$tmp" /tmp/linter_versions.env
fi
source /tmp/linter_versions.env
local var
for var in LINTER_PYTHON_VERSION LINTER_PYLINT_VERSION LINTER_FLAKE8_VERSION LINTER_RUFF_VERSION; do
if [ -z "${!var}" ]; then
echo -e "${Red}${var} not set after sourcing /tmp/linter_versions.env${Color_Off}" >&2
return 1
fi
done
}
ensure_uv() {
[ -f "$HOME/.local/bin/env" ] && source "$HOME/.local/bin/env"
[[ ":$PATH:" != *":$HOME/.local/bin:"* ]] && export PATH="$HOME/.local/bin:$PATH"
command -v uv &>/dev/null && return 0
echo -e "${Yellow}Installing uv...${Color_Off}" >&2
curl -LSf https://astral.sh/uv/install.sh | sh >&2 || return 1
command -v uv &>/dev/null
}
ensure_uv_tool_pinned() {
local name="$1" version="$2" python_version="$3"
ensure_uv || return 1
local installed
installed="$(uv tool list | awk -v n="$name" '$1==n { sub(/^v/, "", $2); print $2 }')"
[[ "$installed" == "$version" ]] && return 0
echo -e "${Yellow}Installing ${name}==${version} (Python ${python_version}) via uv...${Color_Off}" >&2
uv tool install --force --python "$python_version" "${name}==${version}" >&2 || return 1
}
find_python_files_here() {
check_find_python_files
"$FIND_PYTHON_FILES_SCRIPT"
}
check_pylintrc() { if ! [ -f /tmp/pylintrc ]; then wget -O /tmp/pylintrc https://raw.githubusercontent.com/Extend-Robotics/er_build_tools/refs/heads/main/pylintrc; fi; }
check_apt_package() {
if dpkg -l | grep -qw "$1"; then
return 0
fi
sudo DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a apt-get update && \
sudo DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a apt-get install -y \
-o Dpkg::Options::="--force-confnew" "$1"
return 1
}
# Linter wrappers run as subshell functions with PYTHONHOME/PYTHONPATH unset, so uv's Python
# isn't redirected to a foreign stdlib. (Isaac Sim images export PYTHONHOME pointing at a 3.11
# tree; uv's 3.8 then fails with "No module named encodings" both at build and shim launch.)
er_flake8_here() (
unset PYTHONHOME PYTHONPATH
check_pylintrc
check_linter_versions || return 1
ensure_uv_tool_pinned "flake8" "$LINTER_FLAKE8_VERSION" "$LINTER_PYTHON_VERSION" || return 1
echo -e "${Yellow}Running flake8...${Color_Off}" >&2
flake8 --config /tmp/pylintrc --max-line-length ${LINTER_MAX_LINE_LENGTH}
)
er_pylint_here() (
unset PYTHONHOME PYTHONPATH
check_pylintrc
check_linter_versions || return 1
ensure_uv_tool_pinned "pylint" "$LINTER_PYLINT_VERSION" "$LINTER_PYTHON_VERSION" || return 1
echo -e "${Yellow}Running pylint...${Color_Off}" >&2
pylint --rcfile /tmp/pylintrc --max-line-length ${LINTER_MAX_LINE_LENGTH} $(find_python_files_here)
)
er_pylint_sorted_here() { er_pylint_here | sort -V | grep -v "\*\*\*\*\*\*\*\*"; return ${PIPESTATUS[0]}; }
er_pylint_single_file() (
unset PYTHONHOME PYTHONPATH
check_pylintrc
check_linter_versions || return 1
ensure_uv_tool_pinned "pylint" "$LINTER_PYLINT_VERSION" "$LINTER_PYTHON_VERSION" || return 1
echo -e "${Yellow}Running pylint on $1...${Color_Off}" >&2
pylint --rcfile /tmp/pylintrc --max-line-length ${LINTER_MAX_LINE_LENGTH} "$1"
)
er_pylint_single_file_sorted() { er_pylint_single_file "$1" | sort -V | grep -v "\*\*\*\*\*\*\*\*"; return ${PIPESTATUS[0]}; }
er_ruff_here() (
unset PYTHONHOME PYTHONPATH
check_linter_versions || return 1
ensure_uv_tool_pinned "ruff" "$LINTER_RUFF_VERSION" "$LINTER_PYTHON_VERSION" || return 1
echo -e "${Yellow}Running ruff...${Color_Off}" >&2
ruff check
)
er_python_linters_here() { local ret=0
echo
er_pylint_sorted_here || ret=$?
echo
er_flake8_here || ret=$?
echo
er_ruff_here || ret=$?
echo
if [[ $ret -eq 0 ]]; then
echo -e "${Green}All linters passed${Color_Off}"
else
echo -e "${Red}Linters failed (exit code $ret)${Color_Off}"
fi
return $ret
}