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