2012-08-09 05:40:20 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-10-11 00:38:28 +08:00
|
|
|
import codecs
|
2014-03-24 21:03:06 +08:00
|
|
|
import glob
|
2008-07-06 14:39:44 +08:00
|
|
|
import os
|
|
|
|
from optparse import make_option
|
2013-02-13 00:58:49 +08:00
|
|
|
|
2008-07-26 11:01:33 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2013-02-13 03:50:47 +08:00
|
|
|
from django.core.management.utils import find_command, popen_wrapper
|
2013-11-11 18:49:48 +08:00
|
|
|
from django.utils._os import npath, upath
|
2008-07-06 14:39:44 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-10-11 00:38:28 +08:00
|
|
|
def has_bom(fn):
|
2012-05-26 02:37:38 +08:00
|
|
|
with open(fn, 'rb') as f:
|
2012-05-05 20:01:38 +08:00
|
|
|
sample = f.read(4)
|
2012-08-09 05:40:20 +08:00
|
|
|
return sample[:3] == b'\xef\xbb\xbf' or \
|
2013-12-13 04:23:24 +08:00
|
|
|
sample.startswith(codecs.BOM_UTF16_LE) or \
|
|
|
|
sample.startswith(codecs.BOM_UTF16_BE)
|
2010-10-11 00:38:28 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2014-01-05 05:26:24 +08:00
|
|
|
def is_writable(path):
|
|
|
|
# Known side effect: updating file access/modified time to current time if
|
|
|
|
# it is writable.
|
|
|
|
try:
|
|
|
|
with open(path, 'a'):
|
|
|
|
os.utime(path, None)
|
|
|
|
except (IOError, OSError):
|
|
|
|
return False
|
|
|
|
return True
|
2008-07-06 14:39:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
option_list = BaseCommand.option_list + (
|
2014-03-24 21:03:06 +08:00
|
|
|
make_option('--locale', '-l', dest='locale', action='append', default=[],
|
|
|
|
help='Locale(s) to process (e.g. de_AT). Default is to process all. Can be '
|
|
|
|
'used multiple times.'),
|
2014-05-01 15:03:24 +08:00
|
|
|
make_option('--exclude', '-x', dest='exclude', action='append', default=[],
|
2014-03-24 21:03:06 +08:00
|
|
|
help='Locales to exclude. Default is none. Can be used multiple times.'),
|
2008-07-06 14:39:44 +08:00
|
|
|
)
|
|
|
|
help = 'Compiles .po files to .mo files for use with builtin gettext support.'
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
requires_system_checks = False
|
2013-02-04 07:53:48 +08:00
|
|
|
leave_locale_alone = True
|
2014-04-23 21:13:15 +08:00
|
|
|
|
2014-01-05 05:26:24 +08:00
|
|
|
program = 'msgfmt'
|
2014-04-23 21:13:15 +08:00
|
|
|
program_options = ['--check-format']
|
2008-07-06 14:39:44 +08:00
|
|
|
|
|
|
|
def handle(self, **options):
|
|
|
|
locale = options.get('locale')
|
2014-03-24 21:03:06 +08:00
|
|
|
exclude = options.get('exclude')
|
2014-01-05 05:26:24 +08:00
|
|
|
self.verbosity = int(options.get('verbosity'))
|
|
|
|
|
|
|
|
if find_command(self.program) is None:
|
|
|
|
raise CommandError("Can't find %s. Make sure you have GNU gettext "
|
|
|
|
"tools 0.15 or newer installed." % self.program)
|
|
|
|
|
|
|
|
basedirs = [os.path.join('conf', 'locale'), 'locale']
|
|
|
|
if os.environ.get('DJANGO_SETTINGS_MODULE'):
|
|
|
|
from django.conf import settings
|
|
|
|
basedirs.extend([upath(path) for path in settings.LOCALE_PATHS])
|
|
|
|
|
|
|
|
# Gather existing directories.
|
|
|
|
basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs)))
|
|
|
|
|
|
|
|
if not basedirs:
|
|
|
|
raise CommandError("This script should be run from the Django Git "
|
|
|
|
"checkout or your project or app tree, or with "
|
|
|
|
"the settings module specified.")
|
|
|
|
|
2014-03-24 21:03:06 +08:00
|
|
|
# Build locale list
|
|
|
|
all_locales = []
|
2014-01-05 05:26:24 +08:00
|
|
|
for basedir in basedirs:
|
2014-03-24 21:03:06 +08:00
|
|
|
locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % basedir))
|
|
|
|
all_locales.extend(map(os.path.basename, locale_dirs))
|
|
|
|
|
|
|
|
# Account for excluded locales
|
|
|
|
locales = locale or all_locales
|
|
|
|
locales = set(locales) - set(exclude)
|
|
|
|
|
|
|
|
for basedir in basedirs:
|
|
|
|
if locales:
|
|
|
|
dirs = [os.path.join(basedir, l, 'LC_MESSAGES') for l in locales]
|
2014-01-05 05:26:24 +08:00
|
|
|
else:
|
|
|
|
dirs = [basedir]
|
|
|
|
locations = []
|
|
|
|
for ldir in dirs:
|
|
|
|
for dirpath, dirnames, filenames in os.walk(ldir):
|
|
|
|
locations.extend((dirpath, f) for f in filenames if f.endswith('.po'))
|
|
|
|
if locations:
|
|
|
|
self.compile_messages(locations)
|
|
|
|
|
|
|
|
def compile_messages(self, locations):
|
|
|
|
"""
|
|
|
|
Locations is a list of tuples: [(directory, file), ...]
|
|
|
|
"""
|
|
|
|
for i, (dirpath, f) in enumerate(locations):
|
|
|
|
if self.verbosity > 0:
|
|
|
|
self.stdout.write('processing file %s in %s\n' % (f, dirpath))
|
|
|
|
po_path = os.path.join(dirpath, f)
|
|
|
|
if has_bom(po_path):
|
|
|
|
raise CommandError("The %s file has a BOM (Byte Order Mark). "
|
|
|
|
"Django only supports .po files encoded in "
|
|
|
|
"UTF-8 and without any BOM." % po_path)
|
|
|
|
base_path = os.path.splitext(po_path)[0]
|
|
|
|
|
|
|
|
# Check writability on first location
|
|
|
|
if i == 0 and not is_writable(npath(base_path + '.mo')):
|
2014-03-24 21:03:06 +08:00
|
|
|
self.stderr.write("The po files under %s are in a seemingly not writable location. "
|
|
|
|
"mo files will not be updated/created." % dirpath)
|
2014-01-05 05:26:24 +08:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:13:15 +08:00
|
|
|
args = [self.program] + self.program_options + ['-o',
|
2014-01-05 05:26:24 +08:00
|
|
|
npath(base_path + '.mo'), npath(base_path + '.po')]
|
|
|
|
output, errors, status = popen_wrapper(args)
|
|
|
|
if status:
|
|
|
|
if errors:
|
|
|
|
msg = "Execution of %s failed: %s" % (self.program, errors)
|
|
|
|
else:
|
|
|
|
msg = "Execution of %s failed" % self.program
|
|
|
|
raise CommandError(msg)
|