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  from flask_script import Command, Option 
 4  from coprs import db, app 
 5  from coprs.logic import coprs_logic 
 6  from coprs.mail import send_mail, OutdatedChrootMessage 
 7   
 8   
9 -class NotifyOutdatedChrootsCommand(Command):
10 """ 11 Notify all admins of projects with builds in outdated chroots about upcoming deletion. 12 """ 13 option_list = [ 14 Option("--dry-run", action="store_true", 15 help="Do not actually notify the people, but rather print information on stdout"), 16 Option("-e", "--email", action="append", dest="email_filter", 17 help="Notify only "), 18 Option("-a", "--all", action="store_true", 19 help="Notify all (even the recently notified) relevant people"), 20 ] 21
22 - def run(self, dry_run, email_filter, all):
23 self.email_filter = email_filter 24 self.all = all 25 26 if not dry_run: 27 self.dev_instance_warning() 28 29 notifier = DryRunNotifier() if dry_run else Notifier() 30 outdated = coprs_logic.CoprChrootsLogic.filter_outdated(coprs_logic.CoprChrootsLogic.get_multiple()) 31 for user, chroots in self.get_user_chroots_map(outdated).items(): 32 chroots = self.filter_chroots([chroot for chroot in chroots]) 33 if not chroots: 34 continue 35 notifier.notify(user, chroots) 36 notifier.store_timestamp(chroots)
37
38 - def get_user_chroots_map(self, chroots):
39 user_chroot_map = {} 40 for chroot in chroots: 41 for admin in coprs_logic.CoprPermissionsLogic.get_admins_for_copr(chroot.copr): 42 if self.email_filter and admin.mail not in self.email_filter: 43 continue 44 if admin not in user_chroot_map: 45 user_chroot_map[admin] = [] 46 user_chroot_map[admin].append(chroot) 47 return user_chroot_map
48
49 - def filter_chroots(self, chroots):
50 if self.all: 51 return chroots 52 53 filtered = [] 54 for chroot in chroots: 55 if not chroot.delete_notify: 56 filtered.append(chroot) 57 continue 58 59 now = datetime.datetime.now() 60 if (now - chroot.delete_notify).days >= 14: 61 filtered.append(chroot) 62 63 return filtered
64
65 - def dev_instance_warning(self):
66 if app.config["ENV"] != "production" and not self.email_filter: 67 sys.stderr.write("I will not let you send emails to all Copr users from the dev instance!\n") 68 sys.stderr.write("Please use this command with -e myself@foo.bar\n") 69 sys.exit(1)
70 71
72 -class Notifier(object):
73 - def notify(self, user, chroots):
76
77 - def store_timestamp(self, chroots):
78 for chroot in chroots: 79 chroot.delete_notify = datetime.datetime.now() 80 db.session.commit()
81 82
83 -class DryRunNotifier(object):
84 - def notify(self, user, chroots):
85 about = ["{0} ({1})".format(chroot.copr.full_name, chroot.name) for chroot in chroots] 86 print("Notify {} about {}".format(user.mail, about))
87
88 - def store_timestamp(self, chroots):
89 pass
90