forked from tue-robotics/tue-env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcucr-functions.bash
1891 lines (1610 loc) · 59.2 KB
/
cucr-functions.bash
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env bash
CUCR_SYSTEM_DIR="${CUCR_ENV_DIR}"/system # This variable is deprecated and will be removed in a future version of the tool
CUCR_WS_DIR="${CUCR_SYSTEM_DIR}"
export CUCR_SYSTEM_DIR
export CUCR_WS_DIR
if [[ -z "${CUCR_REPOS_DIR}" ]] # Only set this variable if there exists no default in user_setup.bash
then
CUCR_REPOS_DIR="${CUCR_ENV_DIR}"/repos
export CUCR_REPOS_DIR
fi
CUCR_RELEASE_DIR="${CUCR_SYSTEM_DIR}"/release
export CUCR_RELEASE_DIR
# ----------------------------------------------------------------------------------------------------
# HELPER FUNCTIONS
# ----------------------------------------------------------------------------------------------------
function _list_subdirs
{
fs=$(ls "$1")
for f in $fs
do
if [ -d "$1"/"$f" ]
then
echo "$f"
fi
done
}
# ----------------------------------------------------------------------------------------------------
# APT MIRROR SELECTION
# ----------------------------------------------------------------------------------------------------
function cucr-apt-select-mirror
{
# Function to set the fastest APT mirror
# It uses apt-select to generate a new sources.list, based on the current one.
# All Arguments to this functions are passed on to apt-select, so check the
# apt-select documentation for all options.
hash pip3 2> /dev/null|| sudo apt-get install --assume-yes python3-pip
hash apt-select 2> /dev/null|| sudo python3 -m pip install -U apt-select
local mem_pwd
mem_pwd=$PWD
# shellcheck disable=SC2164
cd /tmp
local err_code
apt-select "$@" 2> /dev/null
err_code=$?
if [ $err_code == 4 ]
then
echo -e "Fastest apt mirror is the current one"
elif [ $err_code != 0 ]
then
echo -e "Non zero error code return by apt-select: $err_code"
else
echo -e "Updating the apt mirror with the fastest one"
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bk
sudo cp /tmp/sources.list /etc/apt/sources.list
echo -e "Cleaning up existing apt lists in /var/lib/apt/lists"
sudo rm -rf /var/lib/apt/lists/*
echo -e "Running: sudo apt-get update -qq"
sudo apt-get update -qq
fi
# shellcheck disable=SC2164
cd "$mem_pwd"
}
# ----------------------------------------------------------------------------------------------------
# GIT LOCAL HOUSEKEEPING
# ----------------------------------------------------------------------------------------------------
function _cucr-git-get-default-branch
{
# Takes current dir in case $1 is empty
local default_branch
default_branch=$(git -C "$1" symbolic-ref refs/remotes/origin/HEAD --short 2>/dev/null | sed 's@^origin/@@')
[ -z "$default_branch" ] && default_branch=$(git -C "$1" remote show origin 2>/dev/null | grep HEAD | awk '{print $3}')
echo "$default_branch"
}
export -f _cucr-git-get-default-branch
function __cucr-git-checkout-default-branch
{
local default_branch
default_branch=$(_cucr-git-get-default-branch "$1")
_git_remote_checkout "$1" origin "$default_branch"
}
export -f __cucr-git-checkout-default-branch
function _cucr-git-checkout-default-branch
{
_cucr-repos-do "__cucr-git-checkout-default-branch"
}
function _cucr-git-clean-local
{
# Function to remove stale branches from a git repository (which should
# either be the PWD or one of its parent directories). The function removes
# stale branches in two layers. First it removes all branches that have been
# merged in the remote, then it checks for unmerged branches that have been
# deleted from the remote and prompts for confirmation before removal. If
# the function is called with "--force-remove" flag, then no confirmation is asked
local force_remove
local error_code
local stale_branches
local repo_path
repo_path="$PWD"
local repo
repo=$(basename "$repo_path")
if [ -n "$1" ]
then
if [ "$1" == "--force-remove" ]
then
force_remove=true
else
echo -e "\e[31m[cucr-git-clean-local][Error] Unknown input argument '$1'. Only supported argument is '--force-remove' to forcefully remove unmerged stale branches\e[0m"
return 1
fi
fi
git fetch -p || { echo -e "\e[31m[cucr-git-clean-local] 'git fetch -p' failed in '$repo'.\e[0m"; return 1; }
stale_branches=$(git branch --list --format "%(if:equals=[gone])%(upstream:track)%(then)%(refname)%(end)" \
| sed 's,^refs/heads/,,;/^$/d')
[ -z "$stale_branches" ] && return 0
# If the current branch is a stale branch then change to the default repo
# branch before cleanup
if [[ "$stale_branches" == *$(git rev-parse --abbrev-ref HEAD)* ]]
then
# shellcheck disable=SC2119
__cucr-git-checkout-default-branch
git pull --ff-only --prune > /dev/null 2>&1
error_code=$?
if [ ! $error_code -eq 0 ]
then
echo -e "\e[31m[cucr-git-clean-local] Error pulling upstream on default branch of repository '$repo'. Cancelling branch cleanup.\e[0m"
return 1
fi
fi
local stale_branch stale_branch_count unmerged_stale_branches
stale_branch_count=0
unmerged_stale_branches=""
for stale_branch in $stale_branches
do
git branch -d "$stale_branch" > /dev/null 2>&1
error_code=$?
# If an error occured in safe deletion of a stale branch, add it to the
# list of unmerged stale branches which are to be forcefully removed
# upon confirmation by the user
if [ ! $error_code -eq 0 ]
then
unmerged_stale_branches="${unmerged_stale_branches:+${unmerged_stale_branches} } $stale_branch"
else
((stale_branch_count++))
if [ "${stale_branch_count}" -eq 1 ]
then
echo -e "\e[36m"
echo -e "Removing stale branches:"
echo -e "------------------------"
fi
echo -e "$stale_branch"
fi
done
# Removal of unmerged stale branches. Not a default operation with the high
# level command cucr-git-clean-local
if [ -n "$unmerged_stale_branches" ]
then
unmerged_stale_branches=$(echo "$unmerged_stale_branches" | sed -e 's/^[[:space:]]*//' | tr " " "\n")
# If force_remove is not true then echo the list of unmerged stale
# branches and echo that the user needs to call the command with
# --force-remove to remove these branches
if [ ! "$force_remove" == "true" ]
then
echo -e "\e[33m"
echo -e "Found unmerged stale branches:"
echo -e "------------------------------"
echo -e "$unmerged_stale_branches"
echo
echo -e "[cucr-git-clean-local] To remove these branches call the command with '--force-remove'"
echo -e "\e[0m"
return 0
fi
echo
echo -e "Removing unmerged stale branches:"
echo -e "---------------------------------"
local unmerged_stale_branch
for unmerged_stale_branch in $unmerged_stale_branches
do
git branch -D "$unmerged_stale_branch" > /dev/null 2>&1
error_code=$?
if [ ! $error_code -eq 0 ]
then
echo -e "\e[31m[cucr-git-clean-local] In repository '$repo' error deleting branch: $unmerged_stale_branch\e[0m"
else
echo -e "\e[36m$unmerged_stale_branch"
fi
done
fi
echo
echo -e "[cucr-git-clean-local] Branch cleanup of repository '$repo' complete\e[0m"
return 0
}
function cucr-git-clean-local
{
# Run _cucr-git-clean-local on cucr-env, cucr-env-targets and all current environment
# repositories safely when no input exists
if [ -n "$1" ]
then
if [ "$1" != "--force-remove" ]
then
echo -e "[cucr-git-clean-local][Error] Unknown input argument '$1'. Only supported argument is '--force-remove' to forcefully remove unmerged stale branches"
return 1
fi
fi
_cucr-repos-do "_cucr-git-clean-local $*"
}
function __cucr-git-clean-local
{
local IFS options
IFS=$'\n'
options="'--force-remove'"
# shellcheck disable=SC2178
mapfile -t COMPREPLY < <(compgen -W "$(echo -e "$options")" -- "$cur")
}
complete -F __cucr-git-clean-local cucr-git-clean-local
complete -F __cucr-git-clean-local _cucr-git-clean-local
# ----------------------------------------------------------------------------------------------------
# SSH
# ----------------------------------------------------------------------------------------------------
function _git_split_url
{
local url
url=$1
# The regex can be further constrained using regex101.com
if ! grep -P -q "^(?:(?:git@[^:]+:)|(?:https://))[^:]+\.git$" <<< "${url}"
then
return 1
fi
local web_address domain_name repo_address
if [[ "$url" == *"@"* ]] # SSH
then
web_address=${url#git@}
domain_name=${web_address%%:*}
repo_address=${web_address#*:}
else
web_address=${url#https://}
domain_name=${web_address%%/*}
repo_address=${web_address#*/}
fi
repo_address=${repo_address%.git}
echo -e "$domain_name\t$repo_address"
}
export -f _git_split_url # otherwise not available in sourced files
function _git_https
{
local url
url=$1
[[ $url =~ ^https://.*\.git$ ]] && echo "$url" && return 0
local output
output=$(_git_split_url "$url")
if [[ -z "${output}" ]]
then
return 1
fi
local array domain_name repo_address
read -r -a array <<< "$output"
domain_name=${array[0]}
repo_address=${array[1]}
echo "https://$domain_name/$repo_address.git"
}
export -f _git_https # otherwise not available in sourced files
function _git_ssh
{
local url
url=$1
[[ $url =~ ^git@.*\.git$ ]] && echo "$url" && return 0
local output
output=$(_git_split_url "$url")
if [[ -z "${output}" ]]
then
return 1
fi
local array domain_name repo_address
read -r -a array <<< "$output"
domain_name=${array[0]}
repo_address=${array[1]}
echo "git@$domain_name:$repo_address.git"
}
export -f _git_ssh # otherwise not available in sourced files
function _cucr_git_https_or_ssh
{
local input_url output_url test_var
input_url=$1
# TODO: Remove the use of CUCR_USE_SSH when migration to CUCR_GIT_USE_SSH is complete
[[ -v "CUCR_USE_SSH" ]] && test_var="CUCR_USE_SSH"
[[ -v "CUCR_GIT_USE_SSH" ]] && test_var="CUCR_GIT_USE_SSH"
[[ "$input_url" == *"github"* ]] && [[ -v "CUCR_GITHUB_USE_SSH" ]] && test_var="CUCR_GITHUB_USE_SSH"
[[ "$input_url" == *"gitlab"* ]] && [[ -v "CUCR_GITLAB_USE_SSH" ]] && test_var="CUCR_GITLAB_USE_SSH"
if [[ -n "$test_var" && "${!test_var}" == "true" ]]
then
output_url=$(_git_ssh "$input_url")
else
output_url=$(_git_https "$input_url")
fi
if [[ -z "${output_url}" ]]
then
return 1
fi
echo "$output_url"
}
export -f _cucr_git_https_or_ssh # otherwise not available in sourced files
######################################################################################################################
# Generate the path where a cloned git repository will be stored, based on its url
# Globals:
# CUCR_REPOS_DIR, used as the base directory of the generated path
# Arguments:
# URL, A valid git repository url
# Return:
# Path where the repository must be cloned
######################################################################################################################
function _git_url_to_repos_dir
{
local url output
url=$1
output=$(_git_split_url "$url")
if [[ -z "${output}" ]]
then
return 1
fi
local array domain_name repo_address repos_dir
read -r -a array <<< "$output"
domain_name=${array[0]}
repo_address=${array[1]}
repos_dir="$CUCR_REPOS_DIR"/"$domain_name"/"$repo_address"
echo "${repos_dir}"
}
export -f _git_url_to_repos_dir # otherwise not available in sourced files
######################################################################################################################
# Perform a deep fetch on a git repository
#
# Arguments:
# repo_dir, Path to valid git directory
# If no directory path specified, current dir is assumed
######################################################################################################################
function cucr-git-deep-fetch
{
local repo_dir
repo_dir="${1}"
if [[ -z "${repo_dir}" ]]
then
repo_dir="."
fi
git -C "${repo_dir}" config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git -C "${repo_dir}" remote update
}
export -f cucr-git-deep-fetch
# ----------------------------------------------------------------------------------------------------
# CUCR-MAKE
# ----------------------------------------------------------------------------------------------------
# catkin build/test worflow
# catkin build -DCATKIN_ENABLE_TESTING=OFF # As we don't install test dependencies by default. Test shouldn't be build
# When you want to test, make sure the test dependencies are installed
# catkin build -DCATKIN_ENABLE_TESTING=ON # This will trigger cmake and will create the targets and build the tests
# catkin test # Run the tests. This will not trigger cmake, so it isn't needed to provide -DCATKIN_ENABLE_TESTING=ON.
function cucr-make
{
[[ -z "${CUCR_ROS_DISTRO}" ]] && { echo -e "\e[31;1mError! cucr-env variable CUCR_ROS_DISTRO not set.\e[0m"; return 1; }
[[ -z "${CUCR_ROS_VERSION}" ]] && { echo -e "\e[31;1mError! CUCR_ROS_VERSION is not set.\nSet CUCR_ROS_VERSION before executing this function.\e[0m"; return 1; }
[[ ! -d "${CUCR_SYSTEM_DIR}" ]] && { echo -e "\e[31;1mError! The workspace '${CUCR_SYSTEM_DIR}' does not exist. Run 'cucr-get install ros${CUCR_ROS_VERSION}' first.\e[0m"; return 1; }
if [[ "${CUCR_ROS_VERSION}" -eq 1 ]]
then
local build_tool
build_tool=""
if [ -f "$CUCR_SYSTEM_DIR"/devel/.built_by ]
then
build_tool=$(cat "$CUCR_SYSTEM_DIR"/build/.built_by)
fi
case $build_tool in
'catkin build')
/usr/bin/python3 "$(command -v catkin)" build --workspace "$CUCR_SYSTEM_DIR" "$@"
return $?
;;
'')
/usr/bin/python3 "$(command -v catkin)" config --init --mkdirs --workspace "$CUCR_SYSTEM_DIR" --extend /opt/ros/"$CUCR_ROS_DISTRO" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCATKIN_ENABLE_TESTING=OFF
/usr/bin/python3 "$(command -v catkin)" build --workspace "$CUCR_SYSTEM_DIR" "$@"
touch "$CUCR_SYSTEM_DIR"/devel/.catkin # hack to allow overlaying to this ws while being empty
;;
*)
echo -e "\e[31;1mError! ${build_tool} is not supported (anymore), use catkin tools\e[0m"
return 1
;;
esac
elif [[ "${CUCR_ROS_VERSION}" -eq 2 ]]
then
mkdir -p "$CUCR_SYSTEM_DIR"/src
# Disable symlink install for production
if [ "${CI_INSTALL}" == "true" ]
then
rm -rf "$CUCR_SYSTEM_DIR"/install
python3 -m colcon --log-base "$CUCR_SYSTEM_DIR"/log build --base-paths "$CUCR_SYSTEM_DIR"/src --build-base "$CUCR_SYSTEM_DIR"/build --install-base "$CUCR_SYSTEM_DIR"/install "$@"
else
python3 -m colcon --log-base "$CUCR_SYSTEM_DIR"/log build --merge-install --symlink-install --base-paths "$CUCR_SYSTEM_DIR"/src --build-base "$CUCR_SYSTEM_DIR"/build --install-base "$CUCR_SYSTEM_DIR"/install "$@"
fi
return $?
else
echo -e "\e[31;1mError! ROS_VERSION '${CUCR_ROS_VERSION}' is not supported by cucr-env.\e[0m"
return 1
fi
}
export -f cucr-make
function cucr-make-test
{
[[ -z "${CUCR_ROS_DISTRO}" ]] && { echo -e "\e[31;1mError! cucr-env variable CUCR_ROS_DISTRO not set.\e[0m"; return 1; }
[[ -z "${CUCR_ROS_VERSION}" ]] && { echo -e "\e[31;1mError! CUCR_ROS_VERSION is not set.\nSet CUCR_ROS_VERSION before executing this function.\e[0m"; return 1; }
[[ ! -d "${CUCR_SYSTEM_DIR}" ]] && { echo -e "\e[31;1mError! The workspace '${CUCR_SYSTEM_DIR}' does not exist. Run 'cucr-get install ros${CUCR_ROS_VERSION}' first.\e[0m"; return 1; }
if [[ "${CUCR_ROS_VERSION}" -eq 1 ]]
then
local build_tool
build_tool=""
if [ -f "${CUCR_SYSTEM_DIR}"/build/.built_by ]
then
build_tool=$(cat "${CUCR_SYSTEM_DIR}"/build/.built_by)
fi
case ${build_tool} in
'catkin build')
/usr/bin/python3 "$(command -v catkin)" test --workspace "${CUCR_SYSTEM_DIR}" "$@"
return $?
;;
'')
echo -e "\e[31;1mError! First initialize the workspace and build it, i.e. using cucr-make, before running tests\e[0m"
return 1
;;
*)
echo -e "\e[31;1mError! ${build_tool} is not supported (anymore), use catkin tools\e[0m"
return 1
;;
esac
elif [[ "${CUCR_ROS_VERSION}" -eq 2 ]]
then
if [[ ! -d "${CUCR_SYSTEM_DIR}"/src ]]
then
echo -e "\e[31;1mError! No 'src' directory exists in the workspace '${CUCR_SYSTEM_DIR}'\e[0m"
return 1
fi
if [[ ! -d "${CUCR_SYSTEM_DIR}"/build ]]
then
echo -e "\e[31;1mError! No 'build' directory exists in the workspace '${CUCR_SYSTEM_DIR}'. Build the workspace before running tests\e[0m"
return 1
fi
# Disable symlink install for production
if [ "${CI_INSTALL}" == "true" ]
then
python3 -m colcon --log-base "${CUCR_SYSTEM_DIR}"/log test --base-paths "${CUCR_SYSTEM_DIR}"/src --build-base "${CUCR_SYSTEM_DIR}"/build --install-base "${CUCR_SYSTEM_DIR}"/install --executor sequential --event-handlers console_cohesion+ "$@"
else
python3 -m colcon --log-base "${CUCR_SYSTEM_DIR}"/log test --merge-install --base-paths "${CUCR_SYSTEM_DIR}"/src --build-base "${CUCR_SYSTEM_DIR}"/build --install-base "${CUCR_SYSTEM_DIR}"/install --executor sequential --event-handlers console_cohesion+ "$@"
fi
return $?
else
echo -e "\e[31;1mError! ROS_VERSION '${CUCR_ROS_VERSION}' is not supported by cucr-env.\e[0m"
return 1
fi
}
export -f cucr-make-test
function cucr-make-test-result
{
[[ -z "${CUCR_ROS_DISTRO}" ]] && { echo -e "\e[31;1mError! cucr-env variable CUCR_ROS_DISTRO not set.\e[0m"; return 1; }
[[ -z "${CUCR_ROS_VERSION}" ]] && { echo -e "\e[31;1mError! CUCR_ROS_VERSION is not set.\nSet CUCR_ROS_VERSION before executing this function.\e[0m"; return 1; }
[[ ! -d "${CUCR_SYSTEM_DIR}" ]] && { echo -e "\e[31;1mError! The workspace '${CUCR_SYSTEM_DIR}' does not exist. Run 'cucr-get install ros${CUCR_ROS_VERSION}' first.\e[0m"; return 1; }
if [[ "${CUCR_ROS_VERSION}" -eq 1 ]]
then
local build_tool
build_tool=""
if [ -f "$CUCR_SYSTEM_DIR"/build/.built_by ]
then
build_tool=$(cat "$CUCR_SYSTEM_DIR"/build/.built_by)
fi
case $build_tool in
'catkin build')
python3 "$(command -v catkin)" test_results "${CUCR_SYSTEM_DIR}"/build "$@"
return $?
;;
'')
echo -e "\e[31;1mError! First initialize the workspace and build it, i.e. using cucr-make, before running tests and checking the test results\e[0m"
return 1
;;
*)
echo -e "\e[31;1mError! ${build_tool} is not supported (anymore), use catkin tools\e[0m"
return 1
;;
esac
elif [[ "${CUCR_ROS_VERSION}" -eq 2 ]]
then
if [[ ! -d "${CUCR_SYSTEM_DIR}"/src ]]
then
echo -e "\e[31;1mError! No 'src' directory exists in the workspace '${CUCR_SYSTEM_DIR}'\e[0m"
return 1
fi
if [[ ! -d "${CUCR_SYSTEM_DIR}"/build ]]
then
echo -e "\e[31;1mError! No 'build' directory exists in the workspace '${CUCR_SYSTEM_DIR}'. Build the workspace, run tests before checking test results\e[0m"
return 1
fi
python3 -m colcon --log-base "${CUCR_SYSTEM_DIR}"/log test-result --test-result-base "${CUCR_SYSTEM_DIR}"/build "$@"
return $?
else
echo -e "\e[31;1mError! ROS_VERSION '${CUCR_ROS_VERSION}' is not supported by cucr-env.\e[0m"
return 1
fi
}
export -f cucr-make-test-result
function _cucr-make
{
local cur
cur=${COMP_WORDS[COMP_CWORD]}
local options
[[ "${CUCR_ROS_VERSION}" -eq 2 ]] && options="${options} --packages-select"
mapfile -t COMPREPLY < <(compgen -W "$(_list_subdirs "${CUCR_SYSTEM_DIR}"/src) ${options}" -- "${cur}")
}
complete -F _cucr-make cucr-make
complete -F _cucr-make cucr-make-test
# ----------------------------------------------------------------------------------------------------
# CUCR-STATUS
# ----------------------------------------------------------------------------------------------------
function _robocup_branch_allowed
{
local branch robocup_branch
branch=$1
robocup_branch=$(_cucr_get_robocup_branch)
[ -n "$robocup_branch" ] && [ "$branch" == "$robocup_branch" ] && return 0
# else
return 1
}
function _cucr_get_robocup_branch
{
[ -f "$CUCR_DIR"/user/config/robocup ] && cat "$CUCR_DIR"/user/config/robocup
}
function _cucr-repo-status
{
local name pkg_dir
name=$1
pkg_dir=$2
if [ ! -d "$pkg_dir" ]
then
return 1
fi
local status vctype
# Try git
if git -C "$pkg_dir" rev-parse --git-dir > /dev/null 2>&1
then
# Is git
local res
if res=$(git -C "$pkg_dir" status . --short --branch 2>&1)
then
if echo "$res" | grep -q -E 'behind|ahead' # Check if behind or ahead of branch
then
status=$res
else
status=$(git -C "$pkg_dir" status . --short)
fi
local current_branch
current_branch=$(git -C "$pkg_dir" rev-parse --abbrev-ref HEAD)
local test_branches
# Add branch specified by target
local target_branch version_cache_file
version_cache_file="$CUCR_ENV_DIR/.env/version_cache/$(git -C "$pkg_dir" rev-parse --show-toplevel 2>/dev/null)"
[ -f "$version_cache_file" ] && target_branch=$(cat "$version_cache_file")
[ -n "$target_branch" ] && test_branches="${test_branches:+${test_branches} }$target_branch"
# Add default branch
local default_branch
default_branch=$(_cucr-git-get-default-branch "$pkg_dir")
[ -n "$default_branch" ] && test_branches="${test_branches:+${test_branches} }$default_branch"
# Add robocup branch
local robocup_branch
robocup_branch=$(_cucr_get_robocup_branch)
[ -n "$robocup_branch" ] && test_branches="${test_branches:+${test_branches} }$robocup_branch"
local allowed
allowed="false"
for test_branch in $test_branches
do
if [ "$test_branch" == "$current_branch" ]
then
[ -z "$status" ] && return 0
# else
allowed="true"
break
fi
done
[ "$allowed" != "true" ] && echo -e "\e[1m$name\e[0m is on branch '$current_branch'"
fi
vctype=git
else
vctype=unknown
fi
if [ -n "$vctype" ]
then
if [ -n "$status" ]
then
echo -e ""
echo -e "\e[38;1mM \e[0m($vctype) \e[1m$name\e[0m"
echo -e "--------------------------------------------------"
echo -e "$status"
echo -e "--------------------------------------------------"
fi
fi
}
# ----------------------------------------------------------------------------------------------------
function _cucr-dir-status
{
[ -d "$1" ] || return 1
local fs
fs=$(ls "$1")
for f in $fs
do
local pkg_dir
pkg_dir=$1/$f
_cucr-repo-status "$f" "$pkg_dir"
done
}
# ----------------------------------------------------------------------------------------------------
function cucr-status
{
_cucr-dir-status "$CUCR_SYSTEM_DIR"/src
_cucr-repo-status "cucr-env" "$CUCR_DIR"
_cucr-repo-status "cucr-env-targets" "$CUCR_ENV_TARGETS_DIR"
}
# ----------------------------------------------------------------------------------------------------
function cucr-git-status
{
for pkg_dir in "$CUCR_SYSTEM_DIR"/src/*/
do
local pkg
pkg=$(basename "$pkg_dir")
local branch
if branch=$(git -C "$pkg_dir" rev-parse --abbrev-ref HEAD 2>&1)
then
local hash
hash=$(git -C "$pkg_dir" rev-parse --short HEAD)
printf "\e[0;36m%-20s\e[0m %-15s %s\n" "$branch" "$hash" "$pkg"
fi
done
}
# ----------------------------------------------------------------------------------------------------
# CUCR-REVERT
# ----------------------------------------------------------------------------------------------------
function cucr-revert
{
local human_time
human_time="$*"
for pkg_dir in "$CUCR_SYSTEM_DIR"/src/*/
do
local pkg
pkg=$(basename "$pkg_dir")
local branch
branch=$(git -C "$pkg_dir" rev-parse --abbrev-ref HEAD 2>&1)
if branch=$(git -C "$pkg_dir" rev-parse --abbrev-ref HEAD 2>&1) && [ "$branch" != "HEAD" ]
then
local new_hash current_hash
new_hash=$(git -C "$pkg_dir" rev-list -1 --before="$human_time" "$branch")
current_hash=$(git -C "$pkg_dir" rev-parse HEAD)
local newtime
if git -C "$pkg_dir" diff -s --exit-code "$new_hash" "$current_hash"
then
newtime=$(git -C "$pkg_dir" show -s --format=%ci)
printf "\e[0;36m%-20s\e[0m %-15s \e[1m%s\e[0m %s\n" "$branch is fine" "$new_hash" "$newtime" "$pkg"
else
local newbranch
git -C "$pkg_dir" checkout -q "$new_hash" --
newbranch=$(git -C "$pkg_dir" rev-parse --abbrev-ref HEAD 2>&1)
newtime=$(git -C "$pkg_dir" show -s --format=%ci)
echo "$branch" > "$pkg_dir/.do_not_commit_this"
printf "\e[0;36m%-20s\e[0m %-15s \e[1m%s\e[0m %s\n" "$newbranch based on $branch" "$new_hash" "$newtime" "$pkg"
fi
else
echo "Package $pkg could not be reverted, current state: $branch"
fi
done
}
# ----------------------------------------------------------------------------------------------------
# CUCR-REVERT-UNDO
# ----------------------------------------------------------------------------------------------------
function cucr-revert-undo
{
for pkg_dir in "$CUCR_SYSTEM_DIR"/src/*/
do
local pkg
pkg=$(basename "$pkg_dir")
if [ -f "$pkg_dir/.do_not_commit_this" ]
then
echo "$pkg"
git -C "$pkg_dir" checkout "$(cat "$pkg_dir"/.do_not_commit_this)" --
rm "$pkg_dir/.do_not_commit_this"
fi
done
cucr-git-status
}
# ----------------------------------------------------------------------------------------------------
# CUCR-GET
# ----------------------------------------------------------------------------------------------------
function _cucr_show_file
{
if [ -n "$2" ]
then
echo -e "\e[1m[$1] $2\e[0m"
echo "--------------------------------------------------"
if hash pygmentize 2> /dev/null
then
pygmentize -g "$CUCR_ENV_TARGETS_DIR"/"$1"/"$2"
else
cat "$CUCR_ENV_TARGETS_DIR"/"$1"/"$2"
fi
echo "--------------------------------------------------"
else
echo -e "_cucr_show_file requires target_name and relative file_path in target"
return 1
fi
}
function _cucr_generate_setup_file
{
"$CUCR_DIR"/setup/generate_setup_file_cucr.py || echo "Error during generation of the 'target_setup.bash'"
}
function _cucr_remove_recursively
{
if [ -z "$1" ] || [ -n "$2" ]
then
echo "_cucr_remove_recursively requires and accepts one target"
echo "provided arguments: $*"
return 1
fi
local target cucr_dependencies_dir cucr_dependencies_on_dir error_code
target=$1
cucr_dependencies_dir="$CUCR_ENV_DIR"/.env/dependencies
cucr_dependencies_on_dir="$CUCR_ENV_DIR"/.env/dependencies-on
error_code=0
# If packages depend on the target to be removed, just remove the installed status.
if [ -f "$cucr_dependencies_on_dir"/"$target" ]
then
if [[ -n $(cat "$cucr_dependencies_on_dir"/"$target") ]]
then
# depend-on is not empty, so removing the installed status
echo "[cucr-get] Other targets still depend on $target, so ignoring it"
return 0
else
# depend-on is empty, so remove it and continue to actual removing of the target
echo "[cucr-get] Deleting empty depend-on file of: $target"
rm -f "$cucr_dependencies_on_dir"/"$target"
fi
fi
# If no packages depend on this target, remove it and its dependcies.
if [ -f "$cucr_dependencies_dir"/"$target" ]
then
# Iterate over all depencies of target, which is removed.
while read -r dep
do
# Target is removed, so remove yourself from depend-on files of deps
local dep_dep_on_file tmp_file
dep_dep_on_file="$cucr_dependencies_on_dir"/"$dep"
tmp_file=/tmp/temp_depend_on
if [ -f "$dep_dep_on_file" ]
then
while read -r line
do
[[ $line != "$target" ]] && echo "$line"
done <"$dep_dep_on_file" >"$tmp_file"
mv "$tmp_file" "$dep_dep_on_file"
echo "[cucr-get] Removed '$target' from depend-on file of '$dep'"
else
echo "$target depends on $dep, so $dep_dep_on_file should exist with $target in it"
error_code=1
fi
# Actually remove the deps
local dep_error
_cucr_remove_recursively "$dep"
dep_error=$?
if [ $dep_error -gt 0 ]
then
error_code=1
fi
done < "$cucr_dependencies_dir"/"$target"
rm -f "$cucr_dependencies_dir"/"$target"
else
echo "[cucr-get] No depencies file exist for target: $target"
fi
echo "[cucr-get] Fully uninstalled $target and its dependencies"
return $error_code
}
function cucr-get
{
if [ -z "$1" ]
then
# shellcheck disable=SC1078,SC1079
echo """cucr-get is a tool for installing and removing packages.
Usage: cucr-get COMMAND [ARG1 ARG2 ...]
Possible commands:
dep - Shows target dependencies
install - Installs a package
update - Updates currently installed packages
remove - Removes installed package
list-installed - Lists all manually installed packages
show - Show the contents of (a) package(s)
Possible options:
--debug - Shows more debugging information
--no-ros-deps - Do not install ROS dependencies (Breaks the dependency tree, not all setup files will be sourced)
--doc-depend - Do install doc dependencies, overules config and --no-ros-deps
--no-doc-depend - Do not install doc dependencies, overules config
--test-depend - Do install test dependencies, overules config and --no-ros-deps
--no-test-depend - Do not install test dependencies, overules config
--try-branch=name - Try to checkout the branch (or tag) 'name'. This argument can be specified multiple times
and all the --try-branch arguments are processed in the reverse order of their declaration,
with the last one being the first. 'name' must only be an one word value, not a list or any
other type of string.
"""
return 1
fi
local cucr_dep_dir cucr_installed_dir
cucr_dep_dir=$CUCR_ENV_DIR/.env/dependencies
cucr_installed_dir=$CUCR_ENV_DIR/.env/installed
local error_code
error_code=0
local cmd
cmd=$1
shift
#Create btrfs snapshot if possible and usefull:
if [[ -n "$BTRFS_SNAPSHOT" && "$cmd" =~ ^(install|update|remove)$ ]] && { df --print-type / | grep -q btrfs; }
then
echo "[cucr-get] Creating btrfs snapshot"
sudo mkdir -p /snap/root
sudo btrfs subvolume snapshot / /snap/root/"$(date +%Y-%m-%d_%H:%M:%S)"
fi
if [[ "$cmd" =~ ^(install|remove)$ && -z "$1" ]]
then
echo "Usage: cucr-get $cmd TARGET [TARGET2 ...]"
return 1
fi
if [[ $cmd == "install" || $cmd == "update" ]]
then
if [[ $cmd == "update" ]]
then
for target in "$@"
do
#Skip options
[[ $target = '--'* ]] && continue
if [ -z "$(find "$CUCR_ENV_DIR"/.env/dependencies -maxdepth 1 -name "$target" -type f -printf "%P ")" ]
then
echo "[cucr-get] Package '$target' is not installed."
error_code=1
fi
done
fi
if [ $error_code -eq 0 ]
then
"$CUCR_DIR"/installer/cucr-install.bash "$cmd" "$@"
error_code=$?
if [ $error_code -eq 0 ]
then
_cucr_generate_setup_file
# shellcheck disable=SC1091
source "$CUCR_DIR"/setup_cucr.bash
fi
fi
return $error_code
elif [[ $cmd == "remove" ]]
then
local targets_to_remove
for target in "$@"
do
local resolved_targets
resolved_targets="$(find "$cucr_installed_dir" -maxdepth 1 -name "$target" -type f -printf "%P ")"
if [ -z "$resolved_targets" ]