"""Maintain a cache of stat() information on files.
There are functions to reset the cache or to selectively remove items.
warnings
.warn("The statcache module is obsolete. Use os.stat() instead.",
__all__
= ["stat","reset","forget","forget_prefix","forget_dir",
"forget_except_prefix","isdir"]
# The cache. Keys are pathnames, values are os.stat outcomes.
# Remember that multiple threads may be calling this! So, e.g., that
# path in cache returns 1 doesn't mean the cache will still contain
# path on the next line. Code defensively.
"""Stat a file, possibly out of the cache."""
ret
= cache
.get(path
, None)
cache
[path
] = ret
= _os
.stat(path
)
# For thread saftey, always use forget() internally too.
"""Remove a given item from the cache, if it exists."""
def forget_prefix(prefix
):
"""Remove all pathnames with a given prefix."""
for path
in cache
.keys():
if path
.startswith(prefix
):
"""Forget a directory and all entries except for entries in subdirs."""
# Remove trailing separator, if any. This is tricky to do in a
# x-platform way. For example, Windows accepts both / and \ as
# separators, and if there's nothing *but* a separator we want to
# preserve that this is the root. Only os.path has the platform
from os
.path
import split
, join
prefix
= split(join(prefix
, "xxx"))[0]
for path
in cache
.keys():
# First check that the path at least starts with the prefix, so
# that when it doesn't we can avoid paying for split().
if path
.startswith(prefix
) and split(path
)[0] == prefix
:
def forget_except_prefix(prefix
):
"""Remove all pathnames except with a given prefix.
Normally used with prefix = '/' after a chdir().
for path
in cache
.keys():
if not path
.startswith(prefix
):
"""Return True if directory, else False."""
return S_ISDIR(st
.st_mode
)