annotate Lib/statcache.py @ 13163:a09e8635534d legacy-trunk

Don't try to guess the name of a .def file -- if one is supplied, use it, otherwise just generate an '/export:' option.
author Greg Ward <gward@python.net>
date Thu, 29 Jun 2000 23:09:20 +0000
parents 4a23cc49e2b0
children f34c90989138 785155e2cfca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
776
7259e96a04a9 posix -> os
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # The cache.
776
7259e96a04a9 posix -> os
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
10 # Keys are pathnames, values are `os.stat' outcomes.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 cache = {}
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 def stat(path):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
16 """Stat a file, possibly out of the cache."""
21
961696ef3ab7 Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
17 if cache.has_key(path):
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 return cache[path]
776
7259e96a04a9 posix -> os
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
19 cache[path] = ret = os.stat(path)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 return ret
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 def reset():
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
24 """Reset the cache completely."""
3286
6d4f71430722 Use global statement instead of importing ourselves to get to global
Sjoerd Mullender <sjoerd@acm.org>
parents: 776
diff changeset
25 global cache
6d4f71430722 Use global statement instead of importing ourselves to get to global
Sjoerd Mullender <sjoerd@acm.org>
parents: 776
diff changeset
26 cache = {}
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 def forget(path):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
30 """Remove a given item from the cache, if it exists."""
21
961696ef3ab7 Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
31 if cache.has_key(path):
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 del cache[path]
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def forget_prefix(prefix):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
36 """Remove all pathnames with a given prefix."""
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 n = len(prefix)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 for path in cache.keys():
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
39 if path[:n] == prefix:
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 del cache[path]
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 def forget_dir(prefix):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
44 """Forget about a directory and all entries in it, but not about
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
45 entries in subdirectories."""
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
46 if prefix[-1:] == '/' and prefix <> '/':
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 prefix = prefix[:-1]
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 forget(prefix)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 if prefix[-1:] <> '/':
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 prefix = prefix + '/'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 n = len(prefix)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 for path in cache.keys():
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
53 if path[:n] == prefix:
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 rest = path[n:]
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
55 if rest[-1:] == '/': rest = rest[:-1]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 if '/' not in rest:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 del cache[path]
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 def forget_except_prefix(prefix):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
61 """Remove all pathnames except with a given prefix.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
62 Normally used with prefix = '/' after a chdir()."""
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 n = len(prefix)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 for path in cache.keys():
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 if path[:n] <> prefix:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 del cache[path]
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 def isdir(path):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 3286
diff changeset
70 """Check for directory."""
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 try:
21
961696ef3ab7 Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
72 st = stat(path)
776
7259e96a04a9 posix -> os
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
73 except os.error:
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 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
75 return S_ISDIR(st[ST_MODE])