From 98a99aa5484eb8385641e4f83ac4771411144e28 Mon Sep 17 00:00:00 2001 From: Jan Wagner Date: Thu, 10 Sep 2020 16:01:12 +0200 Subject: [PATCH] Updating d/packaging-helper.py from monitoring-plugins-contrib --- debian/packaging-helper.py | 112 ++++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 38 deletions(-) diff --git a/debian/packaging-helper.py b/debian/packaging-helper.py index 42bf5f5..bd01094 100755 --- a/debian/packaging-helper.py +++ b/debian/packaging-helper.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys @@ -16,10 +16,15 @@ ALLOWED_FIELDS = ('Suggests', 'Description', 'Build-Depends') +ALLOWED_TESTS_FIELDS = ('Depends', + 'Restrictions', + 'Tests', + 'Test-Command') + # find all plugins __basedir__ = os.path.realpath(os.path.dirname(sys.argv[0]) + os.path.sep + '..') __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() @@ -30,19 +35,31 @@ def __get_control_data__(): # returns (plug, parsed control field data) # We look at the first paragraph only! 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]) - for key in data[1].iterkeys(): + 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].keys(): if key not in ALLOWED_FIELDS: raise Exception("Unknown control field in plugin %s: %s" %(data[0],key)) 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(): plugins_depends={} for plugin, _control in __get_control_data__(): plugins_depends[plugin]={} # look trough keys we might want to merge for key in ['Suggests', 'Recommends']: - if _control.has_key(key): + if key in _control: plugins_depends[plugin][key]=deb822.PkgRelation.parse_relations(_control[key]) # check for generated substvars files @@ -52,7 +69,7 @@ def generate_debian_readme_plugins(): substvars = fd.read() try: 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) else: plugins_depends[plugin]['Recommends']=rel @@ -64,12 +81,12 @@ def generate_debian_readme_plugins(): for plugin in __plugins__: if len(plugins_depends[plugin]) > 0: rtext = '%s:' %(plugin,) - if plugins_depends[plugin].has_key('Recommends'): + if 'Recommends' in plugins_depends[plugin]: rtext = '%s\n Required Packages: %s' %( rtext, 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, deb822.PkgRelation.str(plugins_depends[plugin]['Suggests']) @@ -98,22 +115,22 @@ def update_control(): for plugin, _control in __get_control_data__(): # look trough keys we might want to merge - if _control.has_key('Depends'): - print "Don't use 'Depends' in %s/control - use 'Recommends' instead" %(plugin,) + if 'Depends' in _control: + print("Don't use 'Depends' in %s/control - use 'Recommends' instead" %(plugin,)) sys.exit(1) 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]): if not rel in control_data[key]: control_data[key].append(rel) # extract description description = ' * %s' %(plugin,) - if _control.has_key('Version'): + if 'Version' in _control: description = '%s (%s)' %(description, _control['Version']) try: description = '%s: %s' %(description, _control['Description'].replace('\n','\n ')) except KeyError: - print 'Description for plugin %s missing!' %(plugin,) + print('Description for plugin %s missing!' %(plugin,)) sys.exit(1) try: @@ -134,11 +151,11 @@ def update_control(): with open(__basedir__ + os.path.sep + 'debian' + os.path.sep + 'control.in', 'r') as f: control_in = f.read() - for k, v in control_data.iteritems(): + for k, v in control_data.items(): 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': - control_in = control_in.replace('#AUTO_UPDATE_Uploaders#', u', '.join(v)) + control_in = control_in.replace('#AUTO_UPDATE_Uploaders#', ', '.join(v)) else: control_in = control_in.replace('#AUTO_UPDATE_%s#' %(k, ), deb822.PkgRelation.str(v)) @@ -146,6 +163,14 @@ def update_control(): 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(): @@ -153,25 +178,25 @@ def update_copyright(): copyrights = [] for plugin, _control in __get_control_data__(): _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']) try: 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: - print 'copyright file for plugin %s missing!' %(plugin,) + print('copyright file for plugin %s missing!' %(plugin,)) sys.exit(1) copyrights.append(_p_copyright) 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: - f.write(copyright_in.encode('utf-8')) + f.write(copyright_in) def watch(): @@ -181,39 +206,39 @@ def watch(): import hashlib - import urllib2 - url_opener = urllib2.build_opener() + import urllib.request, urllib.error, urllib.parse + url_opener = urllib.request.build_opener() url_opener.addheaders = [('User-agent', 'Debian nagios-plugins-contrib 1.0')] watch_re = re.compile(r'([^ ]+) (.+)') whitespace_re = re.compile(r'\s') for plugin, _control in __get_control_data__(): - if not _control.has_key('Watch'): - print 'WARNING: %s - missing watch information!' %(plugin,) + if 'Watch' not in _control: + print('WARNING: %s - missing watch information!' %(plugin,)) continue try: url, check = watch_re.findall(_control['Watch'])[0] except IndexError: - print 'WARNING: %s - failed to parse Watch line!' %(plugin,) + print('WARNING: %s - failed to parse Watch line!' %(plugin,)) continue try: f=url_opener.open(url) - content = f.read() + content = f.read().decode('utf-8') f.close() except IOError: - print 'WARNING: %s - failed to retrieve %s !' %(plugin,url) + print('WARNING: %s - failed to retrieve %s !' %(plugin,url)) continue check=check.strip() if check.startswith('SHA1:'): check=check.replace('SHA1:','') new_sha=hashlib.sha1(content).hexdigest() 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: - print 'OK: %s' %(plugin,) + print('OK: %s' %(plugin,)) else: - if not _control.has_key('Version'): - print 'WARNING: %s - missing current version information!' %(plugin,) + if 'Version' not in _control: + print('WARNING: %s - missing current version information!' %(plugin,)) continue check_re=re.compile(check) # check for simple matches @@ -224,7 +249,7 @@ def watch(): if not v in found_versions: found_versions.append(v) if not found_versions: - print "WARNING: %s - regex does not match!" %(plugin) + print("WARNING: %s - regex does not match!" %(plugin)) continue new_version = found_versions[0] @@ -232,11 +257,11 @@ def watch(): if (apt_pkg.version_compare(v, found_versions[0]) > 0): new_version = v 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): - 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: - print 'OK: %s' %(plugin,) + print('OK: %s' %(plugin,)) @@ -262,6 +287,14 @@ if __name__ == '__main__': help='Update debian/control' ) + parser.add_option( + '--tests', + dest='tests', + action='store_true', + default=False, + help='Update debian/tests/control' + ) + parser.add_option( '--watch', dest='watch', @@ -278,13 +311,16 @@ if __name__ == '__main__': ) (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() sys.exit(1) if options.control: update_control() + if options.tests: + update_tests() + if options.copyright: update_copyright()