~canonical-django/canonical-django/project-template

« back to all changes in this revision

Viewing changes to trunk/python-packages/django/core/management/commands/compilemessages.py

  • Committer: Matthew Nuzum
  • Date: 2008-11-13 05:46:03 UTC
  • Revision ID: matthew.nuzum@canonical.com-20081113054603-v0kvr6z6xyexvqt3
adding to version control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import sys
 
3
from optparse import make_option
 
4
from django.core.management.base import BaseCommand, CommandError
 
5
 
 
6
try:
 
7
    set
 
8
except NameError:
 
9
    from sets import Set as set     # For Python 2.3
 
10
 
 
11
def compile_messages(locale=None):
 
12
    basedirs = [os.path.join('conf', 'locale'), 'locale']
 
13
    if os.environ.get('DJANGO_SETTINGS_MODULE'):
 
14
        from django.conf import settings
 
15
        basedirs.extend(settings.LOCALE_PATHS)
 
16
 
 
17
    # Gather existing directories.
 
18
    basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs)))
 
19
 
 
20
    if not basedirs:
 
21
        raise CommandError("This script should be run from the Django SVN tree or your project or app tree, or with the settings module specified.")
 
22
 
 
23
    for basedir in basedirs:
 
24
        if locale:
 
25
            basedir = os.path.join(basedir, locale, 'LC_MESSAGES')
 
26
        for dirpath, dirnames, filenames in os.walk(basedir):
 
27
            for f in filenames:
 
28
                if f.endswith('.po'):
 
29
                    sys.stderr.write('processing file %s in %s\n' % (f, dirpath))
 
30
                    pf = os.path.splitext(os.path.join(dirpath, f))[0]
 
31
                    # Store the names of the .mo and .po files in an environment
 
32
                    # variable, rather than doing a string replacement into the
 
33
                    # command, so that we can take advantage of shell quoting, to
 
34
                    # quote any malicious characters/escaping.
 
35
                    # See http://cyberelk.net/tim/articles/cmdline/ar01s02.html
 
36
                    os.environ['djangocompilemo'] = pf + '.mo'
 
37
                    os.environ['djangocompilepo'] = pf + '.po'
 
38
                    if sys.platform == 'win32': # Different shell-variable syntax
 
39
                        cmd = 'msgfmt --check-format -o "%djangocompilemo%" "%djangocompilepo%"'
 
40
                    else:
 
41
                        cmd = 'msgfmt --check-format -o "$djangocompilemo" "$djangocompilepo"'
 
42
                    os.system(cmd)
 
43
 
 
44
 
 
45
class Command(BaseCommand):
 
46
    option_list = BaseCommand.option_list + (
 
47
        make_option('--locale', '-l', dest='locale',
 
48
            help='The locale to process. Default is to process all.'),
 
49
    )
 
50
    help = 'Compiles .po files to .mo files for use with builtin gettext support.'
 
51
 
 
52
    requires_model_validation = False
 
53
    can_import_settings = False
 
54
 
 
55
    def handle(self, **options):
 
56
        locale = options.get('locale')
 
57
        compile_messages(locale)