2010-10-11 00:38:28 +08:00
|
|
|
import codecs
|
2018-06-14 03:37:46 +08:00
|
|
|
import concurrent.futures
|
2014-03-24 21:03:06 +08:00
|
|
|
import glob
|
2008-07-06 14:39:44 +08:00
|
|
|
import os
|
2020-06-19 16:25:42 +08:00
|
|
|
from pathlib import Path
|
2013-02-13 00:58:49 +08:00
|
|
|
|
2008-07-26 11:01:33 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2019-01-28 03:35:17 +08:00
|
|
|
from django.core.management.utils import (
|
|
|
|
find_command, is_ignored_path, popen_wrapper,
|
|
|
|
)
|
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):
|
2020-06-19 16:25:42 +08:00
|
|
|
with fn.open('rb') as f:
|
2012-05-05 20:01:38 +08:00
|
|
|
sample = f.read(4)
|
2015-10-26 00:27:10 +08:00
|
|
|
return sample.startswith((codecs.BOM_UTF8, codecs.BOM_UTF16_LE, 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)
|
2019-01-28 23:01:35 +08:00
|
|
|
except OSError:
|
2014-01-05 05:26:24 +08:00
|
|
|
return False
|
|
|
|
return True
|
2008-07-06 14:39:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Compiles .po files to .mo files for use with builtin gettext support.'
|
|
|
|
|
2020-05-14 06:00:41 +08:00
|
|
|
requires_system_checks = []
|
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
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
def add_arguments(self, parser):
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--locale', '-l', action='append', default=[],
|
2014-06-07 04:39:33 +08:00
|
|
|
help='Locale(s) to process (e.g. de_AT). Default is to process all. '
|
2016-03-29 06:33:29 +08:00
|
|
|
'Can be used multiple times.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--exclude', '-x', action='append', default=[],
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Locales to exclude. Default is none. Can be used multiple times.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2017-04-02 08:03:56 +08:00
|
|
|
'--use-fuzzy', '-f', dest='fuzzy', action='store_true',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Use fuzzy translations.',
|
|
|
|
)
|
2019-01-28 03:35:17 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--ignore', '-i', action='append', dest='ignore_patterns',
|
|
|
|
default=[], metavar='PATTERN',
|
|
|
|
help='Ignore directories matching this glob-style pattern. '
|
|
|
|
'Use multiple times to ignore more.',
|
|
|
|
)
|
2014-06-07 04:39:33 +08:00
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
def handle(self, **options):
|
2016-03-03 10:01:36 +08:00
|
|
|
locale = options['locale']
|
|
|
|
exclude = options['exclude']
|
2019-01-28 03:35:17 +08:00
|
|
|
ignore_patterns = set(options['ignore_patterns'])
|
2016-03-03 10:01:36 +08:00
|
|
|
self.verbosity = options['verbosity']
|
|
|
|
if options['fuzzy']:
|
2014-11-16 06:56:37 +08:00
|
|
|
self.program_options = self.program_options + ['-f']
|
2014-01-05 05:26:24 +08:00
|
|
|
|
|
|
|
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
|
2017-01-20 21:01:02 +08:00
|
|
|
basedirs.extend(settings.LOCALE_PATHS)
|
2014-01-05 05:26:24 +08:00
|
|
|
|
2015-04-16 19:55:37 +08:00
|
|
|
# Walk entire tree, looking for locale directories
|
|
|
|
for dirpath, dirnames, filenames in os.walk('.', topdown=True):
|
|
|
|
for dirname in dirnames:
|
2019-01-28 03:35:17 +08:00
|
|
|
if is_ignored_path(os.path.normpath(os.path.join(dirpath, dirname)), ignore_patterns):
|
|
|
|
dirnames.remove(dirname)
|
|
|
|
elif dirname == 'locale':
|
2015-04-16 19:55:37 +08:00
|
|
|
basedirs.append(os.path.join(dirpath, dirname))
|
|
|
|
|
2014-01-05 05:26:24 +08:00
|
|
|
# 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
|
2017-07-29 22:16:43 +08:00
|
|
|
locales = set(locales).difference(exclude)
|
2014-03-24 21:03:06 +08:00
|
|
|
|
2018-06-06 17:24:25 +08:00
|
|
|
self.has_errors = False
|
2014-03-24 21:03:06 +08:00
|
|
|
for basedir in basedirs:
|
|
|
|
if locales:
|
2020-05-12 14:52:23 +08:00
|
|
|
dirs = [os.path.join(basedir, locale, 'LC_MESSAGES') for locale 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)
|
|
|
|
|
2018-06-06 17:24:25 +08:00
|
|
|
if self.has_errors:
|
|
|
|
raise CommandError('compilemessages generated one or more errors.')
|
|
|
|
|
2014-01-05 05:26:24 +08:00
|
|
|
def compile_messages(self, locations):
|
|
|
|
"""
|
|
|
|
Locations is a list of tuples: [(directory, file), ...]
|
|
|
|
"""
|
2018-06-14 03:37:46 +08:00
|
|
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
|
|
futures = []
|
|
|
|
for i, (dirpath, f) in enumerate(locations):
|
2020-06-19 16:25:42 +08:00
|
|
|
po_path = Path(dirpath) / f
|
|
|
|
mo_path = po_path.with_suffix('.mo')
|
2020-06-19 16:26:26 +08:00
|
|
|
try:
|
|
|
|
if mo_path.stat().st_mtime >= po_path.stat().st_mtime:
|
|
|
|
if self.verbosity > 0:
|
|
|
|
self.stdout.write(
|
|
|
|
'File “%s” is already compiled and up to date.'
|
|
|
|
% po_path
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
if self.verbosity > 0:
|
|
|
|
self.stdout.write('processing file %s in %s' % (f, dirpath))
|
2020-06-19 16:25:42 +08:00
|
|
|
|
2018-06-14 03:37:46 +08:00
|
|
|
if has_bom(po_path):
|
|
|
|
self.stderr.write(
|
|
|
|
'The %s file has a BOM (Byte Order Mark). Django only '
|
|
|
|
'supports .po files encoded in UTF-8 and without any BOM.' % po_path
|
|
|
|
)
|
|
|
|
self.has_errors = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Check writability on first location
|
2020-06-19 16:25:42 +08:00
|
|
|
if i == 0 and not is_writable(mo_path):
|
2018-06-14 03:37:46 +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
|
|
|
|
)
|
|
|
|
self.has_errors = True
|
|
|
|
return
|
|
|
|
|
2021-01-19 15:35:16 +08:00
|
|
|
args = [self.program, *self.program_options, '-o', mo_path, po_path]
|
2018-06-14 03:37:46 +08:00
|
|
|
futures.append(executor.submit(popen_wrapper, args))
|
|
|
|
|
|
|
|
for future in concurrent.futures.as_completed(futures):
|
|
|
|
output, errors, status = future.result()
|
|
|
|
if status:
|
|
|
|
if self.verbosity > 0:
|
|
|
|
if errors:
|
|
|
|
self.stderr.write("Execution of %s failed: %s" % (self.program, errors))
|
|
|
|
else:
|
|
|
|
self.stderr.write("Execution of %s failed" % self.program)
|
|
|
|
self.has_errors = True
|