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