Ticket #17628: 17628-2.diff

File 17628-2.diff, 8.1 KB (added by Claude Paroz, 13 years ago)

Patch with test

  • django/core/management/commands/makemessages.py

    diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py
    index 95c1da0..fa4ae19 100644
    a b def _popen(cmd):  
    4444    p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=os.name != 'nt', universal_newlines=True)
    4545    return p.communicate()
    4646
    47 def walk(root, topdown=True, onerror=None, followlinks=False):
     47def walk(root, topdown=True, onerror=None, followlinks=False, ignore_patterns=[], verbosity=0, stdout=None):
    4848    """
    4949    A version of os.walk that can follow symlinks for Python < 2.6
    5050    """
     51    norm_patterns = map(lambda p:p.endswith('/*') and p[:-2] or p, ignore_patterns)
    5152    for dirpath, dirnames, filenames in os.walk(root, topdown, onerror):
     53        remove_dirs = []
     54        for dirname in dirnames:
     55            if is_ignored(os.path.normpath(os.path.join(dirpath, dirname)), norm_patterns):
     56                remove_dirs.append(dirname)
     57        for dirname in remove_dirs:
     58            dirnames.remove(dirname)
     59            if verbosity > 1:
     60                stdout.write('ignoring directory %s\n' % dirname)
    5261        yield (dirpath, dirnames, filenames)
    5362        if followlinks:
    5463            for d in dirnames:
    def is_ignored(path, ignore_patterns):  
    6675            return True
    6776    return False
    6877
    69 def find_files(root, ignore_patterns, verbosity, symlinks=False):
     78def find_files(root, ignore_patterns, verbosity, stdout, symlinks=False):
    7079    """
    7180    Helper function to get all files in the given root.
    7281    """
    7382    all_files = []
    74     for (dirpath, dirnames, filenames) in walk(".", followlinks=symlinks):
     83    for (dirpath, dirnames, filenames) in walk(".", followlinks=symlinks,
     84            ignore_patterns=ignore_patterns, verbosity=verbosity, stdout=stdout):
    7585        for f in filenames:
    7686            norm_filepath = os.path.normpath(os.path.join(dirpath, f))
    7787            if is_ignored(norm_filepath, ignore_patterns):
    7888                if verbosity > 1:
    79                     sys.stdout.write('ignoring file %s in %s\n' % (f, dirpath))
     89                    stdout.write('ignoring file %s in %s\n' % (f, dirpath))
    8090            else:
    8191                all_files.extend([(dirpath, f)])
    8292    all_files.sort()
    8393    return all_files
    8494
    85 def copy_plural_forms(msgs, locale, domain, verbosity):
     95def copy_plural_forms(msgs, locale, domain, verbosity, stdout):
    8696    """
    8797    Copies plural forms header contents from a Django catalog of locale to
    8898    the msgs string, inserting it at the right place. msgs should be the
    def copy_plural_forms(msgs, locale, domain, verbosity):  
    100110            m = plural_forms_re.search(open(django_po, 'rU').read())
    101111            if m:
    102112                if verbosity > 1:
    103                     sys.stderr.write("copying plural forms: %s\n" % m.group('value'))
     113                    stdout.write("copying plural forms: %s\n" % m.group('value'))
    104114                lines = []
    105115                seen = False
    106116                for line in msgs.split('\n'):
    def write_pot_file(potfile, msgs, file, work_file, is_templatized):  
    133143        f.close()
    134144
    135145def process_file(file, dirpath, potfile, domain, verbosity, extensions, wrap,
    136         location):
     146        location, stdout):
    137147    """
    138148    Extract translatable literals from :param file: for :param domain:
    139149    creating or updating the :param potfile: POT file.
    def process_file(file, dirpath, potfile, domain, verbosity, extensions, wrap,  
    144154    from django.utils.translation import templatize
    145155
    146156    if verbosity > 1:
    147         sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
     157        stdout.write('processing file %s in %s\n' % (file, dirpath))
    148158    _, file_ext = os.path.splitext(file)
    149159    if domain == 'djangojs' and file_ext in extensions:
    150160        is_templatized = True
    def process_file(file, dirpath, potfile, domain, verbosity, extensions, wrap,  
    206216    if is_templatized:
    207217        os.unlink(work_file)
    208218
    209 def write_po_file(pofile, potfile, domain, locale, verbosity,
     219def write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
    210220        copy_pforms, wrap, location, no_obsolete):
    211221    """
    212222    Creates of updates the :param pofile: PO file for :param domain: and :param
    def write_po_file(pofile, potfile, domain, locale, verbosity,  
    232242            raise CommandError(
    233243                "errors happened while running msgmerge\n%s" % errors)
    234244    elif copy_pforms:
    235         msgs = copy_plural_forms(msgs, locale, domain, verbosity)
     245        msgs = copy_plural_forms(msgs, locale, domain, verbosity, stdout)
    236246    msgs = msgs.replace(
    237247        "#. #-#-#-#-#  %s.pot (PACKAGE VERSION)  #-#-#-#-#\n" % domain, "")
    238248    f = open(pofile, 'wb')
    def write_po_file(pofile, potfile, domain, locale, verbosity,  
    250260
    251261def make_messages(locale=None, domain='django', verbosity=1, all=False,
    252262        extensions=None, symlinks=False, ignore_patterns=None, no_wrap=False,
    253         no_location=False, no_obsolete=False):
     263        no_location=False, no_obsolete=False, stdout=sys.stdout):
    254264    """
    255265    Uses the ``locale/`` directory from the Django SVN tree or an
    256266    application/project to process all files with translatable literals for
    def make_messages(locale=None, domain='django', verbosity=1, all=False,  
    312322
    313323    for locale in locales:
    314324        if verbosity > 0:
    315             print "processing language", locale
     325            stdout.write("processing language %s" % locale)
    316326        basedir = os.path.join(localedir, locale, 'LC_MESSAGES')
    317327        if not os.path.isdir(basedir):
    318328            os.makedirs(basedir)
    def make_messages(locale=None, domain='django', verbosity=1, all=False,  
    324334            os.unlink(potfile)
    325335
    326336        for dirpath, file in find_files(".", ignore_patterns, verbosity,
    327                 symlinks=symlinks):
     337                stdout, symlinks=symlinks):
    328338            process_file(file, dirpath, potfile, domain, verbosity, extensions,
    329                     wrap, location)
     339                    wrap, location, stdout)
    330340
    331341        if os.path.exists(potfile):
    332             write_po_file(pofile, potfile, domain, locale, verbosity,
     342            write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
    333343                    not invoked_for_django, wrap, location, no_obsolete)
    334344
    335345
    class Command(NoArgsCommand):  
    386396            extensions = handle_extensions(extensions or ['html', 'txt'])
    387397
    388398        if verbosity > 1:
    389             sys.stdout.write('examining files with the extensions: %s\n'
     399            self.stdout.write('examining files with the extensions: %s\n'
    390400                             % get_text_list(list(extensions), 'and'))
    391401
    392         make_messages(locale, domain, verbosity, process_all, extensions, symlinks, ignore_patterns, no_wrap, no_location, no_obsolete)
     402        make_messages(locale, domain, verbosity, process_all, extensions, symlinks, ignore_patterns, no_wrap, no_location, no_obsolete, self.stdout)
  • tests/regressiontests/i18n/commands/extraction.py

    diff --git a/tests/regressiontests/i18n/commands/extraction.py b/tests/regressiontests/i18n/commands/extraction.py
    index b1fc002..4eb7abc 100644
    a b from __future__ import with_statement  
    44import os
    55import re
    66import shutil
     7from StringIO import StringIO
    78
    89from django.core import management
    910from django.test import TestCase
    class IgnoredExtractorTests(ExtractorTests):  
    177178    def test_ignore_option(self):
    178179        os.chdir(self.test_dir)
    179180        pattern1 = os.path.join('ignore_dir', '*')
    180         management.call_command('makemessages', locale=LOCALE, verbosity=0, ignore_patterns=[pattern1])
     181        stdout = StringIO()
     182        management.call_command('makemessages', locale=LOCALE, verbosity=2,
     183            ignore_patterns=[pattern1], stdout=stdout)
     184        data = stdout.getvalue()
     185        self.assertTrue("ignoring directory ignore_dir" in data)
    181186        self.assertTrue(os.path.exists(self.PO_FILE))
    182187        with open(self.PO_FILE, 'r') as fp:
    183188            po_contents = fp.read()
Back to Top