Skip to content

Instantly share code, notes, and snippets.

@philroche
Last active August 2, 2024 12:37
Show Gist options
  • Save philroche/4fca1e881fdc8d36fc7ca9e4f06faf4a to your computer and use it in GitHub Desktop.
Save philroche/4fca1e881fdc8d36fc7ca9e4f06faf4a to your computer and use it in GitHub Desktop.
Find all packages dependent on a given package across all wolfi package repositories
#!/bin/bash -eu
# Find all packages dependent on a given package across all wolfi package repositories
# Usage: ./find-wolfi-dependencies.sh <package_to_query>
# Example: ./find-wolfi-dependencies.sh "python3"
# package_to_query is the package name to query and is required
# If package_to_query is not provided, the script will exit with an error
package_to_query=${1:-}
if [ -z "${package_to_query}" ]; then
echo "Usage: ${0} <package_to_query>"
exit 1
fi
package_indices_to_query=(
"https://packages.cgr.dev/os/x86_64/APKINDEX.tar.gz" # enterprise_packages
"https://packages.wolfi.dev/os/x86_64/APKINDEX.tar.gz" # wolfi packages
"https://packages.cgr.dev/extras/x86_64/APKINDEX.tar.gz" # extra packages
)
for package_index in "${package_indices_to_query[@]}"; do
# This command is taken from a dependency search @ https://apk.dag.dev/
echo "INDEX : ${package_index}"
curl --silent --location ${package_index} |
tar --extract --to-stdout --ungzip APKINDEX | # See https://wiki.alpinelinux.org/wiki/Apk_spec#APKINDEX_Format for the format of an APKINDEX file
# This awk command will parse the APKINDEX file and list all packages and their dependencies on one space sepearated line
awk -F':' ' # This sets the field separator to a colon (:). This means that awk will split each line into fields based on colons.
$1 == "P" {printf "%s", $2} # If the first field ($1) is "P" (Package name), it prints the second field ($2) followed by a hyphen (-).
$1 == "D" { printf " %s", substr($0, 3)} # If the first field ($1) is "D" (dependencies), it prints a space followed by the entire line starting from the third character (substr($0, 3)).
/^$/ {printf "\n"}' | # If the line is empty (/^$/), it prints a newline character (\n).
grep --invert-match "^${package_to_query} " | # remove the package name itself from the list
grep "${package_to_query}" | # grep for the provided package name
cut --delimiter=" " --fields=1 | # if found then print only the package name and not the dependencies
sort | # sort the package names alphabetically
uniq # only print unique package names
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment