Combine repo-add and repo-remove into one script

They shared about 75% of their code, so there is no real reason we should
maintain them separately. Merge the differences accordingly and add a check
based on the basename of the command used to decide what behavior to follow.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2008-06-14 11:29:29 -05:00
parent 89819b3f92
commit a1dfa8e61f
4 changed files with 76 additions and 245 deletions

View File

@ -16,4 +16,3 @@ src/pacman/util.c
scripts/makepkg.sh.in scripts/makepkg.sh.in
scripts/pacman-optimize.sh.in scripts/pacman-optimize.sh.in
scripts/repo-add.sh.in scripts/repo-add.sh.in
scripts/repo-remove.sh.in

View File

@ -2,18 +2,20 @@
AUTOMAKE_OPTIONS = std-options AUTOMAKE_OPTIONS = std-options
bin_SCRIPTS = \ bin_SCRIPTS = \
$(OURSCRIPTS) \
repo-remove
OURSCRIPTS = \
makepkg \ makepkg \
pacman-optimize \ pacman-optimize \
rankmirrors \ rankmirrors \
repo-add \ repo-add
repo-remove
EXTRA_DIST = \ EXTRA_DIST = \
makepkg.sh.in \ makepkg.sh.in \
pacman-optimize.sh.in \ pacman-optimize.sh.in \
rankmirrors.py.in \ rankmirrors.py.in \
repo-add.sh.in \ repo-add.sh.in
repo-remove.sh.in
# Files that should be removed, but which Automake does not know. # Files that should be removed, but which Automake does not know.
MOSTLYCLEANFILES = $(bin_SCRIPTS) *.tmp MOSTLYCLEANFILES = $(bin_SCRIPTS) *.tmp
@ -42,7 +44,7 @@ edit = sed \
## wrong file by accident. ## wrong file by accident.
# two 'test' lines- make sure we can handle both sh and py type scripts # two 'test' lines- make sure we can handle both sh and py type scripts
# third 'test' line- make sure one of the two checks succeeded # third 'test' line- make sure one of the two checks succeeded
$(bin_SCRIPTS): Makefile $(OURSCRIPTS): Makefile
rm -f $@ $@.tmp rm -f $@ $@.tmp
test -f $(srcdir)/$@.sh.in && $(edit) $(srcdir)/$@.sh.in >$@.tmp || true test -f $(srcdir)/$@.sh.in && $(edit) $(srcdir)/$@.sh.in >$@.tmp || true
test -f $(srcdir)/$@.py.in && $(edit) $(srcdir)/$@.py.in >$@.tmp || true test -f $(srcdir)/$@.py.in && $(edit) $(srcdir)/$@.py.in >$@.tmp || true
@ -55,7 +57,7 @@ makepkg: $(srcdir)/makepkg.sh.in
pacman-optimize: $(srcdir)/pacman-optimize.sh.in pacman-optimize: $(srcdir)/pacman-optimize.sh.in
rankmirrors: $(srcdir)/rankmirrors.py.in rankmirrors: $(srcdir)/rankmirrors.py.in
repo-add: $(srcdir)/repo-add.sh.in repo-add: $(srcdir)/repo-add.sh.in
repo-remove: $(srcdir)/repo-remove.sh.in repo-remove: $(srcdir)/repo-add.sh.in
re-pacman: $(srcdir)/re-pacman.sh.in ln -s repo-add repo-remove
# vim:set ts=2 sw=2 noet: # vim:set ts=2 sw=2 noet:

View File

