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