diff options
author | AUTOMATIC1111 <16777216c@gmail.com> | 2024-01-04 08:15:50 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-04 08:15:50 +0000 |
commit | df62ffbd2525792c115adefdbaeb7799699624b1 (patch) | |
tree | c5bd1c25eae4d890b7f5f79d938ca5e04802bafc /modules/util.py | |
parent | d9034b48a526f0a0c3e8f0dbf7c171bf4f0597fd (diff) | |
parent | 149c9d223463c8fc34f53f26ca06e02be4c8835b (diff) | |
download | stable-diffusion-webui-gfx803-df62ffbd2525792c115adefdbaeb7799699624b1.tar.gz stable-diffusion-webui-gfx803-df62ffbd2525792c115adefdbaeb7799699624b1.tar.bz2 stable-diffusion-webui-gfx803-df62ffbd2525792c115adefdbaeb7799699624b1.zip |
Merge branch 'dev' into avoid-isfiles
Diffstat (limited to 'modules/util.py')
-rw-r--r-- | modules/util.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/modules/util.py b/modules/util.py index d503f267..ee373e92 100644 --- a/modules/util.py +++ b/modules/util.py @@ -66,3 +66,73 @@ def truncate_path(target_path, base_path=cwd): except ValueError:
pass
return abs_target
+
+
+class MassFileListerCachedDir:
+ """A class that caches file metadata for a specific directory."""
+
+ def __init__(self, dirname):
+ self.files = None
+ self.files_cased = None
+ self.dirname = dirname
+
+ stats = ((x.name, x.stat(follow_symlinks=False)) for x in os.scandir(self.dirname))
+ files = [(n, s.st_mtime, s.st_ctime) for n, s in stats]
+ self.files = {x[0].lower(): x for x in files}
+ self.files_cased = {x[0]: x for x in files}
+
+
+class MassFileLister:
+ """A class that provides a way to check for the existence and mtime/ctile of files without doing more than one stat call per file."""
+
+ def __init__(self):
+ self.cached_dirs = {}
+
+ def find(self, path):
+ """
+ Find the metadata for a file at the given path.
+
+ Returns:
+ tuple or None: A tuple of (name, mtime, ctime) if the file exists, or None if it does not.
+ """
+
+ dirname, filename = os.path.split(path)
+
+ cached_dir = self.cached_dirs.get(dirname)
+ if cached_dir is None:
+ cached_dir = MassFileListerCachedDir(dirname)
+ self.cached_dirs[dirname] = cached_dir
+
+ stats = cached_dir.files_cased.get(filename)
+ if stats is not None:
+ return stats
+
+ stats = cached_dir.files.get(filename.lower())
+ if stats is None:
+ return None
+
+ try:
+ os_stats = os.stat(path, follow_symlinks=False)
+ return filename, os_stats.st_mtime, os_stats.st_ctime
+ except Exception:
+ return None
+
+ def exists(self, path):
+ """Check if a file exists at the given path."""
+
+ return self.find(path) is not None
+
+ def mctime(self, path):
+ """
+ Get the modification and creation times for a file at the given path.
+
+ Returns:
+ tuple: A tuple of (mtime, ctime) if the file exists, or (0, 0) if it does not.
+ """
+
+ stats = self.find(path)
+ return (0, 0) if stats is None else stats[1:3]
+
+ def reset(self):
+ """Clear the cache of all directories."""
+ self.cached_dirs.clear()
|