[sysadmin-bin] Make gnome-post-receive-email compatible with python3, take two



commit ebe752e59f29b13af13a6b6acceddcc4a60f2bef
Author: Bartłomiej Piotrowski <bpiotrowski gnome org>
Date:   Wed Jul 22 14:24:48 2020 +0200

    Make gnome-post-receive-email compatible with python3, take two

 git/git.py                   |  22 ++++----
 git/gnome-post-receive-email | 122 +++++++++++++++++++++----------------------
 git/util.py                  |   4 +-
 3 files changed, 74 insertions(+), 74 deletions(-)
---
diff --git a/git/git.py b/git/git.py
index 3c7d3bd..910914b 100644
--- a/git/git.py
+++ b/git/git.py
@@ -19,6 +19,8 @@
 #
 # (These are adapted from git-bz)
 
+from __future__ import print_function
+
 import os
 import re
 from subprocess import Popen, PIPE
@@ -58,7 +60,7 @@ def git_run(command, *args, **kwargs):
     interactive = False
     outfile = None
     do_split_lines = False
-    for (k,v) in kwargs.iteritems():
+    for (k,v) in kwargs.items():
         if k == '_quiet':
             quiet = True
         elif k == '_interactive':
@@ -102,8 +104,8 @@ def git_run(command, *args, **kwargs):
     output, error = process.communicate(input)
     if process.returncode != 0:
         if not quiet and not interactive:
-            print >>sys.stderr, error,
-            print output,
+            print(error, file=sys.stderr)
+            print(output)
         raise CalledProcessError(process.returncode, " ".join(to_run))
 
     if interactive or outfile:
@@ -138,7 +140,7 @@ def rev_list_commits(*args, **kwargs):
         raise RuntimeException("git rev-list didn't return an even number of lines")
 
     result = []
-    for i in xrange(0, len(lines), 2):
+    for i in range(0, len(lines), 2):
         m = re.match("commit\s+([A-Fa-f0-9]+)", lines[i])
         if not m:
             raise RuntimeException("Can't parse commit it '%s'", lines[i])
@@ -154,7 +156,7 @@ def load_commit(commit_id):
 
 # Return True if the commit has multiple parents
 def commit_is_merge(commit):
-    if isinstance(commit, basestring):
+    if isinstance(commit, str):
         commit = load_commit(commit)
 
     parent_count = 0
@@ -168,7 +170,7 @@ def commit_is_merge(commit):
 
 # Return a short one-line summary of the commit
 def commit_oneline(commit):
-    if isinstance(commit, basestring):
+    if isinstance(commit, str):
         commit = load_commit(commit)
 
     return commit.id[0:7]+"... " + commit.subject[0:59]
@@ -206,12 +208,8 @@ def get_project_head_name():
 
     branch_name = 'master'
     try:
-        if int(platform.dist()[1].split('.')[0]) > 6:
-            mainline = git.symbolic_ref('HEAD', short=True, _quiet=True)
-            mainline = mainline.strip()
-        else:
-            mainline = git.symbolic_ref('HEAD', _quiet=True)
-            mainline = mainline.split('/')[-1]
+        mainline = git.symbolic_ref('HEAD', short=True, _quiet=True)
+        mainline = mainline.strip()
 
         if mainline:
             branch_name = mainline
diff --git a/git/gnome-post-receive-email b/git/gnome-post-receive-email
index 829f12c..fcb9ec1 100755
--- a/git/gnome-post-receive-email
+++ b/git/gnome-post-receive-email
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3.6
 #
 # gnome-post-receive-email - Post receive email hook for the GNOME Git repository
 #
@@ -34,13 +34,13 @@ import os
 import pwd
 import sys
 import socket
-from email import Header
+from email.header import Header
 
 
 if socket.gethostname() == 'gitlab.gnome.org':
     import gitlab
 
-    execfile('/home/admin/secret/gitlab_ro')
+    exec(compile(open('/home/admin/secret/gitlab_ro', "rb").read(), '/home/admin/secret/gitlab_ro', 'exec'))
 
     gl = gitlab.Gitlab('https://gitlab.gnome.org', GITLAB_PRIVATE_RO_TOKEN, api_version=4)
 
