dircache.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """Read and cache directory listings.
  2. The listdir() routine returns a sorted list of the files in a directory,
  3. using a cache to avoid reading the directory more often than necessary.
  4. The annotate() routine appends slashes to directories."""
  5. from warnings import warnpy3k
  6. warnpy3k("the dircache module has been removed in Python 3.0", stacklevel=2)
  7. del warnpy3k
  8. import os
  9. __all__ = ["listdir", "opendir", "annotate", "reset"]
  10. cache = {}
  11. def reset():
  12. """Reset the cache completely."""
  13. global cache
  14. cache = {}
  15. def listdir(path):
  16. """List directory contents, using cache."""
  17. try:
  18. cached_mtime, list = cache[path]
  19. del cache[path]
  20. except KeyError:
  21. cached_mtime, list = -1, []
  22. mtime = os.stat(path).st_mtime
  23. if mtime != cached_mtime:
  24. list = os.listdir(path)
  25. list.sort()
  26. cache[path] = mtime, list
  27. return list
  28. opendir = listdir # XXX backward compatibility
  29. def annotate(head, list):
  30. """Add '/' suffixes to directories."""
  31. for i in range(len(list)):
  32. if os.path.isdir(os.path.join(head, list[i])):
  33. list[i] = list[i] + '/'