Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9794b89b5a
|
||
|
|
8445ca7cb4
|
||
|
|
75241f60f4
|
||
|
|
99724905be
|
||
|
|
34ac9cd596
|
||
|
|
5b589cc999
|
||
|
|
d1b1fc9a4e
|
||
|
|
13dbfeb8ee
|
||
|
|
d6a91d6a5f
|
||
|
|
fe3ecc0fa6
|
||
|
|
b1dffffb5c
|
||
|
|
6504662cb8
|
@@ -1,6 +1,9 @@
|
|||||||
# pyinotifyd
|
# 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.
|
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, 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.
|
|
||||||
|
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
|
# Requirements
|
||||||
* [pyinotify](https://github.com/seb-m/pyinotify)
|
* [pyinotify](https://github.com/seb-m/pyinotify)
|
||||||
@@ -70,7 +73,8 @@ task_sched = TaskScheduler(
|
|||||||
### ShellScheduler
|
### 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.
|
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
|
```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.
|
# If it is not present, the command line argument will be an empty string.
|
||||||
shell_sched = ShellScheduler(
|
shell_sched = ShellScheduler(
|
||||||
cmd="/usr/local/bin/task.sh {maskname} {pathname} {src_pathname}")
|
cmd="/usr/local/bin/task.sh {maskname} {pathname} {src_pathname}")
|
||||||
@@ -126,7 +130,7 @@ event_map = {
|
|||||||
"IN_Q_OVERFLOW": None,
|
"IN_Q_OVERFLOW": None,
|
||||||
"IN_UNMOUNT": Cancel(task_sched)}
|
"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)
|
event_map = EventMap(default_sched=task_sched)
|
||||||
```
|
```
|
||||||
The following events are available:
|
The following events are available:
|
||||||
@@ -186,16 +190,16 @@ The following loglevels are available:
|
|||||||
```python
|
```python
|
||||||
# Configure global loglevel
|
# Configure global loglevel
|
||||||
setLoglevel(INFO)
|
setLoglevel(INFO)
|
||||||
```
|
|
||||||
Configure loglevel per *logname*.
|
# Configure loglevel per logname.
|
||||||
```python
|
|
||||||
setLoglevel(INFO, logname="daemon")
|
setLoglevel(INFO, logname="daemon")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Syslog
|
### Syslog
|
||||||
Send log messages to the local syslog server.
|
Send log messages to the local syslog server.
|
||||||
```python
|
```python
|
||||||
# Enable logging to local syslog server (/dev/log). Use *address* to specify a different target.
|
# Enable logging to local syslog server (/dev/log).
|
||||||
|
# Use *address* to specify a different target.
|
||||||
enableSyslog(loglevel=INFO, address="/dev/log")
|
enableSyslog(loglevel=INFO, address="/dev/log")
|
||||||
|
|
||||||
# Enable syslog per logname
|
# Enable syslog per logname
|
||||||
|
|||||||
+11
-10
@@ -15,7 +15,9 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"EventMap"
|
"setLoglevel",
|
||||||
|
"enableSyslog",
|
||||||
|
"EventMap",
|
||||||
"Watch",
|
"Watch",
|
||||||
"Pyinotifyd",
|
"Pyinotifyd",
|
||||||
"DaemonInstance",
|
"DaemonInstance",
|
||||||
@@ -34,7 +36,7 @@ from pyinotify import ProcessEvent
|
|||||||
from pyinotifyd._install import install, uninstall
|
from pyinotifyd._install import install, uninstall
|
||||||
from pyinotifyd.scheduler import TaskScheduler, Cancel
|
from pyinotifyd.scheduler import TaskScheduler, Cancel
|
||||||
|
|
||||||
__version__ = "0.0.3"
|
__version__ = "0.0.5"
|
||||||
|
|
||||||
|
|
||||||
def setLoglevel(loglevel, logname=None):
|
def setLoglevel(loglevel, logname=None):
|
||||||
@@ -434,15 +436,14 @@ def main():
|
|||||||
ch.setFormatter(formatter)
|
ch.setFormatter(formatter)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
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(
|
loop.add_signal_handler(
|
||||||
getattr(signal, "SIGHUP"),
|
signal.SIGTERM, lambda: loop.create_task(
|
||||||
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.reload("SIGHUP", args.config, args.debug)))
|
||||||
|
|
||||||
daemon.start()
|
daemon.start()
|
||||||
|
|||||||
+92
-60
@@ -14,15 +14,96 @@
|
|||||||
# along with pyinotifyd. If not, see <http://www.gnu.org/licenses/>.
|
# along with pyinotifyd. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
import filecmp
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
SYSTEMD_PATH = "/lib/systemd/system"
|
SYSTEMD_PATH = "/lib/systemd/system"
|
||||||
OPENRC = "/sbin/openrc"
|
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():
|
def _check_root():
|
||||||
if os.getuid() != 0:
|
if os.getuid() != 0:
|
||||||
logging.error("you need to have root privileges, please try again")
|
logging.error("you need to have root privileges, please try again")
|
||||||
@@ -47,37 +128,6 @@ def _check_openrc():
|
|||||||
return 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):
|
def install(name):
|
||||||
if not _check_root():
|
if not _check_root():
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
@@ -85,33 +135,16 @@ def install(name):
|
|||||||
pkg_dir = os.path.dirname(__file__)
|
pkg_dir = os.path.dirname(__file__)
|
||||||
|
|
||||||
if _check_systemd():
|
if _check_systemd():
|
||||||
dst = f"{SYSTEMD_PATH}/{name}.service"
|
_install_files(_systemd_files(pkg_dir, name))
|
||||||
src = f"{pkg_dir}/misc/systemd/{name}.service"
|
|
||||||
_copy_missing_file(src, dst)
|
|
||||||
|
|
||||||
if _check_openrc():
|
if _check_openrc():
|
||||||
files = [
|
_install_files(_openrc_files(pkg_dir, name))
|
||||||
(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)
|
|
||||||
|
|
||||||
logging.info("install configuration file")
|
if not _create_dir(f"/etc/{name}"):
|
||||||
config_dir = f"/etc/{name}"
|
logging.error(" => unable to create config dir, giving up ...")
|
||||||
if os.path.isdir(config_dir):
|
sys.exit(3)
|
||||||
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)
|
|
||||||
|
|
||||||
files = [
|
_install_files(_config_files(pkg_dir, name))
|
||||||
(f"{pkg_dir}/misc/config.py.default", f"{config_dir}/config.py")]
|
|
||||||
for src, dst in files:
|
|
||||||
_copy_missing_file(src, dst)
|
|
||||||
|
|
||||||
logging.info(f"{name} successfully installed")
|
logging.info(f"{name} successfully installed")
|
||||||
|
|
||||||
@@ -120,13 +153,12 @@ def uninstall(name):
|
|||||||
if not _check_root():
|
if not _check_root():
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
if _check_systemd():
|
pkg_dir = os.path.dirname(__file__)
|
||||||
_delete_present_file(f"{SYSTEMD_PATH}/{name}.service")
|
|
||||||
|
|
||||||
if _check_openrc():
|
_uninstall_files(_systemd_files(pkg_dir, name))
|
||||||
_delete_present_file(f"/etc/init.d/{name}")
|
_uninstall_files(_openrc_files(pkg_dir, name))
|
||||||
_warn_exists(f"/etc/conf.d/{name}")
|
_uninstall_files(_config_files(pkg_dir, name))
|
||||||
|
|
||||||
_warn_exists(f"/etc/{name}")
|
_delete_dir(f"/etc/{name}")
|
||||||
|
|
||||||
logging.info(f"{name} successfully uninstalled")
|
logging.info(f"{name} successfully uninstalled")
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import os
|
|||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
from inspect import iscoroutinefunction
|
from inspect import iscoroutinefunction
|
||||||
from shlex import quote as shell_quote
|
from shlex import quote as shell_quote
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
@@ -46,11 +45,11 @@ class SchedulerLogger(logging.LoggerAdapter):
|
|||||||
|
|
||||||
class TaskScheduler:
|
class TaskScheduler:
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class TaskState:
|
class TaskState:
|
||||||
id: str = str(uuid4())
|
def __init__(self, id=None, task=None, cancelable=True):
|
||||||
task: asyncio.Task = None
|
self.id = id or str(uuid4())
|
||||||
cancelable: bool = True
|
self.task = task
|
||||||
|
self.cancelable = cancelable
|
||||||
|
|
||||||
def __init__(self, job, files=True, dirs=False, delay=0, logname="sched",
|
def __init__(self, job, files=True, dirs=False, delay=0, logname="sched",
|
||||||
loop=None):
|
loop=None):
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ setup(name = "pyinotifyd",
|
|||||||
# 3 - Alpha
|
# 3 - Alpha
|
||||||
# 4 - Beta
|
# 4 - Beta
|
||||||
# 5 - Production/Stable
|
# 5 - Production/Stable
|
||||||
"Development Status :: 3 - Alpha",
|
"Development Status :: 4 - Beta",
|
||||||
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
|
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
|
||||||
"Operating System :: OS Independent",
|
"Operating System :: OS Independent",
|
||||||
"Programming Language :: Python",
|
"Programming Language :: Python",
|
||||||
|
|||||||
Reference in New Issue
Block a user