1
0
mirror of https://github.com/django/django.git synced 2024-12-25 10:35:48 +00:00

Merge walk and find_files in makemessages command

This commit is contained in:
Claude Paroz 2012-08-21 14:43:37 +02:00
parent ab2f65bb7f
commit fd58d6c258

View File

@ -47,31 +47,27 @@ def _popen(cmd):
output, errors = p.communicate() output, errors = p.communicate()
return output, errors, p.returncode return output, errors, p.returncode
def walk(root, topdown=True, onerror=None, followlinks=False, def find_files(root, ignore_patterns, verbosity, stdout=sys.stdout, symlinks=False):
ignore_patterns=None, verbosity=0, stdout=sys.stdout):
""" """
A version of os.walk that can follow symlinks for Python < 2.6 Helper function to get all files in the given root.
""" """
if ignore_patterns is None:
ignore_patterns = []
dir_suffix = '%s*' % os.sep dir_suffix = '%s*' % os.sep
norm_patterns = [p[:-len(dir_suffix)] if p.endswith(dir_suffix) else p for p in ignore_patterns] norm_patterns = [p[:-len(dir_suffix)] if p.endswith(dir_suffix) else p for p in ignore_patterns]
for dirpath, dirnames, filenames in os.walk(root, topdown, onerror): all_files = []
remove_dirs = [] for dirpath, dirnames, filenames in os.walk(root, topdown=True, followlinks=symlinks):
for dirname in dirnames: for dirname in dirnames[:]:
if is_ignored(os.path.normpath(os.path.join(dirpath, dirname)), norm_patterns): if is_ignored(os.path.normpath(os.path.join(dirpath, dirname)), norm_patterns):
remove_dirs.append(dirname) dirnames.remove(dirname)
for dirname in remove_dirs: if verbosity > 1:
dirnames.remove(dirname) stdout.write('ignoring directory %s\n' % dirname)
if verbosity > 1: for filename in filenames:
stdout.write('ignoring directory %s\n' % dirname) if is_ignored(os.path.normpath(os.path.join(dirpath, filename)), ignore_patterns):
yield (dirpath, dirnames, filenames) if verbosity > 1:
if followlinks: stdout.write('ignoring file %s in %s\n' % (filename, dirpath))
for d in dirnames: else:
p = os.path.join(dirpath, d) all_files.extend([(dirpath, filename)])
if os.path.islink(p): all_files.sort()
for link_dirpath, link_dirnames, link_filenames in walk(p): return all_files
yield (link_dirpath, link_dirnames, link_filenames)
def is_ignored(path, ignore_patterns): def is_ignored(path, ignore_patterns):
""" """
@ -82,23 +78,6 @@ def is_ignored(path, ignore_patterns):
return True return True
return False return False
def find_files(root, ignore_patterns, verbosity, stdout=sys.stdout, symlinks=False):
"""
Helper function to get all files in the given root.
"""
all_files = []
for (dirpath, dirnames, filenames) in walk(root, followlinks=symlinks,
ignore_patterns=ignore_patterns, verbosity=verbosity, stdout=stdout):
for filename in filenames:
norm_filepath = os.path.normpath(os.path.join(dirpath, filename))
if is_ignored(norm_filepath, ignore_patterns):
if verbosity > 1:
stdout.write('ignoring file %s in %s\n' % (filename, dirpath))
else:
all_files.extend([(dirpath, filename)])
all_files.sort()
return all_files
def copy_plural_forms(msgs, locale, domain, verbosity, stdout=sys.stdout): def copy_plural_forms(msgs, locale, domain, verbosity, stdout=sys.stdout):
""" """
Copies plural forms header contents from a Django catalog of locale to Copies plural forms header contents from a Django catalog of locale to