[Git][security-tracker-team/security-tracker][master] Port bin/maintainers to Python 3.x.

Chris Lamb lamby at debian.org
Sat Sep 7 11:31:59 BST 2019



Chris Lamb pushed to branch master at Debian Security Tracker / security-tracker


Commits:
4649cd33 by Chris Lamb at 2019-09-07T10:31:48Z
Port bin/maintainers to Python 3.x.

- - - - -


1 changed file:

- bin/contact-maintainers


Changes:

=====================================
bin/contact-maintainers
=====================================
@@ -1,92 +1,114 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 
 import argparse
 import os
 import pwd
-import subprocess
+import re
 import sys
 import tempfile
-import urllib2
+import urllib
 import warnings
 
 from jinja2 import Template
 
 
 def get_full_name():
-    full_name = os.getenv('DEBFULLNAME')
+    full_name = os.getenv("DEBFULLNAME")
     if full_name:
-        return full_name.decode('utf-8')
-    return pwd.getpwuid(os.getuid()).pw_gecos.split(',')[0].decode('utf-8')
+        return full_name
+    return pwd.getpwuid(os.getuid()).pw_gecos.split(",")[0].decode("utf-8")
+
+
+def get_maintainers(pkg):
+    return u"{}@packages.debian.org".format(pkg)
 
 
 try:
     import rdflib
-
 except ImportError:
-    warnings.warn('python-rdflib not installed; will fall back to PTS email address')
-    def get_maintainers(pkg):
-        return u'{}@packages.debian.org'.format(pkg)
-
+    warnings.warn("python-rdflib not installed; will fall back to PTS email address")
 else:
+
     def get_maintainers(pkg):
-        import re, urllib
 
         # RDF object and predicate references used on PTS
-        project = rdflib.term.URIRef(u'http://packages.qa.debian.org/{}#project'.format(pkg))
-        has_contributor = rdflib.term.URIRef(u'http://schema.org/contributor')
-        is_named = rdflib.term.URIRef(u'http://xmlns.com/foaf/0.1/name')
-        is_same_as = rdflib.term.URIRef(u'http://www.w3.org/2002/07/owl#sameAs')
+        project = rdflib.term.URIRef(
+            u"http://packages.qa.debian.org/{}#project".format(pkg)
+        )
+        has_contributor = rdflib.term.URIRef(u"http://schema.org/contributor")
+        is_named = rdflib.term.URIRef(u"http://xmlns.com/foaf/0.1/name")
+        is_same_as = rdflib.term.URIRef(u"http://www.w3.org/2002/07/owl#sameAs")
 
         maint = []
 
         graph = rdflib.Graph()
         try:
-            graph.parse('https://packages.qa.debian.org/{}/{}.rdf'
-                        .format(re.match('((?:lib)?.)', pkg).group(1), pkg))
-        except urllib2.HTTPError as exc:
+            graph.parse(
+                "https://packages.qa.debian.org/{}/{}.rdf".format(
+                    re.match("((?:lib)?.)", pkg).group(1), pkg
+                )
+            )
+        except urllib.error.HTTPError as exc:
             if exc.code == 404:
                 raise ValueError("unknown package '{}'".format(pkg))
             raise
-        for contrib in graph[project : has_contributor]:
-            names = [n for n in graph[contrib : is_named]]
-            addresses = [urllib.unquote(m.group(1)) for m in
-                         map(re.compile(r'http://webid\.debian\.net/maintainers/(.*)#agent$').match,
-                             graph[contrib : is_same_as])
-                         if m]
+        for contrib in graph[project:has_contributor]:
+            names = [n for n in graph[contrib:is_named]]
+            addresses = [
+                urllib.parse.unquote(m.group(1))
+                for m in map(
+                    re.compile(
+                        r"http://webid\.debian\.net/maintainers/(.*)#agent$"
+                    ).match,
+                    graph[contrib:is_same_as],
+                )
+                if m
+            ]
             if not names or not addresses:
-                warnings.warn('found contributor missing name and/or address')
+                warnings.warn("found contributor missing name and/or address")
                 continue
             address = addresses[0]
-            if '@' not in address:
-                address += '@debian.org'
+            if "@" not in address:
+                address += "@debian.org"
             maint.append(u'"{}" <{}>'.format(names[0], address))
 
-        return u', '.join(maint)
+        return u", ".join(maint)
 
 
 # Parse command line
