diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..ca4758c --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,3 @@ +include LICENSE README.md +recursive-include docs * +recursive-include misc * diff --git a/docs/config.py b/docs/config.py new file mode 100644 index 0000000..4f1612d --- /dev/null +++ b/docs/config.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 + +#################################### +# Example usage of TaskScheduler # +#################################### + +#def custom_job(event, task_id): +# logging.info(f"{task_id}: execute task for {event}") +# +#s = TaskScheduler(delay=10, job=custom_job) + + +##################################################### +# Example usage of TaskScheduler with FileManager # +##################################################### + +#fm = FileManager( +# rules=[ +# {"action": "move", +# "src_re": r"^(?P.*)", +# "dst_re": r"\g.processed"} +# ] +#) +# +#s = TaskScheduler(delay=10, job=fm.job) + + +##################################### +# Example usage of ShellScheduler # +##################################### + +#s = ShellScheduler(cmd="/usr/local/bin/task.sh {maskname} {pathname} {src_pathname}") + + +############################### +# Example pyinotifyd config # +############################### + +#pyinotifyd_config = { +# "watches": [ +# {"path": "/tmp", +# "rec": True, +# "auto_add": True, +# "event_map": { +# "IN_ACCESS": None, +# "IN_ATTRIB": None, +# "IN_CLOSE_NOWRITE": None, +# "IN_CLOSE_WRITE": s.schedule, +# "IN_CREATE": None, +# "IN_DELETE": s.cancel, +# "IN_DELETE_SELF": s.cancel, +# "IN_IGNORED": None, +# "IN_MODIFY": s.cancel, +# "IN_MOVE_SELF": None, +# "IN_MOVED_FROM": s.cancel, +# "IN_MOVED_TO": s.schedule, +# "IN_OPEN": None, +# "IN_Q_OVERFLOW": None, +# "IN_UNMOUNT": s.cancel} +# } +# ], +# "loglevel": logging.INFO, +# "shutdown_timeout": 15} diff --git a/misc/pyinotifyd.service b/misc/pyinotifyd.service new file mode 100644 index 0000000..3e6f542 --- /dev/null +++ b/misc/pyinotifyd.service @@ -0,0 +1,11 @@ +[Unit] +Description=pyinotifyd +After=fs.target + +[Service] +Type=simple +ExecStart=/usr/bin/pyinotifyd +TimeoutStopSec=300 + +[Install] +WantedBy=multi-user.target diff --git a/pyinotifyd.py b/pyinotifyd.py index 0775d4d..b1e158c 100755 --- a/pyinotifyd.py +++ b/pyinotifyd.py @@ -27,6 +27,7 @@ import sys from shlex import quote as shell_quote from uuid import uuid4 +__version__ = "0.0.1" class Task: def __init__(self, event, delay, task_id, job, callback=None, @@ -129,16 +130,19 @@ class ShellScheduler(TaskScheduler): cmd = cmd.replace("{maskname}", shell_quote(maskname)) cmd = cmd.replace("{pathname}", shell_quote(event.pathname)) if hasattr(event, "src_pathname"): - cmd = cmd.replace( - "{src_pathname}", shell_quote(event.src_pathname)) + src_pathname = event.src_pathname + else: + src_pathname = "" + cmd = cmd.replace( + "{src_pathname}", shell_quote(src_pathname)) self._log.info(f"{task_id}: execute shell command: {cmd}") proc = await asyncio.create_subprocess_shell(cmd) await proc.communicate() class FileManager: - def __init__(self, rules, auto_create=False, rec=False, + def __init__(self, rules, auto_create=True, rec=False, logname="FileManager"): self._rules = [] if not isinstance(rules, list): @@ -260,21 +264,27 @@ def main(): description="pyinotifyd", formatter_class=lambda prog: argparse.HelpFormatter( prog, max_help_position=45, width=140)) - parser.add_argument( "-c", "--config", help="path to config file (defaults to /etc/pyinotifyd/config.py)", default="/etc/pyinotifyd/config.py") - parser.add_argument( "-d", "--debug", - help="Log debugging messages.", + help="log debugging messages", + action="store_true") + parser.add_argument( + "-v", + "--version", + help="show version and exit", action="store_true") - args = parser.parse_args() + if args.version: + print(f"pyinotifyd ({__version__})") + sys.exit(0) + default_config = { "watches": [], "loglevel": logging.INFO, diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..3401800 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +version = attr: pyinotifyd.__version__ diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..26477b3 --- /dev/null +++ b/setup.py @@ -0,0 +1,40 @@ +from setuptools import setup + +def read_file(fname): + with open(fname, 'r') as f: + return f.read() + +setup(name = "pyinotifyd", + author = "Thomas Oettli", + author_email = "spacefreak@noop.ch", + description = "Monitoring filesystems events with inotify on Linux and execute tasks.", + license = "GPL 3", + keywords = "inotify daemon", + url = "https://github.com/spacefreak86/pyinotifyd", + py_modules = ["pyinotifyd"], + long_description = read_file("README.md"), + long_description_content_type = "text/markdown", + classifiers = [ + # 3 - Alpha + # 4 - Beta + # 5 - Production/Stable + "Development Status :: 3 - Alpha", + "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", + "Operating System :: OS Independent", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Topic :: Utilities" + ], + include_package_data = True, + entry_points = { + "console_scripts": [ + "pyinotifyd=pyinotifyd:main" + ] + }, + data_files = [ + ("/etc/pyinotifyd", ["docs/config.py"]), + ("/usr/lib/systemd/system", ["misc/pyinotifyd.service"]) + ], + install_requires = ["pyinotify"], + python_requires = ">=3.7" +)