Commit cf8d6b89 by Steve Strassmann

factor logging out of library calls

parent 0ba0bb5b
...@@ -76,4 +76,3 @@ class Configuration: ...@@ -76,4 +76,3 @@ class Configuration:
CONFIGURATION = Configuration(LOCALE_DIR.joinpath('config').normpath()) CONFIGURATION = Configuration(LOCALE_DIR.joinpath('config').normpath())
import os, subprocess, logging import os, subprocess
from config import CONFIGURATION, BASE_DIR
def get_default_logger(): from logger import get_logger
"""Returns a default logger""" from config import CONFIGURATION, BASE_DIR
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
log_handler = logging.StreamHandler()
log_handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s'))
log.addHandler(log_handler)
return log
LOG = get_default_logger()
LOG = get_logger(__name__)
def execute(command, working_directory=BASE_DIR, log=LOG): def execute(command, working_directory=BASE_DIR, log=LOG):
""" """
......
...@@ -18,8 +18,10 @@ See https://edx-wiki.atlassian.net/wiki/display/ENG/PO+File+workflow ...@@ -18,8 +18,10 @@ See https://edx-wiki.atlassian.net/wiki/display/ENG/PO+File+workflow
import os import os
from datetime import datetime from datetime import datetime
from polib import pofile from polib import pofile
from logger import get_logger
from config import BASE_DIR, LOCALE_DIR, CONFIGURATION from config import BASE_DIR, LOCALE_DIR, CONFIGURATION
from execute import execute, create_dir_if_necessary, remove_file, LOG from execute import execute, create_dir_if_necessary, remove_file
# BABEL_CONFIG contains declarations for Babel to extract strings from mako template files # BABEL_CONFIG contains declarations for Babel to extract strings from mako template files
# Use relpath to reduce noise in logs # Use relpath to reduce noise in logs
...@@ -32,6 +34,7 @@ BABEL_OUT = BASE_DIR.relpathto(CONFIGURATION.source_messages_dir.joinpath('mako. ...@@ -32,6 +34,7 @@ BABEL_OUT = BASE_DIR.relpathto(CONFIGURATION.source_messages_dir.joinpath('mako.
SOURCE_WARN = 'This English source file is machine-generated. Do not check it into github' SOURCE_WARN = 'This English source file is machine-generated. Do not check it into github'
def main (): def main ():
log = get_logger(__name__)
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
...@@ -60,7 +63,7 @@ def main (): ...@@ -60,7 +63,7 @@ def main ():
execute(make_djangojs_cmd, working_directory=BASE_DIR) execute(make_djangojs_cmd, working_directory=BASE_DIR)
for filename in generated_files: for filename in generated_files:
LOG.info('Cleaning %s' % filename) log.info('Cleaning %s' % filename)
po = pofile(source_msgs_dir.joinpath(filename)) po = pofile(source_msgs_dir.joinpath(filename))
# replace default headers with edX headers # replace default headers with edX headers
fix_header(po) fix_header(po)
......
...@@ -16,10 +16,11 @@ ...@@ -16,10 +16,11 @@
import os import os
from polib import pofile from polib import pofile
from logger import get_logger
from config import BASE_DIR, CONFIGURATION from config import BASE_DIR, CONFIGURATION
from execute import execute, remove_file, LOG from execute import execute, remove_file
def merge(locale, target='django.po', fail_if_missing=True): def merge(locale, target='django.po', fail_if_missing=True, log=None):
""" """
For the given locale, merge django-partial.po, messages.po, mako.po -> django.po For the given locale, merge django-partial.po, messages.po, mako.po -> django.po
target is the resulting filename target is the resulting filename
...@@ -28,7 +29,8 @@ def merge(locale, target='django.po', fail_if_missing=True): ...@@ -28,7 +29,8 @@ def merge(locale, target='django.po', fail_if_missing=True):
If fail_if_missing is False, and the files to be merged are missing, If fail_if_missing is False, and the files to be merged are missing,
just return silently. just return silently.
""" """
LOG.info('Merging locale={0}'.format(locale)) if log:
log.info('Merging locale={0}'.format(locale))
locale_directory = CONFIGURATION.get_messages_dir(locale) locale_directory = CONFIGURATION.get_messages_dir(locale)
files_to_merge = ('django-partial.po', 'messages.po', 'mako.po') files_to_merge = ('django-partial.po', 'messages.po', 'mako.po')
try: try:
...@@ -70,10 +72,12 @@ def validate_files(dir, files_to_merge): ...@@ -70,10 +72,12 @@ def validate_files(dir, files_to_merge):
raise Exception("I18N: Cannot generate because file not found: {0}".format(pathname)) raise Exception("I18N: Cannot generate because file not found: {0}".format(pathname))
def main (): def main ():
log = get_logger(__name__)
for locale in CONFIGURATION.locales: for locale in CONFIGURATION.locales:
merge(locale) merge(locale)
# Dummy text is not required. Don't raise exception if files are missing. # Dummy text is not required. Don't raise exception if files are missing.
merge(CONFIGURATION.dummy_locale, fail_if_missing=False) merge(CONFIGURATION.dummy_locale, fail_if_missing=False, log=log)
compile_cmd = 'django-admin.py compilemessages' compile_cmd = 'django-admin.py compilemessages'
execute(compile_cmd, working_directory=BASE_DIR) execute(compile_cmd, working_directory=BASE_DIR)
......
import logging
def get_logger(name):
"""
Returns a default logger.
logging.basicConfig does not render to the console
"""
log = logging.getLogger()
log.setLevel(logging.INFO)
log_handler = logging.StreamHandler()
log_handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s'))
log.addHandler(log_handler)
return log
...@@ -2,24 +2,27 @@ import os ...@@ -2,24 +2,27 @@ import os
from unittest import TestCase from unittest import TestCase
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from logger import get_logger
from config import LOCALE_DIR from config import LOCALE_DIR
from execute import call, LOG from execute import call
def test_po_files(): def test_po_files(root=LOCALE_DIR):
""" """
This is a generator. It yields all of the .po files under root, and tests each one. This is a generator. It yields all of the .po files under root, and tests each one.
""" """
for (dirpath, dirnames, filenames) in os.walk(LOCALE_DIR): log = get_logger(__name__)
for (dirpath, dirnames, filenames) in os.walk(root):
for name in filenames: for name in filenames:
print name print name
(base, ext) = os.path.splitext(name) (base, ext) = os.path.splitext(name)
if ext.lower() == '.po': if ext.lower() == '.po':
yield validate_po_file, os.path.join(dirpath, name) yield validate_po_file, os.path.join(dirpath, name), log
def validate_po_file(filename): def validate_po_file(filename, log):
""" """
Call GNU msgfmt -c on each .po file to validate its format. Call GNU msgfmt -c on each .po file to validate its format.
Any errors caught by msgfmt are logged to log.
""" """
# Skip this test for now because it's very noisy # Skip this test for now because it's very noisy
raise SkipTest() raise SkipTest()
...@@ -27,5 +30,5 @@ def validate_po_file(filename): ...@@ -27,5 +30,5 @@ def validate_po_file(filename):
rfile = os.path.relpath(filename, LOCALE_DIR) rfile = os.path.relpath(filename, LOCALE_DIR)
(out, err) = call(['msgfmt','-c', rfile], log=None, working_directory=LOCALE_DIR) (out, err) = call(['msgfmt','-c', rfile], log=None, working_directory=LOCALE_DIR)
if err != '': if err != '':
LOG.warn('\n'+err) log.warn('\n'+err)
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