2003-08-22 02:46:05 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2003-08-27 20:06:26 +02:00
|
|
|
This is a module to contain Debian-specific commands.
|
2003-08-22 02:46:05 +02:00
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-08-22 02:46:05 +02:00
|
|
|
|
2003-08-26 19:08:46 +02:00
|
|
|
import re
|
2003-08-22 02:46:05 +02:00
|
|
|
import gzip
|
2003-09-11 20:46:17 +02:00
|
|
|
import sets
|
2003-10-03 02:23:03 +02:00
|
|
|
import getopt
|
2003-08-22 02:46:05 +02:00
|
|
|
import popen2
|
2003-11-21 19:16:27 +01:00
|
|
|
import socket
|
2003-10-20 19:23:08 +02:00
|
|
|
import urllib
|
2003-09-29 08:08:16 +02:00
|
|
|
import fnmatch
|
2003-08-23 01:00:10 +02:00
|
|
|
import os.path
|
2003-08-22 02:52:10 +02:00
|
|
|
import urllib2
|
2003-08-22 02:46:05 +02:00
|
|
|
from itertools import imap, ifilter
|
|
|
|
|
|
|
|
import conf
|
|
|
|
import utils
|
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
|
|
|
|
|
|
|
def configure(onStart, afterConnect, advanced):
|
|
|
|
# This will be called by setup.py to configure this module. onStart and
|
|
|
|
# afterConnect are both lists. Append to onStart the commands you would
|
|
|
|
# like to be run when the bot is started; append to afterConnect the
|
|
|
|
# commands you would like to be run when the bot has finished connecting.
|
|
|
|
from questions import expect, anything, something, yn
|
|
|
|
onStart.append('load Debian')
|
|
|
|
if not utils.findBinaryInPath('zegrep'):
|
|
|
|
if not advanced:
|
|
|
|
print 'I can\'t find zegrep in your path. This is necessary '
|
2003-10-21 07:11:53 +02:00
|
|
|
print 'to run the file command. I\'ll disable this command '
|
2003-08-22 02:46:05 +02:00
|
|
|
print 'now. When you get zegrep in your path, use the command '
|
2003-10-21 07:11:53 +02:00
|
|
|
print '"enable file" to re-enable the command.'
|
|
|
|
onStart.append('disable file')
|
2003-08-22 02:46:05 +02:00
|
|
|
else:
|
|
|
|
print 'I can\'t find zegrep in your path. If you want to run the '
|
2003-10-21 07:11:53 +02:00
|
|
|
print 'file command with any sort of expediency, you\'ll need '
|
2003-08-22 02:46:05 +02:00
|
|
|
print 'it. You can use a python equivalent, but it\'s about two '
|
|
|
|
print 'orders of magnitude slower. THIS MEANS IT WILL TAKE AGES '
|
|
|
|
print 'TO RUN THIS COMMAND. Don\'t do this.'
|
|
|
|
if yn('Do you want to use a Python equivalent of zegrep?') == 'y':
|
|
|
|
onStart.append('usepythonzegrep')
|
|
|
|
else:
|
2003-10-21 07:11:53 +02:00
|
|
|
print 'I\'ll disable file now.'
|
|
|
|
onStart.append('disable file')
|
2003-08-22 02:46:05 +02:00
|
|
|
|
2003-11-22 05:23:59 +01:00
|
|
|
class Debian(callbacks.Privmsg,
|
|
|
|
plugins.Configurable,
|
|
|
|
plugins.PeriodicFileDownloader):
|
2003-08-22 02:46:05 +02:00
|
|
|
threaded = True
|
|
|
|
periodicFiles = {
|
2003-10-03 06:21:37 +02:00
|
|
|
# This file is only updated once a week, so there's no sense in
|
|
|
|
# downloading a new one every day.
|
2003-08-22 02:46:05 +02:00
|
|
|
'Contents-i386.gz': ('ftp://ftp.us.debian.org/'
|
|
|
|
'debian/dists/unstable/Contents-i386.gz',
|
2003-10-03 06:21:37 +02:00
|
|
|
604800, None)
|
2003-08-22 02:46:05 +02:00
|
|
|
}
|
2003-08-23 01:00:10 +02:00
|
|
|
contents = os.path.join(conf.dataDir, 'Contents-i386.gz')
|
2003-11-22 05:23:59 +01:00
|
|
|
configurables = plugins.ConfigurableDictionary(
|
|
|
|
[('python-zegrep', plugins.ConfigurableBoolType, False,
|
|
|
|
"""An advanced option, mostly just for testing; uses a Python-coded
|
|
|
|
zegrep rather than the actual zegrep executable, generally resulting
|
|
|
|
in a 50x slowdown. What would take 2 seconds will take 100 with this
|
|
|
|
enabled. Don't enable this.""")]
|
|
|
|
)
|
2003-08-22 02:46:05 +02:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
2003-11-22 05:23:59 +01:00
|
|
|
plugins.Configurable.__init__(self)
|
2003-10-05 14:56:56 +02:00
|
|
|
plugins.PeriodicFileDownloader.__init__(self)
|
2003-08-22 02:46:05 +02:00
|
|
|
|
2003-11-22 05:23:59 +01:00
|
|
|
def die(self):
|
|
|
|
callbacks.Privmsg.die(self)
|
|
|
|
plugins.Configurable.die(self)
|
2003-08-22 02:46:05 +02:00
|
|
|
|
2003-10-21 07:11:53 +02:00
|
|
|
def file(self, irc, msg, args):
|
2003-10-03 06:21:37 +02:00
|
|
|
"""[--{regexp,exact}=<value>] [<glob>]
|
2003-09-06 20:29:44 +02:00
|
|
|
|
2003-10-03 06:21:37 +02:00
|
|
|
Returns packages in Debian that includes files matching <glob>. If
|
|
|
|
--regexp is given, returns packages that include files matching the
|
|
|
|
given regexp. If --exact is given, returns packages that include files
|
|
|
|
matching exactly the string given.
|
2003-09-06 20:29:44 +02:00
|
|
|
"""
|
2003-08-22 02:46:05 +02:00
|
|
|
self.getFile('Contents-i386.gz')
|
2003-08-23 01:00:10 +02:00
|
|
|
# Make sure it's anchored, make sure it doesn't have a leading slash
|
|
|
|
# (the filenames don't have leading slashes, and people may not know
|
|
|
|
# that).
|
2003-10-03 06:21:37 +02:00
|
|
|
(optlist, rest) = getopt.getopt(args, '', ['regexp=', 'exact='])
|
2003-10-16 03:59:17 +02:00
|
|
|
if not optlist and not rest:
|
|
|
|
raise callbacks.ArgumentError
|
2003-10-03 06:21:37 +02:00
|
|
|
if len(optlist) + len(rest) > 1:
|
|
|
|
irc.error(msg, 'Only one search option is allowed.')
|
|
|
|
return
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == '--exact':
|
|
|
|
regexp = arg.lstrip('/')
|
|
|
|
elif option == '--regexp':
|
|
|
|
regexp = arg
|
|
|
|
if rest:
|
|
|
|
glob = rest.pop()
|
|
|
|
regexp = fnmatch.translate(glob.lstrip('/'))
|
2003-08-22 02:46:05 +02:00
|
|
|
try:
|
2003-09-15 04:45:39 +02:00
|
|
|
re_obj = re.compile(regexp, re.I)
|
2003-11-22 05:23:59 +01:00
|
|
|
except re.error, e:
|
2003-10-03 06:21:37 +02:00
|
|
|
irc.error(msg, "Error in regexp: %s" % e)
|
2003-08-22 02:46:05 +02:00
|
|
|
return
|
2003-11-22 05:23:59 +01:00
|
|
|
if self.configurables.get('python-zegrep', None):
|
2003-09-15 04:45:39 +02:00
|
|
|
fd = gzip.open(self.contents)
|
2003-11-22 05:23:59 +01:00
|
|
|
r = imap(lambda tup: tup[0],
|
|
|
|
ifilter(lambda tup: tup[0],
|
|
|
|
imap(lambda line:(re_obj.search(line), line),fd)))
|
2003-10-03 06:21:37 +02:00
|
|
|
else:
|
2003-11-22 05:23:59 +01:00
|
|
|
try:
|
|
|
|
(r, w) = popen2.popen4(['zegrep', regexp, self.contents])
|
|
|
|
w.close()
|
|
|
|
except TypeError:
|
|
|
|
# We're on Windows.
|
|
|
|
irc.error(msg, 'This command won\'t work on this platform. '
|
|
|
|
'If you think it should (i.e., you know that '
|
|
|
|
'you have a zegrep binary somewhere) then file '
|
|
|
|
'a bug about it at http://supybot.sf.net/ .')
|
|
|
|
return
|
2003-09-11 20:46:17 +02:00
|
|
|
packages = sets.Set() # Make packages unique
|
2003-09-05 08:47:12 +02:00
|
|
|
try:
|
2003-09-06 03:06:28 +02:00
|
|
|
for line in r:
|
2003-10-03 06:21:37 +02:00
|
|
|
if len(packages) > 100:
|
|
|
|
irc.error(msg, 'More than 100 packages matched, '
|
|
|
|
'please narrow your search.')
|
|
|
|
return
|
2003-09-05 08:47:12 +02:00
|
|
|
try:
|
2003-09-11 20:46:17 +02:00
|
|
|
(filename, pkg_list) = line[:-1].split()
|
2003-09-05 08:47:12 +02:00
|
|
|
if filename == 'FILE':
|
|
|
|
# This is the last line before the actual files.
|
|
|
|
continue
|
|
|
|
except ValueError: # Unpack list of wrong size.
|
|
|
|
continue # We've not gotten to the files yet.
|
2003-09-11 20:46:17 +02:00
|
|
|
packages.update(pkg_list.split(','))
|
2003-09-05 08:47:12 +02:00
|
|
|
finally:
|
2003-11-22 05:23:59 +01:00
|
|
|
if hasattr(r, 'close'):
|
|
|
|
r.close()
|
2003-08-22 02:46:05 +02:00
|
|
|
if len(packages) == 0:
|
|
|
|
irc.reply(msg, 'I found no packages with that file.')
|
|
|
|
else:
|
2003-09-07 08:10:09 +02:00
|
|
|
irc.reply(msg, utils.commaAndify(packages))
|
2003-08-22 02:46:05 +02:00
|
|
|
|
2003-08-22 02:52:10 +02:00
|
|
|
_debreflags = re.DOTALL | re.IGNORECASE
|
2003-10-04 16:57:05 +02:00
|
|
|
_debpkgre = re.compile(r'<a[^>]+>(.*?)</a>', _debreflags)
|
2003-08-22 02:52:10 +02:00
|
|
|
_debbrre = re.compile(r'<td align="center">(\S+)\s*</?td>', _debreflags)
|
2003-10-04 16:57:05 +02:00
|
|
|
_debtablere = re.compile(r'<table[^>]*>(.*?)</table>', _debreflags)
|
2003-08-22 02:52:10 +02:00
|
|
|
_debnumpkgsre = re.compile(r'out of total of (\d+)', _debreflags)
|
|
|
|
_debBranches = ('stable', 'testing', 'unstable', 'experimental')
|
2003-10-21 07:11:53 +02:00
|
|
|
def version(self, irc, msg, args):
|
2003-10-16 16:03:12 +02:00
|
|
|
"""[stable|testing|unstable|experimental] <package name>
|
2003-08-22 02:52:10 +02:00
|
|
|
|
|
|
|
Returns the current version(s) of a Debian package in the given branch
|
|
|
|
(if any, otherwise all available ones are displayed).
|
|
|
|
"""
|
2003-10-20 19:23:08 +02:00
|
|
|
if not args:
|
|
|
|
raise callbacks.ArgumentError
|
2003-10-16 16:03:12 +02:00
|
|
|
if args and args[0] in self._debBranches:
|
|
|
|
branch = args.pop(0)
|
2003-08-22 02:52:10 +02:00
|
|
|
else:
|
|
|
|
branch = 'all'
|
|
|
|
if not args:
|
|
|
|
irc.error(msg, 'You must give a package name.')
|
2003-10-20 19:23:08 +02:00
|
|
|
return
|
2003-08-22 02:52:10 +02:00
|
|
|
responses = []
|
|
|
|
numberOfPackages = 0
|
2003-10-16 16:03:12 +02:00
|
|
|
package = privmsgs.getArgs(args)
|
|
|
|
package = urllib.quote(package)
|
|
|
|
url = 'http://packages.debian.org/cgi-bin/search_packages.pl?keywords'\
|
|
|
|
'=%s&searchon=names&version=%s&release=all' % (package, branch)
|
2003-11-18 11:49:15 +01:00
|
|
|
try:
|
|
|
|
fd = urllib2.urlopen(url)
|
|
|
|
html = fd.read()
|
|
|
|
fd.close()
|
|
|
|
except urllib2.HTTPError, e:
|
|
|
|
irc.error(msg, 'I couldn\'t reach the search page (%s).' % e)
|
|
|
|
return
|
2003-11-21 19:16:27 +01:00
|
|
|
except socket.error, e:
|
2003-11-22 04:57:38 +01:00
|
|
|
if e.args[0] == 110 or e.args[0] == 10060:
|
2003-11-21 19:16:27 +01:00
|
|
|
irc.error(msg, 'Connection timed out to packages.debian.org.')
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise
|
2003-10-16 16:03:12 +02:00
|
|
|
m = self._debnumpkgsre.search(html)
|
|
|
|
if m:
|
|
|
|
numberOfPackages = m.group(1)
|
|
|
|
m = self._debtablere.search(html)
|
|
|
|
if m is None:
|
|
|
|
irc.reply(msg, 'No package found for %s (%s)' % \
|
|
|
|
(urllib.unquote(package), branch))
|
|
|
|
else:
|
|
|
|
tableData = m.group(1)
|
|
|
|
rows = tableData.split('</TR>')
|
|
|
|
for row in rows:
|
|
|
|
pkgMatch = self._debpkgre.search(row)
|
|
|
|
brMatch = self._debbrre.search(row)
|
|
|
|
if pkgMatch and brMatch:
|
|
|
|
s = '%s (%s)' % (pkgMatch.group(1), brMatch.group(1))
|
|
|
|
responses.append(s)
|
|
|
|
resp = 'Total matches: %s, shown: %s. %s' % \
|
|
|
|
(numberOfPackages, len(responses), ', '.join(responses))
|
|
|
|
irc.reply(msg, resp)
|
2003-08-22 02:52:10 +02:00
|
|
|
|
2003-10-03 02:23:03 +02:00
|
|
|
_incomingRe = re.compile(r'<a href="(.*?\.deb)">', re.I)
|
2003-10-21 07:11:53 +02:00
|
|
|
def incoming(self, irc, msg, args):
|
2003-10-03 02:23:03 +02:00
|
|
|
"""[--{regexp,arch}=<value>] [<glob>]
|
2003-09-27 05:54:58 +02:00
|
|
|
|
2003-10-03 02:23:03 +02:00
|
|
|
Checks debian incoming for a matching package name. The arch
|
|
|
|
parameter defaults to i386; --regexp returns only those package names
|
|
|
|
that match a given regexp, and normal matches use standard *nix
|
|
|
|
globbing.
|
|
|
|
"""
|
|
|
|
(optlist, rest) = getopt.getopt(args, '', ['regexp=', 'arch='])
|
|
|
|
predicates = []
|
|
|
|
archPredicate = lambda s: ('_i386.' in s)
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == '--regexp':
|
|
|
|
try:
|
|
|
|
r = utils.perlReToPythonRe(arg)
|
|
|
|
predicates.append(r.search)
|
|
|
|
except ValueError:
|
|
|
|
irc.error('%r is not a valid regexp.' % arg)
|
|
|
|
return
|
|
|
|
elif option == '--arch':
|
|
|
|
arg = '_%s.' % arg
|
|
|
|
archPredicate = lambda s, arg=arg: (arg in s)
|
|
|
|
predicates.append(archPredicate)
|
|
|
|
for arg in rest:
|
|
|
|
predicates.append(lambda s: fnmatch.fnmatch(s, arg))
|
2003-09-29 08:08:16 +02:00
|
|
|
packages = []
|
|
|
|
fd = urllib2.urlopen('http://incoming.debian.org/')
|
|
|
|
for line in fd:
|
|
|
|
m = self._incomingRe.search(line)
|
|
|
|
if m:
|
|
|
|
name = m.group(1)
|
2003-10-03 02:23:03 +02:00
|
|
|
if all(lambda p: p(name), predicates):
|
2003-10-08 22:56:22 +02:00
|
|
|
realname = rsplit(name, '_', 1)[0]
|
2003-10-03 02:23:03 +02:00
|
|
|
packages.append(realname)
|
2003-09-29 08:08:16 +02:00
|
|
|
if len(packages) == 0:
|
|
|
|
irc.error(msg, 'No packages matched that search.')
|
2003-09-27 05:54:58 +02:00
|
|
|
else:
|
2003-09-29 08:08:16 +02:00
|
|
|
irc.reply(msg, utils.commaAndify(packages))
|
2003-10-21 07:29:39 +02:00
|
|
|
incoming = privmsgs.thread(incoming)
|
2003-09-27 05:54:58 +02:00
|
|
|
|
2003-08-22 02:46:05 +02:00
|
|
|
Class = Debian
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|