-parser = argparse.ArgumentParser(
-    description='Get in touch with package maintainers')
-parser.add_argument('--force', action='store_true',
-                    help='Ignore safety checks')
-parser.add_argument('--lts', action='store_true',
-                    help='Act as a member of the LTS team')
-parser.add_argument('--no-dsa', dest='no_dsa', action='store_true',
-                    help='Say that issues are low severity (no need for DSA/DLA)')
-parser.add_argument('--minor', dest='minor_issues', action='store_true',
-                    help='Say that issues are low severity and someone will work on them (LTS team only)')
-parser.add_argument('--mailer', action='store', default='mutt -H {}',
-                    help='Command executed. Must contain {} to be replaced '
-                    'by the filename of the draft contact mail')
-parser.add_argument('package')
-parser.add_argument('cve', nargs='*')
+parser = argparse.ArgumentParser(description="Get in touch with package maintainers")
+parser.add_argument("--force", action="store_true", help="Ignore safety checks")
+parser.add_argument(
+    "--lts", action="store_true", help="Act as a member of the LTS team"
+)
+parser.add_argument(
+    "--no-dsa",
+    dest="no_dsa",
+    action="store_true",
+    help="Say that issues are low severity (no need for DSA/DLA)",
+)
+parser.add_argument(
+    "--minor",
+    dest="minor_issues",
+    action="store_true",
+    help="Say that issues are low severity and someone will work on them (LTS team only)",
+)
+parser.add_argument(
+    "--mailer",
+    action="store",
+    default="mutt -H {}",
+    help="Command executed. Must contain {} to be replaced "
+    "by the filename of the draft contact mail",
+)
+parser.add_argument("package")
+parser.add_argument("cve", nargs="*")
 args = parser.parse_args()
 
-cc = 'debian-lts at lists.debian.org' if args.lts else 'team at security.debian.org'
-team = 'lts' if args.lts else 'sec'
-model = 'no-dsa' if args.no_dsa else 'update-planned'
-minor = '-minor' if args.minor_issues and args.lts else ''
-template_file = 'templates/{}-{}{}.txt'.format(team, model, minor)
+cc = "debian-lts at lists.debian.org" if args.lts else "team at security.debian.org"
+team = "lts" if args.lts else "sec"
+model = "no-dsa" if args.no_dsa else "update-planned"
+minor = "-minor" if args.minor_issues and args.lts else ""
+template_file = "templates/{}-{}{}.txt".format(team, model, minor)
 
 # Basic check
 instructions = "packages/{}.txt".format(args.package)
@@ -100,12 +122,12 @@ dontcall = "data/packages/lts-do-not-call"
 if args.lts and not args.force:
     with open(dontcall) as f:
         for line in f:
-            if line[0] == '#':
+            if line[0] == "#":
                 continue
             if not line.strip():
                 continue
             if line.split()[0] == args.package:
-                print "Maintainer(s) may not be contacted for LTS issues."
+                print("Maintainer(s) may not be contacted for LTS issues.")
                 print("Reason: {}".format(" ".join(line.split()[1:])))
                 print("If you still want to run this script, run it with --force.")
                 sys.exit(1)
@@ -119,21 +141,21 @@ if args.lts and not args.force:
 #   include it in the recipients of the mail
 
 context = {
-    'package': args.package,
-    'sender': get_full_name(),
-    'cve': args.cve,
-    'to': get_maintainers(args.package),
-    'cc': cc,
-    'uploaders': ''
+    "package": args.package,
+    "sender": get_full_name(),
+    "cve": args.cve,
+    "to": get_maintainers(args.package),
+    "cc": cc,
+    "uploaders": "",
 }
 
 # Generate the mail
 with open(template_file) as f:
-    template = Template(f.read().decode('utf-8'))
+    template = Template(f.read())
 
-fd, filename = tempfile.mkstemp(prefix='contact-maintainers', suffix='.txt')
-draft = os.fdopen(fd, 'w')
-draft.write(template.render(context).encode('utf-8'))
+fd, filename = tempfile.mkstemp(prefix="contact-maintainers", suffix=".txt")
+draft = os.fdopen(fd, "wb")
+draft.write(template.render(context).encode("utf-8"))
 draft.close()
 
 os.system(args.mailer.format(filename))



View it on GitLab: https://salsa.debian.org/security-tracker-team/security-tracker/commit/4649cd33fb3760ead97ba8ccbd9ae81dd70cc47d

-- 
View it on GitLab: https://salsa.debian.org/security-tracker-team/security-tracker/commit/4649cd33fb3760ead97ba8ccbd9ae81dd70cc47d
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/debian-security-tracker-commits/attachments/20190907/d4c524d3/attachment-0001.html>


More information about the debian-security-tracker-commits mailing list