#!/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 <] 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 "${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" "${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}" if [ "$dir" = build ]; then ftype=l else ftype=d fi ports="$(find "${option_prefix}/var/macports/${dir}" -name '.*' -prune -o -depth 2 -type $ftype -print | sed 's,^.*/,,' | sort -fu)" for port in ${ports}; do echo "Deleting ${dir} for ${port}" "${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 "${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)))" }