From aa63d0a9b6866445f93f393f4951e06d82a5c4d4 Mon Sep 17 00:00:00 2001 From: "Henrique F. Simoes" Date: Thu, 14 Nov 2024 17:03:31 -0300 Subject: [PATCH] ioc: don't prune pre-selected directories. When pruning modules, some of them might contain special cases where it is harder to automatically detect in the pruning algorithm. While users could workaround this by specifying such special paths under APP_DIRS, this can be handled here by adding extra tooling to be used by modules. Introduce a way for modules to specify extra paths that must be kept, besides those detected by lnls-prune-artifacts. Do this through a .lnls-keep-paths file that specifies all relative paths (directories or files) whose contents must be kept. Paths may be globs, so that they are resolved based on their bash expansion, avoiding hard-coded options when ambiguity can be automatically resolved. These exceptions are considered for all ancestors of a candidate to removal, so that they are not restricted to EPICS modules themselves. This eases implementation because we won't need to detect which parent is a module and should contain .lnls-keep-paths, and instead simply traverse the whole ancestor list looking if any of them defines one. --- base/lnls-prune-artifacts.sh | 40 +++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/base/lnls-prune-artifacts.sh b/base/lnls-prune-artifacts.sh index 4ce99dd..4a4408e 100755 --- a/base/lnls-prune-artifacts.sh +++ b/base/lnls-prune-artifacts.sh @@ -77,6 +77,34 @@ get_used_epics_modules() { filter_out_paths "$all_modules" "$unused_modules" } +# Traverse ancestor directories of each provided path, and concatenate all +# their .lnls-keep-paths defined entries as absolute paths. +get_defined_paths_to_keep() { + for path; do + if [ "${path:0:1}" != "/" ]; then + >&2 echo "error: get_defined_paths_to_keep() expects absolute paths, but got '$path'" + exit 1 + fi + + while true; do + keep_path_file="$path/.lnls-keep-paths" + + if [ -f "$keep_path_file" ]; then + keep_paths=$(cat "$keep_path_file") + + for keep_path in $keep_paths; do + # output it as an absolute path + realpath "$path"/$keep_path + done + fi + + [ "$path" == "/" ] && break + + path=$(dirname $path) + done + done | sort -u +} + prune_module_dirs() { module=$1 @@ -84,6 +112,7 @@ prune_module_dirs() { keep_paths=$(cat << EOF $(find_shared_libraries $module) $(find $module -type f -regex ".*\.\(cmd\|db\|template\|req\|substitutions\)" -printf "%h\n" | sort -u) +$(get_defined_paths_to_keep $module) EOF ) @@ -154,11 +183,16 @@ remove_unused_shared_libraries() { remove_libs=$(echo "$remove_libs" | grep -vx $lib) done + keep_paths=$(get_defined_paths_to_keep $remove_libs) + for lib in $remove_libs; do - size=$(du -hs $lib | cut -f 1) + # if library is not found inside any $keep_dirs, remove it + if find $keep_paths -path "$lib" -exec false {} +; then + size=$(du -hs $lib | cut -f 1) - echo "Removing shared library '$lib' ($size)" - rm -f ${lib%.so*}.so* + echo "Removing shared library '$lib' ($size)" + rm -f ${lib%.so*}.so* + fi done }