generate.py 2.87 KB
Newer Older
1
#!/usr/bin/env python
2 3 4 5 6 7 8 9 10 11 12 13 14 15

"""
 See https://edx-wiki.atlassian.net/wiki/display/ENG/PO+File+workflow


 This task merges and compiles the human-readable .pofiles on the 
 local filesystem into machine-readable .mofiles. This is typically
 necessary as part of the build process since these .mofiles are
 needed by Django when serving the web app.

 The configuration file (in mitx/conf/locale/config) specifies which
 languages to generate.
"""

Steve Strassmann committed
16
import os, sys, logging
17 18
from polib import pofile

19
from config import BASE_DIR, CONFIGURATION
20
from execute import execute
21

22 23 24
LOG = logging.getLogger(__name__)

def merge(locale, target='django.po', fail_if_missing=True):
25 26
    """
    For the given locale, merge django-partial.po, messages.po, mako.po -> django.po
27 28 29 30 31
    target is the resulting filename
    If fail_if_missing is True, and the files to be merged are missing,
    throw an Exception.
    If fail_if_missing is False, and the files to be merged are missing,
    just return silently.
32
    """
33
    LOG.info('Merging locale={0}'.format(locale))
34
    locale_directory = CONFIGURATION.get_messages_dir(locale)
35
    files_to_merge = ('django-partial.po', 'messages.po', 'mako.po')
36 37 38 39 40 41
    try:
        validate_files(locale_directory, files_to_merge)
    except Exception, e:
        if not fail_if_missing:
            return
        raise e
42 43 44 45 46

    # merged file is merged.po
    merge_cmd = 'msgcat -o merged.po ' + ' '.join(files_to_merge)
    execute(merge_cmd, working_directory=locale_directory)

47
    # clean up redunancies in the metadata
48
    merged_filename = locale_directory.joinpath('merged.po')
49 50 51
    clean_metadata(merged_filename)

    # rename merged.po -> django.po (default)
52
    django_filename = locale_directory.joinpath(target)
53 54
    os.rename(merged_filename, django_filename) # can't overwrite file on Windows

55 56 57 58 59
def clean_metadata(file):
    """
    Clean up redundancies in the metadata caused by merging.
    This reads in a PO file and simply saves it back out again.
    """
60
    pofile(file).save()
61

62 63 64 65
def validate_files(dir, files_to_merge):
    """
    Asserts that the given files exist.
    files_to_merge is a list of file names (no directories).
66
    dir is the directory (a path object from path.py) in which the files should appear.
67 68 69
    raises an Exception if any of the files are not in dir.
    """
    for path in files_to_merge:
70 71 72
        pathname = dir.joinpath(path)
        if not pathname.exists():
            raise Exception("I18N: Cannot generate because file not found: {0}".format(pathname))
73 74

def main ():
Steve Strassmann committed
75
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
76

77
    for locale in CONFIGURATION.locales:
78
        merge(locale)
79
    # Dummy text is not required. Don't raise exception if files are missing.
80
    merge(CONFIGURATION.dummy_locale, fail_if_missing=False)
81 82 83 84 85
    compile_cmd = 'django-admin.py compilemessages'
    execute(compile_cmd, working_directory=BASE_DIR)

if __name__ == '__main__':
    main()