Initial application creation
This commit is contained in:
parent
d728d578d9
commit
d288a8fb11
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
|
config.py
|
||||||
# ---> Linux
|
# ---> Linux
|
||||||
*~
|
*~
|
||||||
|
|
||||||
|
6
.gitmodules
vendored
Normal file
6
.gitmodules
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[submodule "fstools"]
|
||||||
|
path = fstools
|
||||||
|
url = https://git.mount-mockery.de/pylib/fstools.git
|
||||||
|
[submodule "report"]
|
||||||
|
path = report
|
||||||
|
url = https://git.mount-mockery.de/pylib/report.git
|
41
__install__.py
Normal file
41
__install__.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#!/bin/python
|
||||||
|
#
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
SERVICE_FILE = """
|
||||||
|
[Unit]
|
||||||
|
Description=File2Mail Service
|
||||||
|
After=network-online.target
|
||||||
|
Wants=network-online.target
|
||||||
|
[Service]
|
||||||
|
User=%(UID)d
|
||||||
|
Group=%(GID)d
|
||||||
|
ExecStart=%(MY_PATH)s/file2mail.sh
|
||||||
|
Type=simple
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def help():
|
||||||
|
print("Usage: prog <UID> <GID> <TARGET_PATH>")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) == 4:
|
||||||
|
try:
|
||||||
|
uid = int(sys.argv[1])
|
||||||
|
gid = int(sys.argv[2])
|
||||||
|
except ValueError:
|
||||||
|
help()
|
||||||
|
else:
|
||||||
|
if os.path.isdir(sys.argv[3]):
|
||||||
|
with open(os.path.join(sys.argv[3], 'file2mail.service'), "w") as fh:
|
||||||
|
fh.write(SERVICE_FILE % {
|
||||||
|
"MY_PATH": os.path.dirname(os.path.abspath(__file__)),
|
||||||
|
"UID": uid,
|
||||||
|
"GID": gid})
|
||||||
|
else:
|
||||||
|
help()
|
||||||
|
else:
|
||||||
|
help()
|
14
config_example/config.py
Normal file
14
config_example/config.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# Folder to be monitored for new files
|
||||||
|
TARGET_FOLDER = "/home/scan"
|
||||||
|
|
||||||
|
# Recipiant for the files found in TARGET_PATH
|
||||||
|
SEND_TO = "scan@mount-mockery.de"
|
||||||
|
|
||||||
|
# Sender
|
||||||
|
SEND_FROM = "Scanner-Buchen <scan@mount-mockery.de>"
|
||||||
|
SMTP_SERVER = "mount-mockery.de"
|
||||||
|
SMTP_USER = "smtp-sendmail"
|
||||||
|
SMTP_PASSWORD = "----XXX----"
|
||||||
|
|
||||||
|
# Possible log-levels: DEBUG, INFO, WARNING, ERROR
|
||||||
|
LOG_LEVEL = "INFO"
|
62
file2mail.py
Normal file
62
file2mail.py
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: UTF-8 -*-
|
||||||
|
import config
|
||||||
|
import fstools
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import mail
|
||||||
|
import os
|
||||||
|
import report
|
||||||
|
import time
|
||||||
|
|
||||||
|
LOCK_FILE = os.path.join(config.TARGET_FOLDER, "lock")
|
||||||
|
PID = os.getpid()
|
||||||
|
|
||||||
|
logger = logging.getLogger('root')
|
||||||
|
report.stdoutLoggingConfigure(log_name_lvl=[('root', config.LOG_LEVEL), ])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
while True:
|
||||||
|
# Check if LOCK_FILE EXISTS
|
||||||
|
if not os.path.exists(LOCK_FILE):
|
||||||
|
# Create LOCK_FILE
|
||||||
|
with open(LOCK_FILE, 'w') as fh:
|
||||||
|
fh.write(str(PID))
|
||||||
|
logger.debug("Creating lock-file %s with PID %d", LOCK_FILE, PID)
|
||||||
|
# Read LOCK_FILE content
|
||||||
|
with open(LOCK_FILE, 'r') as fh:
|
||||||
|
lf_pid = json.loads(fh.read())
|
||||||
|
# Check if LOCK_FILE has my PID
|
||||||
|
if lf_pid == PID:
|
||||||
|
# Scan for files to send
|
||||||
|
for filename in fstools.filelist(config.TARGET_FOLDER):
|
||||||
|
# Exclude LOCK_FILE
|
||||||
|
if filename != LOCK_FILE:
|
||||||
|
logger.info("Found %s to send via E-Mail to %s.", filename, config.SEND_TO)
|
||||||
|
# Send File as E-Mail
|
||||||
|
try:
|
||||||
|
mail.send_mail(
|
||||||
|
config.SEND_FROM,
|
||||||
|
config.SEND_TO,
|
||||||
|
"Gesanntes Dokument - %s" % os.path.basename(filename),
|
||||||
|
"Hier ist das gescannte Dokument vom Scanner in Buchen:",
|
||||||
|
files=[filename],
|
||||||
|
server=config.SMTP_SERVER,
|
||||||
|
username=config.SMTP_USER,
|
||||||
|
password=config.SMTP_PASSWORD
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Exception while sending an E-Mail")
|
||||||
|
else:
|
||||||
|
# Remove file
|
||||||
|
logger.debug("Removing file %s", filename)
|
||||||
|
os.remove(filename )
|
||||||
|
# Remove LOCK_FILE
|
||||||
|
logger.debug("Removing lock-file %s", LOCK_FILE)
|
||||||
|
os.remove(LOCK_FILE)
|
||||||
|
else:
|
||||||
|
logger.warning("LOCK_FILE has PID %d and this proces %d. No action!", lf_pid, PID)
|
||||||
|
else:
|
||||||
|
logger.warning("LOCK_FILE %s already exists. No action!", LOCK_FILE)
|
||||||
|
time.sleep(60)
|
4
file2mail.sh
Executable file
4
file2mail.sh
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
BASEPATH=`dirname $0`
|
||||||
|
python $BASEPATH/file2mail.py
|
1
fstools
Submodule
1
fstools
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit c10e8792abb05671dab6de51cdadda3bf8ead50f
|
48
mail.py
Normal file
48
mail.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import smtplib
|
||||||
|
from pathlib import Path
|
||||||
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
from email.mime.base import MIMEBase
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
from email.utils import COMMASPACE, formatdate
|
||||||
|
from email import encoders
|
||||||
|
|
||||||
|
|
||||||
|
def send_mail(send_from, send_to, subject, message, files=[],
|
||||||
|
server="localhost", username='', password='',
|
||||||
|
use_tls=True):
|
||||||
|
"""Compose and send email with provided info and attachments.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
send_from (str): from name
|
||||||
|
send_to (list[str]): to name(s)
|
||||||
|
subject (str): message title
|
||||||
|
message (str): message body
|
||||||
|
files (list[str]): list of file paths to be attached to email
|
||||||
|
server (str): mail server host name
|
||||||
|
username (str): server auth username
|
||||||
|
password (str): server auth password
|
||||||
|
use_tls (bool): use TLS mode
|
||||||
|
"""
|
||||||
|
msg = MIMEMultipart()
|
||||||
|
msg['From'] = send_from
|
||||||
|
msg['To'] = send_to
|
||||||
|
msg['Date'] = formatdate(localtime=True)
|
||||||
|
msg['Subject'] = subject
|
||||||
|
|
||||||
|
msg.attach(MIMEText(message))
|
||||||
|
|
||||||
|
for path in files:
|
||||||
|
part = MIMEBase('application', "octet-stream")
|
||||||
|
with open(path, 'rb') as file:
|
||||||
|
part.set_payload(file.read())
|
||||||
|
encoders.encode_base64(part)
|
||||||
|
part.add_header('Content-Disposition',
|
||||||
|
'attachment; filename={}'.format(Path(path).name))
|
||||||
|
msg.attach(part)
|
||||||
|
|
||||||
|
smtp = smtplib.SMTP(server)
|
||||||
|
if use_tls:
|
||||||
|
smtp.starttls()
|
||||||
|
smtp.login(username, password)
|
||||||
|
smtp.sendmail(send_from, send_to.split(', '), msg.as_string())
|
||||||
|
smtp.quit()
|
1
report
Submodule
1
report
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit b53dd30eae1d679b7eec4999bec50aed55bc105b
|
Loading…
x
Reference in New Issue
Block a user