Clean up the bash

This commit is contained in:
Kevin MacMartin 2020-01-30 13:34:11 -05:00
parent 52db38c19a
commit b508f2b02a

View file

@ -18,7 +18,7 @@
# specialpkg_check(): Contained in this script, custom version checks go in this function # specialpkg_check(): Contained in this script, custom version checks go in this function
# #
cd "${0%/*}" cd "${0%/*}" || exit
script_directory="$PWD" # Directory containing this script script_directory="$PWD" # Directory containing this script
script_name="${0//*\/}" # Name of this script script_name="${0//*\/}" # Name of this script
package_rootdir="$(readlink -f "$script_directory"/..)" # Directory containing a collection of packages contained in folders package_rootdir="$(readlink -f "$script_directory"/..)" # Directory containing a collection of packages contained in folders
@ -47,7 +47,7 @@ unset c_blue c_white c_yellow c_grey c_red c_green c_reset
# SPECIALPKG CHECK: function for custom version check functions # SPECIALPKG CHECK: function for custom version check functions
function specialpkg_check() { function specialpkg_check() {
# Enter the package root directory # Enter the package root directory
cd "$package_rootdir" cd "$package_rootdir" || exit
# PKGVER CHECK: terminfo-italics # PKGVER CHECK: terminfo-italics
specialpkg=terminfo-italics specialpkg=terminfo-italics
@ -55,16 +55,16 @@ function specialpkg_check() {
| grep Version \ | grep Version \
| sed 's|^[^:]*:\ ||;s|-.*$||') | sed 's|^[^:]*:\ ||;s|-.*$||')
pkgver='' pkgver=''
eval "$(egrep '^\s*pkgver\s*=' $specialpkg/PKGBUILD)" eval "$(grep -E '^\s*pkgver\s*=' $specialpkg/PKGBUILD)"
vercmp_check "$specialpkg" "$upstream_version" "$pkgver" vercmp_check "$specialpkg" "$upstream_version" "$pkgver"
# Return to the script folder # Return to the script folder
cd "$script_directory" cd "$script_directory" || exit
} }
# HELPER FUNCTION: Output readible results of a version comparison # HELPER FUNCTION: Output readible results of a version comparison
function vercomp_display() { function vercomp_display() {
printf '%s\n' "$c_blue[$c_white$1$c_blue]$c_reset ${c_yellow}up: $2$c_reset $c_blue|$c_reset $3" printf '%s\n' "${c_blue}[$c_white$1$c_blue]$c_reset ${c_yellow}up: $2$c_reset $c_blue|$c_reset $3"
} }
# HELPER FUNCTION: Compares versions and handles accordingly # HELPER FUNCTION: Compares versions and handles accordingly
@ -110,7 +110,7 @@ function archversion_check() {
> "$temp_config" > "$temp_config"
# Define $package_definition as an empty archversion.conf template then add the $pkg entry from $archversion_conf # Define $package_definition as an empty archversion.conf template then add the $pkg entry from $archversion_conf
if egrep -q "\[\s*$pkg\s*\]" "$archversion_conf"; then if grep -qE "\[\s*$pkg\s*\]" "$archversion_conf"; then
package_definition="$(sed 's|^\s*#\s*||;/\[\s*'"$pkg"'\s*\]/,$!d;/^$/q' "$archversion_conf")" package_definition="$(sed 's|^\s*#\s*||;/\[\s*'"$pkg"'\s*\]/,$!d;/^$/q' "$archversion_conf")"
else else
package_definition="[$pkg]" package_definition="[$pkg]"
@ -121,11 +121,12 @@ function archversion_check() {
>> "$temp_config" >> "$temp_config"
# Grab the simple archversion output for parsing # Grab the simple archversion output for parsing
archversion_output=$(CONFIG_PACKAGES="$temp_config" CACHE_PACKAGES="$package_cache" \ archversion_output=$(CONFIG_PACKAGES="$temp_config" CACHE_PACKAGES="$package_cache" archversion $archversion_command)
archversion $archversion_command)
upstream_version=$(egrep -o 'up: [^ ]*' <<< "$archversion_output" \ upstream_version=$(grep -oE 'up: [^ ]*' <<< "$archversion_output" \
| sed 's|up: ||') | sed 's|up: ||')
package_version=$(egrep -o 'aur: [^ ]*' <<< "$archversion_output" \
package_version=$(grep -oE 'aur: [^ ]*' <<< "$archversion_output" \
| sed 's|aur: ||' \ | sed 's|aur: ||' \
| sed 's|^[0-9][0-9]*:||') | sed 's|^[0-9][0-9]*:||')
@ -133,13 +134,11 @@ function archversion_check() {
vercmp_check "$pkg" "$upstream_version" "$package_version" vercmp_check "$pkg" "$upstream_version" "$package_version"
# Add a blank line after each package when running debug for easier parsing # Add a blank line after each package when running debug for easier parsing
[[ "$archversion_debug" = '1' ]] \ [[ "$archversion_debug" = '1' ]] && printf '\n'
&& printf '\n'
# Remove the tmp config # Remove the tmp config
[[ -f "$temp_config" ]] \ [[ -f "$temp_config" ]] && rm "$temp_config"
&& rm "$temp_config" done < <(grep -vE '\[DEFAULT\]' "$archversion_conf" | grep -E '^\s*\[[^]]*\]' | grep -oE '[^][]*')
done < <(egrep -v '\[DEFAULT\]' "$archversion_conf" | egrep '^\s*\[[^]]*\]' | egrep -o '[^][]*')
} }
# develversion_conf CHECK # develversion_conf CHECK
@ -156,59 +155,57 @@ function develversion_check() {
# Find the current pkgver() of the package in the AUR # Find the current pkgver() of the package in the AUR
package_version=$(pacaur -i "$pkg" \ package_version=$(pacaur -i "$pkg" \
| sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' \ | sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' \
| egrep '^Version' \ | grep -E '^Version' \
| sed 's|^[^:]*: ||;s|-[0-9]*$||') | sed 's|^[^:]*: ||;s|-[0-9]*$||')
pkgbuild_file="$package_rootdir/$pkg/PKGBUILD" pkgbuild_file="$package_rootdir/$pkg/PKGBUILD"
# Exit and skip this package if either its folder or the PKGBUILD it should contain are missing # Exit and skip this package if either its folder or the PKGBUILD it should contain are missing
[[ ! -f "$pkgbuild_file" ]] && { [[ ! -f "$pkgbuild_file" ]] && {
if [[ ! -d "$package_rootdir/$pkg" ]]; then if [[ ! -d "$package_rootdir/$pkg" ]]; then
printf '%s\n' "$c_blue[$c_white$pkg$c_blue]$c_reset ${c_red}ERROR$c_reset: $pkg does not exist in $package_rootdir" >&2 printf '%s\n' "${c_blue}[$c_white$pkg$c_blue]$c_reset ${c_red}ERROR$c_reset: $pkg does not exist in $package_rootdir" >&2
else else
printf '%s\n' "$c_blue[$c_white$pkg$c_blue]$c_reset ${c_red}ERROR$c_reset: $package_rootdir/$pkg does not contain a PKGBUILD" >&2 printf '%s\n' "${c_blue}[$c_white$pkg$c_blue]$c_reset ${c_red}ERROR$c_reset: $package_rootdir/$pkg does not contain a PKGBUILD" >&2
fi fi
continue continue
} }
# Exit and skip this package if a pkgver() function doesn't exist (ie: not VCS) # Exit and skip this package if a pkgver() function doesn't exist (ie: not VCS)
egrep -q 'pkgver\(' "$pkgbuild_file" || { grep -qE 'pkgver\(' "$pkgbuild_file" || {
printf '%s\n' "$c_blue[$c_white$pkg$c_blue]$c_reset ${c_red}ERROR$c_reset: package doesn't contain a pkgver() function" >&2 printf '%s\n' "${c_blue}[$c_white$pkg$c_blue]$c_reset ${c_red}ERROR$c_reset: package doesn't contain a pkgver() function" >&2
continue continue
} }
# Delete the temporary build folder if it already exists # Delete the temporary build folder if it already exists
[[ -d "$temp_directory/build" ]] \ [[ -d "$temp_directory/build" ]] && rm -rf "$temp_directory/build"
&& rm -rf "$temp_directory/build"
# Create and enter the temporary build folder, exiting with an error if this fails # Create and enter the temporary build folder, exiting with an error if this fails
if install -d "$temp_directory/build"; then if install -d "$temp_directory/build"; then
cd "$temp_directory/build" cd "$temp_directory/build" || exit
else else
printf '%s\n' "${c_red}ERROR$c_reset: Failure to create and enter the temporary build folder @ $temp_directory/build" >&2 printf '%s\n' "${c_red}ERROR$c_reset: Failure to create and enter the temporary build folder @ $temp_directory/build" >&2
exit 1 exit 1
fi fi
# Link all folders in the package directory except pkg+src to avoid re-cloning repos each check # Link all folders in the package directory except pkg+src to avoid re-cloning repos each check
for dir in $(find "$package_rootdir/$pkg" -maxdepth 1 -mindepth 1 -type d | egrep -v '(src|pkg)'); do for dir in $(find "$package_rootdir/$pkg" -maxdepth 1 -mindepth 1 -type d | grep -vE '(src|pkg)'); do
ln -s "$dir" "$temp_directory/build" ln -s "$dir" "$temp_directory/build"
done done
# Copy the package's PKGBUILD to the temporary build folder with its functions stripped out # Copy the package's PKGBUILD to the temporary build folder with its functions stripped out
sed '/^.*\(\).*{[^}]*$/,/^[^{]]*}/d' "$pkgbuild_file" \ sed '/^.*\(\).*{[^}]*$/,/^[^{]]*}/d' "$pkgbuild_file" \
| egrep -v '^\s*install\s*=' \ | grep -vE '^\s*install\s*=' \
> PKGBUILD > PKGBUILD
# Add the package's pkgver() function from the original PKGBUILD into the copy # Add the package's pkgver() function from the original PKGBUILD into the copy
sed '/pkgver(/,$!d' "$pkgbuild_file" \ sed '/pkgver(/,$!d' "$pkgbuild_file" | sed '/^\s*}\s*$/q' >> PKGBUILD
| sed '/^\s*}\s*$/q' \
>> PKGBUILD
# Reset the values we'll be using then source the new PKGBUILD # Reset the values we'll be using then source the new PKGBUILD
unset pkgname epoch pkgver pkgrel unset pkgname epoch pkgver pkgrel
source PKGBUILD source PKGBUILD
[[ ! "$pkgname" = "$pkg" ]] && { [[ ! "$pkgname" = "$pkg" ]] && {
printf '%s\n' "$c_blue[$c_white$pkg$c_blue]$c_reset ${c_red}ERROR$c_reset: This pkgname for this package is $pkgname" >&2 printf '%s\n' "${c_blue}[$c_white$pkg$c_blue]$c_reset ${c_red}ERROR$c_reset: This pkgname for this package is $pkgname" >&2
continue continue
} }
@ -228,12 +225,14 @@ function develversion_check() {
source_array='source=(' source_array='source=('
sha512sum_array='sha512sums=(' sha512sum_array='sha512sums=('
printf '\n' >> PKGBUILD printf '\n' >> PKGBUILD
for src in "${source[@]}"; do for src in "${source[@]}"; do
egrep -q '^\s*(bzr|csv|git|hg|darcs|svn)[^a-zA-Z0-9]' < <(sed 's|^[^:]*::||' <<< "$src") && { grep -qE '^\s*(bzr|csv|git|hg|darcs|svn)[^a-zA-Z0-9]' < <(sed 's|^[^:]*::||' <<< "$src") && {
source_array="$source_array '$src'" source_array="$source_array '$src'"
sha512sum_array="$sha512sum_array 'SKIP'" sha512sum_array="$sha512sum_array 'SKIP'"
} }
done done
sed 's|( |(|' <<< "${source_array})" >> PKGBUILD sed 's|( |(|' <<< "${source_array})" >> PKGBUILD
sed 's|( |(|' <<< "${sha512sum_array})" >> PKGBUILD sed 's|( |(|' <<< "${sha512sum_array})" >> PKGBUILD
@ -242,7 +241,7 @@ function develversion_check() {
if [[ $? = 0 ]]; then if [[ $? = 0 ]]; then
# Calculate the full package version, including epoch (if applicable), pkgver and pkgrel # Calculate the full package version, including epoch (if applicable), pkgver and pkgrel
unset epoch pkgver pkgrel unset epoch pkgver pkgrel
eval "$(egrep '^\s*(epoch|pkgver|pkgrel)\s*=' PKGBUILD)" eval "$(grep -E '^\s*(epoch|pkgver|pkgrel)\s*=' PKGBUILD)"
[[ -n "$epoch" ]] && epoch="$epoch:" [[ -n "$epoch" ]] && epoch="$epoch:"
upstream_version="$epoch$pkgver-$pkgrel" upstream_version="$epoch$pkgver-$pkgrel"
@ -256,10 +255,10 @@ function develversion_check() {
# Display an error if the package can't be found in the package root directory # Display an error if the package can't be found in the package root directory
printf '%s\n' "${c_red}ERROR$c_reset: Failed to find the package $pkg in $package_rootdir" >&2 printf '%s\n' "${c_red}ERROR$c_reset: Failed to find the package $pkg in $package_rootdir" >&2
fi fi
done < <(egrep -v '^#' "$develversion_conf") done < <(grep -vE '^#' "$develversion_conf")
# Move back to $script_directory # Move back to $script_directory
cd "$script_directory" cd "$script_directory" || exit
} }
# check_missingpkgsPKG CHECK # check_missingpkgsPKG CHECK
@ -267,30 +266,34 @@ function missingpkg_check() {
# Create lists of archversion, develversion, specialversion and noversion packages # Create lists of archversion, develversion, specialversion and noversion packages
specialversion_packages='terminfo-italics' specialversion_packages='terminfo-italics'
noversion_packages=$(sed 's|\[||;s|\]||;s|\s*#.*$||' "$noversion_conf") noversion_packages=$(sed 's|\[||;s|\]||;s|\s*#.*$||' "$noversion_conf")
develversion_packages=$(egrep -v '^\s*#' "$develversion_conf") develversion_packages=$(grep -vE '^\s*#' "$develversion_conf")
archversion_packages=$(egrep -v '^\s*#' "$archversion_conf" \ archversion_packages=$(grep -vE '^\s*#' "$archversion_conf" \
| grep -v '[DEFAULT]' \ | grep -v '[DEFAULT]' \
| egrep '^\s*\[[^]]*\]' \ | grep -E '^\s*\[[^]]*\]' \
| sed 's|\[||;s|\]||') | sed 's|\[||;s|\]||')
# Create a list of packages in the package root directory that aren't in any of the above lists # Create a list of packages in the package root directory that aren't in any of the above lists
check_missing_pkgs=$( check_missing_pkgs=$(
cd "$package_rootdir" cd "$package_rootdir" || exit
for pkg in *; do for pkg in *; do
[[ -f "$pkg/PKGBUILD" ]] && { [[ -f "$pkg/PKGBUILD" ]] && {
_pkg=$(sed 's|.*\/||' <<< "$pkg") _pkg=$(sed 's|.*\/||' <<< "$pkg")
! egrep -q "^$_pkg$" <<< "$archversion_packages" \
&& ! egrep -q "^$_pkg$" <<< "$develversion_packages" \ ! grep -qE "^$_pkg$" <<< "$archversion_packages" \
&& ! egrep -q "^$_pkg$" <<< "$specialversion_packages" \ && ! grep -qE "^$_pkg$" <<< "$develversion_packages" \
&& ! egrep -q "^$_pkg$" <<< "$noversion_packages" \ && ! grep -qE "^$_pkg$" <<< "$specialversion_packages" \
&& ! grep -qE "^$_pkg$" <<< "$noversion_packages" \
&& printf '%s\n' "$_pkg" && printf '%s\n' "$_pkg"
} }
done done
cd "$script_directory"
cd "$script_directory" || exit
) )
# Display information about any packages missing from all the package lists # Display information about any packages missing from all the package lists
printf '%s' "$c_blue[${c_white}missing-packages$c_blue]$c_reset: " printf '%s' "${c_blue}[${c_white}missing-packages$c_blue]$c_reset: "
if [[ -n "$check_missing_pkgs" ]]; then if [[ -n "$check_missing_pkgs" ]]; then
# Display how many packages are missing # Display how many packages are missing
printf '%s\n' "$c_red$(wc -l <<< "$check_missing_pkgs")$c_reset" printf '%s\n' "$c_red$(wc -l <<< "$check_missing_pkgs")$c_reset"
@ -342,7 +345,7 @@ check_missingpkgs=0 archversion_debug=0 only_newpkgs=0
# Parse command-line arguments # Parse command-line arguments
for param in "$@"; do for param in "$@"; do
if egrep -q '^-[a-z][a-z]' <<< "$param"; then if grep -qE '^-[a-z][a-z]' <<< "$param"; then
# Parse the argument character by character # Parse the argument character by character
while read -r char; do while read -r char; do
paramparse "$char" paramparse "$char"
@ -369,5 +372,4 @@ develversion_check
specialpkg_check specialpkg_check
# Cleanup the temp folder # Cleanup the temp folder
[[ -d "$temp_directory" ]] \ [[ -d "$temp_directory" ]] && rm -rf "$temp_directory"
&& rm -rf "$temp_directory"