Mercurial > cpython-lru_cache-weakref
annotate Lib/statcache.py @ 15946:4f621708514b legacy-trunk
Rich comparisons fall-out:
- Get rid of long_cmp().
- Renamed Py_TPFLAGS_NEWSTYLENUMBER to Py_TPFLAGS_CHECKTYPES.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 17 Jan 2001 15:33:18 +0000 |
| parents | 3685ef2a55a4 |
| children | 861bf8dcfb58 |
| 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 | 5 |
| 776 | 6 import 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 | 8 |
| 9 # The cache. | |
| 776 | 10 # Keys are pathnames, values are `os.stat' outcomes. |
| 5 | 11 # |
| 12 cache = {} | |
| 13 | |
| 14 | |
| 15 def stat(path): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
16 """Stat a file, possibly out of the cache.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
17 if cache.has_key(path): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
18 return cache[path] |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
19 cache[path] = ret = os.stat(path) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
20 return ret |
| 5 | 21 |
| 22 | |
| 23 def reset(): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
24 """Reset the cache completely.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
25 global cache |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
26 cache = {} |
| 5 | 27 |
| 28 | |
| 29 def forget(path): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
30 """Remove a given item from the cache, if it exists.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
31 if cache.has_key(path): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
32 del cache[path] |
| 5 | 33 |
| 34 | |
| 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 n = len(prefix) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
38 for path in cache.keys(): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
39 if path[:n] == prefix: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
40 del cache[path] |
| 5 | 41 |
| 42 | |
| 43 def forget_dir(prefix): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
44 """Forget about a directory and all entries in it, but not about |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
45 entries in subdirectories.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
46 if prefix[-1:] == '/' and prefix != '/': |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
47 prefix = prefix[:-1] |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
48 forget(prefix) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
49 if prefix[-1:] != '/': |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
50 prefix = prefix + '/' |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
51 n = len(prefix) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
52 for path in cache.keys(): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
53 if path[:n] == prefix: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
54 rest = path[n:] |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
55 if rest[-1:] == '/': rest = rest[:-1] |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
56 if '/' not in rest: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
57 del cache[path] |
| 5 | 58 |
| 59 | |
| 60 def forget_except_prefix(prefix): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
61 """Remove all pathnames except with a given prefix. |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
62 Normally used with prefix = '/' after a chdir().""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
63 n = len(prefix) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
64 for path in cache.keys(): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
65 if path[:n] != prefix: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
66 del cache[path] |
| 5 | 67 |
| 68 | |
| 69 def isdir(path): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
70 """Check for directory.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
71 try: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
72 st = stat(path) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
73 except os.error: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
74 return 0 |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
75 return S_ISDIR(st[ST_MODE]) |
