Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """Maintain a cache of stat() information on files. |
| 2 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 3 | There are functions to reset the cache or to selectively remove items. |
| 4 | """ |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 25d7caf | 1992-03-31 19:04:48 +0000 | [diff] [blame] | 6 | import os |
Guido van Rossum | 40d9304 | 1990-10-21 16:17:34 +0000 | [diff] [blame] | 7 | from stat import * |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 8 | |
| 9 | # The cache. |
Guido van Rossum | 25d7caf | 1992-03-31 19:04:48 +0000 | [diff] [blame] | 10 | # Keys are pathnames, values are `os.stat' outcomes. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 11 | # |
| 12 | cache = {} |
| 13 | |
| 14 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 15 | def stat(path): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 16 | """Stat a file, possibly out of the cache.""" |
| 17 | if cache.has_key(path): |
| 18 | return cache[path] |
| 19 | cache[path] = ret = os.stat(path) |
| 20 | return ret |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 21 | |
| 22 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 23 | def reset(): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 24 | """Reset the cache completely.""" |
| 25 | global cache |
| 26 | cache = {} |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 27 | |
| 28 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 29 | def forget(path): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 30 | """Remove a given item from the cache, if it exists.""" |
| 31 | if cache.has_key(path): |
| 32 | del cache[path] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 33 | |
| 34 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 35 | def forget_prefix(prefix): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 36 | """Remove all pathnames with a given prefix.""" |
| 37 | n = len(prefix) |
| 38 | for path in cache.keys(): |
| 39 | if path[:n] == prefix: |
| 40 | del cache[path] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 41 | |
| 42 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 43 | def forget_dir(prefix): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 44 | """Forget about a directory and all entries in it, but not about |
| 45 | entries in subdirectories.""" |
| 46 | if prefix[-1:] == '/' and prefix != '/': |
| 47 | prefix = prefix[:-1] |
| 48 | forget(prefix) |
| 49 | if prefix[-1:] != '/': |
| 50 | prefix = prefix + '/' |
| 51 | n = len(prefix) |
| 52 | for path in cache.keys(): |
| 53 | if path[:n] == prefix: |
| 54 | rest = path[n:] |
| 55 | if rest[-1:] == '/': rest = rest[:-1] |
| 56 | if '/' not in rest: |
| 57 | del cache[path] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 58 | |
| 59 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 60 | def forget_except_prefix(prefix): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 61 | """Remove all pathnames except with a given prefix. |
| 62 | Normally used with prefix = '/' after a chdir().""" |
| 63 | n = len(prefix) |
| 64 | for path in cache.keys(): |
| 65 | if path[:n] != prefix: |
| 66 | del cache[path] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 67 | |
| 68 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 69 | def isdir(path): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 70 | """Check for directory.""" |
| 71 | try: |
| 72 | st = stat(path) |
| 73 | except os.error: |
| 74 | return 0 |
| 75 | return S_ISDIR(st[ST_MODE]) |