diff --git a/.gitignore b/.gitignore index 899a131..25747e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -config/cfg.ini +execNotifyDir/config/cfg.ini __pycache__/ \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 533968a..c0c35d8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,22 +1,19 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + - + + + + + @@ -71,7 +72,7 @@ + + + + + + + - + \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..48fe44f --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +.PHONY: all install clean copy remove + +all: install + +clean: remove + +install: permissions + +permissions: copy + chmod 755 /usr/local/bin/execNotify + find /usr/local/bin/execNotifyDir \( -type d -exec chmod 755 {} + \) -o \( -type f -exec chmod 644 {} + \) + chown root:root /usr/local/bin/execNotify + chown -R root:root /usr/local/bin/execNotifyDir + +copy: + cp execNotify /usr/local/bin/execNotify + cp -r execNotifyDir/ /usr/local/bin/ + +remove: + rm /usr/local/bin/execNotify + rm -r /usr/local/bin/execNotifyDir diff --git a/execNotify.py b/execNotify similarity index 55% rename from execNotify.py rename to execNotify index df917e3..19683ec 100755 --- a/execNotify.py +++ b/execNotify @@ -1,41 +1,47 @@ #!/usr/bin/env python3 from sys import argv import socket +from typing import List -import exec, mail, config +from execNotifyDir import exec, config, mail def main(): prevMailError = checkPrevError() - success = executeCommand() + + if len(argv) >= 2: + success = executeCommand(argv[1:]) + else: + success = True + if prevMailError: tryToInform() - if not prevMailError or not success: + if 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] +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) + BODY = '' + for key, value in zip(keys, values): + BODY += '=== {} ===\n{}\n'.format(key, value) + print(BODY) - if code != 0: - hostname = socket.gethostname() + if code != 0: + hostname = socket.gethostname() + + SUBJECT = '{} | {}'.format(hostname, str(command)) + mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY) - SUBJECT = '{} | {}'.format(hostname, str(argv[1:])) - mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY) return False + else: + return True def checkPrevError(): diff --git a/execNotifyDir/__init__.py b/execNotifyDir/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/config.py b/execNotifyDir/config.py similarity index 80% rename from config.py rename to execNotifyDir/config.py index 4f46515..0cb17bd 100644 --- a/config.py +++ b/execNotifyDir/config.py @@ -1,6 +1,7 @@ from pathlib import Path import configparser +from execNotifyDir import util def getHostAndPort(): return config['mail']['host'], config['mail']['port'] @@ -27,8 +28,10 @@ def _getCfgFile() -> Path: def _getCfgDir() -> Path: - return Path('config') + return util.getProjectBase().joinpath('execNotifyDir').joinpath('config') +# DEBUG todo +print(_getCfgFile()) config: configparser.ConfigParser = configparser.ConfigParser() config.read(_getCfgFile()) diff --git a/config/cfg.ini.example b/execNotifyDir/config/cfg.ini.example similarity index 100% rename from config/cfg.ini.example rename to execNotifyDir/config/cfg.ini.example diff --git a/exec.py b/execNotifyDir/exec.py similarity index 100% rename from exec.py rename to execNotifyDir/exec.py diff --git a/mail.py b/execNotifyDir/mail.py similarity index 97% rename from mail.py rename to execNotifyDir/mail.py index 2906628..295b482 100644 --- a/mail.py +++ b/execNotifyDir/mail.py @@ -4,9 +4,8 @@ import smtplib, ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart -from pathlib import Path -import util, config +from execNotifyDir import config def sendMailOrWriteToFile(SUBJECT: str, BODY: str): diff --git a/util.py b/execNotifyDir/util.py similarity index 66% rename from util.py rename to execNotifyDir/util.py index 8507c3d..b2768e9 100644 --- a/util.py +++ b/execNotifyDir/util.py @@ -1,6 +1,11 @@ +import os from pathlib import Path +def getProjectBase() -> Path: + return Path(os.path.realpath(__file__)).parent.parent + + def readFirstLine(file: Path) -> str: """ :param file: Path to file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a9fc442 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +# so far there are no requirements except python3