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.
- 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.
- 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.
- 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.