Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add separate method for file/directory metadata modifcation #800

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fc9b03a
add fileattrib and dirattrib event
GreatBahram May 18, 2021
e693eb1
use fileattrib and dirattrib event in inotify module
GreatBahram May 18, 2021
2c66593
add on_attrib on LoggingEventHandler
GreatBahram May 18, 2021
937f9af
add changes to make kqueue support attrib event
GreatBahram May 26, 2021
e2d8db0
bring file/directory attribute change event into fsevents2 module
GreatBahram May 27, 2021
3a652ff
update docs for FileAttrib and DirAttrib Events
GreatBahram May 27, 2021
3b6f258
Make flake8 happy
GreatBahram Jun 1, 2021
151f651
Change the test github workflow
GreatBahram Jun 10, 2021
dc366d6
add on: pull_request workflow statement:
GreatBahram Jun 10, 2021
1ca76cb
WIP: fix two more test cases and make sure they work correctly on bsd…
GreatBahram Jun 10, 2021
c3dc00f
add test cases for file_attrib and dir_attrib
GreatBahram Jun 10, 2021
165a6f4
add chmod function into shell module and add test cases to test it wo…
GreatBahram Jun 10, 2021
b710851
I think I fixed the nested tese case
GreatBahram Jun 10, 2021
424cb47
Make flake8 happier than ever
GreatBahram Jun 10, 2021
58b514f
WIP: Disable the lifecycle test case
GreatBahram Jun 10, 2021
677ac9f
Update fsevents module: this should be tested more than any other mod…
GreatBahram Jun 10, 2021
467b1e2
bugfix test case: test_modify on BSD
GreatBahram Jun 24, 2021
5e94d19
return str repsentation instead of Pathlib object
GreatBahram Jun 24, 2021
60415b7
make the equality method more strict
GreatBahram Jun 24, 2021
9d0e19c
run test work for pull_request and when user asks for it
GreatBahram Jun 24, 2021
dfb27d6
bugfix: document
GreatBahram Jun 24, 2021
5921e2c
Update tests.yml
BoboTiG Jun 24, 2021
2f8b573
Merge branch 'master' into add-attributes-only
BoboTiG Mar 18, 2023
167639f
Update tests.yml
BoboTiG Mar 18, 2023
f751e7d
Update kqueue.py
BoboTiG Mar 18, 2023
482846e
Apply suggestions from code review
BoboTiG Mar 18, 2023
dfa6db5
Apply suggestions from code review
BoboTiG Mar 18, 2023
4987d2b
Merge branch 'master' into add-attributes-only
BoboTiG Apr 22, 2023
008556d
Update test_emitter.py
BoboTiG Apr 22, 2023
408738f
Apply suggestions from code review
BoboTiG Apr 22, 2023
7c8ac0b
Update test_emitter.py
BoboTiG Apr 22, 2023
25adb67
Apply suggestions from code review
BoboTiG Apr 22, 2023
4622197
Update test_emitter.py
BoboTiG Apr 22, 2023
af9436e
Update test_fsevents.py
BoboTiG Apr 22, 2023
8069705
Update test_emitter.py
BoboTiG Apr 24, 2023
6d03156
Update conftest.py
BoboTiG Apr 24, 2023
79bec88
Update utils.py
BoboTiG Apr 24, 2023
f1593f4
Update tests/test_fsevents.py
BoboTiG Apr 24, 2023
d2b3b24
Update tests/test_emitter.py
BoboTiG Apr 24, 2023
814d68e
Apply suggestions from code review
BoboTiG Apr 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/tests.yml
Expand Up @@ -8,6 +8,13 @@ on:
branches:
- '**'

workflow_dispatch:
GreatBahram marked this conversation as resolved.
Show resolved Hide resolved
inputs:
branch:
description: 'The branch, tag or SHA to release from'
required: true
default: 'master'

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name != 'pull_request' && github.sha || '' }}
cancel-in-progress: true
Expand Down
41 changes: 41 additions & 0 deletions src/watchdog/events.py
Expand Up @@ -70,6 +70,15 @@
:members:
:show-inheritance:

.. autoclass:: FileAttribEvent
:members:
:show-inheritance:

.. autoclass:: DirAttribEvent
:members:
:show-inheritance:



Event Handler Classes
---------------------
Expand Down Expand Up @@ -107,6 +116,7 @@
EVENT_TYPE_MODIFIED = "modified"
EVENT_TYPE_CLOSED = "closed"
EVENT_TYPE_OPENED = "opened"
EVENT_TYPE_ATTRIB = "attrib"


@dataclass(unsafe_hash=True)
Expand Down Expand Up @@ -147,6 +157,13 @@ class FileDeletedEvent(FileSystemEvent):
event_type = EVENT_TYPE_DELETED


class FileAttribEvent(FileSystemEvent):
"""
File system event representing file metadata modification on the file system.
"""
event_type = EVENT_TYPE_ATTRIB
BoboTiG marked this conversation as resolved.
Show resolved Hide resolved


