-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
mpbb-cleanup
135 lines (122 loc) · 5.02 KB
/
mpbb-cleanup
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
#!/bin/bash
# -*- coding: utf-8; mode: sh; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=sh:et:sw=4:ts=4:sts=4
# Note:
# This script is sourced by the mpbb wrapper script.
# Do not execute this directly!
cleanup-usage() {
# "prog" is defined in mpbb-help.
# shellcheck disable=SC2154
cat <<EOF
usage: $prog [<global opts>] cleanup
Clean up after a build.
Run \`$prog help' for global options and a list of other subcommands.
EOF
}
format_size() {
size="${1#-}"
if [ "$1" == "$size" ]; then
sign=
else
sign=-
fi
units=KiB
if [ "$size" -ge 1024 ]; then
size=$(((size + 512) / 1024))
units=MiB
if [ "$size" -ge 1024 ]; then
size=$(((size + 512) / 1024))
units=GiB
fi
fi
echo "$sign$size$units"
}
disk_free() {
df -k "$1" | awk 'NR==2 {print $4}'
}
cleanup() {
# if this is the very first build, selfupdate did not install port yet
# $option_prefix is set in mpbb
# shellcheck disable=SC2154
if [ ! -e "${option_prefix}/bin/port" ]; then
echo "---> Skipping cleanup"
echo "port not installed at ${option_prefix}/bin/port"
return
fi
# Provide a way to pause builds to deal with situations like
# https://trac.macports.org/ticket/53587
if [[ ${BUILDBOT_BUILDERNAME:-unknown} == *-watcher ]]; then
# $option_work_dir is set in mpbb
# shellcheck disable=SC2154
waitfile="$option_work_dir/wait"
if [ -f "$waitfile" ]; then
echo "----> Waiting"
waitcount=0
while [ -f "$waitfile" ]; do
if [ "$waitcount" -eq 0 ]; then
echo "waiting while $waitfile exists"
waitcount=100
else
waitcount=$((waitcount - 1))
fi
sleep 6
done
echo
fi
fi
# $option_prefix is set in mpbb
# shellcheck disable=SC2154
disk_free_before=$(disk_free "$option_prefix")
echo "----> Free disk space before cleanup: $(format_size "$disk_free_before")"
echo
# Do extra cleanup if free space is less than this (in KiB)
DISK_FREE_THRESHOLD=15000000
if [[ ${BUILDBOT_BUILDERNAME:-unknown} == *-watcher || "$disk_free_before" -lt "$DISK_FREE_THRESHOLD" ]]; then
if [[ ${BUILDBOT_BUILDERNAME:-unknown} == *-watcher ]]; then
echo
echo "----> Uninstalling unneeded ports"
# $thisdir is set by mpbb and points to the directory in which this script resides
# shellcheck disable=SC2154
"$(readlink "${option_prefix}/bin/port-tclsh")" "${thisdir}/tools/uninstall-unneeded-ports.tcl"
fi
if [ ! -L "${option_prefix}/var/macports/distfiles" ]; then
echo
echo "----> Deleting distfiles"
find "${option_prefix}/var/macports/distfiles" -type f \! -newerat "4 hours ago" -print -delete | sed -E 's/^/Deleting distfile /'
find "${option_prefix}/var/macports/distfiles" -type d -mindepth 1 -empty -print -delete | sed -E 's/^/Deleting directory /'
fi
# clean out failcache at most once every week
timestamp="${option_work_dir}/failcache-cleanup.timestamp"
if [[ ! -f "$timestamp" || $(($(date +%s) - $(stat -f %m "$timestamp"))) -gt 604800 ]]; then
echo
echo "----> Deleting unneeded failcache entries"
"$(readlink "${option_prefix}/bin/port-tclsh")" "${thisdir}/tools/failcache-cleanup.tcl" --failcache_dir "${option_failcache_dir}"
touch "$timestamp"
fi
fi
echo
for dir in build logs; do
echo "----> Deleting ${dir}"
ports="$(find "${option_prefix}/var/macports/${dir}" -name '.*' -prune -o -depth 2 -type d -print | sed 's,^.*/,,' | sort -fu)"
for port in ${ports}; do
echo "Deleting ${dir} for ${port}"
"$(readlink "${option_prefix}/bin/port-tclsh")" "${thisdir}/tools/noisy-delete.tcl" "${option_prefix}/var/macports/${dir}"/*/"${port}"
done
rm -rf "${option_prefix}/var/macports/${dir}"/*
echo
done
# $option_prefix is set in mpbb
# shellcheck disable=SC2154
disk_free_after=$(disk_free "$option_prefix")
echo "----> Free disk space after cleanup: $(format_size "$disk_free_after")"
if [[ "$disk_free_after" -lt "$DISK_FREE_THRESHOLD" ]]; then
echo "----> Trying to free more disk space"
# $thisdir is set by mpbb and points to the directory in which this script resides
# shellcheck disable=SC2154
"$(readlink "${option_prefix}/bin/port-tclsh")" "${thisdir}/tools/reclaim-space.tcl" "$disk_free_after" "$DISK_FREE_THRESHOLD"
# $option_prefix is set in mpbb
# shellcheck disable=SC2154
disk_free_after=$(disk_free "$option_prefix")
echo "----> Free disk space after extra cleanup: $(format_size "$disk_free_after")"
fi
echo "----> Disk space saved by cleanup: $(format_size $((disk_free_after - disk_free_before)))"
}