@@ -134,22 +134,22 @@ class RefChange(object):
 
     def generate_header(self, out, subject, include_revs=True, oldrev=None, newrev=None, cc=None):
         if user_fullname:
-            from_address = "%s <%s src gnome org>" % (Header.Header(user_fullname.decode('utf-8')), 
user_entry)
+            from_address = "%s <%s src gnome org>" % (Header(user_fullname.decode('utf-8')), user_entry)
         else:
             if socket.gethostname() == 'gitlab.gnome.org':
-                from_address = "%s <%s>" % (Header.Header(gitlab_name), gitlab_email)
+                from_address = "%s <%s>" % (Header(gitlab_name), gitlab_email)
             else:
                 from_address = "%s src gnome org" % (user_entry)
 
         if cc is None:
             cc = self.cc
 
-        if isinstance(subject, unicode):
-            subject = Header.Header(subject)
+        if isinstance(subject, str):
+            subject = Header(subject)
         else:
-            subject = Header.Header(subject.decode('utf-8'))
+            subject = Header(subject.decode('utf-8'))
 
-        print >>out, s("""
+        print(s("""
 To: %(recipients)s
 Cc: %(cc)s
 From: %(from_address)s
@@ -166,7 +166,7 @@ Content-Transfer-Encoding: 8bit
             'subject': subject,
             'projectshort': projectshort,
             'refname': self.refname
-       }
+       }, file=out)
 
         if include_revs:
             if oldrev:
@@ -178,16 +178,16 @@ Content-Transfer-Encoding: 8bit
             else:
                 newrev = NULL_REVISION
 
-            print >>out, s("""
+            print(s("""
 X-Git-Oldrev: %(oldrev)s
 X-Git-Newrev: %(newrev)s
 """) % {
             'oldrev': oldrev,
             'newrev': newrev,
-       }
+       }, file=out)
 
         # Trailing newline to signal the end of the header
-        print >>out
+        print(file=out)
 
     def send_main_email(self):
         if not self.get_needs_main_email():
@@ -332,11 +332,11 @@ class BranchChange(RefChange):
                 detail_note = True
             else:
                 detail = ""
-            print >>out, "  " + commit_oneline(commit) + detail
+            print("  " + commit_oneline(commit) + detail, file=out)
 
         if detail_note:
-            print >>out
-            print >>out, "(*) This commit already existed in another branch; no separate mail sent"
+            print(file=out)
+            print("(*) This commit already existed in another branch; no separate mail sent", file=out)
 
     def send_extra_emails(self):
         total = len(self.added_commits)
@@ -402,18 +402,18 @@ class BranchCreation(BranchChange):
 
     def generate_body(self, out):
         if len(self.added_commits) > 0:
-            print >>out, s("""
+            print(s("""
 The branch '%(short_refname)s' was created.
 
 Summary of new commits:
 
 """) % {
             'short_refname': self.short_refname,
-       }
+       }, file=out)
 
             self.generate_commit_summary(out, self.added_commits)
         else:
-            print >>out, s("""
+            print(s("""
 The branch '%(short_refname)s' was created pointing to:
 
  %(commit_oneline)s
@@ -421,7 +421,7 @@ The branch '%(short_refname)s' was created pointing to:
 """) % {
             'short_refname': self.short_refname,
             'commit_oneline': commit_oneline(self.newrev)
-       }
+       }, file=out)
 
 class BranchUpdate(BranchChange):
     def get_project_extra(self):
@@ -453,15 +453,15 @@ class BranchUpdate(BranchChange):
                 return last_commit.subject[0:SUBJECT_MAX_SUBJECT_CHARS]
 
     def generate_body_normal(self, out):
-        print >>out, s("""
+        print(s("""
 Summary of changes:
 
-""")
+"""), file=out)
 
         self.generate_commit_summary(out, self.added_commits)
 
     def generate_body_non_fast_forward(self, out):
-        print >>out, s("""
+        print(s("""
 The branch '%(short_refname)s' was changed in a way that was not a fast-forward update.
 NOTE: This may cause problems for people pulling from the branch. For more information,
 please see:
@@ -472,15 +472,15 @@ Commits removed from the branch:
 
 """) % {
             'short_refname': self.short_refname,
-       }
+       }, file=out)
 
         self.generate_commit_summary(out, self.removed_commits, show_details=False)
 
-        print >>out, s("""
+        print(s("""
 
 Commits added to the branch:
 
-""")
+"""), file=out)
         self.generate_commit_summary(out, self.added_commits)
 
     def generate_body(self, out):
