/
notify.py
executable file
·114 lines (97 loc) · 3.91 KB
/
notify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
## This program is licensed under the GPL v3.0, which is found at the URL below:
## http://opensource.org/licenses/gpl-3.0.html
##
## Copyright (c) 2011 Regents of the University of Michigan.
## All rights reserved.
##
## Redistribution and use in source and binary forms are permitted
## provided that this notice is preserved and that due credit is given
## to the University of Michigan at Ann Arbor. The name of the University
## may not be used to endorse or promote products derived from this
## software without specific prior written permission. This software
## is provided ``as is'' without express or implied warranty.
##
## notify.py (weiwang 2011-09-30)
##
## A Python script to send a text message via a Notifier object.
import getopt, json, os, re, sys, time
def_conf = './notify-conf.json'
def usage():
"""
Usage: %s [options] "recipient" "message"
Where: "recipients" is a name that identifys one (or more) recipient(s) for the
"notifier" configured. The content of this name depends on
the notifier object to be used.
"message" is a message to be sent. Multiple messages are accepted. If
multiple message are specified, they will be joined into one
separated by a single space.
Options: -h | --help Print this help message
-c | --config=<config-file> Specify a configuration file.
Default is "%s"
-d | --debug Debug mode.
-p | --path=<add-path> Addition to the PYTHONPATH for Notifier modules.
-t | --test-config Test the configuration only.
A configuration file is a text file in the JSON format, used to be specify what parameters
each notifier object uses.
"""
sys.stderr.write(usage.__doc__ % (sys.argv[0], def_conf)+"\n")
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "hc:dp:t", ["help", "config=", "debug", "path=", "test-config"])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(1)
debug = False
dryrun = False
conf = def_conf
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-c", "--config"):
conf = a
elif o in ("-d", "--debug"):
debug = True
elif o in ("-p", "--path"):
sys.path += a.split(':')
elif o in ('-t', '--test-config'):
dryrun = True
else:
assert False, "unhandled option"
if len(args) < 1 and not dryrun:
usage()
sys.exit(2)
try:
CONF = { 'notifier': [ 'Notifier' ], 'path': './notifier' }
CONF.update(json.load(open(conf, 'r'), 'utf-8'))
except ValueError, err:
print "Error: %s" % (err)
sys.exit(1)
except:
print "notify: Error loading configuration."
sys.exit(1)
for apath in CONF['path'].split(':'):
if os.path.isdir(apath): sys.path.append(apath)
from Notifier import Notifier
recp = '' if dryrun else args[0]
msg = " ".join(args[1:])
for name in set(re.split('[\s,]+', CONF['notifier'])):
try:
cnf = CONF[name]
klass = cnf['class'] if 'class' in cnf else name
notifier = Notifier.new(klass, cnf)
resp = 'Test success' if dryrun else notifier.notify(recp, msg)
if debug:
print("%s('%s', '%s') = %s\n" % (name, recp, msg, resp))
except Exception as ex: # Call all exceptions so that every configured notifier is visited.
err = "%s %s = %s -- message = %s" % (time.strftime("%Y-%m-%dT%H:%M:%S"), ex.__class__.__name__, str(ex), msg)
try:
flog = Notifier.new("Logfile", CONF['errorlog'])
flog.notify(recp, err)
except Exception as ex:
sys.stderr.write("%s\n" % (err))
sys.exit(0)