Package commands :: Module notify_outdated_chroots
[hide private]
[frames] | no frames]

Source Code for Module commands.notify_outdated_chroots

  1  import sys 
  2  import datetime 
  3  import click 
  4  from coprs import db, app 
  5  from coprs.logic import coprs_logic 
  6  from coprs.mail import send_mail, OutdatedChrootMessage 
  7   
  8   
  9  @click.command() 
 10  @click.option( 
 11      "--dry-run/--no-dry-run", 
 12      default=False, 
 13      help="Do not actually notify the people, but rather print information on stdout" 
 14  ) 
 15  @click.option( 
 16      "--email", "-e", "email_filter", 
 17      help="Notify only " 
 18  ) 
 19  @click.option( 
 20      "--all/--not-all", 
 21      default=False, 
 22      help="Notify all (even the recently notified) relevant people" 
 23  ) 
24 -def notify_outdated_chroots(dry_run, email_filter, all):
25 with app.app_context(): 26 return notify_outdated_chroots_function(dry_run, email_filter, all)
27
28 29 -def notify_outdated_chroots_function(dry_run, email_filter, all):
30 """ 31 Notify all admins of projects with builds in outdated chroots about upcoming deletion. 32 """ 33 34 if not dry_run: 35 dev_instance_warning(email_filter) 36 37 notifier = DryRunNotifier() if dry_run else Notifier() 38 outdated = coprs_logic.CoprChrootsLogic.filter_outdated(coprs_logic.CoprChrootsLogic.get_multiple()) 39 user_chroots_map = get_user_chroots_map(outdated, email_filter).items() 40 for i, (user, chroots) in enumerate(user_chroots_map, start=1): 41 chroots = filter_chroots([chroot for chroot in chroots], all) 42 if not chroots: 43 continue 44 chroots.sort(key=lambda x: x.copr.full_name) 45 notifier.notify(user, chroots) 46 47 # This command will possibly update a lot of chroots and can be a performance issue when committing 48 # all at once. We are going to commit every x actions to avoid that. 49 if i % 1000 == 0: 50 notifier.commit() 51 52 notifier.commit()
53
54 -def get_user_chroots_map(chroots, email_filter):
55 user_chroot_map = {} 56 for chroot in chroots: 57 for admin in coprs_logic.CoprPermissionsLogic.get_admins_for_copr(chroot.copr): 58 if email_filter and admin.mail not in email_filter: 59 continue 60 if admin not in user_chroot_map: 61 user_chroot_map[admin] = [] 62 user_chroot_map[admin].append(chroot) 63 return user_chroot_map
64
65 -def filter_chroots(chroots, all):
66 if all: 67 return chroots 68 69 filtered = [] 70 for chroot in chroots: 71 if not chroot.delete_notify: 72 filtered.append(chroot) 73 continue 74 75 # Skip the chroot if was notified in less than `n` days 76 now = datetime.datetime.now() 77 if (now - chroot.delete_notify).days >= app.config["EOL_CHROOTS_NOTIFICATION_PERIOD"]: 78 filtered.append(chroot) 79 80 return filtered
81
82 -def dev_instance_warning(email_filter):
83 if app.config["ENV"] != "production" and not email_filter: 84 sys.stderr.write("I will not let you send emails to all Copr users from the dev instance!\n") 85 sys.stderr.write("Please use this command with -e myself@foo.bar\n") 86 sys.exit(1)
87
88 89 -class Notifier(object):
90 - def notify(self, user, chroots):
91 msg = OutdatedChrootMessage(chroots) 92 send_mail([user.mail], msg) 93 94 # If `send_mail` didn't raise any exception, 95 # we consider the email to be sent correctly 96 for chroot in chroots: 97 chroot.delete_notify = datetime.datetime.now()
98
99 - def commit(self):
100 db.session.commit()
101
102 103 -class DryRunNotifier(object):
104 - def notify(self, user, chroots):
105 about = ["{0} ({1})".format(chroot.copr.full_name, chroot.name) for chroot in chroots] 106 print("Notify {} about {}".format(user.mail, about))
107
108 - def commit(self):
109 pass
110