@@ -494,11 +494,11 @@ class BranchDeletion(RefChange):
         return "Deleted branch " + self.short_refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The branch '%(short_refname)s' was deleted.
 """) % {
             'short_refname': self.short_refname,
-       }
+       }, file=out)
 
 # ========================
 
@@ -553,7 +553,7 @@ class AnnotatedTagChange(RefChange):
     # Outputs information about the new tag
     def generate_tag_info(self, out):
 
-        print >>out, s("""
+        print(s("""
 Tagger: %(tagger)s
 Date: %(date)s
 
@@ -563,7 +563,7 @@ Date: %(date)s
             'tagger': self.tagger,
             'date': self.date,
             'message': self.message,
-       }
+       }, file=out)
 
         # We take the creation of an annotated tag as being a "mini-release-announcement"
         # and show a 'git shortlog' of the changes since the last tag that was an
@@ -578,18 +578,18 @@ Date: %(date)s
 
         if last_tag:
             revision_range = last_tag + ".." + self.newrev
-            print >>out, s("""
+            print(s("""
 Changes since the last tag '%(last_tag)s':
 
 """) % {
                 'last_tag': last_tag
-      }
+      }, file=out)
         else:
             revision_range = self.newrev
-            print >>out, s("""
+            print(s("""
 Changes:
 
-""")
+"""), file=out)
         out.write(git.shortlog(revision_range))
         out.write("\n")
 
@@ -604,13 +604,13 @@ class AnnotatedTagCreation(AnnotatedTagChange):
         return "Created tag " + self.short_refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The %(tag_type)s '%(short_refname)s' was created.
 
 """) % {
             'tag_type': self.get_tag_type(),
             'short_refname': self.short_refname,
-       }
+       }, file=out)
         self.generate_tag_info(out)
 
 class AnnotatedTagDeletion(AnnotatedTagChange):
@@ -618,7 +618,7 @@ class AnnotatedTagDeletion(AnnotatedTagChange):
         return "Deleted tag " + self.short_refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The %(tag_type)s '%(short_refname)s' was deleted. It previously pointed to:
 
  %(old_commit_oneline)s
@@ -626,14 +626,14 @@ The %(tag_type)s '%(short_refname)s' was deleted. It previously pointed to:
             'tag_type': self.get_tag_type(),
             'short_refname': self.short_refname,
             'old_commit_oneline': commit_oneline(self.old_commit_id)
-       }
+       }, file=out)
 
 class AnnotatedTagUpdate(AnnotatedTagChange):
     def get_subject(self):
         return "Updated tag " + self.short_refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The tag '%(short_refname)s' was replaced with a new tag. It previously
 pointed to:
 
@@ -649,7 +649,7 @@ New tag information:
 """) % {
             'short_refname': self.short_refname,
             'old_commit_oneline': commit_oneline(self.old_commit_id),
-       }
+       }, file=out)
         self.generate_tag_info(out)
 
 # ========================
@@ -659,35 +659,35 @@ class LightweightTagCreation(RefChange):
         return "Created tag " + self.short_refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The lightweight tag '%(short_refname)s' was created pointing to:
 
  %(commit_oneline)s
 """) % {
             'short_refname': self.short_refname,
             'commit_oneline': commit_oneline(self.newrev)
-       }
+       }, file=out)
 
 class LightweightTagDeletion(RefChange):
     def get_subject(self):
         return "Deleted tag " + self.short_refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The lighweight tag '%(short_refname)s' was deleted. It previously pointed to:
 
  %(commit_oneline)s
 """) % {
             'short_refname': self.short_refname,
             'commit_oneline': commit_oneline(self.oldrev)
-       }
+       }, file=out)
 
 class LightweightTagUpdate(RefChange):
     def get_subject(self):
         return "Updated tag " + self.short_refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The lightweight tag '%(short_refname)s' was updated to point to:
 
  %(commit_oneline)s
