rougail/scripts/manage_schedule

91 lines
3.5 KiB
Plaintext
Raw Normal View History

2019-11-23 08:17:35 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from pyeole.schedule import display_schedules, ManageSchedule, \
apply_schedules, add_schedule, del_schedule
from pyeole.ansiprint import print_orange
from optparse import OptionParser, OptionGroup
def main():
parser = OptionParser()
parser.add_option("-l", "--list", dest="list", action="store_true",
default=False, help=u"Liste les scripts activés et désactivés")
parser.add_option("--apply", dest="apply", help=u"Active/désactive les scripts dans le schedule",
action='store_true', default=False)
group = OptionGroup(parser, u"Activer un script (ne pas oublier d'appliquer la configuration)")
group.add_option("-a", "--add", dest="add", help=u"Périodicité : une fois (once), tous les jours (daily), toutes les semaines (weekly) ou tous les mois (monthly)")
group.add_option("-s", "--script", dest="script", help=u"Nom du script")
group.add_option("-m", "--mode", dest="mode", help=u"Mode : avant la sauvegarde (pre) ou après la sauvegarde (post)")
parser.add_option_group(group)
parser.add_option("-d", "--del", dest="delete", help=u"Désactive le script en précisant son nom (ne pas oublier d'appliquer la configuration)")
(option, args) = parser.parse_args()
if option.delete is None and option.add is None and option.list is False and option.apply is False:
parser.print_help()
sys.exit(0)
if (option.delete is not None and option.add is not None) or \
(option.list is True and option.add is not None) or \
(option.delete is not None and option.list is True) or \
(option.apply is True and option.add is not None) or \
(option.apply is True and option.list is True) or \
(option.apply is True and option.delete is True):
parser.print_help()
print "can't list, apply, add or delete together"
print
sys.exit(1)
if option.add is not None and None in [option.script, option.mode]:
print "you should specified -s and -m with -a"
print
parser.print_help()
sys.exit(1)
if option.delete == 'once' and None in [option.script, option.mode]:
print "you must specify mode if you want delete 'once' script"
print
parser.print_help()
sys.exit(1)
if option.delete != 'once' and ((option.script is not None and option.add is None) or \
(option.mode is not None and option.add is None)):
print "you should specified -s and -m only with -a"
print
parser.print_help()
sys.exit(1)
if option.list:
display_schedules()
if option.add is not None:
try:
if option.add == 'once':
add_schedule(option.add, option.mode, option.script)
else:
sch = ManageSchedule()
sch.add(option.script, option.add, option.mode)
sch.save()
except Exception, err:
print_orange(str(err))
sys.exit(1)
if option.delete is not None:
try:
if option.delete == 'once':
del_schedule(option.delete, option.mode, option.script)
else:
sch = ManageSchedule()
sch.delete(option.delete)
sch.save()
except Exception, err:
print_orange(str(err))
sys.exit(1)
if option.apply is not None:
apply_schedules()
if __name__ == '__main__':
main()