exec-notify/execNotify

66 lines
1.5 KiB
Plaintext
Raw Normal View History

2020-12-06 13:18:32 +01:00
#!/usr/bin/env python3
from sys import argv
import socket
2020-12-06 16:40:03 +01:00
from typing import List
2020-12-06 13:18:32 +01:00
2020-12-06 16:40:03 +01:00
from execNotifyDir import exec, config, mail
2020-12-06 13:18:32 +01:00
def main():
prevMailError = checkPrevError()
2020-12-06 16:40:03 +01:00
if len(argv) >= 2:
success = executeCommand(argv[1:])
else:
success = True
2020-12-06 13:18:32 +01:00
if prevMailError:
tryToInform()
2020-12-06 16:40:03 +01:00
if not success:
2020-12-06 13:18:32 +01:00
exit(1)
else:
exit(0)
2020-12-06 16:40:03 +01:00
def executeCommand(command: List) -> bool:
keys = ['command', 'status', 'stderr', 'stdout']
code, stdout, stderr = exec.execute(command)
values = [str(command), str(code), stderr, stdout]
BODY = ''
for key, value in zip(keys, values):
BODY += '=== {} ===\n{}\n'.format(key, value)
print(BODY)
2020-12-06 13:18:32 +01:00
2020-12-06 16:40:03 +01:00
if code != 0:
hostname = socket.gethostname()
2020-12-06 13:18:32 +01:00
2020-12-06 16:40:03 +01:00
SUBJECT = '{} | {}'.format(hostname, str(command))
mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY)
2020-12-06 13:18:32 +01:00
return False
2020-12-06 16:40:03 +01:00
else:
return True
2020-12-06 13:18:32 +01:00
def checkPrevError():
return config.getErrorFile().exists()
def tryToInform():
"""
Try to inform user via mail about previous error(s) that could not be sent to him before.
Maybe this time sending of an email works ;)
"""
SUBJECT = '{} | Some mails not sent!'.format(socket.gethostname())
BODY = 'Please check the file {} for mails which could previously not be sent to you!\n' \
'Note: You may delete the file after reading it ;)'.format(
config.getErrorFile())
mail.sendMail(SUBJECT=SUBJECT, BODY=BODY)
if __name__ == '__main__':
main()