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 | |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 6 | import os as _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 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame^] | 9 | __all__ = ["stat","reset","forget","forget_prefix","forget_dir", |
| 10 | "forget_except_prefix","isdir"] |
| 11 | |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 12 | # The cache. Keys are pathnames, values are os.stat outcomes. |
| 13 | # Remember that multiple threads may be calling this! So, e.g., that |
| 14 | # cache.has_key(path) returns 1 doesn't mean the cache will still contain |
| 15 | # path on the next line. Code defensively. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 16 | |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 17 | cache = {} |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 19 | def stat(path): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 20 | """Stat a file, possibly out of the cache.""" |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 21 | ret = cache.get(path, None) |
| 22 | if ret is None: |
| 23 | cache[path] = ret = _os.stat(path) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 24 | return ret |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 26 | def reset(): |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 27 | """Clear the cache.""" |
| 28 | cache.clear() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 29 | |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 30 | # For thread saftey, always use forget() internally too. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 31 | def forget(path): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 32 | """Remove a given item from the cache, if it exists.""" |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 33 | try: |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 34 | del cache[path] |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 35 | except KeyError: |
| 36 | pass |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 38 | def forget_prefix(prefix): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 39 | """Remove all pathnames with a given prefix.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 40 | for path in cache.keys(): |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 41 | if path.startswith(prefix): |
| 42 | forget(path) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 44 | def forget_dir(prefix): |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 45 | """Forget a directory and all entries except for entries in subdirs.""" |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 46 | |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 47 | # Remove trailing separator, if any. This is tricky to do in a |
| 48 | # x-platform way. For example, Windows accepts both / and \ as |
| 49 | # separators, and if there's nothing *but* a separator we want to |
| 50 | # preserve that this is the root. Only os.path has the platform |
| 51 | # knowledge we need. |
| 52 | from os.path import split, join |
| 53 | prefix = split(join(prefix, "xxx"))[0] |
| 54 | forget(prefix) |
| 55 | for path in cache.keys(): |
| 56 | # First check that the path at least starts with the prefix, so |
| 57 | # that when it doesn't we can avoid paying for split(). |
| 58 | if path.startswith(prefix) and split(path)[0] == prefix: |
| 59 | forget(path) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 61 | def forget_except_prefix(prefix): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 62 | """Remove all pathnames except with a given prefix. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 63 | |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 64 | Normally used with prefix = '/' after a chdir(). |
| 65 | """ |
| 66 | |
| 67 | for path in cache.keys(): |
| 68 | if not path.startswith(prefix): |
| 69 | forget(path) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 71 | def isdir(path): |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 72 | """Return 1 if directory, else 0.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 73 | try: |
| 74 | st = stat(path) |
Tim Peters | 0149e84 | 2001-01-28 05:07:00 +0000 | [diff] [blame] | 75 | except _os.error: |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 76 | return 0 |
| 77 | return S_ISDIR(st[ST_MODE]) |