Skip to content

Instantly share code, notes, and snippets.

@66Ton99
Last active June 21, 2022 23:30
Show Gist options
  • Save 66Ton99/7783b186dfef4823a62423a5ac8392a7 to your computer and use it in GitHub Desktop.
Save 66Ton99/7783b186dfef4823a62423a5ac8392a7 to your computer and use it in GitHub Desktop.
Allow to export license files from RPMs
#!/usr/bin/env bash
# RPM license exporter
# Copyright 2022 66Ton99@gmail.com
extract_file() {
# echo "$1"
# echo "$2"
rpm2cpio "$1" | cpio -iv --to-stdout ".$2" 1> license.tmp 2> /dev/null
ENCODING=$(uchardet license.tmp)
# echo "$ENCODING"
if [[ "$ENCODING" == "unknown" ]]; then
echo "UNKNOWN ENCODING: $1 - $2"
return;
fi
echo "$2" >> licenses.tmp
if [[ "$ENCODING" == "UTF-8" ]]; then
cat license.tmp >> licenses.tmp
else
iconv -f "$ENCODING" -t UTF-8 license.tmp >> licenses.tmp
fi
echo "" >> licenses.tmp
}
rm -f licenses.txt
for fileName in *.rpm; do
echo "$fileName"
rm -f licenses.tmp
NO_LIC=true
FILES=$(rpm -qlp "$fileName")
HAS_LIC=false
for lic in `grep -i "/usr/share/licenses/.*/" <<< "${FILES[*]}"`; do
extract_file "$fileName" "$lic"
HAS_LIC=true
NO_LIC=false
done
if ! $HAS_LIC; then
for lic in `grep -i "licens\|copy" <<< "${FILES[*]}"`; do
extract_file "$fileName" "$lic"
NO_LIC=false
done
fi
if $NO_LIC; then
echo "NO LICENSE: $fileName"
else
(echo "$fileName"; cat licenses.tmp; echo ""; echo "") >> licenses.txt
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment