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