@@ -704,7 +704,7 @@ For more information, please see:
             'short_refname': self.short_refname,
             'commit_oneline': commit_oneline(self.newrev),
             'old_commit_oneline': commit_oneline(self.oldrev)
-       }
+       }, file=out)
 
 # ========================
 
@@ -713,11 +713,11 @@ class InvalidRefDeletion(RefChange):
         return "Deleted invalid ref " + self.refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The ref '%(refname)s' was deleted. It previously pointed nowhere.
 """) % {
             'refname': self.refname,
-       }
+       }, file=out)
 
 # ========================
 
@@ -731,7 +731,7 @@ class MiscCreation(MiscChange):
         return "Unexpected: Created " + self.refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The ref '%(refname)s' was created pointing to:
 
  %(newrev)s
@@ -743,14 +743,14 @@ This is unexpected because:
             'refname': self.refname,
             'newrev': self.newrev,
             'message': self.message
-      }
+      }, file=out)
 
 class MiscDeletion(MiscChange):
     def get_subject(self):
         return "Unexpected: Deleted " + self.refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The ref '%(refname)s' was deleted. It previously pointed to:
 
  %(oldrev)s
@@ -762,14 +762,14 @@ This is unexpected because:
             'refname': self.refname,
             'oldrev': self.oldrev,
             'message': self.message
-      }
+      }, file=out)
 
 class MiscUpdate(MiscChange):
     def get_subject(self):
         return "Unexpected: Updated " + self.refname
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The ref '%(refname)s' was updated from:
 
  %(newrev)s
@@ -786,7 +786,7 @@ This is unexpected because:
             'oldrev': self.oldrev,
             'newrev': self.newrev,
             'message': self.message
-      }
+      }, file=out)
 
 # ========================
 
@@ -799,32 +799,32 @@ class NotesCreation(NotesChange):
         return "Created " + self.short_refname + " note"
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The note '%(short_refname)s' was created.
 
 %(commit_oneline)s
 """) % {
             'short_refname': self.short_refname,
             'commit_oneline': commit_oneline(self.newrev)
-      }
+      }, file=out)
 
 class NotesDeletion(NotesChange):
     def get_subject(self):
         return "Deleted " + self.short_refname + " note"
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The note '%(short_refname)s' was deleted.
 """) % {
             'short_refname': self.short_refname,
-      }
+      }, file=out)
 
 class NotesUpdate(NotesChange):
     def get_subject(self):
         return "Updated " + self.short_refname + " note"
 
     def generate_body(self, out):
-        print >>out, s("""
+        print(s("""
 The note '%(short_refname)s' was updated from:
 
  %(old_commit)s
@@ -836,7 +836,7 @@ To:
             'short_refname': self.short_refname,
             'old_commit': commit_oneline(self.oldrev),
             'new_commit': commit_oneline(self.newrev),
-      }
+      }, file=out)
 
 # ========================
 
@@ -941,7 +941,7 @@ def main():
     global recipients
 
     # No emails for a repository in the process of being imported
-    git_dir = git.rev_parse(git_dir=True, _quiet=True)
+    git_dir = git.rev_parse(git_dir=True, _quiet=True).decode()
     if os.path.exists(os.path.join(git_dir, 'pending')):
         return
 
diff --git a/git/util.py b/git/util.py
index 0e67d04..2286521 100644
--- a/git/util.py
+++ b/git/util.py
@@ -17,6 +17,8 @@
 # along with this program; if not, If not, see
 # http://www.gnu.org/licenses/.
 
+from __future__ import print_function
+
 import os
 import sys
 from subprocess import Popen
@@ -24,7 +26,7 @@ import tempfile
 import time
 
 def die(message):
-    print >>sys.stderr, message
+    print(message, file=sys.stderr)
     sys.exit(1)
 
 # This cleans up our generation code by allowing us to use the same indentation


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]