Added a custom walk function (like os.walk) that supports symlinks for Python<2.6 compatibility.

This commit is contained in:
Jannis Leidel
2011-01-18 10:40:48 +01:00
parent 8fa4dd2ae1
commit c3e8de8c7b
2 changed files with 17 additions and 2 deletions

View File

@@ -6,7 +6,7 @@ from django.core.management.base import NoArgsCommand, CommandError
from compressor.cache import cache
from compressor.conf import settings
from compressor.utils import get_mtime, get_mtime_cachekey
from compressor.utils import get_mtime, get_mtime_cachekey, walk
class Command(NoArgsCommand):
help = "Add or remove all mtime values from the cache"
@@ -57,7 +57,7 @@ class Command(NoArgsCommand):
files_to_add = set()
keys_to_delete = set()
for root, dirs, files in os.walk(settings.MEDIA_ROOT, followlinks=options['follow_links']):
for root, dirs, files in walk(settings.MEDIA_ROOT, followlinks=options['follow_links']):
for dir_ in dirs:
if self.is_ignored(dir_):
dirs.remove(dir_)

View File

@@ -61,3 +61,18 @@ def get_mod_func(callback):
except ValueError:
return callback, ''
return callback[:dot], callback[dot+1:]
def walk(root, topdown=True, onerror=None, followlinks=False):
"""
A version of os.walk that can follow symlinks for Python < 2.6
"""
for dirpath, dirnames, filenames in os.walk(root, topdown, onerror):
yield (dirpath, dirnames, filenames)
if followlinks:
for d in dirnames:
p = os.path.join(dirpath, d)
if os.path.islink(p):
for link_dirpath, link_dirnames, link_filenames in walk(p):
yield (link_dirpath, link_dirnames, link_filenames)