19 Commits
8 changed files with 179 additions and 109 deletions
+26 -30
View File
@@ -1,6 +1,9 @@
# pyinotifyd
A daemon to monitor filesystems events with inotify on Linux and run tasks like filesystem operations (copy, move or delete), a shell commands or custom async python methods.
It is possible to schedule tasks with a delay, delayed tasks can be cancelled again in case a certain event occurs. A useful example would be to run tasks only if a file has not changed within a certain amount of time.
A daemon to monitor filesystems events with inotify on Linux and run tasks like filesystem operations (copy, move or delete), a shell command or custom async python methods.
It is possible to schedule tasks with a delay, which can then be canceled again in case a canceling event occurs. A useful example for this is to run tasks only if a file has not changed within a certain amount of time.
pyinotifyd offers great flexibility through its dev-op configuration approach, which enables you to do almost anything you want.
# Requirements
* [pyinotify](https://github.com/seb-m/pyinotify)
@@ -70,7 +73,8 @@ task_sched = TaskScheduler(
### ShellScheduler
Schedule a shell command *cmd*. Replace **{maskname}**, **{pathname}** and **{src_pathname}** in *cmd* with the actual values of occuring events. This scheduler is based on TaskScheduler and has the same optional arguments.
```python
# Please note that **{src_pathname}** is only present for IN_MOVED_TO events and only in the case where the IN_MOVED_FROM events are watched too.
# Please note that **{src_pathname}** is only present for IN_MOVED_TO events and only
# in the case where the IN_MOVED_FROM events are watched too.
# If it is not present, the command line argument will be an empty string.
shell_sched = ShellScheduler(
cmd="/usr/local/bin/task.sh {maskname} {pathname} {src_pathname}")
@@ -126,7 +130,7 @@ event_map = {
"IN_Q_OVERFLOW": None,
"IN_UNMOUNT": Cancel(task_sched)}
# It is possible to instantiate an event map with a default scheduler set for every event,
# It is possible to instantiate an event map with a default scheduler set for every event
event_map = EventMap(default_sched=task_sched)
```
The following events are available:
@@ -172,42 +176,34 @@ w = Watch(
rec=False,
auto_add=False)
pyinotifyd.add_watch(
watch=w)
pyinotifyd.add_watch(watch=w)
```
## Logging
Pythons [logging](https://docs.python.org/3/howto/logging.html) framework is used to log messages (see https://docs.python.org/3/howto/logging.html).
The following loglevels are available:
* DEBUG
* INFO
* WARNING
* ERROR
* CRITICAL
```python
# Configure global loglevel
setLoglevel(INFO)
Configure the global loglevel. This is the default:
```python
logging.getLogger().setLevel(logging.WARNING)
```
It is possible to configure the loglevel per *logname*. This is an example for logname **sched**:
```python
logging.getLogger("sched").setLevel(logging.INFO)
# Configure loglevel per logname.
setLoglevel(INFO, logname="daemon")
```
### Syslog
Add this to your config file to send log messages to a local syslog server.
Send log messages to the local syslog server.
```python
# send log messages to the Unix socket of the syslog server.
syslog = logging.handlers.SysLogHandler(
address="/dev/log")
# Enable logging to local syslog server (/dev/log).
# Use *address* to specify a different target.
enableSyslog(loglevel=INFO, address="/dev/log")
# set the log format of syslog messages
log_format = "pyinotifyd/%(name)s: %(message)s"
syslog.setFormatter(
logging.Formatter(formatter)
# set the log level for syslog messages
syslog.setLevel(logging.INFO)
# enable syslog for pyinotifyd
logging.getLogger().addHandler(syslog)
# or enable syslog just for the daemon
logging.getLogger("daemon").addHandler(syslog)
# Enable syslog per logname
enableSyslog(lglevel=INFO, name="daemon")
```
# Examples
@@ -38,7 +38,7 @@ python_install_all() {
dodir /etc/${PN}
insinto /etc/${PN}
newins ${PN}/misc/config.py.example config.py
newins ${PN}/misc/config.py.default config.py
use systemd && systemd_dounit ${PN}/misc/${PN}.service
@@ -38,7 +38,7 @@ python_install_all() {
dodir /etc/${PN}
insinto /etc/${PN}
newins ${PN}/misc/config.py.example config.py
newins ${PN}/misc/config.py.default config.py
use systemd && systemd_dounit ${PN}/misc/${PN}.service
+30 -11
View File
@@ -15,7 +15,9 @@
#
__all__ = [
"EventMap"
"setLoglevel",
"enableSyslog",
"EventMap",
"Watch",
"Pyinotifyd",
"DaemonInstance",
@@ -34,7 +36,23 @@ from pyinotify import ProcessEvent
from pyinotifyd._install import install, uninstall
from pyinotifyd.scheduler import TaskScheduler, Cancel
__version__ = "0.0.2"
__version__ = "0.0.5"
def setLoglevel(loglevel, logname=None):
logger = logging.getLogger(logname)
logger.setLevel(loglevel)
def enableSyslog(loglevel=None, address="/dev/log", logname=None):
logger = logging.getLogger(logname)
syslog = logging.handlers.SysLogHandler(address=address)
syslog.setFormatter(
logging.Formatter(f"{Pyinotifyd.name}/%(name)s: %(message)s"))
if loglevel:
syslog.setLevel(loglevel)
logger.addHandler(syslog)
class _SchedulerList:
@@ -184,8 +202,10 @@ class Pyinotifyd:
def from_cfg_file(config_file):
config = {}
name = Pyinotifyd.name
exec("import logging", {}, config)
exec("from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL",
{}, config)
exec(f"from {name} import Pyinotifyd, Watch", {}, config)
exec(f"from {name} import setLoglevel, enableSyslog", {}, config)
exec(f"from {name}.scheduler import *", {}, config)
with open(config_file, "r") as fh:
exec(fh.read(), {}, config)
@@ -416,15 +436,14 @@ def main():
ch.setFormatter(formatter)
loop = asyncio.get_event_loop()
for signame in ["SIGINT", "SIGTERM"]:
loop.add_signal_handler(
getattr(signal, signame),
lambda: loop.create_task(
daemon.shutdown(signame)))
loop.add_signal_handler(
getattr(signal, "SIGHUP"),
lambda: loop.create_task(
signal.SIGTERM, lambda: loop.create_task(
daemon.shutdown("SIGTERM")))
loop.add_signal_handler(
signal.SIGINT, lambda: loop.create_task(
daemon.shutdown("SIGINT")))
loop.add_signal_handler(
signal.SIGHUP, lambda: loop.create_task(
daemon.reload("SIGHUP", args.config, args.debug)))
daemon.start()
+92 -60
View File
@@ -14,15 +14,96 @@
# along with pyinotifyd. If not, see <http://www.gnu.org/licenses/>.
#
import filecmp
import logging
import os
import shutil
import sys
SYSTEMD_PATH = "/lib/systemd/system"
OPENRC = "/sbin/openrc"
def _systemd_files(pkg_dir, name):
return [
(f"{pkg_dir}/misc/systemd/{name}.service",
f"{SYSTEMD_PATH}/{name}.service", True)]
def _openrc_files(pkg_dir, name):
return [
(f"{pkg_dir}/misc/openrc/{name}.initd", f"/etc/init.d/{name}", True),
(f"{pkg_dir}/misc/openrc/{name}.confd", f"/etc/conf.d/{name}", False)]
def _config_files(pkg_dir, name):
return [
(f"{pkg_dir}/misc/config.py.default", f"/etc/{name}/config.py", False)]
def _install_files(files):
for src, dst, force in files:
if os.path.exists(dst):
if os.path.isdir(dst):
logging.error(
" => unable to copy file, destination path is a directory")
continue
elif not force:
logging.info(f" => file {dst} already exists")
continue
try:
logging.info(f" => install file {dst}")
shutil.copy2(src, dst)
except Exception as e:
logging.error(f" => unable to install file {dst}: {e}")
def _uninstall_files(files):
for src, dst, force in files:
if not os.path.isfile(dst):
continue
if not force and not filecmp.cmp(src, dst, shallow=True):
logging.warning(
f" => keep modified file {dst}, "
f"you have to remove it manually")
continue
try:
logging.info(f" => uninstall file {dst}")
os.remove(dst)
except Exception as e:
logging.error(f" => unable to uninstall file {dst}: {e}")
def _create_dir(path):
if os.path.isdir(path):
logging.info(f" => directory {path} already exists")
else:
try:
logging.info(f" => create directory {path}")
os.mkdir(path)
except Exception as e:
logging.error(f" => unable to create directory {path}: {e}")
return False
return True
def _delete_dir(path):
if os.path.isdir(path):
if not os.listdir(path):
try:
logging.info(f" => delete directory {path}")
os.rmdir(path)
except Exception as e:
logging.error(f" => unable to delete directory {path}: {e}")
else:
logging.warning(f" => keep non-empty directory {path}")
def _check_root():
if os.getuid() != 0:
logging.error("you need to have root privileges, please try again")
@@ -47,37 +128,6 @@ def _check_openrc():
return openrc
def _copy_missing_file(src, dst):
if os.path.exists(dst):
logging.info(f" => file {dst} already installed")
else:
try:
logging.info(f" => install file {dst}")
shutil.copy2(src, dst)
except Exception as e:
logging.error(f" => unable to install file {dst}: {e}")
def _delete_present_file(f):
if os.path.isfile(f):
try:
logging.info(f" => uninstall file {f}")
os.remove(f)
except Exception as e:
logging.error(f" => unable to uninstall file {f}: {e}")
def _warn_exists(path):
if os.path.isdir(path):
logging.warning(
f" => directory {path} is still present, "
f"you have to remove it manually")
else:
logging.warning(
f" => file {path} is still present, "
f"you have to remove it manually")
def install(name):
if not _check_root():
sys.exit(2)
@@ -85,33 +135,16 @@ def install(name):
pkg_dir = os.path.dirname(__file__)
if _check_systemd():
dst = f"{SYSTEMD_PATH}/{name}.service"
src = f"{pkg_dir}/misc/systemd/{name}.service"
_copy_missing_file(src, dst)
_install_files(_systemd_files(pkg_dir, name))
if _check_openrc():
files = [
(f"{pkg_dir}/misc/openrc/{name}.initd", f"/etc/init.d/{name}"),
(f"{pkg_dir}/misc/openrc/{name}.confd", f"/etc/conf.d/{name}")]
for src, dst in files:
_copy_missing_file(src, dst)
_install_files(_openrc_files(pkg_dir, name))
logging.info("install configuration file")
config_dir = f"/etc/{name}"
if os.path.isdir(config_dir):
logging.info(f" => directory {config_dir} already exists")
else:
try:
logging.info(f" => create directory {config_dir}")
os.mkdir(config_dir)
except Exception as e:
logging.error(f" => unable to create directory {config_dir}: {e}")
sys.exit(3)
if not _create_dir(f"/etc/{name}"):
logging.error(" => unable to create config dir, giving up ...")
sys.exit(3)
files = [
(f"{pkg_dir}/misc/config.py.default", f"{config_dir}/config.py")]
for src, dst in files:
_copy_missing_file(src, dst)
_install_files(_config_files(pkg_dir, name))
logging.info(f"{name} successfully installed")
@@ -120,13 +153,12 @@ def uninstall(name):
if not _check_root():
sys.exit(2)
if _check_systemd():
_delete_present_file(f"{SYSTEMD_PATH}/{name}.service")
pkg_dir = os.path.dirname(__file__)
if _check_openrc():
_delete_present_file(f"/etc/init.d/{name}")
_warn_exists(f"/etc/conf.d/{name}")
_uninstall_files(_systemd_files(pkg_dir, name))
_uninstall_files(_openrc_files(pkg_dir, name))
_uninstall_files(_config_files(pkg_dir, name))
_warn_exists(f"/etc/{name}")
_delete_dir(f"/etc/{name}")
logging.info(f"{name} successfully uninstalled")
+24
View File
@@ -82,3 +82,27 @@
# event_map = event_map,
# rec=True,
# auto_add=True)
################
# Log config #
################
# set global loglevel
#setLoglevel(DEBUG)
# set loglevel per logname
#setLoglevel(
# DEBUG,
# logname="daemon")
# enable syslog
#enableSyslog(
# loglevel=DEBUG,
# address="/dev/log")
# enable syslog per logname
#enableSyslog(
# loglevel=DEBUG,
# address="/dev/log",
# logname="sched")
+4 -5
View File
@@ -25,7 +25,6 @@ import os
import re
import shutil
from dataclasses import dataclass
from inspect import iscoroutinefunction
from shlex import quote as shell_quote
from uuid import uuid4
@@ -46,11 +45,11 @@ class SchedulerLogger(logging.LoggerAdapter):
class TaskScheduler:
@dataclass
class TaskState:
id: str = str(uuid4())
task: asyncio.Task = None
cancelable: bool = True
def __init__(self, id=None, task=None, cancelable=True):
self.id = id or str(uuid4())
self.task = task
self.cancelable = cancelable
def __init__(self, job, files=True, dirs=False, delay=0, logname="sched",
loop=None):
+1 -1
View File
@@ -18,7 +18,7 @@ setup(name = "pyinotifyd",
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
"Development Status :: 3 - Alpha",
"Development Status :: 4 - Beta",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Operating System :: OS Independent",
"Programming Language :: Python",