Commit 561ecc76 by Calen Pennington Committed by David Baumgold

Make i18n tests quieter

parent c87a1138
...@@ -22,7 +22,7 @@ $ ./dummy.py ...@@ -22,7 +22,7 @@ $ ./dummy.py
generates output conf/locale/$DUMMY_LOCALE/LC_MESSAGES, generates output conf/locale/$DUMMY_LOCALE/LC_MESSAGES,
where $DUMMY_LOCALE is the dummy_locale value set in the i18n config where $DUMMY_LOCALE is the dummy_locale value set in the i18n config
""" """
from __future__ import print_function
import re import re
import sys import sys
...@@ -197,17 +197,20 @@ def new_filename(original_filename, new_locale): ...@@ -197,17 +197,20 @@ def new_filename(original_filename, new_locale):
return new_file.abspath() return new_file.abspath()
def main(): def main(verbosity=1):
""" """
Generate dummy strings for all source po files. Generate dummy strings for all source po files.
""" """
SOURCE_MSGS_DIR = CONFIGURATION.source_messages_dir SOURCE_MSGS_DIR = CONFIGURATION.source_messages_dir
for locale, converter in zip(CONFIGURATION.dummy_locales, [Dummy(), Dummy2()]): for locale, converter in zip(CONFIGURATION.dummy_locales, [Dummy(), Dummy2()]):
print "Processing source language files into dummy strings, locale {}:".format(locale) if verbosity:
print("Processing source language files into dummy strings, locale {}:".format(locale))
for source_file in CONFIGURATION.source_messages_dir.walkfiles('*.po'): for source_file in CONFIGURATION.source_messages_dir.walkfiles('*.po'):
print ' ', source_file.relpath() if verbosity:
print(' ', source_file.relpath())
make_dummy(SOURCE_MSGS_DIR.joinpath(source_file), locale, converter) make_dummy(SOURCE_MSGS_DIR.joinpath(source_file), locale, converter)
print if verbosity:
print()
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -4,7 +4,7 @@ from i18n.config import BASE_DIR ...@@ -4,7 +4,7 @@ from i18n.config import BASE_DIR
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
def execute(command, working_directory=BASE_DIR): def execute(command, working_directory=BASE_DIR, stderr=subprocess.STDOUT):
""" """
Executes shell command in a given working_directory. Executes shell command in a given working_directory.
Command is a string to pass to the shell. Command is a string to pass to the shell.
...@@ -12,7 +12,7 @@ def execute(command, working_directory=BASE_DIR): ...@@ -12,7 +12,7 @@ def execute(command, working_directory=BASE_DIR):
""" """
LOG.info("Executing in %s ...", working_directory) LOG.info("Executing in %s ...", working_directory)
LOG.info(command) LOG.info(command)
subprocess.check_call(command, cwd=working_directory, stderr=subprocess.STDOUT, shell=True) subprocess.check_call(command, cwd=working_directory, stderr=stderr, shell=True)
def call(command, working_directory=BASE_DIR): def call(command, working_directory=BASE_DIR):
......
...@@ -21,6 +21,7 @@ import os ...@@ -21,6 +21,7 @@ import os
import os.path import os.path
import logging import logging
import sys import sys
import argparse
from path import path from path import path
from polib import pofile from polib import pofile
...@@ -31,39 +32,48 @@ from i18n.segment import segment_pofiles ...@@ -31,39 +32,48 @@ from i18n.segment import segment_pofiles
EDX_MARKER = "edX translation file" EDX_MARKER = "edX translation file"
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
DEVNULL = open(os.devnull, 'wb')
def base(path1, *paths): def base(path1, *paths):
"""Return a relative path from BASE_DIR to path1 / paths[0] / ... """ """Return a relative path from BASE_DIR to path1 / paths[0] / ... """
return BASE_DIR.relpathto(path1.joinpath(*paths)) return BASE_DIR.relpathto(path1.joinpath(*paths))
def main():
def main(verbosity=1):
logging.basicConfig(stream=sys.stdout, level=logging.INFO) logging.basicConfig(stream=sys.stdout, level=logging.INFO)
create_dir_if_necessary(LOCALE_DIR) create_dir_if_necessary(LOCALE_DIR)
source_msgs_dir = CONFIGURATION.source_messages_dir source_msgs_dir = CONFIGURATION.source_messages_dir
remove_file(source_msgs_dir.joinpath('django.po')) remove_file(source_msgs_dir.joinpath('django.po'))
# Extract strings from mako templates. # Extract strings from mako templates.
babel_mako_cmd = 'pybabel extract -F {config} -c "Translators:" . -o {output}' verbosity_map = {
0: "-q",
1: "",
2: "-v",
}
babel_verbosity = verbosity_map.get(verbosity, "")
babel_mako_cmd = 'pybabel {verbosity} extract -F {config} -c "Translators:" . -o {output}'
babel_mako_cmd = babel_mako_cmd.format( babel_mako_cmd = babel_mako_cmd.format(
verbosity=babel_verbosity,
config=base(LOCALE_DIR, 'babel_mako.cfg'), config=base(LOCALE_DIR, 'babel_mako.cfg'),
output=base(CONFIGURATION.source_messages_dir, 'mako.po'), output=base(CONFIGURATION.source_messages_dir, 'mako.po'),
) )
execute(babel_mako_cmd, working_directory=BASE_DIR) execute(babel_mako_cmd, working_directory=BASE_DIR, stderr=DEVNULL)
makemessages = "django-admin.py makemessages -l en" makemessages = "django-admin.py makemessages -l en -v{}".format(verbosity)
ignores = " ".join('--ignore="{}/*"'.format(d) for d in CONFIGURATION.ignore_dirs) ignores = " ".join('--ignore="{}/*"'.format(d) for d in CONFIGURATION.ignore_dirs)
if ignores: if ignores:
makemessages += " " + ignores makemessages += " " + ignores
# Extract strings from django source files, including .py files. # Extract strings from django source files, including .py files.
make_django_cmd = makemessages + ' --extension html' make_django_cmd = makemessages + ' --extension html'
execute(make_django_cmd, working_directory=BASE_DIR) execute(make_django_cmd, working_directory=BASE_DIR, stderr=DEVNULL)
# Extract strings from Javascript source files. # Extract strings from Javascript source files.
make_djangojs_cmd = makemessages + ' -d djangojs --extension js' make_djangojs_cmd = makemessages + ' -d djangojs --extension js'
execute(make_djangojs_cmd, working_directory=BASE_DIR) execute(make_djangojs_cmd, working_directory=BASE_DIR, stderr=DEVNULL)
# makemessages creates 'django.po'. This filename is hardcoded. # makemessages creates 'django.po'. This filename is hardcoded.
# Rename it to django-partial.po to enable merging into django.po later. # Rename it to django-partial.po to enable merging into django.po later.
...@@ -90,13 +100,14 @@ def main(): ...@@ -90,13 +100,14 @@ def main():
output_file = source_msgs_dir / (app_name + ".po") output_file = source_msgs_dir / (app_name + ".po")
files_to_clean.add(output_file) files_to_clean.add(output_file)
babel_cmd = 'pybabel extract -F {config} -c "Translators:" {app} -o {output}' babel_cmd = 'pybabel {verbosity} extract -F {config} -c "Translators:" {app} -o {output}'
babel_cmd = babel_cmd.format( babel_cmd = babel_cmd.format(
verbosity=babel_verbosity,
config=LOCALE_DIR / 'babel_third_party.cfg', config=LOCALE_DIR / 'babel_third_party.cfg',
app=app_name, app=app_name,
output=output_file, output=output_file,
) )
execute(babel_cmd, working_directory=app_dir) execute(babel_cmd, working_directory=app_dir, stderr=DEVNULL)
# Segment the generated files. # Segment the generated files.
segmented_files = segment_pofiles("en") segmented_files = segment_pofiles("en")
...@@ -191,4 +202,7 @@ def is_key_string(string): ...@@ -191,4 +202,7 @@ def is_key_string(string):
return len(string) > 1 and string[0] == '_' return len(string) > 1 and string[0] == '_'
if __name__ == '__main__': if __name__ == '__main__':
main() parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='count', default=0)
args = parser.parse_args()
main(verbosity=args.verbose)
...@@ -24,6 +24,7 @@ from i18n.config import BASE_DIR, CONFIGURATION ...@@ -24,6 +24,7 @@ from i18n.config import BASE_DIR, CONFIGURATION
from i18n.execute import execute from i18n.execute import execute
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
DEVNULL = open(os.devnull, "wb")
def merge(locale, target='django.po', sources=('django-partial.po',), fail_if_missing=True): def merge(locale, target='django.po', sources=('django-partial.po',), fail_if_missing=True):
...@@ -124,8 +125,8 @@ def main(argv=None): ...@@ -124,8 +125,8 @@ def main(argv=None):
for locale in CONFIGURATION.dummy_locales: for locale in CONFIGURATION.dummy_locales:
merge_files(locale, fail_if_missing=False) merge_files(locale, fail_if_missing=False)
compile_cmd = 'django-admin.py compilemessages' compile_cmd = 'django-admin.py compilemessages -v0'
execute(compile_cmd, working_directory=BASE_DIR) execute(compile_cmd, working_directory=BASE_DIR, stderr=DEVNULL)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -33,7 +33,7 @@ class TestExtract(TestCase): ...@@ -33,7 +33,7 @@ class TestExtract(TestCase):
super(TestExtract, self).setUp() super(TestExtract, self).setUp()
if not SETUP_HAS_RUN: if not SETUP_HAS_RUN:
# Run extraction script. Warning, this takes 1 minute or more # Run extraction script. Warning, this takes 1 minute or more
extract.main() extract.main(verbosity=0)
SETUP_HAS_RUN = True SETUP_HAS_RUN = True
def get_files(self): def get_files(self):
......
...@@ -23,8 +23,8 @@ class TestGenerate(TestCase): ...@@ -23,8 +23,8 @@ class TestGenerate(TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
extract.main() extract.main(verbosity=0)
dummy.main() dummy.main(verbosity=0)
def setUp(self): def setUp(self):
# Subtract 1 second to help comparisons with file-modify time succeed, # Subtract 1 second to help comparisons with file-modify time succeed,
......
...@@ -10,7 +10,11 @@ namespace :i18n do ...@@ -10,7 +10,11 @@ namespace :i18n do
desc "Extract localizable strings from sources" desc "Extract localizable strings from sources"
task :extract => ["i18n:validate:gettext", "assets:coffee"] do task :extract => ["i18n:validate:gettext", "assets:coffee"] do
sh(File.join(REPO_ROOT, "i18n", "extract.py")) command = File.join(REPO_ROOT, "i18n", "extract.py")
if verbose == true
command += " -vv"
end
sh(command)
end end
desc "Compile localizable strings from sources, extracting strings first." desc "Compile localizable strings from sources, extracting strings first."
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment