watchdog.utils.dirsnapshot#
- module:
watchdog.utils.dirsnapshot
- synopsis:
Directory snapshots and comparison.
- author:
yesudeep@google.com (Yesudeep Mangalapilly)
- author:
Mickaël Schoentgen <contact@tiger-222.fr>
Where are the moved events? They “disappeared”
This implementation does not take partition boundaries into consideration. It will only work when the directory tree is entirely on the same file system. More specifically, any part of the code that depends on inode numbers can break if partition boundaries are crossed. In these cases, the snapshot diff will represent file/directory movement as created and deleted events.
Classes#
- class watchdog.utils.dirsnapshot.DirectorySnapshot(path: str, *, recursive: bool = True, stat: Callable[[str], os.stat_result] = <built-in function stat>, listdir: Callable[[str | None], Iterator[os.DirEntry]] = <built-in function scandir>)[source]#
Bases:
objectA snapshot of stat information of files in a directory.
- Parameters:
path (
str) – The directory path for which a snapshot should be taken.recursive (
bool) –Trueif the entire directory tree should be included in the snapshot;Falseotherwise.stat –
Use custom stat function that returns a stat structure for path. Currently only st_dev, st_ino, st_mode and st_mtime are needed.
A function taking a
pathas argument which will be called for every entry in the directory tree.listdir – Use custom listdir function. For details see
os.scandir.
- birthtime(path: bytes | str) int | None[source]#
Returns the birth/creation time in nanoseconds for path, or None if unsupported.
macOS / BSD: uses
st.st_birthtime_nsLinux: uses
statx(2)stx_btime, captured when taking the snapshot (see https://man7.org/linux/man-pages/man2/statx.2.html)
- path(uid: tuple[int, int]) bytes | str | None[source]#
Returns path for id. None if id is unknown to this snapshot.
- stat_info(path: bytes | str) stat_result[source]#
Returns a stat information object for the specified path from the snapshot.
Attached information is subject to change. Do not use unless you specify stat in constructor. Use
inode(),mtime(),isdir()instead.- Parameters:
path – The path for which stat information should be obtained from a snapshot.
- walk(root: str) Iterator[tuple[str, os.stat_result]][source]#
Walk the directory tree, yielding
(path, stat)for every entry.A directory is never re-entered while it is still on the walk’s own path, so a symlink loop pointing back at an ancestor cannot replay the tree at every alias level (#1507). A directory reachable twice through sibling symlinks is walked once per path, so a snapshot’s paths never depend on
scandirorder (#1509 review).
- class watchdog.utils.dirsnapshot.DirectorySnapshotDiff(ref: DirectorySnapshot, snapshot: DirectorySnapshot, *, ignore_device: bool = False)[source]#
Bases:
objectCompares two directory snapshots and creates an object that represents the difference between the two snapshots.
- Parameters:
ref (
DirectorySnapshot) – The reference directory snapshot.snapshot (
DirectorySnapshot) – The directory snapshot which will be compared with the reference snapshot.ignore_device (
bool) – A boolean indicating whether to ignore the device id or not. By default, a file may be uniquely identified by a combination of its first inode and its device id. The problem is that the device id may (or may not) change between system boots. This problem would cause the DirectorySnapshotDiff to think a file has been deleted and created again but it would be the exact same file. Set to True only if you are sure you will always use the same device.
- class ContextManager(path: str, *, recursive: bool = True, stat: Callable[[str], os.stat_result] = <built-in function stat>, listdir: Callable[[str | None], Iterator[os.DirEntry]] = <built-in function scandir>, ignore_device: bool = False)[source]#
Bases:
objectContext manager that creates two directory snapshots and a diff object that represents the difference between the two snapshots.
- Parameters:
path (
str) – The directory path for which a snapshot should be taken.recursive (
bool) –Trueif the entire directory tree should be included in the snapshot;Falseotherwise.stat –
Use custom stat function that returns a stat structure for path. Currently only st_dev, st_ino, st_mode and st_mtime are needed.
A function taking a
pathas argument which will be called for every entry in the directory tree.listdir – Use custom listdir function. For details see
os.scandir.ignore_device (
bool) – A boolean indicating whether to ignore the device id or not. By default, a file may be uniquely identified by a combination of its first inode and its device id. The problem is that the device id may (or may not) change between system boots. This problem would cause the DirectorySnapshotDiff to think a file has been deleted and created again but it would be the exact same file. Set to True only if you are sure you will always use the same device.
- property dirs_attrib: list[bytes | str]#
List of directories whose attributes or permissions were modified.
- property dirs_moved: list[tuple[bytes | str, bytes | str]]#
List of directories that were moved.
Each event is a two-tuple the first item of which is the path that has been renamed to the second item in the tuple.
- property files_attrib: list[bytes | str]#
List of files whose attributes or permissions were modified.
- property files_moved: list[tuple[bytes | str, bytes | str]]#
List of files that were moved.
Each event is a two-tuple the first item of which is the path that has been renamed to the second item in the tuple.
- static is_attrib_modified(ref_stat: stat_result, snap_stat: stat_result) bool[source]#
Determines whether permissions, file attributes, or ownership have changed.
- class watchdog.utils.dirsnapshot.EmptyDirectorySnapshot[source]#
Bases:
DirectorySnapshotClass to implement an empty snapshot. This is used together with DirectorySnapshot and DirectorySnapshotDiff in order to get all the files/folders in the directory as created.
- birthtime(path: bytes | str) int | None[source]#
Mock up method to return birth time. As the snapshot is intended to be empty, it always returns None.