From 103c7243a2d50dd46c0b5efefdc2e1b1d24e30e0 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 29 May 2007 16:53:15 -0400 Subject: Get --help and --version options working on all scripts Added the autoconf option std-options to the scripts/ directory, which checks to ensure all programs have both --help and --version options. A few things needed cleaning up to get this working. To test these types of options, use the 'make distcheck' target. Signed-off-by: Dan McGee --- scripts/.gitignore | 1 + scripts/Makefile.am | 5 +- scripts/abs.in | 4 +- scripts/makepkg.in | 27 +++++-- scripts/makeworld.in | 88 +++++++++++----------- scripts/pacman-optimize.in | 40 +++++----- scripts/rankmirrors | 173 ------------------------------------------- scripts/rankmirrors.in | 178 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 272 insertions(+), 244 deletions(-) delete mode 100755 scripts/rankmirrors create mode 100644 scripts/rankmirrors.in (limited to 'scripts') diff --git a/scripts/.gitignore b/scripts/.gitignore index 58630324..17a2a41e 100644 --- a/scripts/.gitignore +++ b/scripts/.gitignore @@ -3,6 +3,7 @@ gensync makepkg makeworld pacman-optimize +rankmirrors repo-add repo-remove updatesync diff --git a/scripts/Makefile.am b/scripts/Makefile.am index ddacb0e6..45f0957d 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -1,6 +1,5 @@ -# TODO: this would be nice to enable, but we need --help and --version -# flags on all of our scripts first (this is what it checks) -#AUTOMAKE_OPTIONS = std-options +# enforce that all scripts have a --help and --version option +AUTOMAKE_OPTIONS = std-options bin_SCRIPTS = \ abs \ diff --git a/scripts/abs.in b/scripts/abs.in index b30c1752..7ed677d4 100644 --- a/scripts/abs.in +++ b/scripts/abs.in @@ -67,12 +67,12 @@ update() { done } -if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then +if [ "$1" = "-h" -o "$1" = "--help" ]; then usage exit 0 fi -if [ "$1" = "-V" ] || [ "$1" = "--version" ]; then +if [ "$1" = "-V" -o "$1" = "--version" ]; then version exit 0 fi diff --git a/scripts/makepkg.in b/scripts/makepkg.in index 0d147041..2884e5db 100644 --- a/scripts/makepkg.in +++ b/scripts/makepkg.in @@ -600,7 +600,7 @@ installpackage() { } usage() { - printf "$(gettext "makepkg version %s")\n" "$myver" + printf "$(gettext "makepkg (pacman) %s")\n" "$myver" echo printf "$(gettext "Usage: %s [options]")\n" "$0" echo @@ -633,6 +633,15 @@ usage() { echo } +version() { + printf "$(gettext "makepkg (pacman) %s")\n" "$myver" + echo "$(gettext "Copyright (C) 2002-2007 Judd Vinet .")" + echo + echo "$(gettext "This is free software; see the source for copying conditions.")" + echo "$(gettext "There is NO WARRANTY, to the extent permitted by law.")" + echo +} + ARGLIST=$@ #preserve environment variables @@ -684,6 +693,10 @@ while [ "$#" -ne "0" ]; do usage exit 0 ;; + --version) + version + exit 0 + ;; --*) usage exit 1 @@ -699,10 +712,6 @@ while [ "$#" -ne "0" ]; do f) FORCE=1 ;; F) INFAKEROOT=1 ;; g) GENINTEG=1 ;; - h) - usage - exit 0 - ;; i) INSTALL=1 ;; L) LOGGING=1 ;; m) USE_COLOR="n" ;; @@ -712,6 +721,14 @@ while [ "$#" -ne "0" ]; do R) REPKG=1 ;; s) DEP_BIN=1 ;; S) SUDO=1 ;; + h) + usage + exit 0 + ;; + V) + version + exit 0 + ;; -) OPTIND=0 break diff --git a/scripts/makeworld.in b/scripts/makeworld.in index 8783fc9d..ca5b7261 100644 --- a/scripts/makeworld.in +++ b/scripts/makeworld.in @@ -58,7 +58,7 @@ version() { printf "Copyright (C) 2002-2007 Judd Vinet .\n" echo printf "This is free software; see the source for copying conditions.\n" - printf "There is NO WARRANTY, to the extent permitted by the law.\n" + printf "There is NO WARRANTY, to the extent permitted by law.\n" echo } @@ -83,53 +83,53 @@ for arg in $*; do --nobuild) MAKEPKG_OPTS="$MAKEPKG_OPTS -o" ;; --nocolor) MAKEPKG_OPTS="$MAKEPKG_OPTS -m" ;; --help) - usage - exit 0 - ;; + usage + exit 0 + ;; --version) - version - exit 0 - ;; + version + exit 0 + ;; --*) - usage - exit 1 - ;; + usage + exit 1 + ;; -*) - while getopts "chisSbdfrBCemoSV-" opt; do - case $opt in - c) MAKEPKG_OPTS="$MAKEPKG_OPTS -c" ;; - i) MAKEPKG_OPTS="$MAKEPKG_OPTS -i" ;; - s) MAKEPKG_OPTS="$MAKEPKG_OPTS -s" ;; - b) MAKEPKG_OPTS="$MAKEPKG_OPTS -b" ;; - d) MAKEPKG_OPTS="$MAKEPKG_OPTS -d" ;; - f) MAKEPKG_OPTS="$MAKEPKG_OPTS -f" ;; - r) MAKEPKG_OPTS="$MAKEPKG_OPTS -r" ;; - B) MAKEPKG_OPTS="$MAKEPKG_OPTS -B" ;; - C) MAKEPKG_OPTS="$MAKEPKG_OPTS -C" ;; - e) MAKEPKG_OPTS="$MAKEPKG_OPTS -e" ;; - m) MAKEPKG_OPTS="$MAKEPKG_OPTS -m" ;; - o) MAKEPKG_OPTS="$MAKEPKG_OPTS -o" ;; - S) MAKEPKG_OPTS="$MAKEPKG_OPTS -S" ;; - h) - usage - exit 0 - ;; - V) - version - exit 0 - ;; - -) - OPTIND=0 - break - ;; - esac - done - ;; + while getopts "chisSbdfrBCemoSV-" opt; do + case $opt in + c) MAKEPKG_OPTS="$MAKEPKG_OPTS -c" ;; + i) MAKEPKG_OPTS="$MAKEPKG_OPTS -i" ;; + s) MAKEPKG_OPTS="$MAKEPKG_OPTS -s" ;; + b) MAKEPKG_OPTS="$MAKEPKG_OPTS -b" ;; + d) MAKEPKG_OPTS="$MAKEPKG_OPTS -d" ;; + f) MAKEPKG_OPTS="$MAKEPKG_OPTS -f" ;; + r) MAKEPKG_OPTS="$MAKEPKG_OPTS -r" ;; + B) MAKEPKG_OPTS="$MAKEPKG_OPTS -B" ;; + C) MAKEPKG_OPTS="$MAKEPKG_OPTS -C" ;; + e) MAKEPKG_OPTS="$MAKEPKG_OPTS -e" ;; + m) MAKEPKG_OPTS="$MAKEPKG_OPTS -m" ;; + o) MAKEPKG_OPTS="$MAKEPKG_OPTS -o" ;; + S) MAKEPKG_OPTS="$MAKEPKG_OPTS -S" ;; + h) + usage + exit 0 + ;; + V) + version + exit 0 + ;; + -) + OPTIND=0 + break + ;; + esac + done + ;; *) - dest=$arg - shift - break - ;; + dest=$arg + shift + break + ;; esac shift if [ "$dest" != "" ]; then diff --git a/scripts/pacman-optimize.in b/scripts/pacman-optimize.in index 2bfec40e..cce4a5c1 100644 --- a/scripts/pacman-optimize.in +++ b/scripts/pacman-optimize.in @@ -21,6 +21,11 @@ # myver='@PACKAGE_VERSION@' +#TODO replace with configure-time db location +dbroot="/var/lib/pacman" +#TODO replace with configure-time lockfile location +lockfile="/tmp/pacman.lck" + error() { if [ "$USECOLOR" = "YES" -o "$USECOLOR" = "yes" ]; then @@ -30,11 +35,12 @@ error() { fi } -source /etc/rc.conf #for USECOLOR -source /etc/rc.d/functions +# determine current USECOLOR setting +#[ -f "/etc/rc.conf" ] && source /etc/rc.conf +#[ -f "/etc/rc.d/functions" ] && source /etc/rc.d/functions usage() { - echo "pacman-optimize $myver" + echo "pacman-optimize (pacman) $myver" echo "usage: $0 [pacman_db_root]" echo echo "pacman-optimize is a little hack that should improve the performance" @@ -64,26 +70,26 @@ die() { } die_r() { - rm -f /tmp/pacman.lck + rm -f $lockfile die $@ } -dbroot="/var/lib/pacman" +if [ "$1" = "-h" -o "$1" = "--help" ]; then + usage + exit 0 +fi + +if [ "$1" = "-V" -o "$1" = "--version" ]; then + version + exit 0 +fi if [ "$1" != "" ]; then - if [ "$1" = "-h" -o "$1" = "--help" ]; then - usage - exit 0 - fi - if [ "$1" = "-V" -o "$1" = "--version" ]; then - version - exit 0 - fi dbroot="$1" fi # make sure pacman isn't running -if [ -f /tmp/pacman.lck ]; then +if [ -f $lockfile ]; then die "Pacman lockfile was found. Cannot run while pacman is running." fi @@ -96,7 +102,7 @@ if [ ! -w "$dbroot" ]; then fi # do not let pacman run while we do this -touch /tmp/pacman.lck +touch $lockfile # step 1: sum the old db stat_busy "Md5sum'ing the old database" @@ -134,7 +140,7 @@ if [ $? -ne 0 ]; then # failed stat_fail # leave /tmp/pacsums.old and .new for checking to see what doesn't match up - rm -rf "$dbroot.new" /tmp/pacman.lck /tmp/pacmanDB.tgz + rm -rf "$dbroot.new" $lockfile /tmp/pacmanDB.tgz die_r "integrity check FAILED, reverting to old database" fi stat_done @@ -147,7 +153,7 @@ tar -zxpf /tmp/pacmanDB.tgz -C "$dbroot"/ stat_done # remove the lock file, sum files, and .tgz of database -rm -f /tmp/pacman.lck /tmp/pacsums.old /tmp/pacsums.new /tmp/pacmanDB.tgz +rm -f $lockfile /tmp/pacsums.old /tmp/pacsums.new /tmp/pacmanDB.tgz echo echo "Finished. Your pacman database has been optimized." diff --git a/scripts/rankmirrors b/scripts/rankmirrors deleted file mode 100755 index 3eb10b5a..00000000 --- a/scripts/rankmirrors +++ /dev/null @@ -1,173 +0,0 @@ -#! /usr/bin/python -# -# rankmirrors : read a list of mirrors from a file and rank them by speed -# -# Original Idea copyright (c) 2006 R.G. -# Modified 2006 by Dan McGee -# -# 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, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, -# USA. -# -import os, sys, datetime, time, socket, urllib2 -from optparse import OptionParser - -def createOptParser(): - usage = "usage: %prog [options] MIRRORFILE | URL" - description = "Ranks pacman mirrors by their connection and opening " \ - "speed. Pacman mirror files are located in /etc/pacman.d/. It " \ - "can also rank one mirror if the URL is provided." - parser = OptionParser(usage = usage, description = description) - parser.add_option("-n", type = "int", dest = "num", default = 0, - help = "number of servers to output, 0 for all") - parser.add_option("-t", "--times", action = "store_true", - dest = "times", default = False, - help = "only output mirrors and their response times") - parser.add_option("-u", "--url", action = "store_true", dest = "url", - default = False, help = "test a specific url") - parser.add_option("-v", "--verbose", action = "store_true", - dest = "verbose", default = False, - help = "be verbose in ouptut") - return parser - -def timeCmd(cmd): - before = time.time() - try: - cmd() - except KeyboardInterrupt, ki: - raise ki - except socket.timeout, ioe: - return 'timeout' - except Exception, e: - return 'unreachable' - return time.time() - before - -def talkToServer(serverUrl): - opener = urllib2.build_opener() - # retrieve first 50,000 bytes only - tmp = opener.open(serverUrl).read(50000) - -def getFuncToTime(serverUrl): - return lambda : talkToServer(serverUrl) - -def cmpPairBySecond(p1, p2): - if p1[1] == p2[1]: - return 0 - if p1[1] < p2[1]: - return -1 - return 1 - -def printResults(servers, time, verbose, num): - items = servers.items() - items.sort(cmpPairBySecond) - itemsLen = len(items) - numToShow = num - if numToShow > itemsLen or numToShow == 0: - numToShow = itemsLen - if itemsLen > 0: - if time: - print - print ' Servers sorted by time (seconds):' - for i in items[0:numToShow]: - if i[1] == 'timeout' or i[1] == 'unreachable': - print i[0], ':', i[1] - else: - print i[0], ':', "%.2f" % i[1] - else: - for i in items[0:numToShow]: - print 'Server =', i[0] - -if __name__ == "__main__": - parser = createOptParser() - (options, args) = parser.parse_args() - - if len(args) != 1: - parser.print_help(sys.stderr) - sys.exit(0) - - # allows connections to time out if they take too long - socket.setdefaulttimeout(10) - - if options.url: - if options.verbose: - print 'Testing', args[0] + '...' - try: - serverToTime = timeCmd(getFuncToTime(args[0])) - except KeyboardInterrupt, ki: - sys.exit(1) - if serverToTime == 'timeout' or serverToTime == 'unreachable': - print args[0], ':', serverToTime - else: - print args[0], ':', "%.2f" % serverToTime - sys.exit(0) - - if not os.path.isfile(args[0]): - print >>sys.stderr, 'rankmirrors: file', args[0], 'does not exist.' - sys.exit(1) - - fl = open(args[0], 'r') - serverToTime = {} - if options.times: - print 'Querying servers, this may take some time...' - else: - print "# Server list generated by rankmirrors on", - print datetime.date.today() - for ln in fl.readlines(): - splitted = ln.split('=') - if splitted[0].strip() != 'Server': - if not options.times: - print ln, - continue - - serverUrl = splitted[1].strip() - if serverUrl[-1] == '\n': - serverUrl = serverUrl[0:-1] - if options.verbose and options.times: - print serverUrl, '...', - elif options.verbose: - print '#', serverUrl, '...', - elif options.times: - print ' * ', - sys.stdout.flush() - - # add *.db.tar.gz to server name. the repo name is parsed - # from the mirror url; it is the third (or fourth) dir - # from the end, where the url is http://foo/bar/REPO/os/arch - try: - splitted2 = serverUrl.split('/') - if serverUrl[-1] != '/': - repoName = splitted2[-3] - dbFileName = '/' + repoName + '.db.tar.gz' - else: - repoName = splitted2[-4] - dbFileName = repoName + '.db.tar.gz' - except: - dbFileName = '' - - try: - serverToTime[serverUrl] = timeCmd(getFuncToTime(serverUrl + dbFileName)) - if options.verbose: - try: - print "%.2f" % serverToTime[serverUrl] - except: - print serverToTime[serverUrl] - except: - print - printResults(serverToTime, options.times, options.verbose, - options.num) - sys.exit(0) - - printResults(serverToTime, options.times, options.verbose, options.num) - -# vim: set ts=4 sw=4 et: diff --git a/scripts/rankmirrors.in b/scripts/rankmirrors.in new file mode 100644 index 00000000..da0811c0 --- /dev/null +++ b/scripts/rankmirrors.in @@ -0,0 +1,178 @@ +#! /usr/bin/python +# +# rankmirrors : read a list of mirrors from a file and rank them by speed +# +# Original Idea copyright (c) 2006 R.G. +# Modified 2006 by Dan McGee +# +# 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, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, +# USA. +# +import os, sys, datetime, time, socket, urllib2 +from optparse import OptionParser + +def createOptParser(): + usage = "usage: %prog [options] MIRRORFILE | URL" + version = '%prog (pacman) @PACKAGE_VERSION@' + description = "Ranks pacman mirrors by their connection and opening " \ + "speed. Pacman mirror files are located in /etc/pacman.d/. It " \ + "can also rank one mirror if the URL is provided." + parser = OptionParser(usage = usage, version = version, + description = description) + parser.add_option("-n", type = "int", dest = "num", default = 0, + help = "number of servers to output, 0 for all") + parser.add_option("-t", "--times", action = "store_true", + dest = "times", default = False, + help = "only output mirrors and their response times") + parser.add_option("-u", "--url", action = "store_true", dest = "url", + default = False, help = "test a specific url") + parser.add_option("-v", "--verbose", action = "store_true", + dest = "verbose", default = False, + help = "be verbose in ouptut") + # The following two options should be automatic + #parser.add_option("-h", "--help", action = "help") + #parser.add_option("-V", "--version", action = "version") + return parser + +def timeCmd(cmd): + before = time.time() + try: + cmd() + except KeyboardInterrupt, ki: + raise ki + except socket.timeout, ioe: + return 'timeout' + except Exception, e: + return 'unreachable' + return time.time() - before + +def talkToServer(serverUrl): + opener = urllib2.build_opener() + # retrieve first 50,000 bytes only + tmp = opener.open(serverUrl).read(50000) + +def getFuncToTime(serverUrl): + return lambda : talkToServer(serverUrl) + +def cmpPairBySecond(p1, p2): + if p1[1] == p2[1]: + return 0 + if p1[1] < p2[1]: + return -1 + return 1 + +def printResults(servers, time, verbose, num): + items = servers.items() + items.sort(cmpPairBySecond) + itemsLen = len(items) + numToShow = num + if numToShow > itemsLen or numToShow == 0: + numToShow = itemsLen + if itemsLen > 0: + if time: + print + print ' Servers sorted by time (seconds):' + for i in items[0:numToShow]: + if i[1] == 'timeout' or i[1] == 'unreachable': + print i[0], ':', i[1] + else: + print i[0], ':', "%.2f" % i[1] + else: + for i in items[0:numToShow]: + print 'Server =', i[0] + +if __name__ == "__main__": + parser = createOptParser() + (options, args) = parser.parse_args() + + if len(args) != 1: + parser.print_help(sys.stderr) + sys.exit(0) + + # allows connections to time out if they take too long + socket.setdefaulttimeout(10) + + if options.url: + if options.verbose: + print 'Testing', args[0] + '...' + try: + serverToTime = timeCmd(getFuncToTime(args[0])) + except KeyboardInterrupt, ki: + sys.exit(1) + if serverToTime == 'timeout' or serverToTime == 'unreachable': + print args[0], ':', serverToTime + else: + print args[0], ':', "%.2f" % serverToTime + sys.exit(0) + + if not os.path.isfile(args[0]): + print >>sys.stderr, 'rankmirrors: file', args[0], 'does not exist.' + sys.exit(1) + + fl = open(args[0], 'r') + serverToTime = {} + if options.times: + print 'Querying servers, this may take some time...' + else: + print "# Server list generated by rankmirrors on", + print datetime.date.today() + for ln in fl.readlines(): + splitted = ln.split('=') + if splitted[0].strip() != 'Server': + if not options.times: + print ln, + continue + + serverUrl = splitted[1].strip() + if serverUrl[-1] == '\n': + serverUrl = serverUrl[0:-1] + if options.verbose and options.times: + print serverUrl, '...', + elif options.verbose: + print '#', serverUrl, '...', + elif options.times: + print ' * ', + sys.stdout.flush() + + # add *.db.tar.gz to server name. the repo name is parsed + # from the mirror url; it is the third (or fourth) dir + # from the end, where the url is http://foo/bar/REPO/os/arch + try: + splitted2 = serverUrl.split('/') + if serverUrl[-1] != '/': + repoName = splitted2[-3] + dbFileName = '/' + repoName + '.db.tar.gz' + else: + repoName = splitted2[-4] + dbFileName = repoName + '.db.tar.gz' + except: + dbFileName = '' + + try: + serverToTime[serverUrl] = timeCmd(getFuncToTime(serverUrl + dbFileName)) + if options.verbose: + try: + print "%.2f" % serverToTime[serverUrl] + except: + print serverToTime[serverUrl] + except: + print + printResults(serverToTime, options.times, options.verbose, + options.num) + sys.exit(0) + + printResults(serverToTime, options.times, options.verbose, options.num) + +# vim: set ts=4 sw=4 et: -- cgit v1.2.3-70-g09d2