diff --git a/README.md b/README.md index 3737999..b13446b 100644 --- a/README.md +++ b/README.md @@ -27,14 +27,22 @@ FileManager moves, copy or deletes files and/or directories following a list of A rule holds an *action* (move, copy or delete) and a regular expression *src_re*. The FileManager task will be executed if *src_re* matches the path of an event. If the action is copy or move, the destination path *dst_re* is mandatory and if *action* is delete and *rec* is set to True, non-empty directories will be deleted recursively. -With *auto_create* set to True, possibly missing subdirectories in *dst_re* are created automatically. Regex subgroups or named-subgroups may be used in *src_re* and *dst_re*. Use *logname* in log messages. -The optional *filemode*, *dirmode*, *user* and *group* arguments are applied to every file and/or directory that is processed by FileManager. That includes automatically created subdirectories. +With *auto_create* set to True, possibly missing subdirectories in *dst_re* are created automatically. Regex subgroups or named-subgroups may be used in *src_re* and *dst_re*. +Set the mode of moved/copied files/directories with *filemode* and *dirmode*. Ownership of moved/copied files/directories is set with *user* and *group*. Mode and ownership is also set to automatically created subdirectories. +Use *logname* in log messages. ```python -rule = Rule( - action="move", src_re="^/src_path/(?P.*).to_move$", - dst_re="/dst_path/\g", auto_create=False, rec=False) +rule = Rule(action="move", + src_re="^/src_path/(?P.*).to_move$", + dst_re="/dst_path/\g", + auto_create=False, + rec=False, + filemode=None, + dirmode=None, + user=None, + group=None) -fm = FileManager(rules=[rule], logname="FileManager") +fm = FileManager(rules=[rule], + logname="FileManager") ``` FileManager provides a task **fm.task**. @@ -45,7 +53,11 @@ pyinotifyd has different schedulers to schedule tasks with an optional delay. Th TaskScheduler schedules *task* with an optional *delay* in seconds. Use the *files* and *dirs* arguments to schedule tasks only for files and/or directories. Use *logname* in log messages. All arguments except for *task* are optional. ```python -s = TaskScheduler(task=task, files=True, dirs=False, delay=0, logname="TaskScheduler") +s = TaskScheduler(task=task, + files=True, + dirs=False, + delay=0, + logname="TaskScheduler") ``` TaskScheduler provides two tasks which can be bound to an event in an event map. * **s.schedule** @@ -85,13 +97,18 @@ The following event types are available: ### Watches Watch watches *path* for event types in *event_map* and execute the corresponding task(s). If *rec* is True, a watch will be added on each subdirectory in *path*. If *auto_add* is True, a watch will be added automatically on newly created subdirectories in *path*. ```python -watch = Watch(path="/tmp", event_map=event_map, rec=False, auto_add=False) +watch = Watch(path="/tmp", + event_map=event_map, + rec=False, + auto_add=False) ``` ### PyinotifydConfig pyinotifyd expects an instance of PyinotifydConfig named **pyinotifyd_config** that holds its config options. The options are a list of *watches*, the *loglevel* (see https://docs.python.org/3/library/logging.html#levels) and the *shutdown_timeout*. pyinotifyd will wait *shutdown_timeout* seconds for pending tasks to complete during shutdown. ```python -pyinotifyd_config = PyinotifydConfig(watches=[watch], loglevel=logging.INFO, shutdown_timeout=30) +pyinotifyd_config = PyinotifydConfig(watches=[watch], + loglevel=logging.INFO, + shutdown_timeout=30) ``` ### Autostart @@ -111,47 +128,64 @@ systemctl start pyinotifyd.service async def task(event, task_id): logging.info(f"{task_id}: execute example task: {event}") -s = TaskScheduler(task=task, files=True, dirs=True) +s = TaskScheduler(task=task, + files=True, + dirs=True) event_map = EventMap(default_task=s.schedule) -watch = Watch(path="/tmp", event_map=event_map, rec=True, auto_add=True) -pyinotifyd_config = PyinotifydConfig( - watches=[watch], loglevel=logging.INFO, shutdown_timeout=5) +watch = Watch(path="/tmp", + event_map=event_map, + rec=True, + auto_add=True) + +pyinotifyd_config = PyinotifydConfig(watches=[watch], + loglevel=logging.INFO, + shutdown_timeout=5) ``` ### Schedule Shell commands for specific events on files ```python -s = ShellScheduler( - cmd="/usr/local/sbin/task.sh {pathname}", files=True, dirs=False) +s = ShellScheduler(cmd="/usr/local/sbin/task.sh {pathname}", files=True, dirs=False) event_map = EventMap({"IN_WRITE_CLOSE": s.schedule}) -watch = Watch(path="/tmp", event_map=event_map, rec=True, auto_add=True) +watch = Watch(path="/tmp", + event_map=event_map, + rec=True, + auto_add=True) -pyinotifyd_config = PyinotifydConfig( - watches=[watch], loglevel=logging.INFO, shutdown_timeout=5) +pyinotifyd_config = PyinotifydConfig(watches=[watch], + loglevel=logging.INFO, + shutdown_timeout=5) ``` ### Move, copy or delete newly created files after a delay ```python move_rule = Rule(action="move", - src_re="^/src_path/(?P.*)\.to_move$", - dst_re="/dst_path/\g", - auto_create=True) + src_re="^/src_path/(?P.*)\.to_move$", + dst_re="/dst_path/\g", + auto_create=True, + filemode=0o644, + dirmode=0o755) copy_rule = Rule(action="copy", - src_re="^/src_path/(?P.*)\.to_copy$", - dst_re="/dst_path/\g", - auto_create=True) + src_re="^/src_path/(?P.*)\.to_copy$", + dst_re="/dst_path/\g", + auto_create=True, + filemode=0o644, + dirmode=0o755) delete_rule = Rule(action="delete", - src_re="^/src_path/(?P.*)\.to_delete$", - rec=False) + src_re="^/src_path/(?P.*)\.to_delete$", + rec=False) fm = FileManager(rules=[move_rule, copy_rule, delete_rule]) -s = TaskScheduler(task=fm.task, delay=30, files=True, dirs=False) +s = TaskScheduler(task=fm.task, + delay=30, + files=True, + dirs=False) event_map = EventMap({"IN_CLOSE_WRITE": s.schedule, "IN_DELETE": s.cancel, @@ -159,10 +193,15 @@ event_map = EventMap({"IN_CLOSE_WRITE": s.schedule, "IN_MODIFY": s.cancel, "IN_MOVED_TO": s.schedule, "IN_UNMOUNT": s.cancel}) -watch = Watch(path="/src_path", event_map=event_map, rec=True, auto_add=True) + +watch = Watch(path="/src_path", + event_map=event_map, + rec=True, + auto_add=True) # note that shutdown_timeout should be greater than the greatest scheduler delay, # otherwise pending tasks may get cancelled during shutdown. -pyinotifyd_config = PyinotifydConfig( - watches=[watch], loglevel=logging.INFO, shutdown_timeout=35) +pyinotifyd_config = PyinotifydConfig(watches=[watch], + loglevel=logging.INFO, + shutdown_timeout=35) ```