@ -1,9 +1,11 @@
#!/bin/bash #!/bin/bash
# #
# repo-add - add a package to a given repo database file # repo-add - add a package to a given repo database file
# repo-remove - remove a package entry from a given repo database file
# @configure_input@ # @configure_input@
# #
# Copyright (c) 2006-2008 Aaron Griffin <aaron@archlinux.org> # Copyright (c) 2006-2008 Aaron Griffin <aaron@archlinux.org>
# Copyright (c) 2007-2008 Dan McGee <dan@archlinux.org>
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -55,33 +57,41 @@ error() {
# print usage instructions # print usage instructions
usage() { usage() {
printf "repo-add (pacman) %s\n\n" "$myver" printf "repo-add, repo-remove (pacman) %s\n\n" "$myver"
printf "$(gettext "Usage: %s [-q] <path-to-db> <package> ...\n\n")" "$0" printf "$(gettext "Usage: repo-add [-q] <path-to-db> <package> ...\n")"
printf "$(gettext "Usage: repo-remove [-q] <path-to-db> <packagename> ...\n\n")"
printf "$(gettext "\ printf "$(gettext "\
repo-add will update a package database by reading a package file.\n\ repo-add will update a package database by reading a package file.\n\
Multiple packages to add can be specified on the command line.\n\n")" Multiple packages to add can be specified on the command line.\n\n")"
printf "$(gettext "\ printf "$(gettext "\
The -q/--quiet flag will force this program to run silently except\n\ repo-remove will update a package database by removing the package name\n\
specified on the command line from the given repo database. Multiple\n\
packages to remove can be specified on the command line.\n\n")"
printf "$(gettext "\
The -q/--quiet flag to either program will force silent running except\n\
in the case of warnings or errors.\n\n")" in the case of warnings or errors.\n\n")"
echo "$(gettext "Example: repo-add /path/to/repo.db.tar.gz pacman-3.0.0.pkg.tar.gz")" echo "$(gettext "Example: repo-add /path/to/repo.db.tar.gz pacman-3.0.0.pkg.tar.gz")"
echo "$(gettext "Example: repo-remove /path/to/repo.db.tar.gz kernel26")"
} }
version() { version() {
printf "repo-add (pacman) %s\n" "$myver" printf "repo-add, repo-remove (pacman) %s\n\n" "$myver"
printf "$(gettext "\ printf "$(gettext "\
Copyright (C) 2006-2008 Aaron Griffin <aaron@archlinux.org>.\n\n\ Copyright (C) 2006-2008 Aaron Griffin <aaron@archlinux.org>.\n\
Copyright (c) 2007-2008 Dan McGee <dan@archlinux.org>.\n\n\
This is free software; see the source for copying conditions.\n\ This is free software; see the source for copying conditions.\n\
There is NO WARRANTY, to the extent permitted by law.\n")" There is NO WARRANTY, to the extent permitted by law.\n")"
} }
# test if a file is a repository DB # test if a file is a repository DB
# arg1 - command name (repo-add, repo-remove)
test_repo_db_file () { test_repo_db_file () {
if [ -f "$REPO_DB_FILE" ]; then if [ -f "$REPO_DB_FILE" ]; then
if bsdtar -tf "$REPO_DB_FILE" | grep -q "/desc"; then if bsdtar -tf "$REPO_DB_FILE" | grep -q "/desc"; then
return 0 # YES return 0 # YES
fi fi
else elif [ "$1" == "repo-add" ]; then
return 0 # YES - No database file is also allowed return 0 # YES - No database file is also allowed if we are adding
fi fi
return 1 # NO return 1 # NO
@ -177,14 +187,8 @@ db_write_entry()
return 1 return 1
fi fi
# remove any other package in the DB with same name # remove an existing entry if it exists, ignore failures
local existing db_remove_entry "$pkgname"
for existing in *; do
if [ "${existing%-*-*}" = "$pkgname" ]; then
msg2 "$(gettext "Removing existing package '%s'...")" "$existing"
rm -rf "$existing"
fi
done
# create package directory # create package directory
mkdir "$pkgname-$pkgver" mkdir "$pkgname-$pkgver"
@ -251,6 +255,23 @@ db_write_entry()
popd 2>&1 >/dev/null popd 2>&1 >/dev/null
} # end db_write_entry } # end db_write_entry
# remove existing entries from the DB
# arg1 - package name
db_remove_entry() {
pushd "$gstmpdir" 2>&1 >/dev/null
# remove any other package in the DB with same name
local existing
for existing in *; do
if [ "${existing%-*-*}" = "$1" ]; then
msg2 "$(gettext "Removing existing package '%s'...")" "$existing"
rm -rf "$existing"
fi
done
popd 2>&1 >/dev/null
} # end db_remove_entry
# PROGRAM START # PROGRAM START
# determine whether we have gettext; make it a no-op if we do not # determine whether we have gettext; make it a no-op if we do not
@ -301,10 +322,17 @@ if [ -r ~/.makepkg.conf ]; then
fi fi
# main routine # main routine
gstmpdir=$(mktemp -d /tmp/repo-add.XXXXXXXXXX) || (\ gstmpdir=$(mktemp -d /tmp/repo-tools.XXXXXXXXXX) || (\
error "$(gettext "Cannot create temp directory for database building.")"; \ error "$(gettext "Cannot create temp directory for database building.")"; \
exit 1) exit 1)
# figure out what program we are
cmd="$(basename $0)"
if [ "$cmd" != "repo-add" -a "$cmd" != "repo-remove" ]; then
error "$(gettext "Invalid command name '%s' specified.")" "$cmd"
exit 1
fi
success=0 success=0
# parse arguments # parse arguments
for arg in "$@"; do for arg in "$@"; do
@ -316,7 +344,7 @@ for arg in "$@"; do
elif [ -z "$REPO_DB_FILE" ]; then elif [ -z "$REPO_DB_FILE" ]; then
# store absolute path to repo DB # store absolute path to repo DB
REPO_DB_FILE=$($realpath "$arg") REPO_DB_FILE=$($realpath "$arg")
if ! test_repo_db_file; then if ! test_repo_db_file $cmd; then
error "$(gettext "Repository file '%s' is not a proper pacman database.")" "$REPO_DB_FILE" error "$(gettext "Repository file '%s' is not a proper pacman database.")" "$REPO_DB_FILE"
exit 1 exit 1
elif [ -f "$REPO_DB_FILE" ]; then elif [ -f "$REPO_DB_FILE" ]; then
@ -324,18 +352,28 @@ for arg in "$@"; do
bsdtar -xf "$REPO_DB_FILE" -C "$gstmpdir" bsdtar -xf "$REPO_DB_FILE" -C "$gstmpdir"
fi fi
else else
if [ -f "$arg" ]; then if [ "$cmd" == "repo-add" ]; then
if ! bsdtar -tf "$arg" .PKGINFO 2>&1 >/dev/null; then if [ -f "$arg" ]; then
error "$(gettext "'%s' is not a package file, skipping")" "$arg" if ! bsdtar -tf "$arg" .PKGINFO 2>&1 >/dev/null; then
else error "$(gettext "'%s' is not a package file, skipping")" "$arg"
msg "$(gettext "Adding package '%s'")" "$arg" else
msg "$(gettext "Adding package '%s'")" "$arg"
if db_write_entry "$arg"; then if db_write_entry "$arg"; then
success=1 success=1
fi
fi fi
else
error "$(gettext "Package '%s' not found.")" "$arg"
fi
elif [ "$cmd" == "repo-remove" ]; then
msg "$(gettext "Searching for package '%s'...")" "$arg"
if db_remove_entry "$arg"; then
success=1
else
error "$(gettext "Package matching '%s' not found.")" "$arg"
fi fi
else
error "$(gettext "Package '%s' not found.")" "$arg"
fi fi
fi fi
done done
@ -356,6 +394,9 @@ if [ $success -eq 1 ]; then
esac esac
bsdtar -c${TAR_OPT}f "$REPO_DB_FILE" * bsdtar -c${TAR_OPT}f "$REPO_DB_FILE" *
elif [ "$cmd" == "repo-remove" ]; then
error "$(gettext "All packages have been removed from the database. Deleting '%s'.")" "$REPO_DB_FILE"
rm "$REPO_DB_FILE"
fi fi
popd 2>&1 >/dev/null popd 2>&1 >/dev/null

View File

@ -1,211 +0,0 @@
#!/bin/bash
#
# repo-remove - remove a package entry from a given repo database file
# @configure_input@
#
# Copyright (c) 2007-2008 Dan McGee <dan@archlinux.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# gettext initialization
export TEXTDOMAIN='pacman'
export TEXTDOMAINDIR='@localedir@'
myver='@PACKAGE_VERSION@'
confdir='@sysconfdir@'
QUIET=0
REPO_DB_FILE=""
msg() {
[ $QUIET -ne 0 ] && return
local mesg=$1; shift
printf "==> ${mesg}\n" "$@" >&1
}
msg2() {
[ $QUIET -ne 0 ] && return
local mesg=$1; shift
printf " -> ${mesg}\n" "$@" >&1
}
warning() {
local mesg=$1; shift
printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
}
error() {
local mesg=$1; shift
printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
}
# print usage instructions
usage() {
printf "$(gettext "repo-remove %s\n\n")" $myver
printf "$(gettext "usage: %s [-q] <path-to-db> <packagename> ...\n\n")" "$0"
printf "$(gettext "\
repo-remove will update a package database by removing the package name\n\
specified on the command line from the given repo database. Multiple\n\
packages to remove can be specified on the command line.\n\n")"
printf "$(gettext "\
The -q/--quiet flag will force this program to run silently except\n\
in the case of warnings or errors.\n\n")"
echo "$(gettext "Example: repo-remove /path/to/repo.db.tar.gz kernel26")"
}
version() {
printf "repo-remove (pacman) %s\n" "$myver"
printf "$(gettext "\
Copyright (c) 2007-2008 Dan McGee <dan@archlinux.org>.\n\n\
This is free software; see the source for copying conditions.\n\
There is NO WARRANTY, to the extent permitted by law.\n")"
}
# test if a file is a repository DB
test_repo_db_file () {
if [ -f "$REPO_DB_FILE" ]; then
if bsdtar -tf "$REPO_DB_FILE" | grep -q "/desc"; then
return 0 # YES
fi
fi
return 1 # NO
}
# remove existing entries from the DB
db_remove_entry() {
pushd "$gstmpdir" 2>&1 >/dev/null
# remove any other package in the DB with same name
local existing
for existing in *; do
if [ "${existing%-*-*}" = "$1" ]; then
msg2 "$(gettext "Removing existing package '%s'...")" "$existing"
rm -rf "$existing"
fi
done
popd 2>&1 >/dev/null
} # end db_remove_entry
# PROGRAM START
# determine whether we have gettext; make it a no-op if we do not
if [ ! $(type -t gettext) ]; then
gettext() {
echo "$@"
}
fi
# check for help flags
if [ "$1" = "-h" -o "$1" = "--help" ]; then
usage
exit 0
fi
# check for version flags
if [ "$1" = "-V" -o "$1" = "--version" ]; then
version
exit 0
fi
# check for correct number of args
if [ $# -lt 2 ]; then
usage
exit 1
fi
# check for and store the name of a realpath-like program
if [ $(type -t realpath) ]; then
realpath='realpath'
elif [ $(type -t readlink) ]; then
realpath='readlink -f'
else
error "$(gettext "Either realpath or readlink are required by repo-add.")"
exit 1 # $E_MISSING_PROGRAM
fi
# source system and user makepkg.conf
if [ -r "$confdir/makepkg.conf" ]; then
source "$confdir/makepkg.conf"
else
error "$(gettext "%s not found. Cannot continue.")" "$confdir/makepkg.conf"
exit 1 # $E_CONFIG_ERROR
fi
if [ -r ~/.makepkg.conf ]; then
source ~/.makepkg.conf
fi
# main routine
gstmpdir=$(mktemp -d /tmp/repo-remove.XXXXXXXXXX) || (\
error "$(gettext "Cannot create temp directory for database building.")"; \
exit 1)
success=0
# parse arguments
for arg in "$@"; do
if [ "$arg" == "--quiet" -o "$arg" == "-q" ]; then
QUIET=1
elif [ -z "$REPO_DB_FILE" ]; then
# store absolute path to repo DB
REPO_DB_FILE=$($realpath "$arg")
if ! test_repo_db_file; then
error "$(gettext "Repository file '%s' is not a proper pacman database.")" "$REPO_DB_FILE"
exit 1
elif [ -f "$REPO_DB_FILE" ]; then
msg "$(gettext "Extracting database to a temporary location...")"
bsdtar -xf "$REPO_DB_FILE" -C "$gstmpdir"
fi
else
msg "$(gettext "Searching for package '%s'...")" "$arg"
if db_remove_entry "$arg"; then
success=1
else
error "$(gettext "Package matching '%s' not found.")" "$arg"
fi
fi
done
# if all operations were a success, re-zip database
if [ $success -eq 1 ]; then
msg "$(gettext "Creating updated database file '%s'")" "$REPO_DB_FILE"
pushd "$gstmpdir" 2>&1 >/dev/null
if [ -n "$(ls)" ]; then
[ -f "${REPO_DB_FILE}.old" ] && rm "${REPO_DB_FILE}.old"
[ -f "$REPO_DB_FILE" ] && mv "$REPO_DB_FILE" "${REPO_DB_FILE}.old"
case "$DB_COMPRESSION" in
gz) TAR_OPT="z" ;;
bz2) TAR_OPT="j" ;;
*) warning "$(gettext "No compression set.")" ;;
esac
bsdtar -c${TAR_OPT}f "$REPO_DB_FILE" *
else
error "$(gettext "All packages have been removed from the database. Deleting '%s'.")" "$REPO_DB_FILE"
rm "$REPO_DB_FILE"
fi
popd 2>&1 >/dev/null
else
msg "$(gettext "No packages modified, nothing to do.")"
fi
# remove the temp directory used to unzip
[ -d "$gstmpdir" ] && rm -rf "$gstmpdir"
# vim: set ts=2 sw=2 noet: