[sysadmin-bin] More py3 compatibility changes



commit 5fcf785e0aebf1a16788a722518c4495ac8a3a05
Author: Andrea Veri <averi redhat com>
Date:   Thu Nov 12 21:28:28 2020 +0100

    More py3 compatibility changes

 cleanup-inactive-ldap-accounts.py | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
---
diff --git a/cleanup-inactive-ldap-accounts.py b/cleanup-inactive-ldap-accounts.py
index ddd6da2..85c29d4 100755
--- a/cleanup-inactive-ldap-accounts.py
+++ b/cleanup-inactive-ldap-accounts.py
@@ -10,7 +10,7 @@ import sys
 import time
 
 from optparse import OptionParser
-from email.MIMEText import MIMEText
+from email.mime.text import MIMEText
 
 import gnome_ldap_utils as Glu
 import gitlab
@@ -19,7 +19,7 @@ import gitlab
 def get_disk_path(project_id):
     repo_path = "/var/opt/gitlab/git-data/repositories/@hashed"
     id_str = str(project_id)
-    hashed = hashlib.sha256(id_str).hexdigest()
+    hashed = hashlib.sha256(id_str.encode()).hexdigest()
     path = os.path.join(repo_path, hashed[0:2], hashed[2:4], hashed)
     return "{}.git".format(path)
 
@@ -39,7 +39,7 @@ if socket.gethostname() != 'gitlab.gnome.org':
     print("You are not allowed to run this script on a different host than gitlab.gnome.org, exiting...", 
end='\n')
     sys.exit(1)
 
-glu = Gnome_ldap_utils(LDAP_GROUP_BASE, LDAP_HOST, LDAP_USER_BASE, LDAP_USER, LDAP_PASSWORD)
+glu = Glu.Gnome_ldap_utils(LDAP_GROUP_BASE, LDAP_HOST, LDAP_USER_BASE, LDAP_USER, LDAP_PASSWORD)
 
 gl = gitlab.Gitlab('https://gitlab.gnome.org', GITLAB_PRIVATE_RW_TOKEN, api_version=4)
 grp = gl.groups.get('GNOME')
@@ -53,11 +53,11 @@ last_pushed_times = {}
 
 for repository in repositories:
     try:
-        pushlog = open('%s/gnome_pushlog' % (repository), 'r')
+        pushlog = open(f"{ repository }/gnome_pushlog", 'rb')
         for line in pushlog.readlines():
-            fields = line.rstrip().split('\t')
-            username = fields[3]
-            pushtime = calendar.timegm(time.strptime(fields[4], '%a, %d %b %Y %H:%M:%S +0000'))
+            fields = line.rstrip().split(b'\t')
+            username = fields[3].decode('utf-8')
+            pushtime = calendar.timegm(time.strptime(fields[4].decode('utf-8'), '%a, %d %b %Y %H:%M:%S 
+0000'))
             if not username in last_pushed_times or pushtime > last_pushed_times[username]:
                 last_pushed_times[username] = pushtime
     except IOError as e:
@@ -66,11 +66,11 @@ for repository in repositories:
 
 now = time.time()
 
-for user, last_pushed in last_pushed_times.iteritems():
+for user, last_pushed in last_pushed_times.items():
     if last_pushed < now - 2 * 365 * 24 * 60 * 60:
         last_pushed = time.gmtime(last_pushed)
         if options.print_inactive_accounts:
-            print (f"{ user }: { time.strftime('%d-%m-%Y', last_pushed)) }", end='\n')
+            print (f"{ user }: { time.strftime('%d-%m-%Y', last_pushed) }", end='\n')
 
 
 def user_is_current(username):
@@ -78,12 +78,12 @@ def user_is_current(username):
 
 
 def add_remove_comment_to_user(username, group):
-    new_comment = 'Removed from group %s by cleanup-inactive-ldap-accounts at %s.' % (group, 
datetime.date.today())
+    new_comment = f"Removed from group { group } by cleanup-inactive-ldap-accounts at { 
datetime.date.today() }."
 
     ldap_fields = glu.get_attributes_from_ldap(username, 'cn', 'description', 'mail')
-    current_comment = ldap_fields[2]
-    name = ldap_fields[1]
-    mail = ldap_fields[3]
+    current_comment = ldap_fields[2].decode('utf-8')
+    name = ldap_fields[1].decode('utf-8')
+    mail = ldap_fields[3].decode('utf-8')
 
     if current_comment is None:
         comment = new_comment
@@ -113,9 +113,8 @@ the GNOME Accounts Team"""
         server = smtplib.SMTP("localhost")
         server.sendmail(msg['From'], msg['To'], msg.as_string())
         server.quit()
-    except smtplib.SMTPException:
-        # Too bad, they'll have to contact sysadmin
-        pass
+    except smtplib.SMTPException as e:
+        print(e)
 
     return True
 
@@ -137,7 +136,7 @@ for gnomecvs_user in gnomecvs_users:
 for ftpadmin_user in ftpadmin_users:
     if not user_is_current(ftpadmin_user):
         if options.verbose:
-            print("Removing user { ftpadmin_user } from ftpadmin", end='\n')
+            print(f"Removing user { ftpadmin_user } from ftpadmin", end='\n')
 
         glu.remove_user_from_ldap_group(ftpadmin_user, 'ftpadmin')
         add_remove_comment_to_user(gnomecvs_user, 'ftpadmin')


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