From 561ecc7682235fe76fc30fdc9c06b0bb58829990 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 29 Jan 2014 16:24:51 -0500 Subject: [PATCH] Make i18n tests quieter --- i18n/dummy.py | 13 ++++++++----- i18n/execute.py | 4 ++-- i18n/extract.py | 34 ++++++++++++++++++++++++---------- i18n/generate.py | 5 +++-- i18n/tests/test_extract.py | 2 +- i18n/tests/test_generate.py | 4 ++-- rakelib/i18n.rake | 6 +++++- 7 files changed, 45 insertions(+), 23 deletions(-) diff --git a/i18n/dummy.py b/i18n/dummy.py index 27309fc342..481cfc54fd 100755 --- a/i18n/dummy.py +++ b/i18n/dummy.py @@ -22,7 +22,7 @@ $ ./dummy.py generates output conf/locale/$DUMMY_LOCALE/LC_MESSAGES, where $DUMMY_LOCALE is the dummy_locale value set in the i18n config """ - +from __future__ import print_function import re import sys @@ -197,17 +197,20 @@ def new_filename(original_filename, new_locale): return new_file.abspath() -def main(): +def main(verbosity=1): """ Generate dummy strings for all source po files. """ SOURCE_MSGS_DIR = CONFIGURATION.source_messages_dir 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'): - print ' ', source_file.relpath() + if verbosity: + print(' ', source_file.relpath()) make_dummy(SOURCE_MSGS_DIR.joinpath(source_file), locale, converter) - print + if verbosity: + print() if __name__ == '__main__': diff --git a/i18n/execute.py b/i18n/execute.py index 7a079ab1d6..0f672a2d73 100644 --- a/i18n/execute.py +++ b/i18n/execute.py @@ -4,7 +4,7 @@ from i18n.config import BASE_DIR 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. Command is a string to pass to the shell. @@ -12,7 +12,7 @@ def execute(command, working_directory=BASE_DIR): """ LOG.info("Executing in %s ...", working_directory) 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): diff --git a/i18n/extract.py b/i18n/extract.py index e6d3b642eb..28f798cff4 100755 --- a/i18n/extract.py +++ b/i18n/extract.py @@ -21,6 +21,7 @@ import os import os.path import logging import sys +import argparse from path import path from polib import pofile @@ -31,39 +32,48 @@ from i18n.segment import segment_pofiles EDX_MARKER = "edX translation file" - LOG = logging.getLogger(__name__) +DEVNULL = open(os.devnull, 'wb') def base(path1, *paths): """Return a relative path from BASE_DIR to path1 / paths[0] / ... """ return BASE_DIR.relpathto(path1.joinpath(*paths)) -def main(): + +def main(verbosity=1): logging.basicConfig(stream=sys.stdout, level=logging.INFO) create_dir_if_necessary(LOCALE_DIR) source_msgs_dir = CONFIGURATION.source_messages_dir remove_file(source_msgs_dir.joinpath('django.po')) # 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( + verbosity=babel_verbosity, config=base(LOCALE_DIR, 'babel_mako.cfg'), 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) if ignores: makemessages += " " + ignores # Extract strings from django source files, including .py files. 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. 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. # Rename it to django-partial.po to enable merging into django.po later. @@ -90,13 +100,14 @@ def main(): output_file = source_msgs_dir / (app_name + ".po") 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( + verbosity=babel_verbosity, config=LOCALE_DIR / 'babel_third_party.cfg', app=app_name, output=output_file, ) - execute(babel_cmd, working_directory=app_dir) + execute(babel_cmd, working_directory=app_dir, stderr=DEVNULL) # Segment the generated files. segmented_files = segment_pofiles("en") @@ -191,4 +202,7 @@ def is_key_string(string): return len(string) > 1 and string[0] == '_' if __name__ == '__main__': - main() + parser = argparse.ArgumentParser() + parser.add_argument('--verbose', '-v', action='count', default=0) + args = parser.parse_args() + main(verbosity=args.verbose) diff --git a/i18n/generate.py b/i18n/generate.py index d5d246d9a2..8880980da0 100755 --- a/i18n/generate.py +++ b/i18n/generate.py @@ -24,6 +24,7 @@ from i18n.config import BASE_DIR, CONFIGURATION from i18n.execute import execute LOG = logging.getLogger(__name__) +DEVNULL = open(os.devnull, "wb") def merge(locale, target='django.po', sources=('django-partial.po',), fail_if_missing=True): @@ -124,8 +125,8 @@ def main(argv=None): for locale in CONFIGURATION.dummy_locales: merge_files(locale, fail_if_missing=False) - compile_cmd = 'django-admin.py compilemessages' - execute(compile_cmd, working_directory=BASE_DIR) + compile_cmd = 'django-admin.py compilemessages -v0' + execute(compile_cmd, working_directory=BASE_DIR, stderr=DEVNULL) if __name__ == '__main__': diff --git a/i18n/tests/test_extract.py b/i18n/tests/test_extract.py index 7a15df7307..4293fbe661 100644 --- a/i18n/tests/test_extract.py +++ b/i18n/tests/test_extract.py @@ -33,7 +33,7 @@ class TestExtract(TestCase): super(TestExtract, self).setUp() if not SETUP_HAS_RUN: # Run extraction script. Warning, this takes 1 minute or more - extract.main() + extract.main(verbosity=0) SETUP_HAS_RUN = True def get_files(self): diff --git a/i18n/tests/test_generate.py b/i18n/tests/test_generate.py index ce93dce3ed..c9cb0c37e8 100644 --- a/i18n/tests/test_generate.py +++ b/i18n/tests/test_generate.py @@ -23,8 +23,8 @@ class TestGenerate(TestCase): @classmethod def setUpClass(cls): - extract.main() - dummy.main() + extract.main(verbosity=0) + dummy.main(verbosity=0) def setUp(self): # Subtract 1 second to help comparisons with file-modify time succeed, diff --git a/rakelib/i18n.rake b/rakelib/i18n.rake index c8546b47fb..6f2241de18 100644 --- a/rakelib/i18n.rake +++ b/rakelib/i18n.rake @@ -10,7 +10,11 @@ namespace :i18n do desc "Extract localizable strings from sources" 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 desc "Compile localizable strings from sources, extracting strings first."