Mercurial > cpython-lru_cache-weakref
annotate Lib/statcache.py @ 15164:8930ec3bc00a legacy-trunk
Added additional test cases for pulldom modifications.
| author | Lars Gustäbel <lars@gustaebel.de> |
|---|---|
| date | Wed, 11 Oct 2000 22:36:00 +0000 |
| parents | 4a23cc49e2b0 |
| children | f34c90989138 785155e2cfca |
| 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): | |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
16 """Stat a file, possibly out of the cache.""" |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
17 if cache.has_key(path): |
| 5 | 18 return cache[path] |
| 776 | 19 cache[path] = ret = os.stat(path) |
| 5 | 20 return ret |
| 21 | |
| 22 | |
| 23 def reset(): | |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
24 """Reset the cache completely.""" |
|
3286
6d4f71430722
Use global statement instead of importing ourselves to get to global
Sjoerd Mullender <sjoerd@acm.org>
parents:
776
diff
changeset
|
25 global cache |
|
6d4f71430722
Use global statement instead of importing ourselves to get to global
Sjoerd Mullender <sjoerd@acm.org>
parents:
776
diff
changeset
|
26 cache = {} |
| 5 | 27 |
| 28 | |
| 29 def forget(path): | |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
30 """Remove a given item from the cache, if it exists.""" |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
31 if cache.has_key(path): |
| 5 | 32 del cache[path] |
| 33 | |
| 34 | |
| 35 def forget_prefix(prefix): | |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
36 """Remove all pathnames with a given prefix.""" |
| 5 | 37 n = len(prefix) |
| 38 for path in cache.keys(): | |
| 559 | 39 if path[:n] == prefix: |
| 5 | 40 del cache[path] |
| 41 | |
| 42 | |
| 43 def forget_dir(prefix): | |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
44 """Forget about a directory and all entries in it, but not about |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
45 entries in subdirectories.""" |
| 559 | 46 if prefix[-1:] == '/' and prefix <> '/': |
| 5 | 47 prefix = prefix[:-1] |
| 48 forget(prefix) | |
| 49 if prefix[-1:] <> '/': | |
| 50 prefix = prefix + '/' | |
| 51 n = len(prefix) | |
| 52 for path in cache.keys(): | |
| 559 | 53 if path[:n] == prefix: |
| 5 | 54 rest = path[n:] |
| 559 | 55 if rest[-1:] == '/': rest = rest[:-1] |
| 5 | 56 if '/' not in rest: |
| 57 del cache[path] | |
| 58 | |
| 59 | |
| 60 def forget_except_prefix(prefix): | |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
61 """Remove all pathnames except with a given prefix. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
62 Normally used with prefix = '/' after a chdir().""" |
| 5 | 63 n = len(prefix) |
| 64 for path in cache.keys(): | |
| 65 if path[:n] <> prefix: | |
| 66 del cache[path] | |
| 67 | |
| 68 | |
| 69 def isdir(path): | |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
70 """Check for directory.""" |
| 5 | 71 try: |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
72 st = stat(path) |
| 776 | 73 except os.error: |
| 5 | 74 return 0 |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
75 return S_ISDIR(st[ST_MODE]) |