class FileModifiedEvent(FileSystemEvent):
"""File system event representing file modification on the file system."""

Expand Down Expand Up @@ -207,6 +224,14 @@ class DirMovedEvent(FileSystemMovedEvent):
is_directory = True


class DirAttribEvent(FileSystemEvent):
"""
File system event representing directory metadata modification on the file system.
"""
event_type = EVENT_TYPE_ATTRIB
BoboTiG marked this conversation as resolved.
Show resolved Hide resolved
is_directory = True


class FileSystemEventHandler:
"""
Base file system event handler that you can override methods from.
Expand All @@ -228,6 +253,7 @@ def dispatch(self, event: FileSystemEvent) -> None:
EVENT_TYPE_MOVED: self.on_moved,
EVENT_TYPE_CLOSED: self.on_closed,
EVENT_TYPE_OPENED: self.on_opened,
EVENT_TYPE_ATTRIB: self.on_attrib,
}[event.event_type](event)

def on_any_event(self, event: FileSystemEvent) -> None:
Expand Down Expand Up @@ -293,6 +319,15 @@ def on_opened(self, event: FileSystemEvent) -> None:
:class:`FileOpenedEvent`
"""

def on_attrib(self, event: FileSystemEvent) -> None:
"""Called when a file or directory metadata is modified.

:param event:
Event representing file/directory metadata modification.
:type event:
:class:`FileAttribEvent` or :class:`DirAttribEvent`
"""


