Plugin deb_packages: prepare migration to Python3

This commit is contained in:
Lars Kruse 2020-11-26 02:20:37 +01:00
parent 518644057e
commit 5301b7f5d5
1 changed files with 35 additions and 35 deletions

View File

@ -27,6 +27,9 @@ TODO: check whether cachefile matches the config
BUG: If a package will be upgraded, and brings in new dependencies, BUG: If a package will be upgraded, and brings in new dependencies,
these new deps will not be counted. WONTFIX these new deps will not be counted. WONTFIX
""" """
from __future__ import print_function
import sys import sys
import argparse import argparse
import apt_pkg import apt_pkg
@ -39,10 +42,11 @@ import re
from collections import defaultdict, namedtuple from collections import defaultdict, namedtuple
from types import StringTypes, TupleType, DictType, ListType, BooleanType from types import StringTypes, TupleType, DictType, ListType, BooleanType
class EnvironmentConfigBroken(Exception): pass class EnvironmentConfigBroken(Exception): pass
# print environmental things # print environmental things
# for k,v in os.environ.iteritems(): print >> sys.stderr, "%r : %r" % (k,v) # for k,v in os.environ.items(): print("%r : %r" % (k,v), file=sys.stderr)
def getEnv(name, default=None, cast=None): def getEnv(name, default=None, cast=None):
""" """
@ -59,7 +63,7 @@ def getEnv(name, default=None, cast=None):
var = default var = default
except: except:
# now probably the cast went wrong # now probably the cast went wrong
print >> sys.stderr, "for environment variable %r, %r is no valid value"%(name, var) print("for environment variable %r, %r is no valid value" % (name, var), file=sys.stderr)
var = default var = default
return var return var
@ -217,19 +221,19 @@ class TreeTwig(defaultdict):
super(TreeTwig, self).__init__(defaultFactory) super(TreeTwig, self).__init__(defaultFactory)
def printAsTree(self, indent=0): def printAsTree(self, indent=0):
for k, tree in self.iteritems(): for k, tree in self.items():
print " " * indent, repr(k) print(" " * indent, repr(k))
if isinstance(tree, TreeTwig): if isinstance(tree, TreeTwig):
printTree(tree, indent+1) printTree(tree, indent+1)
else: else:
print tree print(tree)
def printAsLine(self): def printAsLine(self):
print self.asLine() print(self.asLine())
def asLine(self): def asLine(self):
values = "" values = ""
for key, residue in self.iteritems(): for key, residue in self.items():
if residue: if residue:
values += " %r" % key values += " %r" % key
if isinstance(residue, TreeTwig): if isinstance(residue, TreeTwig):
@ -310,7 +314,7 @@ def getOptionsTree(cache, keys=None):
d = t d = t
for key in keys: for key in keys:
if not key: if not key:
print f print(f)
dKey = f.__getattribute__(key) dKey = f.__getattribute__(key)
d = d[dKey] d = d[dKey]
return t return t
@ -504,14 +508,14 @@ class PackageStat(defaultdict):
"graph_vlabel packages".format(**d) "graph_vlabel packages".format(**d)
def printConfig(self): def printConfig(self):
print self.configHead() print(self.configHead())
for options, item in self.options_sorted: for options, item in self.options_sorted:
if not self.packetHandler.includeNow and self.optionIsDpkgStatus(details=options): if not self.packetHandler.includeNow and self.optionIsDpkgStatus(details=options):
continue continue
i = self.configD(options, item) i = self.configD(options, item)
print "{rrdName}.label {options}".format(**i) print("{rrdName}.label {options}".format(**i))
print "{rrdName}.info {info}".format(**i) print("{rrdName}.info {info}".format(**i))
print "{rrdName}.draw AREASTACK".format(**i) print("{rrdName}.draw AREASTACK".format(**i))
def optionIsDpkgStatus(self, details, options=None): def optionIsDpkgStatus(self, details, options=None):
""" """
@ -530,14 +534,15 @@ class PackageStat(defaultdict):
return isNow return isNow
def printValues(self): def printValues(self):
print "\nmultigraph packages_{option}_{type}".format(option=self.generate_rrd_name_from(self.option), print("\nmultigraph packages_{option}_{type}"
type=self.packetHandler.type) .format(option=self.generate_rrd_name_from(self.option),
type=self.packetHandler.type))
for options, item in self.options_sorted: for options, item in self.options_sorted:
if not self.packetHandler.includeNow and self.optionIsDpkgStatus(details=options): if not self.packetHandler.includeNow and self.optionIsDpkgStatus(details=options):
continue continue
i = self.configD(options, item) i = self.configD(options, item)
i['value'] = len(self.get(options, [])) i['value'] = len(self.get(options, []))
print "{rrdName}.value {value}".format(**i) print("{rrdName}.value {value}".format(**i))
self._printExtInfoPackageList(options) self._printExtInfoPackageList(options)
def _printExtInfoPackageList(self, options): def _printExtInfoPackageList(self, options):
@ -545,10 +550,9 @@ class PackageStat(defaultdict):
packageList = self[options] packageList = self[options]
packageCount = len( packageList ) packageCount = len( packageList )
if 0 < packageCount <= MAX_LIST_SIZE_EXT_INFO: if 0 < packageCount <= MAX_LIST_SIZE_EXT_INFO:
print "%s.extinfo " % rrdName, print("%s.extinfo %s" % (rrdName, " ".join(
for item in packageList: self.packetHandler.extInfoItemString.format(i=item) for item in packageList)))
print self.packetHandler.extInfoItemString.format(i=item),
print
packetHandlerD = {} packetHandlerD = {}
""" Dictionary for PacketHandlerclasses with its 'type'-key """ """ Dictionary for PacketHandlerclasses with its 'type'-key """
@ -644,7 +648,7 @@ class Munin(object):
} }
self.envConfig = self._envParser() self.envConfig = self._envParser()
self._envValidater() self._envValidater()
# print >> sys.stderr, self.envConfig # print(self.envConfig, file=sys.stderr)
self.statL = [] self.statL = []
if self.envConfig: if self.envConfig:
for config in self.envConfig: for config in self.envConfig:
@ -655,7 +659,7 @@ class Munin(object):
extInfo = config['show_ext']) extInfo = config['show_ext'])
self.statL.append(packageStat) self.statL.append(packageStat)
if not self.statL: if not self.statL:
print "# no munin config found in environment vars" print("# no munin config found in environment vars")
def execute(self): def execute(self):
self.args = self.argParser.parse_args(self.commandLineArgs) self.args = self.argParser.parse_args(self.commandLineArgs)
@ -715,7 +719,7 @@ class Munin(object):
out = StringIO.StringIO() out = StringIO.StringIO()
sys.stdout = out sys.stdout = out
# run writes now to new sys.stdout # run writes now to new sys.stdout
print "# executed at %r (%r)" %(strftime("%s"), strftime("%c")) print("# executed at %r (%r)" % (strftime("%s"), strftime("%c")))
self._run() self._run()
sys.stdout = stdoutDef sys.stdout = stdoutDef
# print output to stdout # print output to stdout
@ -729,13 +733,13 @@ class Munin(object):
# 'No such file or directory' # 'No such file or directory'
os.makedirs( os.path.dirname(CACHE_FILE) ) os.makedirs( os.path.dirname(CACHE_FILE) )
else: else:
print sys.stderr.write("%r : %r" % (e, CACHE_FILE)) sys.stderr.write("%r : %r" % (e, CACHE_FILE))
finally: finally:
# restore stdout # restore stdout
sys.stdout = stdoutDef sys.stdout = stdoutDef
else: else:
with open(CACHE_FILE,'r') as data: with open(CACHE_FILE,'r') as data:
print data.read() print(data.read())
def _run(self): def _run(self):
# p … package # p … package
@ -760,7 +764,7 @@ class Munin(object):
stat.printConfig() stat.printConfig()
def autoconf(self): def autoconf(self):
print 'yes' print('yes')
def _argParser(self): def _argParser(self):
parser = argparse.ArgumentParser(description="Show some statistics "\ parser = argparse.ArgumentParser(description="Show some statistics "\
@ -815,7 +819,7 @@ class Munin(object):
elif m.group('res') == 'show_ext': elif m.group('res') == 'show_ext':
configPart['show_ext'][m.group('optNumber')] = os.getenv(var) configPart['show_ext'][m.group('optNumber')] = os.getenv(var)
else: else:
print >> sys.stderr, "configuration option %r was ignored" % (var) print("configuration option %r was ignored" % (var), file=sys.stderr)
# we have now dicts for 'sort_by' and 'show_ext' keys # we have now dicts for 'sort_by' and 'show_ext' keys
# changing them to lists # changing them to lists
for graphConfig in config.itervalues(): for graphConfig in config.itervalues():
@ -830,25 +834,21 @@ class Munin(object):
""" """
for graph in self.envConfig: for graph in self.envConfig:
if graph['type'] not in ('installed', 'upgradable'): if graph['type'] not in ('installed', 'upgradable'):
print >> sys.stderr, \ print("GraphType must be 'installed' or 'upgradable' but not %r %s"
"GraphType must be 'installed' or 'upgradable' but not %r"%(graph.type), \ % (graph.type, graph), file=sys.stderr)
graph
raise EnvironmentConfigBroken("Environment Config broken") raise EnvironmentConfigBroken("Environment Config broken")
if not graph['sort_by']: if not graph['sort_by']:
print >> sys.stderr, \ print("Graph must be sorted by anything", file=sys.stderr)
"Graph must be sorted by anything"
raise EnvironmentConfigBroken("Environment Config broken") raise EnvironmentConfigBroken("Environment Config broken")
# check for valid options for sort_by # check for valid options for sort_by
unusableOptions = set(graph['sort_by']) - PackageStat.viewSet unusableOptions = set(graph['sort_by']) - PackageStat.viewSet
if unusableOptions: if unusableOptions:
print >> sys.stderr, \ print("%r are not valid options for 'sort_by'" % unusableOptions, file=sys.stderr)
"%r are not valid options for 'sort_by'" % (unusableOptions)
raise EnvironmentConfigBroken("Environment Config broken") raise EnvironmentConfigBroken("Environment Config broken")
# check for valid options for sort_by # check for valid options for sort_by
unusableOptions = set(graph['show_ext']) - PackageStat.viewSet unusableOptions = set(graph['show_ext']) - PackageStat.viewSet
if unusableOptions: if unusableOptions:
print >> sys.stderr, \ print("%r are not valid options for 'show_ext'" % x, file=sys.stderr)
"%r are not valid options for 'show_ext'" % (x)
raise EnvironmentConfigBroken("Environment Config broken") raise EnvironmentConfigBroken("Environment Config broken")
if __name__=='__main__': if __name__=='__main__':