commands.wrap update

This commit is contained in:
James Vega 2004-10-22 22:08:03 +00:00
parent d5241eb07a
commit dd1550afcc
2 changed files with 52 additions and 84 deletions

View File

@ -52,6 +52,7 @@ from itertools import imap, ifilter
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins import supybot.plugins as plugins
import supybot.privmsgs as privmsgs import supybot.privmsgs as privmsgs
import supybot.registry as registry import supybot.registry as registry
@ -109,7 +110,7 @@ class Debian(callbacks.Privmsg,
def die(self): def die(self):
callbacks.Privmsg.die(self) callbacks.Privmsg.die(self)
def file(self, irc, msg, args): def file(self, irc, msg, args, optlist, glob):
"""[--{regexp,exact}=<value>] [<glob>] """[--{regexp,exact}=<value>] [<glob>]
Returns packages in Debian that includes files matching <glob>. If Returns packages in Debian that includes files matching <glob>. If
@ -121,19 +122,17 @@ class Debian(callbacks.Privmsg,
# Make sure it's anchored, make sure it doesn't have a leading slash # 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 # (the filenames don't have leading slashes, and people may not know
# that). # that).
(optlist, rest) = getopt.getopt(args, '', ['regexp=', 'exact=']) if not optlist and not glob:
if not optlist and not rest:
raise callbacks.ArgumentError raise callbacks.ArgumentError
if len(optlist) + len(rest) > 1: if optlist and glob > 1:
irc.error('Only one search option is allowed.') irc.error('You must specify either a glob or a regexp/exact '
return 'search, but not both.')
for (option, arg) in optlist: for (option, arg) in optlist:
if option == '--exact': if option == 'exact':
regexp = arg.lstrip('/') regexp = arg.lstrip('/')
elif option == '--regexp': elif option == 'regexp':
regexp = arg regexp = arg
if rest: if glob:
glob = rest.pop()
regexp = fnmatch.translate(glob.lstrip('/')) regexp = fnmatch.translate(glob.lstrip('/'))
regexp = regexp.rstrip('$') regexp = regexp.rstrip('$')
regexp = ".*%s.* " % regexp regexp = ".*%s.* " % regexp
@ -182,12 +181,13 @@ class Debian(callbacks.Privmsg,
irc.reply('I found no packages with that file.') irc.reply('I found no packages with that file.')
else: else:
irc.reply(utils.commaAndify(packages)) irc.reply(utils.commaAndify(packages))
file = wrap(file, [getopts({'regexp':'regexpMatcher', 'exact':'text'}),
additional('text')])
_debreflags = re.DOTALL | re.IGNORECASE _debreflags = re.DOTALL | re.IGNORECASE
_deblistre = re.compile(r'<h3>Package ([^<]+)</h3>(.*?)</ul>', _debreflags) _deblistre = re.compile(r'<h3>Package ([^<]+)</h3>(.*?)</ul>', _debreflags)
_debBranches = ('stable', 'testing', 'unstable', 'experimental') def version(self, irc, msg, args, optlist, branch, package):
def version(self, irc, msg, args): """[--exact] [{stable,testing,unstable,experimental}] <package name>
"""[--exact] [stable|testing|unstable|experimental] <package name>
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 --exact is (if any, otherwise all available ones are displayed). If --exact is
@ -196,24 +196,12 @@ class Debian(callbacks.Privmsg,
""" """
url = 'http://packages.debian.org/cgi-bin/search_packages.pl?keywords'\ url = 'http://packages.debian.org/cgi-bin/search_packages.pl?keywords'\
'=%s&searchon=names&version=%s&release=all&subword=1' '=%s&searchon=names&version=%s&release=all&subword=1'
if not args:
raise callbacks.ArgumentError
(optlist, rest) = getopt.getopt(args, '', ['exact'])
for (option, _) in optlist: for (option, _) in optlist:
if option == '--exact': if option == 'exact':
url = url.replace('&subword=1','') url = url.replace('&subword=1','')
if rest and rest[0] in self._debBranches:
branch = rest.pop(0)
else:
branch = 'all'
if not rest:
irc.error('You must give a package name.')
return
responses = [] responses = []
package = privmsgs.getArgs(rest)
if '*' in package: if '*' in package:
irc.error('Wildcard characters can not be specified.') irc.error('Wildcard characters can not be specified.', Raise=True)
return
package = urllib.quote(package) package = urllib.quote(package)
url = url % (package, branch) url = url % (package, branch)
try: try:
@ -224,10 +212,8 @@ class Debian(callbacks.Privmsg,
if 'is down at the moment' in html: if 'is down at the moment' in html:
irc.error('Packages.debian.org is down at the moment. ' irc.error('Packages.debian.org is down at the moment. '
'Please try again later.') 'Please try again later.', Raise=True)
return
pkgs = self._deblistre.findall(html) pkgs = self._deblistre.findall(html)
#self.log.warning(pkgs)
if not pkgs: if not pkgs:
irc.reply('No package found for %s (%s)' % irc.reply('No package found for %s (%s)' %
(urllib.unquote(package), branch)) (urllib.unquote(package), branch))
@ -254,32 +240,29 @@ class Debian(callbacks.Privmsg,
resp = '%s matches found: %s' % \ resp = '%s matches found: %s' % \
(len(responses), '; '.join(responses)) (len(responses), '; '.join(responses))
irc.reply(resp) irc.reply(resp)
version = wrap(version, [getopts({'exact':''}),
optional(('literal', ('stable', 'testing',
'unstable', 'experimental')), 'all'),
'text'])
_incomingRe = re.compile(r'<a href="(.*?\.deb)">', re.I) _incomingRe = re.compile(r'<a href="(.*?\.deb)">', re.I)
def incoming(self, irc, msg, args): def incoming(self, irc, msg, args, optlist, globs):
"""[--{regexp,arch}=<value>] <glob> """[--{regexp,arch}=<value>] [<glob> ...]
Checks debian incoming for a matching package name. The arch Checks debian incoming for a matching package name. The arch
parameter defaults to i386; --regexp returns only those package names parameter defaults to i386; --regexp returns only those package names
that match a given regexp, and normal matches use standard *nix that match a given regexp, and normal matches use standard *nix
globbing. globbing.
""" """
(optlist, rest) = getopt.getopt(args, '', ['regexp=', 'arch='])
predicates = [] predicates = []
archPredicate = lambda s: ('_i386.' in s) archPredicate = lambda s: ('_i386.' in s)
for (option, arg) in optlist: for (option, arg) in optlist:
if option == '--regexp': if option == 'regexp':
try:
r = utils.perlReToPythonRe(arg)
predicates.append(r.search) predicates.append(r.search)
except ValueError: elif option == 'arch':
irc.error('%r is not a valid regexp.' % arg)
return
elif option == '--arch':
arg = '_%s.' % arg arg = '_%s.' % arg
archPredicate = lambda s, arg=arg: (arg in s) archPredicate = lambda s, arg=arg: (arg in s)
predicates.append(archPredicate) predicates.append(archPredicate)
globs = privmsgs.getArgs(rest).split()
for glob in globs: for glob in globs:
glob = glob.replace('*', '.*').replace('?', '.?') glob = glob.replace('*', '.*').replace('?', '.?')
predicates.append(re.compile(r'.*%s.*' % glob).search) predicates.append(re.compile(r'.*%s.*' % glob).search)
@ -300,26 +283,18 @@ class Debian(callbacks.Privmsg,
irc.error('No packages matched that search.') irc.error('No packages matched that search.')
else: else:
irc.reply(utils.commaAndify(packages)) irc.reply(utils.commaAndify(packages))
incoming = privmsgs.thread(incoming) incoming = thread(wrap(incoming,
[getopts({'regexp':'regexpMatcher', 'arch':'text'}),
any('text')]))
_newpkgre = re.compile(r'<li><a href[^>]+>([^<]+)</a>') _newpkgre = re.compile(r'<li><a href[^>]+>([^<]+)</a>')
def new(self, irc, msg, args): def new(self, irc, msg, args, optlist, glob):
"""[--{main,contrib,non-free}] [<glob>] """[{main,contrib,non-free}] [<glob>]
Checks for packages that have been added to Debian's unstable branch Checks for packages that have been added to Debian's unstable branch
in the past week. If no glob is specified, returns a list of all in the past week. If no glob is specified, returns a list of all
packages. If no section is specified, defaults to main. packages. If no section is specified, defaults to main.
""" """
options = ['main', 'contrib', 'non-free']
(optlist, rest) = getopt.getopt(args, '', options)
section = 'main'
for (option, _) in optlist:
option = option.lstrip('-')
if option in options:
section = option
glob = privmsgs.getArgs(rest, required=0, optional=1)
if not glob:
glob = '*'
if '?' not in glob and '*' not in glob: if '?' not in glob and '*' not in glob:
glob = '*%s*' % glob glob = '*%s*' % glob
try: try:
@ -328,8 +303,6 @@ class Debian(callbacks.Privmsg,
except webutils.WebError, e: except webutils.WebError, e:
irc.error(str(e)) irc.error(str(e))
packages = [] packages = []
#self.log.warning(section)
#self.log.warning(glob)
for line in fd: for line in fd:
m = self._newpkgre.search(line) m = self._newpkgre.search(line)
if m: if m:
@ -341,6 +314,9 @@ class Debian(callbacks.Privmsg,
irc.reply(utils.commaAndify(packages)) irc.reply(utils.commaAndify(packages))
else: else:
irc.error('No packages matched that search.') irc.error('No packages matched that search.')
new = wrap(new, [optional(('literal', ('main', 'contrib', 'non-free')),
'main'),
additional('text', '*')])
_severity = re.compile(r'.*(?:severity set to `([^\']+)\'|' _severity = re.compile(r'.*(?:severity set to `([^\']+)\'|'
r'severity:\s+([^\s]+))', re.I) r'severity:\s+([^\s]+))', re.I)
@ -349,20 +325,11 @@ class Debian(callbacks.Privmsg,
_subject = re.compile(r'<br>([^<]+)</h1>', re.I | re.S) _subject = re.compile(r'<br>([^<]+)</h1>', re.I | re.S)
_date = re.compile(r'Date: ([^;]+);', re.I | re.S) _date = re.compile(r'Date: ([^;]+);', re.I | re.S)
_searches = (_package, _subject, _reporter, _date) _searches = (_package, _subject, _reporter, _date)
def bug(self, irc, msg, args): def bug(self, irc, msg, args, bug):
"""<num> """<num>
Returns a description of the bug with bug id <num>. Returns a description of the bug with bug id <num>.
""" """
bug = privmsgs.getArgs(args)
if ' ' in bug:
irc.error('Only one bug can be looked up at a time.')
return
try:
int(bug)
except ValueError:
irc.error('<num> must be an integer.')
return
url = 'http://bugs.debian.org/%s' % bug url = 'http://bugs.debian.org/%s' % bug
text = webutils.getUrl(url) text = webutils.getUrl(url)
if "There is no record of Bug" in text: if "There is no record of Bug" in text:
@ -383,6 +350,7 @@ class Debian(callbacks.Privmsg,
irc.reply(resp) irc.reply(resp)
else: else:
irc.reply('I was unable to properly parse the BTS page.') irc.reply('I was unable to properly parse the BTS page.')
bug = wrap(bug, [('id', 'bug')])
Class = Debian Class = Debian

View File

@ -32,29 +32,29 @@ import time
from testsupport import * from testsupport import *
if network:
class DebianTestCase(PluginTestCase, PluginDocumentation): class DebianTestCase(PluginTestCase, PluginDocumentation):
plugins = ('Debian',) plugins = ('Debian',)
timeout = 100 timeout = 100
cleanDataDir = False cleanDataDir = False
fileDownloaded = False fileDownloaded = False
def setUp(self, nick='test'): if network:
PluginTestCase.setUp(self) def setup(self, nick='test'):
plugintestcase.setup(self)
try: try:
dataDir = conf.supybot.directories.data() datadir = conf.supybot.directories.data()
if os.path.exists(os.path.join(dataDir, if os.path.exists(os.path.join(datadir,
'Contents-i386.gz')): 'contents-i386.gz')):
pass pass
else: else:
print print
print "Downloading files, this may take awhile" print "downloading files, this may take awhile"
filename = os.path.join(dataDir, 'Contents-i386.gz') filename = os.path.join(datadir, 'contents-i386.gz')
while not os.path.exists(filename): while not os.path.exists(filename):
time.sleep(1) time.sleep(1)
print "Download complete" print "download complete"
print "Starting test ..." print "starting test ..."
self.fileDownloaded = True self.filedownloaded = true
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
@ -79,7 +79,7 @@ if network:
self.assertRegexp('file --exact bin/gaim', r'net/gaim') self.assertRegexp('file --exact bin/gaim', r'net/gaim')
def testDebincoming(self): def testDebincoming(self):
self.assertHelp('incoming') self.assertNotError('incoming')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: