blob: 85a3e79a04e529788e58ace3d5c1c63a337e815a [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""Maintain a cache of stat() information on files.
2
Guido van Rossume7b146f2000-02-04 15:28:42 +00003There are functions to reset the cache or to selectively remove items.
4"""
Guido van Rossumc6360141990-10-13 19:23:40 +00005
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
Guido van Rossumc6360141990-10-13 19:23:40 +000015def stat(path):
Tim Peters495ad3c2001-01-15 01:36:40 +000016 """Stat a file, possibly out of the cache."""
17 if cache.has_key(path):
18 return cache[path]
19 cache[path] = ret = os.stat(path)
20 return ret
Guido van Rossumc6360141990-10-13 19:23:40 +000021
22
Guido van Rossumc6360141990-10-13 19:23:40 +000023def reset():
Tim Peters495ad3c2001-01-15 01:36:40 +000024 """Reset the cache completely."""
25 global cache
26 cache = {}
Guido van Rossumc6360141990-10-13 19:23:40 +000027
28
Guido van Rossumc6360141990-10-13 19:23:40 +000029def forget(path):
Tim Peters495ad3c2001-01-15 01:36:40 +000030 """Remove a given item from the cache, if it exists."""
31 if cache.has_key(path):
32 del cache[path]
Guido van Rossumc6360141990-10-13 19:23:40 +000033
34
Guido van Rossumc6360141990-10-13 19:23:40 +000035def forget_prefix(prefix):
Tim Peters495ad3c2001-01-15 01:36:40 +000036 """Remove all pathnames with a given prefix."""
37 n = len(prefix)
38 for path in cache.keys():
39 if path[:n] == prefix:
40 del cache[path]
Guido van Rossumc6360141990-10-13 19:23:40 +000041
42
Guido van Rossumc6360141990-10-13 19:23:40 +000043def forget_dir(prefix):
Tim Peters495ad3c2001-01-15 01:36:40 +000044 """Forget about a directory and all entries in it, but not about
45 entries in subdirectories."""
46 if prefix[-1:] == '/' and prefix != '/':
47 prefix = prefix[:-1]
48 forget(prefix)
49 if prefix[-1:] != '/':
50 prefix = prefix + '/'
51 n = len(prefix)
52 for path in cache.keys():
53 if path[:n] == prefix:
54 rest = path[n:]
55 if rest[-1:] == '/': rest = rest[:-1]
56 if '/' not in rest:
57 del cache[path]
Guido van Rossumc6360141990-10-13 19:23:40 +000058
59
Guido van Rossumc6360141990-10-13 19:23:40 +000060def forget_except_prefix(prefix):
Tim Peters495ad3c2001-01-15 01:36:40 +000061 """Remove all pathnames except with a given prefix.
62 Normally used with prefix = '/' after a chdir()."""
63 n = len(prefix)
64 for path in cache.keys():
65 if path[:n] != prefix:
66 del cache[path]
Guido van Rossumc6360141990-10-13 19:23:40 +000067
68
Guido van Rossumc6360141990-10-13 19:23:40 +000069def isdir(path):
Tim Peters495ad3c2001-01-15 01:36:40 +000070 """Check for directory."""
71 try:
72 st = stat(path)
73 except os.error:
74 return 0
75 return S_ISDIR(st[ST_MODE])