blob: 770aef0662f4d502bee54e031d1217f2d34ae05f [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# Module 'statcache'
2#
3# Maintain a cache of file stats.
4# There are functions to reset the cache or to selectively remove items.
5
Guido van Rossum25d7caf1992-03-31 19:04:48 +00006import os
Guido van Rossum40d93041990-10-21 16:17:34 +00007from stat import *
Guido van Rossumc6360141990-10-13 19:23:40 +00008
9# The cache.
Guido van Rossum25d7caf1992-03-31 19:04:48 +000010# Keys are pathnames, values are `os.stat' outcomes.
Guido van Rossumc6360141990-10-13 19:23:40 +000011#
12cache = {}
13
14
15# Stat a file, possibly out of the cache.
16#
17def stat(path):
Guido van Rossum40d93041990-10-21 16:17:34 +000018 if cache.has_key(path):
Guido van Rossumc6360141990-10-13 19:23:40 +000019 return cache[path]
Guido van Rossum25d7caf1992-03-31 19:04:48 +000020 cache[path] = ret = os.stat(path)
Guido van Rossumc6360141990-10-13 19:23:40 +000021 return ret
22
23
24# Reset the cache completely.
Guido van Rossumc6360141990-10-13 19:23:40 +000025#
26def reset():
Sjoerd Mullendere5fe4af1995-12-07 10:16:45 +000027 global cache
28 cache = {}
Guido van Rossumc6360141990-10-13 19:23:40 +000029
30
31# Remove a given item from the cache, if it exists.
32#
33def forget(path):
Guido van Rossum40d93041990-10-21 16:17:34 +000034 if cache.has_key(path):
Guido van Rossumc6360141990-10-13 19:23:40 +000035 del cache[path]
Guido van Rossumc6360141990-10-13 19:23:40 +000036
37
38# Remove all pathnames with a given prefix.
39#
40def forget_prefix(prefix):
41 n = len(prefix)
42 for path in cache.keys():
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000043 if path[:n] == prefix:
Guido van Rossumc6360141990-10-13 19:23:40 +000044 del cache[path]
45
46
47# Forget about a directory and all entries in it, but not about
48# entries in subdirectories.
49#
50def forget_dir(prefix):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000051 if prefix[-1:] == '/' and prefix <> '/':
Guido van Rossumc6360141990-10-13 19:23:40 +000052 prefix = prefix[:-1]
53 forget(prefix)
54 if prefix[-1:] <> '/':
55 prefix = prefix + '/'
56 n = len(prefix)
57 for path in cache.keys():
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000058 if path[:n] == prefix:
Guido van Rossumc6360141990-10-13 19:23:40 +000059 rest = path[n:]
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000060 if rest[-1:] == '/': rest = rest[:-1]
Guido van Rossumc6360141990-10-13 19:23:40 +000061 if '/' not in rest:
62 del cache[path]
63
64
65# Remove all pathnames except with a given prefix.
66# Normally used with prefix = '/' after a chdir().
67#
68def forget_except_prefix(prefix):
69 n = len(prefix)
70 for path in cache.keys():
71 if path[:n] <> prefix:
72 del cache[path]
73
74
75# Check for directory.
76#
77def isdir(path):
78 try:
Guido van Rossum40d93041990-10-21 16:17:34 +000079 st = stat(path)
Guido van Rossum25d7caf1992-03-31 19:04:48 +000080 except os.error:
Guido van Rossumc6360141990-10-13 19:23:40 +000081 return 0
Guido van Rossum40d93041990-10-21 16:17:34 +000082 return S_ISDIR(st[ST_MODE])