Mercurial > cpython-lru_cache-weakref
annotate Lib/statcache.py @ 32975:9b188158181f legacy-trunk
Typo fix; use 'in-place' instead of 'inplace'
| author | Andrew M. Kuchling <amk@amk.ca> |
|---|---|
| date | Fri, 06 Aug 2004 18:55:09 +0000 |
| parents | 65cf478950ff |
| children |
| rev | line source |
|---|---|
|
11708
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11707
diff
changeset
|
1 """Maintain a cache of stat() information on files. |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11707
diff
changeset
|
2 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
3 There are functions to reset the cache or to selectively remove items. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
4 """ |
| 5 | 5 |
|
22928
e2dec3cdf3c2
Add deprecation warnings for modules as documented
Neal Norwitz <nnorwitz@gmail.com>
parents:
22919
diff
changeset
|
6 import warnings |
|
e2dec3cdf3c2
Add deprecation warnings for modules as documented
Neal Norwitz <nnorwitz@gmail.com>
parents:
22919
diff
changeset
|
7 warnings.warn("The statcache module is obsolete. Use os.stat() instead.", |
|
e2dec3cdf3c2
Add deprecation warnings for modules as documented
Neal Norwitz <nnorwitz@gmail.com>
parents:
22919
diff
changeset
|
8 DeprecationWarning) |
|
e2dec3cdf3c2
Add deprecation warnings for modules as documented
Neal Norwitz <nnorwitz@gmail.com>
parents:
22919
diff
changeset
|
9 del warnings |
|
e2dec3cdf3c2
Add deprecation warnings for modules as documented
Neal Norwitz <nnorwitz@gmail.com>
parents:
22919
diff
changeset
|
10 |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
11 import os as _os |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
12 from stat import * |
| 5 | 13 |
| 16613 | 14 __all__ = ["stat","reset","forget","forget_prefix","forget_dir", |
| 15 "forget_except_prefix","isdir"] | |
| 16 | |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
17 # The cache. Keys are pathnames, values are os.stat outcomes. |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
18 # Remember that multiple threads may be calling this! So, e.g., that |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22928
diff
changeset
|
19 # path in cache returns 1 doesn't mean the cache will still contain |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
20 # path on the next line. Code defensively. |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
21 |
| 5 | 22 cache = {} |
| 23 | |
| 24 def stat(path): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
25 """Stat a file, possibly out of the cache.""" |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
26 ret = cache.get(path, None) |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
27 if ret is None: |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
28 cache[path] = ret = _os.stat(path) |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
29 return ret |
| 5 | 30 |
| 31 def reset(): | |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
32 """Clear the cache.""" |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
33 cache.clear() |
| 5 | 34 |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
35 # For thread saftey, always use forget() internally too. |
| 5 | 36 def forget(path): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
37 """Remove a given item from the cache, if it exists.""" |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
38 try: |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
39 del cache[path] |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
40 except KeyError: |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
41 pass |
| 5 | 42 |
| 43 def forget_prefix(prefix): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
44 """Remove all pathnames with a given prefix.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
45 for path in cache.keys(): |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
46 if path.startswith(prefix): |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
47 forget(path) |
| 5 | 48 |
| 49 def forget_dir(prefix): | |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
50 """Forget a directory and all entries except for entries in subdirs.""" |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
51 |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
52 # Remove trailing separator, if any. This is tricky to do in a |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
53 # x-platform way. For example, Windows accepts both / and \ as |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
54 # separators, and if there's nothing *but* a separator we want to |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
55 # preserve that this is the root. Only os.path has the platform |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
56 # knowledge we need. |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
57 from os.path import split, join |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
58 prefix = split(join(prefix, "xxx"))[0] |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
59 forget(prefix) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
60 for path in cache.keys(): |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
61 # First check that the path at least starts with the prefix, so |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
62 # that when it doesn't we can avoid paying for split(). |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
63 if path.startswith(prefix) and split(path)[0] == prefix: |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
64 forget(path) |
| 5 | 65 |
| 66 def forget_except_prefix(prefix): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
67 """Remove all pathnames except with a given prefix. |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
68 |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
69 Normally used with prefix = '/' after a chdir(). |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
70 """ |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
71 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
72 for path in cache.keys(): |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
73 if not path.startswith(prefix): |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
74 forget(path) |
| 5 | 75 |
| 76 def isdir(path): | |
|
22919
54229c35132e
Update docstring to reflect code change to bool
Neal Norwitz <nnorwitz@gmail.com>
parents:
22867
diff
changeset
|
77 """Return True if directory, else False.""" |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
78 try: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
79 st = stat(path) |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
80 except _os.error: |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
16613
diff
changeset
|
81 return False |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
82 return S_ISDIR(st.st_mode) |
