Updating d/packaging-helper.py from monitoring-plugins-contrib

This commit is contained in:
Jan Wagner 2020-09-10 16:01:12 +02:00
parent e7b9753fa7
commit 98a99aa548

View file

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys import sys
@ -16,10 +16,15 @@ ALLOWED_FIELDS = ('Suggests',
'Description', 'Description',
'Build-Depends') 'Build-Depends')
ALLOWED_TESTS_FIELDS = ('Depends',
'Restrictions',
'Tests',
'Test-Command')
# find all plugins # find all plugins
__basedir__ = os.path.realpath(os.path.dirname(sys.argv[0]) + os.path.sep + '..') __basedir__ = os.path.realpath(os.path.dirname(sys.argv[0]) + os.path.sep + '..')
__plugins__ = [p for p in os.listdir(__basedir__) __plugins__ = [p for p in os.listdir(__basedir__)
if (os.path.isdir(__basedir__ + os.path.sep + p) and p!='debian' and p!='.git' and p!='.pc')] if (os.path.isdir(__basedir__ + os.path.sep + p) and p!='debian' and p!='.git' and p!='.pc' and p!='redhat')]
__plugins__.sort() __plugins__.sort()
@ -30,19 +35,31 @@ def __get_control_data__():
# returns (plug, parsed control field data) # returns (plug, parsed control field data)
# We look at the first paragraph only! # We look at the first paragraph only!
for plugin in __plugins__: for plugin in __plugins__:
data=(plugin, [x for x in deb822.Packages.iter_paragraphs(file(__basedir__ + os.path.sep+ plugin + os.path.sep + 'control'))][0]) data=(plugin, [x for x in deb822.Packages.iter_paragraphs(open(__basedir__ + os.path.sep+ plugin + os.path.sep + 'control'))][0])
for key in data[1].iterkeys(): for key in data[1].keys():
if key not in ALLOWED_FIELDS: if key not in ALLOWED_FIELDS:
raise Exception("Unknown control field in plugin %s: %s" %(data[0],key)) raise Exception("Unknown control field in plugin %s: %s" %(data[0],key))
yield data yield data
def __get_tests_data__():
# returns (plug, parsed control field data)
# We look at the first paragraph only!
for plugin in __plugins__:
tests_file = __basedir__ + os.path.sep+ plugin + os.path.sep + 'tests'
if os.path.exists(tests_file):
data=(plugin, [x for x in deb822.Packages.iter_paragraphs(open(tests_file))][0])
for key in data[1].keys():
if key not in ALLOWED_TESTS_FIELDS:
raise Exception("Unknown tests/control field in plugin %s: %s" %(data[0],key))
yield data
def generate_debian_readme_plugins(): def generate_debian_readme_plugins():
plugins_depends={} plugins_depends={}
for plugin, _control in __get_control_data__(): for plugin, _control in __get_control_data__():
plugins_depends[plugin]={} plugins_depends[plugin]={}
# look trough keys we might want to merge # look trough keys we might want to merge
for key in ['Suggests', 'Recommends']: for key in ['Suggests', 'Recommends']:
if _control.has_key(key): if key in _control:
plugins_depends[plugin][key]=deb822.PkgRelation.parse_relations(_control[key]) plugins_depends[plugin][key]=deb822.PkgRelation.parse_relations(_control[key])
# check for generated substvars files # check for generated substvars files
@ -52,7 +69,7 @@ def generate_debian_readme_plugins():
substvars = fd.read() substvars = fd.read()
try: try:
rel = deb822.PkgRelation.parse_relations(__shlibs_re__.findall(substvars)[0]) rel = deb822.PkgRelation.parse_relations(__shlibs_re__.findall(substvars)[0])
if plugins_depends[plugin].has_key('Recommends'): if 'Recommends' in plugins_depends[plugin]:
plugins_depends[plugin]['Recommends'].extend(rel) plugins_depends[plugin]['Recommends'].extend(rel)
else: else:
plugins_depends[plugin]['Recommends']=rel plugins_depends[plugin]['Recommends']=rel
@ -64,12 +81,12 @@ def generate_debian_readme_plugins():
for plugin in __plugins__: for plugin in __plugins__:
if len(plugins_depends[plugin]) > 0: if len(plugins_depends[plugin]) > 0:
rtext = '%s:' %(plugin,) rtext = '%s:' %(plugin,)
if plugins_depends[plugin].has_key('Recommends'): if 'Recommends' in plugins_depends[plugin]:
rtext = '%s\n Required Packages: %s' %( rtext = '%s\n Required Packages: %s' %(
rtext, rtext,
deb822.PkgRelation.str(plugins_depends[plugin]['Recommends']) deb822.PkgRelation.str(plugins_depends[plugin]['Recommends'])
) )
if plugins_depends[plugin].has_key('Suggests'): if 'Suggests' in plugins_depends[plugin]:
rtext = '%s\n Optional Packages: %s' %( rtext = '%s\n Optional Packages: %s' %(
rtext, rtext,
deb822.PkgRelation.str(plugins_depends[plugin]['Suggests']) deb822.PkgRelation.str(plugins_depends[plugin]['Suggests'])
@ -98,22 +115,22 @@ def update_control():
for plugin, _control in __get_control_data__(): for plugin, _control in __get_control_data__():
# look trough keys we might want to merge # look trough keys we might want to merge
if _control.has_key('Depends'): if 'Depends' in _control:
print "Don't use 'Depends' in %s/control - use 'Recommends' instead" %(plugin,) print("Don't use 'Depends' in %s/control - use 'Recommends' instead" %(plugin,))
sys.exit(1) sys.exit(1)
for key in ['Build-Depends', 'Suggests', 'Recommends']: for key in ['Build-Depends', 'Suggests', 'Recommends']:
if _control.has_key(key): if key in _control:
for rel in deb822.PkgRelation.parse_relations(_control[key]): for rel in deb822.PkgRelation.parse_relations(_control[key]):
if not rel in control_data[key]: if not rel in control_data[key]:
control_data[key].append(rel) control_data[key].append(rel)
# extract description # extract description
description = ' * %s' %(plugin,) description = ' * %s' %(plugin,)
if _control.has_key('Version'): if 'Version' in _control:
description = '%s (%s)' %(description, _control['Version']) description = '%s (%s)' %(description, _control['Version'])
try: try:
description = '%s: %s' %(description, _control['Description'].replace('\n','\n ')) description = '%s: %s' %(description, _control['Description'].replace('\n','\n '))
except KeyError: except KeyError:
print 'Description for plugin %s missing!' %(plugin,) print('Description for plugin %s missing!' %(plugin,))
sys.exit(1) sys.exit(1)
try: try:
@ -134,11 +151,11 @@ def update_control():
with open(__basedir__ + os.path.sep + 'debian' + os.path.sep + 'control.in', 'r') as f: with open(__basedir__ + os.path.sep + 'debian' + os.path.sep + 'control.in', 'r') as f:
control_in = f.read() control_in = f.read()
for k, v in control_data.iteritems(): for k, v in control_data.items():
if k == 'Description': if k == 'Description':
control_in = control_in.replace('#AUTO_UPDATE_Description#', u'\n'.join(v)) control_in = control_in.replace('#AUTO_UPDATE_Description#', '\n'.join(v))
elif k == 'Uploaders': elif k == 'Uploaders':
control_in = control_in.replace('#AUTO_UPDATE_Uploaders#', u', '.join(v)) control_in = control_in.replace('#AUTO_UPDATE_Uploaders#', ', '.join(v))
else: else:
control_in = control_in.replace('#AUTO_UPDATE_%s#' %(k, ), deb822.PkgRelation.str(v)) control_in = control_in.replace('#AUTO_UPDATE_%s#' %(k, ), deb822.PkgRelation.str(v))
@ -146,6 +163,14 @@ def update_control():
f.write(control_in) f.write(control_in)
def update_tests():
with open(__basedir__ + os.path.sep + 'debian' + os.path.sep + 'tests' + os.path.sep + 'control', 'w') as f:
f.write("# DO NOT HAND HACK - MANAGED BY debian/packaging-helper.py\n#\n#\n")
for plugin, _control in __get_tests_data__():
if 'Depends' in _control and '@' not in _control['Depends']:
_control['Depends'] += ', @'
f.write(str(_control))
f.write("\n")
def update_copyright(): def update_copyright():
@ -153,25 +178,25 @@ def update_copyright():
copyrights = [] copyrights = []
for plugin, _control in __get_control_data__(): for plugin, _control in __get_control_data__():
_p_copyright = '%s:\n\n' %(plugin,) _p_copyright = '%s:\n\n' %(plugin,)
if _control.has_key('Homepage'): if 'Homepage' in _control:
_p_copyright = '%sThe plugin was downloaded from: \n%s\n\n' %(_p_copyright, _control['Homepage']) _p_copyright = '%sThe plugin was downloaded from: \n%s\n\n' %(_p_copyright, _control['Homepage'])
try: try:
with open(__basedir__ + os.path.sep + plugin + os.path.sep + 'copyright', 'r') as f: with open(__basedir__ + os.path.sep + plugin + os.path.sep + 'copyright', 'r') as f:
_p_copyright = '%s %s' %(_p_copyright, f.read().decode('utf-8').replace('\n','\n ')) _p_copyright = '%s %s' %(_p_copyright, f.read().replace('\n','\n '))
except IOError: except IOError:
print 'copyright file for plugin %s missing!' %(plugin,) print('copyright file for plugin %s missing!' %(plugin,))
sys.exit(1) sys.exit(1)
copyrights.append(_p_copyright) copyrights.append(_p_copyright)
with open(__basedir__ + os.path.sep + 'debian' + os.path.sep + 'copyright.in', 'r') as f: with open(__basedir__ + os.path.sep + 'debian' + os.path.sep + 'copyright.in', 'r') as f:
copyright_in = f.read().decode('utf-8') copyright_in = f.read()
copyright_in = copyright_in.replace('#AUTO_UPDATE_Copyright#', u'\n\n------------------------------------------------------------------------------\n\n'.join(copyrights)) copyright_in = copyright_in.replace('#AUTO_UPDATE_Copyright#', '\n\n------------------------------------------------------------------------------\n\n'.join(copyrights))
with open(__basedir__ + os.path.sep + 'debian' + os.path.sep + 'copyright', 'w') as f: with open(__basedir__ + os.path.sep + 'debian' + os.path.sep + 'copyright', 'w') as f:
f.write(copyright_in.encode('utf-8')) f.write(copyright_in)
def watch(): def watch():
@ -181,39 +206,39 @@ def watch():
import hashlib import hashlib
import urllib2 import urllib.request, urllib.error, urllib.parse
url_opener = urllib2.build_opener() url_opener = urllib.request.build_opener()
url_opener.addheaders = [('User-agent', 'Debian nagios-plugins-contrib 1.0')] url_opener.addheaders = [('User-agent', 'Debian nagios-plugins-contrib 1.0')]
watch_re = re.compile(r'([^ ]+) (.+)') watch_re = re.compile(r'([^ ]+) (.+)')
whitespace_re = re.compile(r'\s') whitespace_re = re.compile(r'\s')
for plugin, _control in __get_control_data__(): for plugin, _control in __get_control_data__():
if not _control.has_key('Watch'): if 'Watch' not in _control:
print 'WARNING: %s - missing watch information!' %(plugin,) print('WARNING: %s - missing watch information!' %(plugin,))
continue continue
try: try:
url, check = watch_re.findall(_control['Watch'])[0] url, check = watch_re.findall(_control['Watch'])[0]
except IndexError: except IndexError:
print 'WARNING: %s - failed to parse Watch line!' %(plugin,) print('WARNING: %s - failed to parse Watch line!' %(plugin,))
continue continue
try: try:
f=url_opener.open(url) f=url_opener.open(url)
content = f.read() content = f.read().decode('utf-8')
f.close() f.close()
except IOError: except IOError:
print 'WARNING: %s - failed to retrieve %s !' %(plugin,url) print('WARNING: %s - failed to retrieve %s !' %(plugin,url))
continue continue
check=check.strip() check=check.strip()
if check.startswith('SHA1:'): if check.startswith('SHA1:'):
check=check.replace('SHA1:','') check=check.replace('SHA1:','')
new_sha=hashlib.sha1(content).hexdigest() new_sha=hashlib.sha1(content).hexdigest()
if check != new_sha: if check != new_sha:
print 'UPDATE NECESSARY: %s - SHA1 checksum does not match! New checksum: %s' %(plugin,new_sha) print('UPDATE NECESSARY: %s - SHA1 checksum does not match! New checksum: %s' %(plugin,new_sha))
else: else:
print 'OK: %s' %(plugin,) print('OK: %s' %(plugin,))
else: else:
if not _control.has_key('Version'): if 'Version' not in _control:
print 'WARNING: %s - missing current version information!' %(plugin,) print('WARNING: %s - missing current version information!' %(plugin,))
continue continue
check_re=re.compile(check) check_re=re.compile(check)
# check for simple matches # check for simple matches
@ -224,7 +249,7 @@ def watch():
if not v in found_versions: if not v in found_versions:
found_versions.append(v) found_versions.append(v)
if not found_versions: if not found_versions:
print "WARNING: %s - regex does not match!" %(plugin) print("WARNING: %s - regex does not match!" %(plugin))
continue continue
new_version = found_versions[0] new_version = found_versions[0]
@ -232,11 +257,11 @@ def watch():
if (apt_pkg.version_compare(v, found_versions[0]) > 0): if (apt_pkg.version_compare(v, found_versions[0]) > 0):
new_version = v new_version = v
if (apt_pkg.version_compare(new_version, _control['Version'].strip()) > 0): if (apt_pkg.version_compare(new_version, _control['Version'].strip()) > 0):
print 'UPDATE NECESSARY: %s - found new version %s' %(plugin, new_version) print('UPDATE NECESSARY: %s - found new version %s' %(plugin, new_version))
elif (apt_pkg.version_compare(new_version, _control['Version'].strip()) < 0): elif (apt_pkg.version_compare(new_version, _control['Version'].strip()) < 0):
print 'WARNING: %s - could not find the current version (found: %s, control says: %s)!' %(plugin, new_version, _control['Version']) print('WARNING: %s - could not find the current version (found: %s, control says: %s)!' %(plugin, new_version, _control['Version']))
else: else:
print 'OK: %s' %(plugin,) print('OK: %s' %(plugin,))
@ -262,6 +287,14 @@ if __name__ == '__main__':
help='Update debian/control' help='Update debian/control'
) )
parser.add_option(
'--tests',
dest='tests',
action='store_true',
default=False,
help='Update debian/tests/control'
)
parser.add_option( parser.add_option(
'--watch', '--watch',
dest='watch', dest='watch',
@ -278,13 +311,16 @@ if __name__ == '__main__':
) )
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if not (options.control or options.copyright or options.watch or options.generate_readme): if not (options.control or options.copyright or options.watch or options.tests or options.generate_readme):
parser.print_help() parser.print_help()
sys.exit(1) sys.exit(1)
if options.control: if options.control:
update_control() update_control()
if options.tests:
update_tests()
if options.copyright: if options.copyright:
update_copyright() update_copyright()