Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1b1fc9a4e
|
||
|
|
13dbfeb8ee
|
||
|
|
d6a91d6a5f
|
||
|
|
fe3ecc0fa6
|
||
|
|
b1dffffb5c
|
||
|
|
6504662cb8
|
@@ -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)
|
||||
@@ -186,16 +189,16 @@ The following loglevels are available:
|
||||
```python
|
||||
# Configure global loglevel
|
||||
setLoglevel(INFO)
|
||||
```
|
||||
Configure loglevel per *logname*.
|
||||
```python
|
||||
|
||||
# Configure loglevel per logname.
|
||||
setLoglevel(INFO, logname="daemon")
|
||||
```
|
||||
|
||||
### Syslog
|
||||
Send log messages to the local syslog server.
|
||||
```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")
|
||||
|
||||
# Enable syslog per logname
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
#
|
||||
|
||||
__all__ = [
|
||||
"EventMap"
|
||||
"setLoglevel",
|
||||
"enableSyslog",
|
||||
"EventMap",
|
||||
"Watch",
|
||||
"Pyinotifyd",
|
||||
"DaemonInstance",
|
||||
@@ -34,7 +36,7 @@ from pyinotify import ProcessEvent
|
||||
from pyinotifyd._install import install, uninstall
|
||||
from pyinotifyd.scheduler import TaskScheduler, Cancel
|
||||
|
||||
__version__ = "0.0.3"
|
||||
__version__ = "0.0.4"
|
||||
|
||||
|
||||
def setLoglevel(loglevel, logname=None):
|
||||
|
||||
+91
-59
@@ -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}")
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user