class PatternMatchingEventHandler(FileSystemEventHandler):
"""
Expand Down Expand Up @@ -489,6 +524,12 @@ def on_modified(self, event: FileSystemEvent) -> None:
what = "directory" if event.is_directory else "file"
self.logger.info("Modified %s: %s", what, event.src_path)

def on_attrib(self, event):
super().on_attrib(event)

what = "directory" if event.is_directory else "file"
self.logger.info("Attrib %s: %s", what, event.src_path)

def on_closed(self, event: FileSystemEvent) -> None:
super().on_closed(event)

Expand Down
27 changes: 21 additions & 6 deletions src/watchdog/observers/fsevents.py
Expand Up @@ -32,10 +32,12 @@
import _watchdog_fsevents as _fsevents # type: ignore[import]

from watchdog.events import (
DirAttribEvent,
DirCreatedEvent,
DirDeletedEvent,
DirModifiedEvent,
DirMovedEvent,
FileAttribEvent,
FileCreatedEvent,
FileDeletedEvent,
FileModifiedEvent,
Expand Down Expand Up @@ -132,6 +134,10 @@ def _queue_modified_event(self, event, src_path, dirname):
cls = DirModifiedEvent if event.is_directory else FileModifiedEvent
self.queue_event(cls(src_path))

def _queue_attrib_event(self, event, src_path, dirname):
cls = DirAttribEvent if event.is_directory else FileAttribEvent
self.queue_event(cls(src_path))

def _queue_renamed_event(self, src_event, src_path, dst_path, src_dirname, dst_dirname):
cls = DirMovedEvent if src_event.is_directory else FileMovedEvent
dst_path = self._encode_path(dst_path)
Expand Down Expand Up @@ -212,9 +218,12 @@ def queue_events(self, timeout, events):

self._fs_view.add(event.inode)

if event.is_modified or self._is_meta_mod(event):
if event.is_modified:
self._queue_modified_event(event, src_path, src_dirname)

elif event.is_inode_meta_mod or event.is_xattr_mod:
BoboTiG marked this conversation as resolved.
Show resolved Hide resolved
self._queue_attrib_event(event, src_path, src_dirname)

self._queue_deleted_event(event, src_path, src_dirname)
self._fs_view.discard(event.inode)

Expand All @@ -224,10 +233,13 @@ def queue_events(self, timeout, events):

self._fs_view.add(event.inode)

if event.is_modified or self._is_meta_mod(event):
if event.is_modified:
self._queue_modified_event(event, src_path, src_dirname)

if event.is_renamed:
elif event.is_inode_meta_mod or event.is_xattr_mod:
BoboTiG marked this conversation as resolved.
Show resolved Hide resolved
self._queue_attrib_event(event, src_path, src_dirname)

elif event.is_renamed:
# Check if we have a corresponding destination event in the watched path.
dst_event = next(
iter(e for e in events if e.is_renamed and e.inode == event.inode),
Expand All @@ -251,10 +263,13 @@ def queue_events(self, timeout, events):

events.remove(dst_event)

if dst_event.is_modified or self._is_meta_mod(dst_event):
if dst_event.is_modified:
self._queue_modified_event(dst_event, dst_path, dst_dirname)

if dst_event.is_removed:
elif dst_event.is_inode_meta_mod or dst_event.is_xattr_mod:
BoboTiG marked this conversation as resolved.
Show resolved Hide resolved
self._queue_attrib_event(dst_event, dst_path, dst_dirname)

elif dst_event.is_removed:
self._queue_deleted_event(dst_event, dst_path, dst_dirname)
self._fs_view.discard(dst_event.inode)

Expand All @@ -276,7 +291,7 @@ def queue_events(self, timeout, events):
# Skip further coalesced processing.
continue

if event.is_removed:
elif event.is_removed:
# Won't occur together with renamed.
self._queue_deleted_event(event, src_path, src_dirname)
self._fs_view.discard(event.inode)
Expand Down
10 changes: 9 additions & 1 deletion src/watchdog/observers/fsevents2.py
Expand Up @@ -58,10 +58,12 @@
)

from watchdog.events import (
DirAttribEvent,
DirCreatedEvent,
DirDeletedEvent,
DirModifiedEvent,
DirMovedEvent,
FileAttribEvent,
FileCreatedEvent,
FileDeletedEvent,
FileModifiedEvent,
Expand Down Expand Up @@ -197,6 +199,7 @@ def queue_events(self, timeout):
events = self._fsevents.read_events()
if events is None:
return

i = 0
while i < len(events):
event = events[i]
Expand Down Expand Up @@ -228,10 +231,14 @@ def queue_events(self, timeout):
self.queue_event(DirModifiedEvent(os.path.dirname(event.path)))
# TODO: generate events for tree

elif event.is_modified or event.is_inode_meta_mod or event.is_xattr_mod:
elif event.is_modified:
cls = DirModifiedEvent if event.is_directory else FileModifiedEvent
self.queue_event(cls(event.path))

elif event.is_inode_meta_mod or event.is_xattr_mod:
cls = DirAttribEvent if event.is_directory else FileAttribEvent
self.queue_event(cls(event.path))

elif event.is_created:
cls = DirCreatedEvent if event.is_directory else FileCreatedEvent
self.queue_event(cls(event.path))
Expand All @@ -241,6 +248,7 @@ def queue_events(self, timeout):
cls = DirDeletedEvent if event.is_directory else FileDeletedEvent
self.queue_event(cls(event.path))
self.queue_event(DirModifiedEvent(os.path.dirname(event.path)))

i += 1


Expand Down
4 changes: 3 additions & 1 deletion src/watchdog/observers/inotify.py
Expand Up @@ -72,10 +72,12 @@
from typing import Type

from watchdog.events import (
DirAttribEvent,
DirCreatedEvent,
DirDeletedEvent,
DirModifiedEvent,
DirMovedEvent,
FileAttribEvent,
FileClosedEvent,
FileCreatedEvent,
FileDeletedEvent,
Expand Down Expand Up @@ -164,7 +166,7 @@ def queue_events(self, timeout, full_events=False):
for sub_event in generate_sub_created_events(src_path):
self.queue_event(sub_event)
elif event.is_attrib:
cls = DirModifiedEvent if event.is_directory else FileModifiedEvent
cls = DirAttribEvent if event.is_directory else FileAttribEvent
self.queue_event(cls(src_path))
elif event.is_modify:
cls = DirModifiedEvent if event.is_directory else FileModifiedEvent
Expand Down
7 changes: 4 additions & 3 deletions src/watchdog/observers/kqueue.py
Expand Up @@ -85,10 +85,12 @@
EVENT_TYPE_CREATED,
EVENT_TYPE_DELETED,
EVENT_TYPE_MOVED,
DirAttribEvent,
DirCreatedEvent,
DirDeletedEvent,
DirModifiedEvent,
DirMovedEvent,
FileAttribEvent,
FileCreatedEvent,
FileDeletedEvent,
FileModifiedEvent,
Expand Down Expand Up @@ -126,7 +128,6 @@
def absolute_path(path):
return os.path.abspath(os.path.normpath(path))


# Flag tests.


Expand Down Expand Up @@ -543,9 +544,9 @@ def _gen_kqueue_events(self, kev, ref_snapshot, new_snapshot):
yield event
elif is_attrib_modified(kev):
if descriptor.is_directory:
yield DirModifiedEvent(src_path)
yield DirAttribEvent(src_path)
else:
yield FileModifiedEvent(src_path)
yield FileAttribEvent(src_path)
elif is_modified(kev):
if descriptor.is_directory:
if self.watch.is_recursive or self.watch.path == src_path:
Expand Down
14 changes: 5 additions & 9 deletions tests/shell.py
Expand Up @@ -28,15 +28,6 @@
import tempfile
import time

# def tree(path='.', show_files=False):
# print(path)
# padding = ''
# for root, directories, filenames in os.walk(path):
# print(padding + os.path.basename(root) + os.path.sep)
# padding = padding + ' '
# for filename in filenames:
# print(padding + filename)


def cd(path):
os.chdir(path)
Expand Down Expand Up @@ -124,6 +115,11 @@ def msize(path):
os.utime(path, (0, 0))


def chmod(path, mode):
""""Change file mode bits."""
BoboTiG marked this conversation as resolved.
Show resolved Hide resolved
os.chmod(path, mode)


def mount_tmpfs(path):
os.system(f"sudo mount -t tmpfs none {path}")

Expand Down