ubuntu-bots/PackageInfo/config.py

161 lines
7.3 KiB
Python

# -*- Encoding: utf-8 -*-
###
# 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 supybot.conf as conf
import supybot.registry as registry
def configure(advanced):
# This will be called by Supybot to configure this module. advanced is
# a bool that specifies whether the user identified himself as an advanced
# user or not. You should affect your configuration by manipulating the
# registry as appropriate.
def makeSourceUbuntu(release):
return """deb http://archive.ubuntu.com/ubuntu/ %s main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ %s main restricted universe multiverse
""" % (release, release)
def makeSourceDebian(release):
return """deb http://deb.debian.org/debian/ %s main contrib non-free
deb-src deb http://deb.debian.org/debian/ %s main contrib non-free
""" % (release, release)
def makeSourceDebianSec(release):
return """deb http://security.debian.org/ %s/updates main contrib non-free
deb-src deb http://security.debian.org/ %s/updates main contrib non-free
""" % (release, release)
from supybot.questions import output, expect, something, yn
import os, subprocess
def anything(prompt, default=None):
"""Because supybot is pure fail"""
from supybot.questions import expect
return expect(prompt, [], default=default)
PackageInfo = conf.registerPlugin('PackageInfo', True)
def getRepeatdelay():
output("How many seconds should the bot wait before repeating package information?")
repeatdelay = something("Enter a number greater or equal to 0.", default=PackageInfo.repeatdelay._default)
try:
repeatdelay = int(repeatdelay)
if repeatdelay < 0:
raise TypeError
except TypeError:
output("Invalid value '%s', it must be an integer greater or equal to 0." % repeatdelay)
return getRepeatdelay()
else:
return repeatdelay
enabled = yn("Enable this plugin in all channels?", default=PackageInfo.enabled._default)
if advanced:
prefixchar = something("Which prefix character should the bot respond to?", default=PackageInfo.prefixchar._default)
defaultRelease = something("What should be the default release when none is specified?", default=PackageInfo.defaultRelease._default)
repeatdelay = getRepeatdelay()
aptdir = something("Which directory should be used for the apt cache when looking up packages?", default=PackageInfo.aptdir._default)
# People tend to think this should be /var/cache/apt
while aptdir.startswith('/var'): #NOTE: This is not a good hack. Maybe just blacklist /var/cache/apt (or use apt to report back the cache dir)
output("NO! Do not use your system's apt directory")
aptdir = something("Which directory should be used for the apt cache when looking up packages?", default=PackageInfo.aptdir._default)
else:
prefixchar = PackageInfo.prefixchar._default
defaultRelease = PackageInfo.defaultRelease._default
repeatdelay = PackageInfo.repeatdelay._default
aptdir = PackageInfo.aptdir._default
PackageInfo.enabled.setValue(enabled)
PackageInfo.prefixchar.setValue(prefixchar)
PackageInfo.defaultRelease.setValue(defaultRelease)
PackageInfo.repeatdelay.setValue(repeatdelay)
PackageInfo.aptdir.setValue(aptdir)
default_dists = set(['bionic', 'focal', 'hirsute', 'impish', 'jammy',
'oldstable', 'stable', 'unstable', 'testing', 'experimental'])
pluginDir = os.path.abspath(os.path.dirname(__file__))
update_apt = os.path.join(pluginDir, 'update_apt')
default_dists.add(defaultRelease)
## Create the aptdir
try:
os.makedirs(aptdir)
except OSError: # The error number would be OS dependent (17 on Linux 2.6, ?? on others). So just pass on this
pass
for release in default_dists:
filename = os.path.join(aptdir, "%s.list" % release)
try:
output("Creating %s" % filename)
fd = open(filename, 'wb')
fd.write("# Apt sources list for %s\n" % release)
if release in ('oldstable', 'stable', 'unstable', 'testing', 'experimental'):
fd.write(makeSourceDebian(release))
if release in ('oldstable', 'stable', 'testing'):
fd.write(makeSourceDebian(release + '-updates'))
fd.write(makeSourceDebianSec(release))
else:
fd.write(makeSourceUbuntu(release))
fd.write(makeSourceUbuntu(release + '-updates'))
fd.write(makeSourceUbuntu(release + '-security'))
fd.close()
if release in ('unstable', 'experimental'):
continue
for sub in ('backports', 'proposed'):
sub_release = "%s-%s" % (release, sub)
filename = os.path.join(aptdir, "%s.list" % sub_release)
output("Creating %s" % filename)
fd = open(filename, 'wb')
fd.write("# Apt sources list for %s\n" % sub_release)
if release in ('oldstable', 'stable', 'testing'):
fd.write(makeSourceDebian(sub_release.replace('proposed', 'proposed-updates')))
else:
fd.write(makeSourceUbuntu(sub_release))
fd.close()
except Exception as e:
output("Error writing to %r: %r (%s)" % (filename, str(e), type(e)))
if yn("In order for the plugin to use these sources, you must run the 'update_apt' script, do you want to do this now?", default=True):
os.environ['DIR'] = aptdir # the update_apt script checks if DIR is set and uses it if it is
if subprocess.getstatus(update_apt) != 0:
output("There was an error running update_apt, please run '%s -v' to get more information" % update_apt)
if subprocess.getstatusoutput('which apt-file') != 0:
output("You need to install apt-file in order to use the !find command of this plugin")
PackageInfo = conf.registerPlugin('PackageInfo')
conf.registerChannelValue(PackageInfo, 'enabled',
registry.Boolean(True, "Enable package lookup"))
conf.registerChannelValue(PackageInfo, 'prefixchar',
conf.ValidPrefixChars('!', "Character the bot will respond to"))
conf.registerChannelValue(PackageInfo, 'defaultRelease',
registry.String('impish', "Default release to use when none is specified"))
conf.registerChannelValue(PackageInfo, 'repeatdelay',
registry.Integer(60, "Number of seconds to wait between repeated package info calls"))
conf.registerChannelValue(PackageInfo, 'listReleasesOnError',
registry.Boolean(True, "Send list of all valid releases in private on error"))
conf.registerGlobalValue(PackageInfo, 'aptdir',
conf.Directory(conf.supybot.directories.data.dirize('aptdir'), "Path to the apt directory", private=True))