Mercurial > cpython-lru_cache-weakref
annotate Lib/statcache.py @ 6719:faebc1940432 legacy-trunk
Understand -Xlinker option.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 02 Dec 1997 20:38:11 +0000 |
| parents | 6d4f71430722 |
| children | 6f4146846337 |
| rev | line source |
|---|---|
| 5 | 1 # Module 'statcache' |
| 2 # | |
| 3 # Maintain a cache of file stats. | |
| 4 # There are functions to reset the cache or to selectively remove items. | |
| 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 # Stat a file, possibly out of the cache. | |
| 16 # | |
| 17 def stat(path): | |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
18 if cache.has_key(path): |
| 5 | 19 return cache[path] |
| 776 | 20 cache[path] = ret = os.stat(path) |
| 5 | 21 return ret |
| 22 | |
| 23 | |
| 24 # Reset the cache completely. | |
| 25 # | |
| 26 def reset(): | |
|
3286
6d4f71430722
Use global statement instead of importing ourselves to get to global
Sjoerd Mullender <sjoerd@acm.org>
parents:
776
diff
changeset
|
27 global cache |
|
6d4f71430722
Use global statement instead of importing ourselves to get to global
Sjoerd Mullender <sjoerd@acm.org>
parents:
776
diff
changeset
|
28 cache = {} |
| 5 | 29 |
| 30 | |
| 31 # Remove a given item from the cache, if it exists. | |
| 32 # | |
| 33 def forget(path): | |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
34 if cache.has_key(path): |
| 5 | 35 del cache[path] |
| 36 | |
| 37 | |
| 38 # Remove all pathnames with a given prefix. | |
| 39 # | |
| 40 def forget_prefix(prefix): | |
| 41 n = len(prefix) | |
| 42 for path in cache.keys(): | |
| 559 | 43 if path[:n] == prefix: |
| 5 | 44 del cache[path] |
| 45 | |
| 46 | |
| 47 # Forget about a directory and all entries in it, but not about | |
| 48 # entries in subdirectories. | |
| 49 # | |
| 50 def forget_dir(prefix): | |
| 559 | 51 if prefix[-1:] == '/' and prefix <> '/': |
| 5 | 52 prefix = prefix[:-1] |
| 53 forget(prefix) | |
| 54 if prefix[-1:] <> '/': | |
| 55 prefix = prefix + '/' | |
| 56 n = len(prefix) | |
| 57 for path in cache.keys(): | |
| 559 | 58 if path[:n] == prefix: |
| 5 | 59 rest = path[n:] |
| 559 | 60 if rest[-1:] == '/': rest = rest[:-1] |
| 5 | 61 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 # | |
| 68 def 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 # | |
| 77 def isdir(path): | |
| 78 try: | |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
79 st = stat(path) |
| 776 | 80 except os.error: |
| 5 | 81 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
|
82 return S_ISDIR(st[ST_MODE]) |
