Skip to content

Instantly share code, notes, and snippets.

@ArrayBolt3
Created November 1, 2024 05:06
Show Gist options
  • Save ArrayBolt3/7f2bf1edd8550fb3f23768255ec2ba22 to your computer and use it in GitHub Desktop.
Save ArrayBolt3/7f2bf1edd8550fb3f23768255ec2ba22 to your computer and use it in GitHub Desktop.
A script for visualizing the package dependency interactions in a Debian metapackage.
#!/bin/bash
#
# Generates graphviz DOT source code from a control file. The graph represents
# the dependency interaction for all binary packages in the control file. Only
# depends are handled right now, recommends and suggests are ignored.
trim_string () {
sed 's/^[ \t]*//;s/[ \t]*$//'
}
echo_dotcode() {
package_name="${1:-}";
readarray -d'|' -t package_list <<< "${package_name}"
for package in "${package_list[@]}"; do
real_package="$(trim_string <<< "${package}")"
echo " \"${current_package}\" -> \"${real_package}\";"
done
}
file_name="${1:-}"
[ -z "${file_name}" ] && exit 1
readarray -t file_contents < <(cat "${file_name}")
line_type=''
line_val=''
line_type_is_new='n'
current_package=''
echo 'digraph G {'
echo ' rankdir="LR";'
for line in "${file_contents[@]}"; do
if [[ "${line}" =~ ^[a-zA-Z]*: ]]; then
line_type="$(cut -d':' -f1 <<< "${line}")"
line_type_is_new='y'
else
line_type_is_new='n'
fi
if [ "${line_type_is_new}" = 'y' ]; then
line_val="$(cut -d':' -f2- <<< "${line}" | trim_string)"
else
line_val="$(trim_string <<< "${line}")"
fi
if [ "${line_type}" = 'Package' ]; then
current_package="${line_val}"
continue
elif [ "${line_type}" = 'Depends' ]; then
package_name="${line_val//','}"
if [[ "${package_name}" =~ [{}] ]]; then
continue
fi
echo_dotcode "${package_name}"
continue
fi
done
echo '}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment