blob: 1eebbbc61d63b50d21e95135445b59ad21a433ff [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# Module 'dircache'
2#
3# Return a sorted list of the files in a POSIX directory, using a cache
4# to avoid reading the directory more often than necessary.
5# Also contains a subroutine to append slashes to directories.
6
7import posix
8import path
9
10cache = {}
11
12def listdir(path): # List directory contents, using cache
13 try:
14 cached_mtime, list = cache[path]
15 del cache[path]
16 except RuntimeError:
17 cached_mtime, list = -1, []
18 try:
19 mtime = posix.stat(path)[8]
20 except posix.error:
21 return []
22 if mtime <> cached_mtime:
23 try:
24 list = posix.listdir(path)
25 except posix.error:
26 return []
27 list.sort()
28 cache[path] = mtime, list
29 return list
30
31opendir = listdir # XXX backward compatibility
32
33def annotate(head, list): # Add '/' suffixes to directories
34 for i in range(len(list)):
Guido van Rossum784ca6c1991-08-16 13:28:23 +000035 if path.isdir(path.join(head, list[i])):
Guido van Rossumc6360141990-10-13 19:23:40 +000036 list[i] = list[i] + '/'