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 chroots.sort(key=lambda x: x.copr.full_name) 36 notifier.notify(user, chroots) 37 notifier.store_timestamp(chroots)
38
39 - def get_user_chroots_map(self, chroots):
40 user_chroot_map = {} 41 for chroot in chroots: 42 for admin in coprs_logic.CoprPermissionsLogic.get_admins_for_copr(chroot.copr): 43 if self.email_filter and admin.mail not in self.email_filter: 44 continue 45 if admin not in user_chroot_map: 46 user_chroot_map[admin] = [] 47 user_chroot_map[admin].append(chroot) 48 return user_chroot_map
49
50 - def filter_chroots(self, chroots):
51 if self.all: 52 return chroots 53 54 filtered = [] 55 for chroot in chroots: 56 if not chroot.delete_notify: 57 filtered.append(chroot) 58 continue 59 60 now = datetime.datetime.now() 61 if (now - chroot.delete_notify).days >= 14: 62 filtered.append(chroot) 63 64 return filtered
65
66 - def dev_instance_warning(self):
67 if app.config["ENV"] != "production" and not self.email_filter: 68 sys.stderr.write("I will not let you send emails to all Copr users from the dev instance!\n") 69 sys.stderr.write("Please use this command with -e myself@foo.bar\n") 70 sys.exit(1)
71 72
73 -class Notifier(object):
74 - def notify(self, user, chroots):
77
78 - def store_timestamp(self, chroots):
79 for chroot in chroots: 80 chroot.delete_notify = datetime.datetime.now() 81 db.session.commit()
82 83
84 -class DryRunNotifier(object):
85 - def notify(self, user, chroots):
86 about = ["{0} ({1})".format(chroot.copr.full_name, chroot.name) for chroot in chroots] 87 print("Notify {} about {}".format(user.mail, about))
88
89 - def store_timestamp(self, chroots):
90 pass
91