From 3d087845f1e6d12dada8f8244956a3e3a622371c Mon Sep 17 00:00:00 2001 From: tsimpson Date: Mon, 9 Apr 2012 17:28:49 +0100 Subject: [PATCH] Update PackageInfo scripts to be a little more useful and robust. Also, in a break from character, I added some documentation. --- PackageInfo/update_apt | 147 +++++++++++++++++++++++++++++------- PackageInfo/update_apt_file | 140 +++++++++++++++++++++++++--------- 2 files changed, 223 insertions(+), 64 deletions(-) diff --git a/PackageInfo/update_apt b/PackageInfo/update_apt index 6b1bb66..fb1872f 100755 --- a/PackageInfo/update_apt +++ b/PackageInfo/update_apt @@ -1,51 +1,142 @@ #!/usr/bin/env bash -# Set DIR to the same value as supybot.plugins.PackageInfo.aptdir -if [ -z "$DIR" ]; then - DIR=/home/bot/aptdir -fi +# Either set DIR to the same value as supybot.plugins.PackageInfo.aptdir, +# or use the --dir command-line option. +DIR="" -# Be quiet bt default -DEFAULT_OPTS="-qq" +# Be quiet by default. +VERBOSE=0 + + +## Please don't change anything below this line, unless you really know what +## you are doing and don't bother me with whatever errors it produces :) + +# Print usage information. +usage() { + echo "Usage $0 [OPTION]..." + echo "Updates the APT package cache for PackageInfo" + echo "" + echo "-h, --help Display this message and exit." + echo "-v, --verbose Be more verbose than normal." + echo "-V, --very-verbose Be even more verbose than normal." + echo "-d, --dir[=DIR] Sets the directory to use when updating the APT package cache." + echo "" + echo "Note:" + echo " Please separate each option with a space, eg:" + echo " $0 -v -d /home/bot/aptdir" + echo " Rather than:" + echo " $0 -vd /home/bot/aptdir" + echo "" + echo "This script is intended to be ran automatically (eg: cron), so it shows no output by default." + echo "You can make the script more verbose with either the -v/--verbose or -V/--very-verbose options." + echo "The -d/--dir option sets the directory where this script looks for *.list files for apt-get." +} + +# Prints an error message, usage (above), then exit with the specified exit value. +error() { + local exit_val=$1 + shift + echo $@ >&2 + usage >&2 + exit $exit_val +} + +# Runs apt-get update in the specified directory for the specified distribution. +update_apt() { + local apt_dir="$1" + local dist="$2" + local apt_args="" + + if [ $VERBOSE -eq 0 ]; then + apt_args="-qq" + elif [ $VERBOSE -eq 1 ]; then + apt_args="-q" + fi + + apt-get $apt_args -o="APT::Architecture=i386" \ + -o="APT::Architectures::=i386" \ + -o "APT::Architectures::=amd64" \ + -o="Dir::State::Lists=$apt_dir/$dist" \ + -o="Dir::State::Status=$apt_dir/$dist.status" \ + -o="Dir::Cache=$apt_dir/cache" \ + -o="Dir::Etc::SourceList=$apt_dir/$dist.list" \ + update + + return $? +} + +# main() + +# Acepted arguments are: +# -h,--help +# -v,--verbose +# -V,--very-verbose +# -d,--dir[=DIR] # Check command-line arguments -while [ "x$1" != "x" ]; do +while [ $# -gt 0 ]; do case "$1" in + -h|--help) + usage + exit 0 + ;; -v|--verbose) - DEFAULT_OPTS="-q" + VERBOSE=1 ;; -V|--very-verbose) - DEFAULT_OPTS="" + VERBOSE=2 ;; -d|--dir) - if [ "x$2" == "x" ]; then - echo "\"-d|--dir\" requires an argument" >&2 - exit 1 - fi + [ -z "$2" ] && error 1 "\"-d|--dir\" requires an argument." shift DIR="$1" ;; + --dir=*) + DIR="${1:6}" + [ -z "$DIR" ] && error 1 "\"--dir\" requires an argument." + ;; -*) - echo "Unknown option \"$1\"" >&2 - exit 1 + error 1 "Unknown option \"$1\"." ;; *) - echo "This script takes no non-argument parameters" >&2 - exit 1 + error 1 "This script takes no non-argument parameters." ;; esac shift done -for DIST in "$DIR"/*.list; do - test -h $DIST && continue - DIST=${DIST:${#DIR}} - DIST=${DIST/.list} - touch "$DIR/$DIST.status" - mkdir -p "$DIR/$DIST/partial" - apt-get ${DEFAULT_OPTS} -o "Dir::State::Lists=$DIR/$DIST" \ - -o "Dir::etc::sourcelist=$DIR/$DIST.list" \ - -o "Dir::State::status=$DIR/$DIST.status" \ - -o "Dir::Cache=$DIR/cache" \ - -o "APT::Architecture=i386" update +apt_get=$(which apt-get 2>/dev/null) + +# Check that apt-get exists and bail if it doesn't. +if [ $? -ne 0 ]; then + echo "ERROR: apt-get not found. Please install apt-get in your \$PATH." >&2 + exit 1 +fi + +#TODO: Remove this section and error out if DIR is not set, +# This could hide errors where DIR/-d was not set, an error message. +if [ -z "$DIR" ]; then + DIR=/home/bot/aptdir + echo "WARNING: No DIR set and no -d/--dir option given, defaulting to \"$DIR\"" >&2 + echo "WARNING: Please set DIR on line 5 of $(readlink -f $0) or use the -d/--dir option" >&2 +fi + +#[ -z "$DIR" ] && error 1 "ERROT: Please set DIR on line 5 of $(readlink -f $0) or use the -d/--dir option" + +DIR="$(echo $DIR | sed 's,/*$,,')" # Normalize $DIR + +items=$(ls "${DIR}"/*.list 2>/dev/null) +[ $? -ne 0 ] && error 1 "Could not find \"*.list\" files in \"$DIR\"." + +for DIST in $items; do + [ -h "$DIST" ] && continue # Ignore symbolic links + # Extract the distribution name from the .list file name. + DIST="${DIST:${#DIR}}" + DIST="${DIST/.list}" + DIST="${DIST:1}" + + touch "${DIR}/${DIST}.status" # Create APT status file + mkdir -p "${DIR}/${DIST}/partial" # APT needs this to exist + update_apt "$DIR" "$DIST" # Update the package list with apt-get done + diff --git a/PackageInfo/update_apt_file b/PackageInfo/update_apt_file index d1a3fed..f6476b4 100755 --- a/PackageInfo/update_apt_file +++ b/PackageInfo/update_apt_file @@ -1,59 +1,127 @@ #!/usr/bin/env bash -if [ ! -x "$(which apt-file)" ]; then - echo "Please install apt-file" - exit 1 -fi - -# Set DIR to the same value as supybot.plugins.PackageInfo.aptdir -if [ -z "$DIR" ]; then - DIR=/home/bot/aptdir -fi +# Either set DIR to the same value as supybot.plugins.PackageInfo.aptdir, +# or use the --dir command-line option. +DIR="" # Be quiet by default -if [ -z "$VERBOSE" ]; then - VERBOSE="no" -fi +VERBOSE=0 + +## Please don't change anything below this line, unless you really know what +## you are doing and don't bother me with whatever errors it produces :) + +# Print usage information. +usage() { + echo "Usage $0 [OPTION]..." + echo "Updates the apt-file cache for PackageInfo" + echo "" + echo "-h, --help Display this message and exit." + echo "-v, --verbose Be more verbose than normal." + echo "-d, --dir[=DIR] Sets the directory to use when updating the apt-file cache." + echo "" + echo "Note:" + echo " Please separate each option with a space, eg:" + echo " $0 -v -d /home/bot/aptdir" + echo " Rather than:" + echo " $0 -vd /home/bot/aptdir" + echo "" + echo "This script is intended to be ran automatically (eg: cron), so it shows no output by default." + echo "You can make the script more verbose with the -v/--verbose option." + echo "The -d/--dir option sets the directory where this script looks for *.list files for apt-file." +} + +# Prints an error message, usage (above), then exit with the specified exit value. +error() { + local exit_val=$1 + shift + echo $@ >&2 + usage >&2 + exit $exit_val +} + +# Runs apt-file in the specified directory against the specified distribution. +update_apt() { + local DIST="$1" + if [ $VERBOSE -eq 0 ]; then + apt-file -N -l -c "$DIR/apt-file/$DIST" -s "$DIR/$DIST.list" -a i386 update >/dev/null 2>&1 + else + apt-file -N -l -c "$DIR/apt-file/$DIST" -s "$DIR/$DIST.list" -a i386 update + fi +} + +# main() + +# Acepted arguments are: +# -h,--help +# -v,--verbose +# -d,--dir[=DIR] # Check command-line arguments -while [ "x$1" != "x" ]; do +while [ $# -ne 0 ]; do case "$1" in + -h|--help) + usage + exit 0 + ;; -v|--verbose) - VERBOSE="yes" + VERBOSE=1 ;; -d|--dir) - if [ "x$2" == "x" ]; then - echo "\"-d|--dir\" requires an argument" >&2 - exit 1 - fi + [ -z "$2" ] && error 1 "\"-d|--dir\" requires an argument." shift DIR="$1" ;; + --dir=*) + DIR="${1:6}" + [ -z "$DIR" ] && error 1 "\"--dir\" requires an argument." + ;; -*) - echo "Unknown option \"$1\"" >&2 - exit 1 + error 1 "Unknown option \"$1\"." ;; *) - echo "This script takes no non-argument parameterss" >&2 - exit 1 + error 1 "This script takes no non-argument parameterss." ;; esac shift done -for DIST in "$DIR"/*.list; do - test -h $DIST && continue - DIST=${DIST:${#DIR}} - DIST=${DIST/.list} - mkdir -p "$DIR/apt-file/$DIST" - if [ "${VERBOSE}" != "no" ]; then - echo "Processing $DIST" - apt-file -l -c "$DIR/apt-file/$DIST" -s "$DIR/$DIST.list" update - else - apt-file -l -c "$DIR/apt-file/$DIST" -s "$DIR/$DIST.list" update >/dev/null 2>&1 - fi - RET=$? - if [ ! $RET ]; then - echo "apt-file failed for $DIST!" +apt_file=$(which apt-file 2>/dev/null) + +# Check if apt-file is installed and bail if it isn't. +if [ $? -ne 0 ]; then + echo "ERROR: apt-file not found. Please install apt-file in your \$PATH." >&2 + exit 1 +fi + +#TODO: Remove this and error out if DIR is not set, +# This is legacy code and needs to disappear sometime. +if [ -z "$DIR" ]; then + DIR=/home/bot/aptdir + echo "WARNING: No DIR set and no -d/--dir option given, defaulting to \"$DIR\"" >&2 + echo "WARNING: Please set DIR on line 5 of $(readlink -f $0) or use the -d/--dir option" >&2 +fi + +#[ -z "$DIR" ] && error 1 "Please set DIR on line 5 of $(readlink -f $0) or use the -d/--dir option" + +DIR="$(echo $DIR | sed 's,/*$,,')" # Normalize $DIR + +items=$(ls "${DIR}"/*.list 2>/dev/null) +[ $? -ne 0 ] && error 1 "ERROR: Could not find \"*.list\" files in \"$DIR\"." + +for DIST in $items; do + [ -h $DIST ] && continue # Ignore symbolic links + # Extract the distribution from the .list file name + DIST="${DIST:${#DIR}}" + DIST="${DIST/.list}" + DIST="${DIST:1}" + + mkdir -p "$DIR/apt-file/$DIST" # Create apt-file directory, if it doesn't exist + + [ $VERBOSE -ne 0 ] && echo "INFO: Processing $DIST" + update_apt "$DIST" # Update apt-file database + if [ $? -ne 0 ]; then + [ $VERBOSE -eq 0 ] && echo "Try passing -v to get the error message." >&2 + error 1 "ERROR: apt-file failed for ${DIST}!." fi done +