annotate Lib/statcache.py @ 16413:8819867f14ef legacy-trunk

Patch derived from Trent's 101162: a Python/C API testing framework. STILL NEEDS UNIX BUILD CHANGES.
author Tim Peters <tim.peters@gmail.com>
date Fri, 02 Feb 2001 05:57:15 +0000
parents 861bf8dcfb58
children f68dbffff927
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
1 """Maintain a cache of stat() information on files.
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
2
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
3 There are functions to reset the cache or to selectively remove items.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
4 """
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
6 import os as _os
21
961696ef3ab7 Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
7 from stat import *
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
9 # The cache. Keys are pathnames, values are os.stat outcomes.
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
10 # Remember that multiple threads may be calling this! So, e.g., that
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
11 # cache.has_key(path) returns 1 doesn't mean the cache will still contain
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
12 # path on the next line. Code defensively.
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
13
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 cache = {}
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 def stat(path):
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
17 """Stat a file, possibly out of the cache."""
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
18 ret = cache.get(path, None)
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
19 if ret is None:
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
20 cache[path] = ret = _os.stat(path)
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
21 return ret
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 def reset():
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
24 """Clear the cache."""
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
25 cache.clear()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
27 # For thread saftey, always use forget() internally too.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 def forget(path):
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
29 """Remove a given item from the cache, if it exists."""
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
30 try:
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
31 del cache[path]
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
32 except KeyError:
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
33 pass
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def forget_prefix(prefix):
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
36 """Remove all pathnames with a given prefix."""
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
37 for path in cache.keys():
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
38 if path.startswith(prefix):
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
39 forget(path)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 def forget_dir(prefix):
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
42 """Forget a directory and all entries except for entries in subdirs."""
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
43
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
44 # Remove trailing separator, if any. This is tricky to do in a
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
45 # x-platform way. For example, Windows accepts both / and \ as
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
46 # separators, and if there's nothing *but* a separator we want to
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
47 # preserve that this is the root. Only os.path has the platform
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
48 # knowledge we need.
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
49 from os.path import split, join
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
50 prefix = split(join(prefix, "xxx"))[0]
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
51 forget(prefix)
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
52 for path in cache.keys():
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
53 # First check that the path at least starts with the prefix, so
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
54 # that when it doesn't we can avoid paying for split().
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
55 if path.startswith(prefix) and split(path)[0] == prefix:
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
56 forget(path)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def forget_except_prefix(prefix):
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
59 """Remove all pathnames except with a given prefix.
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
60
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
61 Normally used with prefix = '/' after a chdir().
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
62 """
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
63
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
64 for path in cache.keys():
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
65 if not path.startswith(prefix):
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
66 forget(path)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 def isdir(path):
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
69 """Return 1 if directory, else 0."""
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
70 try:
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
71 st = stat(path)
16317
861bf8dcfb58 SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
72 except _os.error:
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
73 return 0
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
74 return S_ISDIR(st[ST_MODE])