diff --git a/.gitignore b/.gitignore index 7f7cccc..031fad5 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ var/ *.egg-info/ .installed.cfg *.egg +.venv/ # PyInstaller # Usually these files are written by a python script from a template diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..9d27305 --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +"s2watchdog" = {editable = true, path = "."} + +[dev-packages] + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..416e693 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,25 @@ +{ + "_meta": { + "hash": { + "sha256": "a7568a70424d02f7de75f3c40f0434e4a03581a9e51e8f18117c08b4025b9261" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "s2watchdog": { + "editable": true, + "path": "." + } + }, + "develop": {} +} diff --git a/s2watchdog/__init__.py b/s2watchdog/__init__.py new file mode 100644 index 0000000..966eb36 --- /dev/null +++ b/s2watchdog/__init__.py @@ -0,0 +1,2 @@ +def test(): + print('TESTRUN') diff --git a/s2watchdog/pysema.py b/s2watchdog/pysema.py new file mode 100644 index 0000000..4610b13 --- /dev/null +++ b/s2watchdog/pysema.py @@ -0,0 +1,64 @@ +''' simple tool for sending email with sendmail, postfix or ssmtpd + + import pysema + send( + ['alice@example.com', 'bob@example.com'], + 'really good subject line', + 'Message body goes here' + ) +''' + +import subprocess +from email.message import EmailMessage + + +# path to the sendmail programm +SENDMAIL_PATH = '/usr/sbin/sendmail' + + +class SendMailException(Exception): + ''' Exception while using pysema ''' + pass + + +def send(to=None, subject='', message='', **headers): + msg = create_message(to, subject, message, **headers) + send_message(msg) + + +def create_message(to=None, subject='', message='', **headers): + if isinstance(to, str): + recipients = to + elif isinstance(to, (list, set, tuple)): + recipients = ', '.join(to) + else: + raise SendMailException('No recipients given') + + # ensure that the 'from' keyword (if given) is uppercase + sender_mail = headers.pop('from', None) + if sender_mail is not None: + headers['From'] = sender_mail + + headers['To'] = recipients + headers['Subject'] = subject + + msg = EmailMessage() + msg.set_content(message) + for key, value in headers.items(): + msg[key] = value + + return msg + + +def send_message(msg): + if not isinstance(msg, EmailMessage): + raise SendMailException('Type Error: not an EmailMessage') + + try: + subprocess.run( + [SENDMAIL_PATH, '-t', '-oi'], + input=msg.as_bytes(), + check=True + ) + except subprocess.CalledProcessError as e: + raise SendMailException from e diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..4117d12 --- /dev/null +++ b/setup.py @@ -0,0 +1,35 @@ +import os + +from setuptools import setup, find_packages + +here = os.path.abspath(os.path.dirname(__file__)) +with open(os.path.join(here, 'README.md')) as f: + README = f.read() + +requires = [ +] + +setup( + name='s2watchdog', + version='0.0.1', + description='simple watchdog script for our deep freezer alert system', + long_description=README, + classifiers=[ + 'Programming Language :: Python', + 'Environment :: Console', + 'Topic :: System :: Monitoring', + ], + author='Holger Frey', + author_email='frey@imtek.de', + url='https://git.cpi.imtek.uni-freiburg.de/holgi/s2watchdog', + keywords='alerts', + packages=find_packages(), + include_package_data=True, + zip_safe=False, + install_requires=requires, + entry_points={ + 'console_scripts': [ + 's2watchdog = s2watchdog:test', + ], + }, +)