Mercurial > cpython-lru_cache-weakref
annotate Lib/statcache.py @ 3044:7e7bb00a0a2e legacy-trunk
Added functionality: locate CW projects, copy them, empty them and
binhex them.
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Thu, 31 Aug 1995 13:47:14 +0000 |
| parents | 7259e96a04a9 |
| children | 6d4f71430722 |
| 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 # Hack: to reset a global variable, we import this module. | |
| 26 # | |
| 27 def reset(): | |
| 28 import statcache | |
| 29 # Check that we really imported the same module | |
| 30 if cache is not statcache.cache: | |
| 31 raise 'sorry, statcache identity crisis' | |
| 32 statcache.cache = {} | |
| 33 | |
| 34 | |
| 35 # Remove a given item from the cache, if it exists. | |
| 36 # | |
| 37 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
|
38 if cache.has_key(path): |
| 5 | 39 del cache[path] |
| 40 | |
| 41 | |
| 42 # Remove all pathnames with a given prefix. | |
| 43 # | |
| 44 def forget_prefix(prefix): | |
| 45 n = len(prefix) | |
| 46 for path in cache.keys(): | |
| 559 | 47 if path[:n] == prefix: |
| 5 | 48 del cache[path] |
| 49 | |
| 50 | |
| 51 # Forget about a directory and all entries in it, but not about | |
| 52 # entries in subdirectories. | |
| 53 # | |
| 54 def forget_dir(prefix): | |
| 559 | 55 if prefix[-1:] == '/' and prefix <> '/': |
| 5 | 56 prefix = prefix[:-1] |
| 57 forget(prefix) | |
| 58 if prefix[-1:] <> '/': | |
| 59 prefix = prefix + '/' | |
| 60 n = len(prefix) | |
| 61 for path in cache.keys(): | |
| 559 | 62 if path[:n] == prefix: |
| 5 | 63 rest = path[n:] |
| 559 | 64 if rest[-1:] == '/': rest = rest[:-1] |
| 5 | 65 if '/' not in rest: |
| 66 del cache[path] | |
| 67 | |
| 68 | |
| 69 # Remove all pathnames except with a given prefix. | |
| 70 # Normally used with prefix = '/' after a chdir(). | |
| 71 # | |
| 72 def forget_except_prefix(prefix): | |
| 73 n = len(prefix) | |
| 74 for path in cache.keys(): | |
| 75 if path[:n] <> prefix: | |
| 76 del cache[path] | |
| 77 | |
| 78 | |
| 79 # Check for directory. | |
| 80 # | |
| 81 def isdir(path): | |
| 82 try: | |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
83 st = stat(path) |
| 776 | 84 except os.error: |
| 5 | 85 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
|
86 return S_ISDIR(st[ST_MODE]) |
