add log messages for chown and chmod
This commit is contained in:
@@ -319,14 +319,23 @@ class FileManager:
|
|||||||
def add_rule(self, *args, **kwargs):
|
def add_rule(self, *args, **kwargs):
|
||||||
self._rules.append(Rule(*args, **kwargs))
|
self._rules.append(Rule(*args, **kwargs))
|
||||||
|
|
||||||
async def _chmod_and_chown(self, path, mode, chown):
|
async def _chmod_and_chown(self, path, mode, chown, task_id):
|
||||||
if mode is not None:
|
if mode is not None:
|
||||||
|
self._log.debug(f"{task_id}: chmod {oct(mode)} '{path}'")
|
||||||
os.chmod(path, mode)
|
os.chmod(path, mode)
|
||||||
|
|
||||||
if chown is not None:
|
if chown is not None:
|
||||||
|
changes = ""
|
||||||
|
if chown[0] is not None:
|
||||||
|
changes = chown[0]
|
||||||
|
|
||||||
|
if chown[1] is not None:
|
||||||
|
changes = f"{changes}:{chown[1]}"
|
||||||
|
|
||||||
|
self._log.debug(f"{task_id}: chown {changes} '{path}'")
|
||||||
shutil.chown(path, *chown)
|
shutil.chown(path, *chown)
|
||||||
|
|
||||||
async def _set_mode_and_owner(self, path, rule):
|
async def _set_mode_and_owner(self, path, rule, task_id):
|
||||||
if (rule.user is rule.group is None):
|
if (rule.user is rule.group is None):
|
||||||
chown = None
|
chown = None
|
||||||
else:
|
else:
|
||||||
@@ -336,20 +345,20 @@ class FileManager:
|
|||||||
work_on_files = not (rule.filemode is chown is None)
|
work_on_files = not (rule.filemode is chown is None)
|
||||||
|
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
await self._chmod_and_chown(path, rule.dirmode, chown)
|
await self._chmod_and_chown(path, rule.dirmode, chown, task_id)
|
||||||
if work_on_dirs or work_on_files:
|
if work_on_dirs or work_on_files:
|
||||||
for root, dirs, files in os.walk(path):
|
for root, dirs, files in os.walk(path):
|
||||||
if work_on_dirs:
|
if work_on_dirs:
|
||||||
for p in [os.path.join(root, d) for d in dirs]:
|
for p in [os.path.join(root, d) for d in dirs]:
|
||||||
await self._chmod_and_chown(
|
await self._chmod_and_chown(
|
||||||
p, rule.dirmode, chown)
|
p, rule.dirmode, chown, task_id)
|
||||||
|
|
||||||
if work_on_files:
|
if work_on_files:
|
||||||
for p in [os.path.join(root, f) for f in files]:
|
for p in [os.path.join(root, f) for f in files]:
|
||||||
await self._chmod_and_chown(
|
await self._chmod_and_chown(
|
||||||
p, rule.filemode, chown)
|
p, rule.filemode, chown, task_id)
|
||||||
else:
|
else:
|
||||||
await self._chmod_and_chown(path, rule.filemode, chown)
|
await self._chmod_and_chown(path, rule.filemode, chown, task_id)
|
||||||
|
|
||||||
async def task(self, event, task_id):
|
async def task(self, event, task_id):
|
||||||
path = event.pathname
|
path = event.pathname
|
||||||
@@ -369,13 +378,13 @@ class FileManager:
|
|||||||
dst = rule.src_re.sub(rule.dst_re, path)
|
dst = rule.src_re.sub(rule.dst_re, path)
|
||||||
if not dst:
|
if not dst:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f"unable to {rule.action} '{path}', "
|
f"{task_id}: unable to {rule.action} '{path}', "
|
||||||
f"resulting destination path is empty")
|
f"resulting destination path is empty")
|
||||||
|
|
||||||
if os.path.exists(dst):
|
if os.path.exists(dst):
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f"unable to move file from '{path} to '{dst}', "
|
f"{task_id}: unable to move file from '{path} "
|
||||||
f"dstination path exists already")
|
f"to '{dst}', dstination path exists already")
|
||||||
|
|
||||||
dst_dir = os.path.dirname(dst)
|
dst_dir = os.path.dirname(dst)
|
||||||
if not os.path.isdir(dst_dir) and rule.auto_create:
|
if not os.path.isdir(dst_dir) and rule.auto_create:
|
||||||
@@ -389,7 +398,7 @@ class FileManager:
|
|||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
os.makedirs(dst_dir)
|
os.makedirs(dst_dir)
|
||||||
await self._set_mode_and_owner(first_subdir, rule)
|
await self._set_mode_and_owner(first_subdir, rule, task_id)
|
||||||
|
|
||||||
self._log.info(
|
self._log.info(
|
||||||
f"{task_id}: {rule.action} '{path}' to '{dst}'")
|
f"{task_id}: {rule.action} '{path}' to '{dst}'")
|
||||||
@@ -402,7 +411,7 @@ class FileManager:
|
|||||||
else:
|
else:
|
||||||
os.rename(path, dst)
|
os.rename(path, dst)
|
||||||
|
|
||||||
await self._set_mode_and_owner(dst, rule)
|
await self._set_mode_and_owner(dst, rule, task_id)
|
||||||
|
|
||||||
elif rule.action == "delete":
|
elif rule.action == "delete":
|
||||||
self._log.info(
|
self._log.info(
|
||||||
|
|||||||
Reference in New Issue
Block a user