Skip to content

Commit e1cae56

Browse files
author
Nathan Lee
committed
Addition of --silent option and re-write of email notification module
1 parent 0b3355c commit e1cae56

5 files changed

Lines changed: 40 additions & 31 deletions

File tree

pyrunner/core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def __init__(self):
9393
'nozip' : { 'type': bool, 'preserve': False, 'env': 'APP_NOZIP' , 'value': None, 'default': False },
9494
'dump_logs' : { 'type': bool, 'preserve': False, 'env': 'APP_DUMP_LOGS' , 'value': None, 'default': False },
9595
'email' : { 'type': str , 'preserve': False, 'env': 'APP_EMAIL' , 'value': None, 'default': None },
96+
'silent' : { 'type': bool, 'preserve': False, 'env': 'APP_SILENT' , 'value': None, 'default': False },
9697
'debug' : { 'type': bool, 'preserve': False, 'env': 'APP_DEBUG' , 'value': None, 'default': False },
9798
'tickrate' : { 'type': int , 'preserve': False, 'env': 'APP_TICKRATE' , 'value': None, 'default': 1 },
9899
'time_between_tasks' : { 'type': int , 'preserve': True, 'env': 'APP_TIME_BETWEEN_TASKS', 'value': None, 'default': 0 },

pyrunner/core/engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def initiate(self, **kwargs):
130130
node.execute()
131131
self.register.running_nodes.add(node)
132132

133-
if not kwargs.get('silent'):
133+
if not kwargs.get('silent') and not self.config['silent']:
134134
self._print_current_state()
135135

136136
# Check for input requests from interactive mode
@@ -166,7 +166,7 @@ def initiate(self, **kwargs):
166166
if self._on_destroy_func:
167167
self._on_destroy_func()
168168

169-
if not kwargs.get('silent'):
169+
if not kwargs.get('silent') and not self.config['silent']:
170170
self._print_final_state()
171171

172172
if not self.config['test_mode'] and self.save_state_func:

pyrunner/core/pyrunner.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
from pyrunner.core.signal import SignalHandler, SIG_ABORT, SIG_PAUSE, SIG_PULSE
3333
from pyrunner.version import __version__
3434

35+
from pyrunner.notification import Notification
36+
3537
from datetime import datetime as datetime
3638
import pickle
3739
import time
@@ -41,7 +43,7 @@ class PyRunner:
4143
def __init__(self, **kwargs):
4244
self._environ = os.environ.copy()
4345
self.config = Config()
44-
self.notification = notification.EmailNotification()
46+
self._notification = notification.EmailNotification()
4547
self.signal_handler = SignalHandler(self.config)
4648

4749
self.serde_obj = serde.ListSerDe()
@@ -84,6 +86,16 @@ def load_proc_file(self, proc_file, restart=False):
8486

8587
return True
8688

89+
@property
90+
def notification(self):
91+
return self._notification
92+
@notification.setter
93+
def notification(self, o):
94+
if not issubclass(o, Notification):
95+
raise TypeError('Not an extension of pyrunner.notification.Notification')
96+
self._notification = o
97+
return self
98+
8799
@property
88100
def version(self):
89101
return __version__
@@ -342,7 +354,7 @@ def parse_args(self, run_getopts=True):
342354
opt_list = 'c:l:n:e:x:N:D:A:t:drhiv'
343355
longopt_list = [
344356
'setup', 'help', 'nozip', 'interactive', 'abort',
345-
'restart', 'version', 'dryrun', 'debug',
357+
'restart', 'version', 'dryrun', 'debug', 'silent',
346358
'preserve-context', 'dump-logs', 'allow-duplicate-jobs',
347359
'email=', 'email-on-fail=', 'email-on-success=',
348360
'env=', 'cvar=', 'context=', 'time-between-tasks=',
@@ -410,6 +422,8 @@ def parse_args(self, run_getopts=True):
410422
self.config['exec_proc_name'] = arg
411423
elif opt == '--abort':
412424
abort = True
425+
elif opt == '--silent':
426+
self.config['silent'] = True
413427
elif opt in ['--serde']:
414428
if arg.lower() == 'json':
415429
self.plugin_serde(serde.JsonSerDe())

pyrunner/notification/email.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616

17-
from subprocess import Popen, STDOUT, PIPE
17+
import time, smtplib, os
1818
from datetime import datetime as datetime
19-
import time
2019
from pyrunner.notification.abstract import Notification
20+
from email.message import EmailMessage
2121

2222
class EmailNotification(Notification):
2323

2424
def emit_notification(self, config, register):
25+
if not config['email']:
26+
print('Email address not provided - skipping notification email')
27+
return 0
28+
2529
failed_objects = register.failed_nodes
26-
subject = ''
27-
message = ''
30+
subject, message = '', ''
2831
attachments = [config.ctllog_file]
2932

33+
# Build message body
3034
message += "Dear User,\n\n"
3135

3236
if failed_objects:
@@ -48,29 +52,19 @@ def emit_notification(self, config, register):
4852

4953
message += "Log Directory: {}\n\n".format(config['log_dir'])
5054

51-
if not config['email']:
52-
print('Email address not provided - skipping notification email')
53-
return 0
54-
5555
print('Sending Email Notification to: {}'.format(config['email']))
56-
command = ['mailx', '-s', subject]
57-
58-
if attachments:
59-
for a in attachments:
60-
command.append('-a')
61-
command.append(a)
62-
63-
command.append(config['email'])
6456

65-
proc = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
66-
out, err = proc.communicate(input = str.encode(message))
67-
retcode = proc.returncode
57+
msg = EmailMessage()
58+
msg["From"] = os.environ['USER']
59+
msg["Subject"] = subject
60+
msg["To"] = config['email']
61+
msg.set_content(message)
6862

69-
if retcode > 0:
70-
if out:
71-
print(out)
72-
if err:
73-
print(err)
74-
raise RuntimeError('Error while sending notification email')
63+
# Attach ctllog file and any failure logs, if any
64+
for filepath in attachments:
65+
with open(filepath, 'r') as f:
66+
msg.add_attachment(f.read(), filename=os.path.basename(filepath))
7567

76-
return retcode
68+
s = smtplib.SMTP('localhost')
69+
s.send_message(msg)
70+
s.quit()

pyrunner/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '5.1.2'
1+
__version__ = '5.2.0'

0 commit comments

Comments
 (0)