From 7f2110ee24b8cef5408119cd5d76a2a679931b55 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 10 Sep 2017 00:35:53 -0700 Subject: [PATCH] PkgInfo: remove old distro-specific functions, update tests (#64) --- PkgInfo/plugin.py | 210 ---------------------------------------------- PkgInfo/test.py | 80 +++++++++--------- 2 files changed, 42 insertions(+), 248 deletions(-) diff --git a/PkgInfo/plugin.py b/PkgInfo/plugin.py index ce4861b..2b6da1c 100644 --- a/PkgInfo/plugin.py +++ b/PkgInfo/plugin.py @@ -579,87 +579,6 @@ class PkgInfo(callbacks.Plugin): vlist = wrap(vlist, ['somethingWithoutSpaces', 'somethingWithoutSpaces', getopts({'reverse': ''})]) - def archlinux(self, irc, msg, args, pkg, opts): - """ [--exact] - - Looks up in the Arch Linux package repositories. - If --exact is given, will output only exact matches. - """ - pkg = pkg.lower() - - if 'exact' in dict(opts): - encoded = urlencode({'name': pkg}) - else: - encoded = urlencode({'q': pkg}) - - url = 'https://www.archlinux.org/packages/search/json/?' + encoded - friendly_url = 'https://www.archlinux.org/packages/?' + encoded - - self.log.debug("PkgInfo: using url %s for 'archlinux' command", url) - - fd = utils.web.getUrl(url) - data = json.loads(fd.decode("utf-8")) - - if data['valid'] and data['results']: - # We want one entry per package, but the API gives one - # entry per architecture! Remove duplicates with a set: - results = set() - - # For each package, store the available architectures as - # a list. - archs = defaultdict(list) - for pkgdata in data['results']: - # Expand the package data dict into arguments for formatting - s = "\x02{pkgname}\x02 - {pkgdesc} \x02({pkgver})\x02".format(**pkgdata) - - if self.registryValue("verbose"): - # In verbose mode, also show the repo the package is in. - s += " [\x02%s\x02]" % pkgdata['repo'] - - results.add(s) - archs[s].append(pkgdata['arch']) - - irc.reply(format('Found %n: %L; View more at %u', - (len(results), 'result'), sorted(results), - friendly_url)) - else: - irc.error("No results found.", Raise=True) - archlinux = wrap(archlinux, ['somethingWithoutSpaces', getopts({'exact': ''})]) - - def archaur(self, irc, msg, args, pkg): - """ - - Looks up in the Arch Linux AUR.""" - pkg = pkg.lower() - baseurl = 'https://aur.archlinux.org/rpc.php?type=search&' - url = baseurl + urlencode({'arg': pkg}) - self.log.debug("PkgInfo: using url %s for 'archaur' command", url) - fd = utils.web.getUrl(url) - data = json.loads(fd.decode("utf-8")) - if data["type"] == "error": - irc.error(data["results"], Raise=True) - count = data["resultcount"] - if count: - # We want this to be limited to prevent overflow warnings - # in the bot. - if count > 150: - count = '150+' - s = format("Found %n: ", (data["resultcount"], 'result')) - for x in data['results'][:150]: - verboseInfo = '' - if self.registryValue("verbose"): - verboseInfo = format("[ID: %s Votes: %s]", x['ID'], - x['NumVotes']) - s += "{name} - {desc} \x02({version} {verbose})\x02, " \ - .format(name=x['Name'], desc=x['Description'], - version=x['Version'], verbose=verboseInfo) - friendly_url = 'https://aur.archlinux.org/packages/?' + \ - urlencode({'K': pkg}) - irc.reply(s + format('View more at: %u', friendly_url)) - else: - irc.error("No results found.", Raise=True) - archaur = wrap(archaur, ['somethingWithoutSpaces']) - @wrap(['somethingWithoutSpaces', 'somethingWithoutSpaces']) def filesearch(self, irc, msg, args, release, query): """ @@ -703,107 +622,6 @@ class PkgInfo(callbacks.Plugin): e = "No results found." irc.error(e) - @wrap(['somethingWithoutSpaces', - 'somethingWithoutSpaces', - getopts({'exact': ''})]) - def linuxmint(self, irc, msg, args, release, query, opts): - """ [--exact] - - Looks up in Linux Mint's repositories. If --exact is given, - look up packages by the exact package name. Otherwise, look it up - as a simple glob pattern.""" - - addr = 'http://packages.linuxmint.com/list.php?release=' + \ - quote(release) - - try: - fd = utils.web.getUrl(addr).decode("utf-8") - except utils.web.Error as e: - irc.error(str(e), Raise=True) - - soup = BeautifulSoup(fd) - # Linux Mint puts their package lists in tables - results = soup.find_all("td") - - packages = [] - query = query.lower() - exact = 'exact' in dict(opts) - - for result in results: - name = result.contents[0].string # Package name - - if query == name or (query in name and not exact): - # This feels like really messy code, but we have to find tags - # relative to our results. - # Ascend to find the section name (in

): - section = result.parent.parent.parent.previous_sibling.\ - previous_sibling.string - - # Find the package version in the next ; for some reason we - # have to go two siblings further, as the first .next_sibling - # returns '\n'. This is mentioned briefly in Beautiful Soup 4's - # documentation... - version = result.next_sibling.next_sibling.string - - # We format our found dictionary this way because the same - # package can exist multiple times in different sections of - # the repository (e.g. one in Main, one in Backports, etc.) - packages.append('%s \x02(%s)\x02 [\x02%s\x02]' % (name, version, section)) - - if packages: # If we have results - s = format('Found %n: %L, %s %u', (len(packages), 'result'), packages, - _('View more at: '), addr) - irc.reply(s) - else: - irc.error('No results found.') - - - @wrap([getopts({'release': 'somethingWithoutSpaces'}), additional('somethingWithoutSpaces')]) - def fedora(self, irc, msg, args, opts, query): - """[--release ] [] - - Looks up in Fedora's repositories. Globs (*, ?) are supported here. is - the release version: e.g. 'f25' or 'master' (for rawhide). If no package is given, a list - of available releases will be shown.""" - opts = dict(opts) - if query is None: - # No package given; show available releases. - url = 'https://admin.fedoraproject.org/pkgdb/api/collections?format=json' - else: - url = 'https://admin.fedoraproject.org/pkgdb/api/packages/%s?format=json' % quote(query) - if 'release' in opts: - url += '&branches=%s' % quote(opts['release']) - - self.log.debug("PkgInfo: using url %s for 'fedora' command", url) - try: - fd = utils.web.getUrl(url).decode("utf-8") - except utils.web.Error as e: - if '404' in str(e): - e = 'No results found.' - if '*' not in query: - e += " Try wrapping your query with *'s: '*%s*'" % query - irc.error(e, Raise=True) - data = json.loads(fd) - - if query is None: - data = data['collections'] - collections = ['%s (%s %s, %s)' % (ircutils.bold(c['branchname']), c['name'], c['version'], c['status']) for c in data] - s = format('Available releases to look up: %L', sorted(collections)) - else: - def formatdesc(s): - # Fedora's package descriptions have newlines inserted in them at strange positions, - # sometimes even inside sentences. We'll break at the first sentence here: - s = s.split('.')[0].strip() - s = re.sub('\n+', ' ', s) - return s - - results = ['%s: %s' % (ircutils.bold(pkg['name']), formatdesc(pkg['description'])) - for pkg in data["packages"]] - friendly_url = 'https://apps.fedoraproject.org/packages/s/%s' % query - s = format('Found %n: %s; View more at %u', (len(results), 'result'), '; '.join(results), - friendly_url) - irc.reply(s) - @wrap(['positiveInt', additional('somethingWithoutSpaces'), additional('somethingWithoutSpaces'), getopts({'arch': 'somethingWithoutSpaces', 'exact': '', 'startswith': ''})]) def centos(self, irc, msg, args, release, repo, query, opts): @@ -888,34 +706,6 @@ class PkgInfo(callbacks.Plugin): else: irc.error('No results found.') - @wrap(['something', getopts({'exact': ''})]) - def freebsd(self, irc, msg, args, search, optlist): - """ [--exact] - - Searches for in FreeBSD's Ports database (case sensitive). - If --exact is given, only exact port name matches will be shown.""" - search - url = 'https://www.freebsd.org/cgi/ports.cgi?' + urlencode({'query': search}) - data = utils.web.getUrl(url) - soup = BeautifulSoup(data) - res = {} - exact = 'exact' in dict(optlist) - for dt in soup.find_all('dt'): - pkgname = dt.text - if exact and pkgname.rsplit('-', 1)[0] != search: - continue - # In this case, we only want the first line of the description, in order - # to keep things short. - desc = dt.next_sibling.next_sibling.text.split('\n')[0] - res[pkgname] = desc - if res: - # Output results in the form "pkg1: description; pkg2: description; ..." - s = ["%s: %s" % (ircutils.bold(pkg), desc) for pkg, desc in res.items()] - s = format('Found %n: %s; View more at %u', (len(res), 'result'), '; '.join(s), url) - irc.reply(s) - else: - irc.error('No results found.') - Class = PkgInfo # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/PkgInfo/test.py b/PkgInfo/test.py index 9542e00..76b0561 100644 --- a/PkgInfo/test.py +++ b/PkgInfo/test.py @@ -37,59 +37,63 @@ class PkgInfoTestCase(PluginTestCase): timeout = 12 - def testPkg(self): - self.assertRegexp('pkg sid bash', 'Package: .*?bash .*?') - self.assertRegexp('pkg trusty apt', 'Package: .*?apt .*?') + def test_pkg_debian(self): + self.assertNotError('pkg sid bash') self.assertNotError('pkg sid bash --depends') + self.assertNotError('pkg sid ffmpeg --source') self.assertNotError('pkg sid vlc --source --depends') - self.assertError('pkg afdsfsadf asfasfasf') - self.assertError('pkg sid afsadfasfsa') + self.assertError('pkg unittestinvaliddistignore unittestinvalidpackageignore') + self.assertError('pkg sid unittestinvalidpackageignore') + self.assertNotError('pkgsearch debian python') - def testVlist(self): - self.assertError('vlist all afdsafas') + def test_pkg_ubuntu(self): + self.assertNotError('pkg xenial apt') + self.assertNotError('pkg xenial python3 --depends') + self.assertNotError('pkg xenial ubuntu-meta --source') + self.assertNotError('pkg xenial ubuntu-meta --source --depends') + self.assertNotError('pkgsearch ubuntu gtk') + + def test_vlist(self): + self.assertNotError('vlist debian bash') + self.assertNotError('vlist ubuntu variety') + self.assertError('vlist all unittestinvalidpackageignore') self.assertError('vlist invalid-distro firefox') - self.assertRegexp('vlist debian bash', 'Found [1-9][0-9]* ' - 'results: (.*?\(.*?\))+') - @unittest.skip("Remote server is too unreliable (2017-02-23)") - def testArchLinux(self): - self.assertError('archlinux afdsfbjeiog') - self.assertNotError('archlinux bash') - self.assertRegexp('archlinux pacman --exact', - 'Found 1.*?pacman') + #@unittest.skip("Remote server is too unreliable (2017-02-23)") + def test_pkg_arch(self): + self.assertNotError('pkg arch audacity') + self.assertNotError('pkgsearch arch ffmpeg') + self.assertError('pkg arch unittestinvalidpackageignore') + self.assertError('pkgsearch arch unittestinvalidpackageignore') - @unittest.skip("Remote server is too unreliable (2017-02-23)") - def testArchAUR(self): - self.assertRegexp('archaur yaourt', 'Found [1-9][0-9]* results:' - '.*?yaourt.*?') + #@unittest.skip("Remote server is too unreliable (2017-02-23)") + def test_pkg_archaur(self): + self.assertNotError('pkg archaur yaourt') + self.assertNotError('pkgsearch archaur yaourt') + self.assertError('pkg archaur unittestinvalidpackageignore') + self.assertError('pkgsearch archaur unittestinvalidpackageignore') - def testMintPkg(self): - self.assertRegexp('linuxmint rebecca cinnamon', 'session') + def test_pkg_mint(self): + self.assertNotError('pkgsearch sonya cinnamon') + self.assertNotError('pkg sonya cinnamon') + self.assertError('pkg mint unittestinvalidpackageignore') + self.assertError('pkgsearch mint unittestinvalidpackageignore') - def testPkgsearch(self): - self.assertRegexp('pkgsearch debian python', 'python') + def test_pkg_freebsd(self): + self.assertNotError('pkg freebsd lxterminal') + self.assertNotError('pkgsearch freebsd gnome') + self.assertRegexp('pkg freebsd python3 --depends', 'python3.*?requires') + self.assertError('pkg freebsd unittestinvalidpackageignore') + self.assertError('pkgsearch freebsd unittestinvalidpackageignore') - def testFilesearch(self): + def test_filesearch(self): self.assertRegexp('filesearch sid supybot', 'limnoria') - def testFedora(self): - self.assertRegexp('fedora --release master bash*', 'bash') - self.assertRegexp('fedora gnome-terminal', 'GNOME') - # Not found queries that don't have any wildcards in them - # should tell the user to try wrapping the search with *'s, since - # Fedora's API always uses glob matches. - self.assertRegexp('fedora sfasdfadsfasdfas', 'Try wrapping your query with \*') - - def testCentOS(self): + def test_centos(self): self.assertRegexp('centos 7 os git-', 'git-all') self.assertRegexp('centos 6 os bash --arch i386', 'i686.rpm') self.assertNotError('centos 7 extras python') # This should be stripped. self.assertNotRegexp('centos 7 extras "a"', 'Parent Directory') - def testFreeBSD(self): - self.assertRegexp('freebsd lxterminal --exact', 'Found 1 result:.*?LXDE') - self.assertNotError('freebsd bash') - self.assertError('freebsd asdfasjkfalewrghaekglae') - # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: