Resync all running ubottu code to bzr branch, they should now be in sync again

This commit is contained in:
Terence Simpson
2009-10-12 19:26:35 +01:00
parent 46a4ea34d2
commit 7791f6e416
39 changed files with 4983 additions and 440 deletions

View File

@ -14,7 +14,7 @@
import exceptions
import warnings
warnings.filterwarnings("ignore", "apt API not stable yet", exceptions.FutureWarning)
import commands, os, apt
import commands, os, apt, urllib
from email import FeedParser
def component(arg):
@ -23,11 +23,11 @@ def component(arg):
class Apt:
def __init__(self, plugin):
os.environ["LANG"] = "C" # Workaround issues with localized package descriptions
self.aptdir = plugin.registryValue('aptdir')
self.distros = []
self.plugin = plugin
self.log = plugin.log
os.environ["LANG"] = "C"
if self.aptdir:
self.distros = [x[:-5] for x in os.listdir(self.aptdir) if x.endswith('.list')]
self.distros.sort()
@ -38,21 +38,23 @@ class Apt:
-o"Dir::Cache=%s/cache"\\
-o"APT::Architecture=i386"\\
%%s %%s""" % tuple([self.aptdir]*4)
self.aptfilecommand = """apt-file -s %s/%%s.list -c %s/apt-file/%%s -l search %%s""" % tuple([self.aptdir]*2)
self.aptfilecommand = """apt-file -s %s/%%s.list -c %s/apt-file/%%s -l search %%s""" % (self.aptdir, self.aptdir)
def find(self, pkg, checkdists, filelookup=True):
_pkg = ''.join([x for x in pkg.strip().split(None,1)[0] if x.isalnum() or x in '.-_+'])
distro = checkdists
if len(pkg.strip().split()) > 1:
distro = ''.join([x for x in pkg.strip().split(None,2)[1] if x.isalnum or x in '.-_+'])
distro = ''.join([x for x in pkg.strip().split(None,2)[1] if x.isalnum() or x in '.-_+'])
if distro not in self.distros:
return "%s is not a valid distribution %s" % (distro, self.distros)
return "%s is not a valid distribution: %s" % (distro, ", ".join(self.distros))
pkg = _pkg
data = commands.getoutput(self.aptcommand % (distro, distro, distro, 'search -n', pkg))
#self.log.info("command output: %r" % data)
if not data:
if filelookup:
data = commands.getoutput(self.aptfilecommand % (distro, distro, pkg)).split()
#self.log.info("command output: %r" % ' '.join(data))
if data:
if data[0] == 'sh:': # apt-file isn't installed
self.log.error("apt-file is not installed")
@ -61,7 +63,7 @@ class Apt:
self.log.error("Please run the 'update_apt_file' script")
return "Cache out of date, please contact the administrator"
if data[0] == "Use" and data[1] == "of":
url = "http://packages.ubuntu.com/search?searchon=contents&keywords=%s&mode=&suite=%s&arch=any" % (urllib.quote(pkg),distro)
url = "http://packages.ubuntu.com/search?searchon=contents&keywords=%s&mode=&suite=%s&arch=any" % (urllib.quote(pkg), distro)
return url
if len(data) > 5:
return "File %s found in %s (and %d others)" % (pkg, ', '.join(data[:5]), len(data)-5)
@ -75,6 +77,8 @@ class Apt:
return "Found: %s" % ', '.join(pkgs[:5])
def info(self, pkg, checkdists):
if not pkg.strip():
return ''
_pkg = ''.join([x for x in pkg.strip().split(None,1)[0] if x.isalnum() or x in '.-_+'])
distro = checkdists
if len(pkg.strip().split()) > 1:
@ -82,7 +86,7 @@ class Apt:
if not distro:
distro = checkdists
if distro not in self.distros:
return "%s is not a valid distribution %s" % (distro, self.distros)
return "%r is not a valid distribution: %s" % (distro, ", ".join(self.distros))
checkdists = distro
@ -104,6 +108,8 @@ class Apt:
if type(p) == type(""):
self.log.error("apt returned an error, do you have the deb-src URLs in %s.list?" % distro)
return "Package lookup faild"
if not p.get("Version", None):
continue
if apt.VersionCompare(maxp['Version'], p['Version']) < 0:
maxp = p
del parser
@ -118,6 +124,8 @@ class Apt:
if type(p) == type(""):
self.log.error("apt returned an error, do you have the deb-src URLs in %s.list?" % distro)
return "Package lookup faild"
if not p['Version']:
continue
if apt.VersionCompare(maxp2['Version'], p['Version']) < 0:
maxp2 = p
del parser
@ -130,3 +138,40 @@ class Apt:
maxp['Priority'], maxp['Version'], distro, int(maxp['Size'])/1024, maxp['Installed-Size'], archs))
return 'Package %s does not exist in %s' % (pkg, checkdists)
# Simple test
if __name__ == "__main__":
import sys
argv = sys.argv
argc = len(argv)
if argc == 1:
print "Need at least one arg"
sys.exit(1)
if argc > 3:
print "Only takes 2 args"
sys.exit(1)
class FakePlugin:
class FakeLog:
def error(*args, **kwargs):
pass
def __init__(self):
self.log = self.FakeLog()
def registryValue(self, *args, **kwargs):
return "/home/jussi/bot/aptdir"
command = argv[1].split(None, 1)[0]
try:
lookup = argv[1].split(None, 1)[1]
except:
print "Need something to lookup"
sys.exit(1)
dists = "hardy"
if argc == 3:
dists = argv[2]
plugin = FakePlugin()
aptlookup = Apt(plugin)
if command == "find":
print aptlookup.find(lookup, dists)
else:
print aptlookup.info(lookup, dists)

View File

@ -32,10 +32,11 @@ import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.ircmsgs as ircmsgs
import supybot.callbacks as callbacks
import supybot.ircutils as ircutils
import supybot.conf as conf
import supybot.ircdb as ircdb
import supybot.conf as conf
import os
import packages
reload(packages)
@ -47,7 +48,6 @@ def get_user(msg):
return False
return user
class PackageInfo(callbacks.Plugin):
"""Lookup package information via apt-cache/apt-file"""
threaded = True
@ -65,87 +65,146 @@ class PackageInfo(callbacks.Plugin):
return (before, [])
def __getRelease(self, irc, release, channel, doError=True):
if release:
return release
release = self.registryValue("defaultRelease", channel)
if not release:
defaultRelease = self.registryValue("defaultRelease", channel)
if not defaultRelease:
if doError:
irc.error("'supybot.plugins.PackageInfo.defaultRelease' is not set")
return None
return release
return (None, None)
if not release:
return (defaultRelease, None)
(release, rest) = (release.split(' ', 1) + [None])[:2]
if release[0] in ('|', '>'):
return (defaultRelease, "%s %s" % (release, rest))
return (release, rest)
def __getChannel(self, channel):
return ircutils.isChannel(channel) and channel or None
def info(self, irc, msg, args, package, release):
def real_info(self, irc, msg, args, package, release):
"""<package> [<release>]
Lookup information for <package>, optionally in <release>
"""
channel = self.__getChannel(msg.args[0])
release = self.__getRelease(irc, release, channel)
(release, rest) = self.__getRelease(irc, release, channel)
if not release:
return
irc.reply(self.Apt.info(package, release))
reply = self.Apt.info(package, release)
if rest:
if rest[0] == '|':
try:
target = rest.split()[1]
if target.lower() == "me":
target = msg.nick
irc.reply("%s: %s" % (target, reply))
return
except Exception, e:
self.log.info("Info: Exception in pipe: %r" % e)
pass
elif rest[0] == '>':
try:
target = rest.split()[1]
if target.lower() == "me":
target = msg.nick
irc.queueMsg(ircmsgs.privmsg(target, "<%s> wants you to know: %s" % (msg.nick, reply)))
return
except Exception, e:
self.log.info("Info: Exception in redirect: %r" % e)
pass
info = wrap(info, ['text', optional('text')])
irc.reply(reply)
def find(self, irc, msg, args, package, release):
info = wrap(real_info, ['anything', optional('text')])
def real_find(self, irc, msg, args, package, release):
"""<package/filename> [<release>]
Search for <package> or, of that fails, find <filename>'s package(s).
Optionally in <release>
"""
channel = self.__getChannel(msg.args[0])
release = self.__getRelease(irc, release, channel)
(release, rest) = self.__getRelease(irc, release, channel)
if not release:
return
irc.reply(self.Apt.find(package, release))
reply = self.Apt.find(package, release)
if rest:
if rest[0] == '|':
try:
target = rest.split()[1]
if target.lower() == "me":
target = msg.nick
irc.reply("%s: %s" % (target, reply))
return
except Exception, e:
self.log.info("Find: Exception in pipe: %r" % e)
pass
elif rest[0] == '>':
try:
target = rest.split()[1]
if target.lower() == "me":
target = msg.nick
irc.queueMsg(ircmsgs.privmsg(target, "<%s> wants you to know: %s" % (msg.nick, reply)))
return
except Exception, e:
self.log.info("Find: Exception in redirect: %r" % e)
pass
find = wrap(find, ['text', optional('text')])
irc.reply(reply)
find = wrap(real_find, ['anything', optional('text')])
def privmsg(self, irc, msg, user):
text = msg.args[1]
release = self.__getRelease(irc, None, channel, False)
channel = self.__getChannel(msg.args[0])
text = msg.args[1].strip()
if text[0] == self.registryValue("prefixchar"):
text = text[1:]
if user and text[0] in str(conf.supybot.reply.whenAddressedBy.get('chars')):
return
if text[:4] == "find":
irc.reply(self.Apt.find(text[4:].strip(), release))
(cmd, rest) = (text.split(' ', 1) + [None])[:2]
if cmd not in ("find", "info"):
return
if not rest:
return
(term, rest) = (rest.split(' ', 1) + [None])[:2]
if cmd == "find":
self.real_find(irc, msg, [], term, rest)
else:
irc.reply(self.Apt.info(text[4:].strip(), release))
self.real_info(irc, msg, [], term, rest)
def chanmsg(self, irc, msg, user):
channel = self.__getChannel(msg.args[0])
text = msg.args[1]
release = self.__getRelease(irc, None, channel, False)
text = msg.args[1].strip()
if text[0] != self.registryValue("prefixchar", channel):
return
text = text[1:]
if not text[:4] in ("find", "info"):
(cmd, rest) = (text.split(' ', 1) + [None])[:2]
if cmd not in ("find", "info"):
return
if text[:4] == "find":
irc.reply(self.Apt.find(text[4:].strip(), release))
if not rest:
return
(term, rest) = (rest.split(' ', 1) + [None])[:2]
if cmd == "find":
self.real_find(irc, msg, [], term, rest)
else:
irc.reply(self.Apt.info(text[4:].strip(), release))
self.real_info(irc, msg, [], term, rest)
def doPrivmsg(self, irc, msg):
if chr(1) in msg.args[1]: # CTCP
return
if not msg.args[1]:
return
channel = self.__getChannel(msg.args[0])
if not self.registryValue("enabled", channel):
return
user = get_user(msg)
if channel:
self.chanmsg(irc, msg, user)
elif user:
return
else:
if user:
return
self.privmsg(irc, msg, user)
def inFilter(self, irc, msg):
if not conf.supybot.get("defaultIgnore"):
return msg
if msg.command != "PRIVMSG":
return msg
text = msg.args[1]
@ -154,7 +213,7 @@ class PackageInfo(callbacks.Plugin):
return msg
channel = self.__getChannel(msg.args[0])
if channel:
if text[:5] not in ("!info", "!find", "@info", "@find"):
if not text[:5] in ("!info", "!find", "@info", "@find"):
return msg
else:
if text[:5] in ("info ", "find ", "!info", "!find", "@info", "@find"):

31
PackageInfo/update_apt Executable file → Normal file
View File

@ -1,6 +1,33 @@
#!/bin/bash
DIR=/home/bot/data/apt
DIR=/home/bot/aptdir
DEFAULT_OPTS="-qq"
while [ "x$1" != "x" ]; do
case "$1" in
-v|--verbose)
DEFAULT_OPTS=""
;;
-d|--dir)
if [ "x$2" == "x" ]; then
echo "\"-d|--dir\" requires an argument" >&2
exit 1
fi
shift
DIR="$1"
;;
-*)
echo "Unknown option \"$1\"" >&2
exit 1
;;
*)
echo "This script takes no arguments" >&2
exit 1
;;
esac
shift
done
for DIST in "$DIR"/*.list; do
test -h $DIST && continue
@ -8,7 +35,7 @@ for DIST in "$DIR"/*.list; do
DIST=${DIST/.list}
touch "$DIR/$DIST.status"
mkdir -p "$DIR/$DIST/partial"
apt-get -qq -o "Dir::State::Lists=$DIR/$DIST" \
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" \

35
PackageInfo/update_apt_file Executable file → Normal file
View File

@ -5,14 +5,45 @@ if [ -x "$(which apt-file)" ]; then
exit 1
fi
DIR=/home/bot/data/apt
DIR=/home/bot/aptdir
VERBOSE="no"
while [ "x$1" != "x" ]; do
case "$1" in
-v|--verbose)
VERBOSE="yes"
;;
-d|--dir)
if [ "x$2" == "x" ]; then
echo "\"-d|--dir\" requires an argument" >&2
exit 1
fi
shift
DIR="$1"
;;
-*)
echo "Unknown option \"$1\"" >&2
exit 1
;;
*)
echo "This script takes no arguments" >&2
exit 1
;;
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"
apt-file -l -c "$DIR/apt-file/$DIST" -s "$DIR/$DIST.list" update >/dev/null 2>&1
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!"