197 lines
7.9 KiB
Python
197 lines
7.9 KiB
Python
# -*- Encoding: utf-8 -*-
|
|
###
|
|
# Copyright (c) 2006-2007 Dennis Kaarsemaker
|
|
# Copyright (c) 2008-2010 Terence Simpson
|
|
# Copyright (c) 2017- Krytarik Raido
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of version 2 of the GNU General Public License as
|
|
# published by the Free Software Foundation.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
###
|
|
|
|
import warnings
|
|
warnings.filterwarnings("ignore", "apt API not stable yet", FutureWarning)
|
|
import subprocess, os, apt, re
|
|
import supybot.utils as utils
|
|
from email.parser import FeedParser
|
|
|
|
def component(arg):
|
|
if '/' in arg:
|
|
return arg[:arg.find('/')]
|
|
return 'main'
|
|
|
|
def description(pkg):
|
|
if 'Description-en' in pkg:
|
|
return pkg['Description-en'].split('\n')[0]
|
|
elif 'Description' in pkg:
|
|
return pkg['Description'].split('\n')[0]
|
|
return "Description not available"
|
|
|
|
def apt_cache(aptdir, distro, cmd, pkg):
|
|
return subprocess.check_output(['apt-cache',
|
|
'-oAPT::Architecture=amd64',
|
|
'-oAPT::Architectures::=i386',
|
|
'-oAPT::Architectures::=amd64',
|
|
'-oDir::State::Lists=%s/%s' % (aptdir, distro),
|
|
'-oDir::State::Status=%s/%s.status' % (aptdir, distro),
|
|
'-oDir::Etc::SourceList=%s/%s.list' % (aptdir, distro),
|
|
'-oDir::Etc::SourceParts=""',
|
|
'-oDir::Cache=%s/cache' % aptdir] +
|
|
cmd + [pkg.lower()]).decode('utf8')
|
|
|
|
def apt_file(aptdir, distro, pkg):
|
|
return subprocess.check_output(['apt-file',
|
|
'-s', '%s/%s.list' % (aptdir, distro),
|
|
'-c', '%s/apt-file/%s' % (aptdir, distro),
|
|
'-l', '-i', '-a', 'amd64',
|
|
'search', pkg]).decode('utf8')
|
|
|
|
class Apt:
|
|
def __init__(self, plugin):
|
|
self.aptdir = plugin.registryValue('aptdir')
|
|
self.distros = []
|
|
self.plugin = plugin
|
|
self.log = plugin.log
|
|
os.environ["LANG"] = "C"
|
|
if self.aptdir:
|
|
self.distros = sorted([x[:-5] for x in os.listdir(self.aptdir) if x.endswith('.list')])
|
|
|
|
def _parse(self, pkg):
|
|
parser = FeedParser()
|
|
parser.feed(pkg)
|
|
return parser.close()
|
|
|
|
def find(self, pkg, distro, filelookup=True):
|
|
if distro not in self.distros:
|
|
return "%r is not a valid release: %s" % (distro, ", ".join(self.distros))
|
|
|
|
if distro.split('-')[0] in ('oldstable', 'stable', 'unstable', 'testing', 'experimental'):
|
|
pkgTracURL = "https://packages.debian.org"
|
|
else:
|
|
pkgTracURL = "https://packages.ubuntu.com"
|
|
|
|
try:
|
|
data = apt_cache(self.aptdir, distro, ['search', '-n'], pkg)
|
|
except subprocess.CalledProcessError as e:
|
|
data = e.output
|
|
if not data:
|
|
if filelookup:
|
|
try:
|
|
data = apt_file(self.aptdir, distro, pkg).split()
|
|
except subprocess.CalledProcessError as e:
|
|
data = e.output
|
|
if data:
|
|
if data[0] == 'sh:': # apt-file isn't installed
|
|
self.log.error("PackageInfo/packages: apt-file is not installed")
|
|
return "Please use %s/ to search for files" % pkgTracURL
|
|
if data[0] == 'E:': # No files in the cache dir
|
|
self.log.error("PackageInfo/packages: Please run the 'update_apt_file' script")
|
|
return "Cache out of date, please contact the administrator"
|
|
if data[0] == "Use" and data[1] == "of":
|
|
return "%s/search?searchon=contents&keywords=%s&mode=exactfilename&suite=%s&arch=any" % (pkgTracURL, utils.web.urlquote(pkg), distro)
|
|
if len(data) > 10:
|
|
return "File %s found in %s and %d others <%s/search?searchon=contents&keywords=%s&mode=exactfilename&suite=%s&arch=any>" % (pkg, ', '.join(data[:10]), len(data)-10, pkgTracURL, utils.web.urlquote(pkg), distro)
|
|
return "File %s found in %s" % (pkg, ', '.join(data))
|
|
return 'Package/file %s does not exist in %s' % (pkg, distro)
|
|
return "No packages matching '%s' could be found" % pkg
|
|
pkgs = [x.split()[0] for x in data.split('\n') if x]
|
|
if len(pkgs) > 10:
|
|
return "Found: %s and %d others <%s/search?keywords=%s&searchon=names&suite=%s§ion=all>" % (', '.join(pkgs[:10]), len(pkgs)-10, pkgTracURL, utils.web.urlquote(pkg), distro)
|
|
else:
|
|
return "Found: %s" % ', '.join(pkgs[:5])
|
|
|
|
def raw_info(self, pkg, distro, archlookup=True):
|
|
if distro not in self.distros:
|
|
return "%r is not a valid release: %s" % (distro, ", ".join(self.distros))
|
|
|
|
try:
|
|
data = apt_cache(self.aptdir, distro, ['show'], pkg)
|
|
except subprocess.CalledProcessError as e:
|
|
data = e.output
|
|
if not data or 'E: No packages found' in data:
|
|
return 'Package %s does not exist in %s' % (pkg, distro)
|
|
|
|
maxp = {'Version': '0~'}
|
|
packages = list(map(self._parse, [x for x in data.split('\n\n') if x]))
|
|
for p in packages:
|
|
if apt.apt_pkg.version_compare(maxp['Version'], p['Version']) <= 0:
|
|
maxp = p
|
|
|
|
if not archlookup:
|
|
return maxp
|
|
|
|
try:
|
|
data2 = apt_cache(self.aptdir, distro, ['showsrc'], pkg)
|
|
except subprocess.CalledProcessError:
|
|
return maxp
|
|
|
|
maxp2 = {'Version': '0~'}
|
|
packages2 = list(map(self._parse, [x for x in data2.split('\n\n') if x]))
|
|
for p in packages2:
|
|
if apt.apt_pkg.version_compare(maxp2['Version'], p['Version']) <= 0:
|
|
maxp2 = p
|
|
|
|
archs = re.match(r'.*^ %s \S+ \S+ \S+ arch=(?P<arch>\S+)$' % re.escape(pkg), maxp2['Package-List'],
|
|
re.I | re.M | re.DOTALL)
|
|
if archs:
|
|
archs = archs.group('arch').split(',')
|
|
if not ('any' in archs or 'all' in archs):
|
|
maxp['Architectures'] = ', '.join(archs)
|
|
|
|
return maxp
|
|
|
|
def info(self, pkg, distro):
|
|
maxp = self.raw_info(pkg, distro)
|
|
if isinstance(maxp, str):
|
|
return maxp
|
|
return("%s (source: %s): %s. In component %s, is %s. Version %s (%s), package size %s kB, installed size %s kB%s" %
|
|
(maxp['Package'], maxp.get('Source', None) or maxp['Package'], description(maxp), component(maxp['Section']),
|
|
maxp['Priority'], maxp['Version'], distro, int(maxp['Size'])/1024, maxp['Installed-Size'],
|
|
". (Only available for %s)" % maxp['Architectures'] if maxp.get('Architectures', None) else ""))
|
|
|
|
def depends(self, pkg, distro):
|
|
maxp = self.raw_info(pkg, distro, archlookup=False)
|
|
if isinstance(maxp, str):
|
|
return maxp
|
|
return("%s (version: %s, %s): Depends on %s%s" %
|
|
(maxp['Package'], maxp['Version'], distro, maxp.get('Depends', None) or "nothing",
|
|
". Recommends %s" % maxp['Recommends'] if maxp.get('Recommends', None) else ""))
|
|
|
|
# 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/bot/aptdir"
|
|
|
|
try:
|
|
(command, lookup) = argv[1].split(None, 1)
|
|
except:
|
|
print("Need something to look up")
|
|
sys.exit(1)
|
|
dist = "zesty"
|
|
if argc == 3:
|
|
dist = argv[2]
|
|
plugin = FakePlugin()
|
|
aptlookup = Apt(plugin)
|
|
print(getattr(aptlookup, command)(lookup, dist))
|