chroot_version_hooks: Add hook to clean up removed rust packages.
dev-rust ebuilds typically have unique slots and do not block each other
so they accumulate over time. It is about time for a cleanup.
BUG=None
TEST=bash chroot_version_hooks.d/191_cleanup_dev_rust
Change-Id: If03023cf0c201c30c28e73c7d408328e4d93ce62
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/3276492
Tested-by: Allen Webb <allenwebb@google.com>
Auto-Submit: Allen Webb <allenwebb@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Allen Webb <allenwebb@google.com>
diff --git a/chroot_version_hooks.d/192_cleanup_dev_rust b/chroot_version_hooks.d/192_cleanup_dev_rust
new file mode 100644
index 0000000..32e2011
--- /dev/null
+++ b/chroot_version_hooks.d/192_cleanup_dev_rust
@@ -0,0 +1,36 @@
+# Copyright 2021 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Clean up rust packages which no longer have corresponding ebuilds
+#
+# This is approxmiated by removing versions with no binary package since
+# eclean will remove binary packages without a corresponding ebuild.
+
+export CLEAN_DELAY=0
+
+do_clean() {
+ local suffix="$1"
+ local emerge_cmd=()
+ local args
+
+ if [[ -z "${suffix}" ]]; then
+ emerge_cmd+=( sudo )
+ fi
+ emerge_cmd+=( "emerge${suffix}" )
+
+ args=( $("equery${suffix}" l -b --format='=$cpv' 'dev-rust/*' ) )
+ if [[ ${#args[@]} -eq 0 ]]; then
+ return
+ fi
+
+ "${emerge_cmd[@]}" -C "${args[@]}"
+}
+
+do_clean
+for board_root in /build/*; do
+ board_name=${board_root##*/}
+ if [[ -d "${board_root}/var/db/pkg/dev-rust" ]]; then
+ do_clean "-${board_name}"
+ fi
+done