Staging
v0.5.0
https://github.com/python/cpython
Raw File
Tip revision: 5f55933b3a78b7f3557ac81a2411a1b50bffab33 authored by Georg Brandl on 05 March 2011, 13:54:46 UTC
Close 2.1 branch.
Tip revision: 5f55933
dircache.py
"""Read and cache directory listings.

The listdir() routine returns a sorted list of the files in a directory,
using a cache to avoid reading the directory more often than necessary.
The annotate() routine appends slashes to directories."""

import os

__all__ = ["listdir", "opendir", "annotate", "reset"]

cache = {}

def reset():
    """Reset the cache completely."""
    global cache
    cache = {}

def listdir(path):
    """List directory contents, using cache."""
    try:
        cached_mtime, list = cache[path]
        del cache[path]
    except KeyError:
        cached_mtime, list = -1, []
    try:
        mtime = os.stat(path)[8]
    except os.error:
        return []
    if mtime != cached_mtime:
        try:
            list = os.listdir(path)
        except os.error:
            return []
        list.sort()
    cache[path] = mtime, list
    return list

opendir = listdir # XXX backward compatibility

def annotate(head, list):
    """Add '/' suffixes to directories."""
    for i in range(len(list)):
        if os.path.isdir(os.path.join(head, list[i])):
            list[i] = list[i] + '/'
back to top