Holger Frey
6 years ago
6 changed files with 139 additions and 0 deletions
@ -0,0 +1,12 @@
@@ -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" |
@ -0,0 +1,25 @@
@@ -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": {} |
||||
} |
@ -0,0 +1,2 @@
@@ -0,0 +1,2 @@
|
||||
def test(): |
||||
print('TESTRUN') |
@ -0,0 +1,64 @@
@@ -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 |
@ -0,0 +1,35 @@
@@ -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', |
||||
], |
||||
}, |
||||
) |
Loading…
Reference in new issue