mirror of
https://codeberg.org/privacy1st/exec-notify
synced 2024-12-22 23:16:04 +01:00
60 lines
1.5 KiB
Python
Executable File
60 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from sys import argv
|
|
import socket
|
|
|
|
import exec, mail, config
|
|
|
|
|
|
def main():
|
|
prevMailError = checkPrevError()
|
|
success = executeCommand()
|
|
if prevMailError:
|
|
tryToInform()
|
|
|
|
if not prevMailError or not success:
|
|
exit(1)
|
|
else:
|
|
exit(0)
|
|
|
|
|
|
def executeCommand() -> bool:
|
|
if len(argv) < 2:
|
|
return True
|
|
else:
|
|
keys = ['command', 'status', 'stderr', 'stdout']
|
|
code, stdout, stderr = exec.execute(argv[1:])
|
|
values = [str(argv[1:]), str(code), stderr, stdout]
|
|
|
|
BODY = ''
|
|
for key, value in zip(keys, values):
|
|
BODY += '=== {} ===\n{}\n'.format(key, value)
|
|
print(BODY)
|
|
|
|
if code != 0:
|
|
hostname = socket.gethostname()
|
|
|
|
SUBJECT = '{} | {}'.format(hostname, str(argv[1:]))
|
|
mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY)
|
|
return False
|
|
|
|
|
|
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()
|