Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1b1fc9a4e
|
||
|
|
13dbfeb8ee
|
||
|
|
d6a91d6a5f
|
||
|
|
fe3ecc0fa6
|
||
|
|
b1dffffb5c
|
||
|
|
6504662cb8
|
||
|
|
e8fe7915c1
|
||
|
|
4726c17366
|
||
|
|
1a8e7ccb92
|
||
|
|
dbe7ba83b6
|
||
|
|
56a8586168
|
||
|
|
977f06436d
|
||
|
|
2441ba2048
|
@@ -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)
|
||||||
@@ -172,42 +175,34 @@ w = Watch(
|
|||||||
rec=False,
|
rec=False,
|
||||||
auto_add=False)
|
auto_add=False)
|
||||||
|
|
||||||
pyinotifyd.add_watch(
|
pyinotifyd.add_watch(watch=w)
|
||||||
watch=w)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Logging
|
## 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).
|
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:
|
# Configure loglevel per logname.
|
||||||
```python
|
setLoglevel(INFO, logname="daemon")
|
||||||
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)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Syslog
|
### 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
|
```python
|
||||||
# send log messages to the Unix socket of the syslog server.
|
# Enable logging to local syslog server (/dev/log).
|
||||||
syslog = logging.handlers.SysLogHandler(
|
# Use *address* to specify a different target.
|
||||||
address="/dev/log")
|
enableSyslog(loglevel=INFO, address="/dev/log")
|
||||||
|
|
||||||
# set the log format of syslog messages
|
# Enable syslog per logname
|
||||||
log_format = "pyinotifyd/%(name)s: %(message)s"
|
enableSyslog(lglevel=INFO, name="daemon")
|
||||||
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)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ python_install_all() {
|
|||||||
|
|
||||||
dodir /etc/${PN}
|
dodir /etc/${PN}
|
||||||
insinto /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
|
use systemd && systemd_dounit ${PN}/misc/${PN}.service
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ python_install_all() {
|
|||||||
|
|
||||||
dodir /etc/${PN}
|
dodir /etc/${PN}
|
||||||
insinto /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
|
use systemd && systemd_dounit ${PN}/misc/${PN}.service
|
||||||
|
|
||||||
|
|||||||
+23
-3
@@ -15,7 +15,9 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"EventMap"
|
"setLoglevel",
|
||||||
|
"enableSyslog",
|
||||||
|
"EventMap",
|
||||||
"Watch",
|
"Watch",
|
||||||
"Pyinotifyd",
|
"Pyinotifyd",
|
||||||
"DaemonInstance",
|
"DaemonInstance",
|
||||||
@@ -34,7 +36,23 @@ 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.2"
|
__version__ = "0.0.4"
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
class _SchedulerList:
|
||||||
@@ -184,8 +202,10 @@ class Pyinotifyd:
|
|||||||
def from_cfg_file(config_file):
|
def from_cfg_file(config_file):
|
||||||
config = {}
|
config = {}
|
||||||
name = Pyinotifyd.name
|
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 Pyinotifyd, Watch", {}, config)
|
||||||
|
exec(f"from {name} import setLoglevel, enableSyslog", {}, config)
|
||||||
exec(f"from {name}.scheduler import *", {}, config)
|
exec(f"from {name}.scheduler import *", {}, config)
|
||||||
with open(config_file, "r") as fh:
|
with open(config_file, "r") as fh:
|
||||||
exec(fh.read(), {}, config)
|
exec(fh.read(), {}, config)
|
||||||
|
|||||||
+91
-59
@@ -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):
|
|
||||||
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)
|
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")
|
||||||
|
|||||||
@@ -82,3 +82,27 @@
|
|||||||
# event_map = event_map,
|
# event_map = event_map,
|
||||||
# rec=True,
|
# rec=True,
|
||||||
# auto_add=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")
|
||||||
|
|||||||
Reference in New Issue
Block a user