-
Notifications
You must be signed in to change notification settings - Fork 32
/
clean_thirdparty.sh
executable file
·138 lines (123 loc) · 3.33 KB
/
clean_thirdparty.sh
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
#!/usr/bin/env bash
set -euo pipefail
# shellcheck source=./yb-thirdparty-common.sh
. "${BASH_SOURCE[0]%/*}/yb-thirdparty-common.sh"
# -------------------------------------------------------------------------------------------------
# Functions
# -------------------------------------------------------------------------------------------------
show_usage() {
cat <<-EOT
${0##*/} -- cleans third-party builds from various subdirectories of the thirdparty directory.
If invoked with --all, cleans all third-party builds.
Usage: ${0##*/} [<options>] [<dependency_names>]
Options:
-h, --help
Show usage
--downloads, --download, -d
Also clean downloads for the chosen dependencies. This could cause large dependencies to be
re-downloaded, so should be used carefully.
--all
Clean all third-party dependency build artifacts. This is done using a "git clean" command.
EOT
}
realpath() {
python -c "import os; import sys; print(os.path.realpath(sys.argv[1]))" "$@"
}
delete_dir() {
if [[ $# -ne 1 ]]; then
fatal "delete_dir expects exactly one arugment, got $#"
fi
local dir_path
dir_path=$( realpath "$1" )
if [[ -d $dir_path ]]; then
log "DELETING directory '$dir_path'"
( set -x; rm -rf "$dir_path" )
else
log "'$dir_path' is not a directory or does not exist"
fi
}
delete_file() {
if [[ $# -ne 1 ]]; then
fatal "delete_file expects exactly one arugment, got $#"
fi
local file_glob=$1
local file_paths=( "$file_glob" )
local file_path
for file_path in "${file_paths[@]}"; do
file_path=$( realpath "$file_path" )
if [[ -f $file_path ]]; then
log "DELETING file '$file_path'"
( set -x; rm -f "$file_path" )
else
log "'$file_path' is not a file or does not exist"
fi
done
}
# -------------------------------------------------------------------------------------------------
# Main script
# -------------------------------------------------------------------------------------------------
cd "$YB_THIRDPARTY_DIR"
dependency_names_to_clean=()
if [[ $# -eq 0 ]]; then
show_usage >&2
exit 1
fi
clean_all=false
delete_downloads=false
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_usage >&2
exit 1
;;
--all)
clean_all=true
;;
--downloads|--download|-d)
delete_downloads=true
;;
-*)
fatal "Invalid option: $1"
;;
*)
dependency_names_to_clean+=( "$1" )
esac
shift
done
if "$clean_all"; then
exclusions=(
'*.sw?'
venv/
.vscode/
.mypy_cache/
__pycache__/
)
if ! "$delete_downloads"; then
exclusions+=( download/ )
fi
git_clean_args=( -dxf )
for exclusion in "${exclusions[@]}"; do
git_clean_args+=( --exclude "$exclusion" )
done
# shellcheck disable=SC2086
( set -x; git clean "${git_clean_args[@]}" )
exit
fi
for dep_name in "${dependency_names_to_clean[@]}"; do
(
set -x
rm -rfv \
"$YB_THIRDPARTY_DIR"/build/{common,uninstrumented,tsan}/{"$dep_name","$dep_name-"*,.build-stamp-"$dep_name"}
rm -rfv "$YB_THIRDPARTY_DIR/src/${dep_name}-"*
)
for top_build_dir in "$YB_THIRDPARTY_DIR"/build/{common,uninstrumented,tsan}; do
if [[ -d $top_build_dir ]]; then
(
cd "$top_build_dir"
delete_file ".build-stamp-$dep_name"
)
else
log "Directory '$top_build_dir' does not exist, ignoring"
fi
done
done