Tweaked deblookup and changed its named to debversion.

This commit is contained in:
Jeremy Fincher 2003-07-23 15:10:41 +00:00
parent d3268414e3
commit d346d877c9

View File

@ -37,7 +37,7 @@ from baseplugin import *
import re import re
import time import time
import httplib import random
import urllib import urllib
import urllib2 import urllib2
@ -356,73 +356,54 @@ class Http(callbacks.Privmsg):
else: else:
irc.error(msg, 'The format of the was odd.') irc.error(msg, 'The format of the was odd.')
def deblookup(self, irc, msg, args): _debreflags = re.DOTALL | re.IGNORECASE
"""[stable|testing|unstable|experimental] <packagename> _debpkgre = re.compile(r'<a.*>(.*?)</a>', _debreflags)
_debbrre = re.compile(r'<td align="center">(\S+)\s*</?td>', _debreflags)
_debtablere = re.compile(r'<table\s*[^>]*>(.*?)</table>', _debreflags)
_debnumpkgsre = re.compile(r'out of total of (\d+)', _debreflags)
_debBranches = ('stable', 'testing', 'unstable', 'experimental')
def debversion(self, irc, msg, args):
"""<package name> [stable|testing|unstable|experimental]
Returns the current version(s) of a Debian package in the given branch Returns the current version(s) of a Debian package in the given branch
(if any, otherwise all available ones are displayed). (if any, otherwise all available ones are displayed).
""" """
branches = ['stable', 'testing', 'unstable', 'experimental'] if args and args[-1] in self._debBranches:
host = "packages.debian.org" branch = args.pop()
page = "/cgi-bin/search_packages.pl"
max_packages = 10
if args[0] in branches:
branch = args[0]
del args[0] # Chop this part off, we don't need it
else: else:
branch = None branch = 'all'
if not args:
# Barf if we don't get a package irc.error(msg, 'You must give a package name.')
if len(args) == 0: responses = []
irc.error(msg, 'Please supply a package name.') numberOfPackages = 0
s = "" # The reply string (to-be)
for package in args: for package in args:
cgi_params = \ fd = urllib2.urlopen('http://packages.debian.org/cgi-bin/' \
"?keywords=%s&searchon=names&version=%s&release=all" % \ 'search_packages.pl?' \
(package, branch or "all") 'keywords=%s&searchon=names&' \
conn = httplib.HTTPConnection(host) 'version=%s&release=all' % \
conn.request("GET", page + cgi_params) (package, branch))
response = conn.getresponse() html = fd.read()
fd.close()
if response.status != 200: m = self._debtablere.search(html)
irc.error(msg, "Bad response from debian.org: %d" % \ if m is None:
response.status) responses.append('No package found for: %s (%s)' % \
(package, branch))
else: else:
data = response.read() tableData = m.group(1)
match = re.search('<TABLE .*?>.*?</TABLE>', \ rows = tableData.split('</TR>')
data, re.DOTALL | re.I) m = self._debnumpkgsre.search(tableData)
if match is None: if m:
s += "No package found for: %s (%s)" % \ numberOfPackages += int(m.group(1))
(package, branch or "all") for row in rows:
else: pkgMatch = self._debpkgre.search(row)
table_data = match.group() brMatch = self._debbrre.search(row)
## Now run through each of the rows, building up the reply if pkgMatch and brMatch:
rows = table_data.split("</TR>") s = '%s (%s)' % (pkgMatch.group(1), brMatch.group(1))
responses.append(s)
# Check for greater than max_packages (note that the first random.shuffle(responses)
# and last row don't have pkgs) ircutils.shrinkList(responses, ', ', 400)
num_pkgs_match = re.search('out of total of (?P<num>\d+)', \ s = 'Total matches: %s, shown: %s. %s' % \
data, re.DOTALL | re.I) (numberOfPackages, len(responses), ', '.join(responses))
num_pkgs = int(num_pkgs_match.group('num'))
if num_pkgs > max_packages:
s += "%d packages found, displaying %d: " % \
(num_pkgs, max_packages)
last_row = max_packages
else:
last_row = -1
for row in rows[1:last_row]:
pkg_match = re.search("<a.*>(?P<pkg_ver>.*?)</a>", \
row, re.DOTALL | re.I)
br_match = re.search(
"<td ALIGN=\"center\">(?P<pkg_br>.*?)</?td>", \
row, re.DOTALL | re.I)
s += "%s (%s) ;; " % \
(pkg_match.group('pkg_ver').strip(),
br_match.group('pkg_br').strip())
irc.reply(msg, s) irc.reply(msg, s)