Skip to content

Commit

Permalink
Refs #31692 -- Updated compilemessages and tests to use pathlib.
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep authored and felixxm committed Jun 22, 2020
1 parent f386454 commit ed0a040
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
14 changes: 7 additions & 7 deletions django/core/management/commands/compilemessages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import concurrent.futures
import glob
import os
from pathlib import Path

from django.core.management.base import BaseCommand, CommandError
from django.core.management.utils import (
Expand All @@ -10,7 +11,7 @@


def has_bom(fn):
with open(fn, 'rb') as f:
with fn.open('rb') as f:
sample = f.read(4)
return sample.startswith((codecs.BOM_UTF8, codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE))

Expand Down Expand Up @@ -123,28 +124,27 @@ def compile_messages(self, locations):
for i, (dirpath, f) in enumerate(locations):
if self.verbosity > 0:
self.stdout.write('processing file %s in %s' % (f, dirpath))
po_path = os.path.join(dirpath, f)
po_path = Path(dirpath) / f
mo_path = po_path.with_suffix('.mo')

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
base_path = os.path.splitext(po_path)[0]

# Check writability on first location
if i == 0 and not is_writable(base_path + '.mo'):
if i == 0 and not is_writable(mo_path):
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

args = [self.program] + self.program_options + [
'-o', base_path + '.mo', base_path + '.po'
]
args = [self.program, *self.program_options, '-o', mo_path, po_path]
futures.append(executor.submit(popen_wrapper, args))

for future in concurrent.futures.as_completed(futures):
Expand Down
11 changes: 6 additions & 5 deletions tests/i18n/test_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PoFileTests(MessageCompilationTests):

LOCALE = 'es_AR'
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
MO_FILE_EN = 'locale/en/LC_MESSAGES/django.mo'

def test_bom_rejection(self):
stderr = StringIO()
Expand All @@ -43,17 +44,17 @@ def test_bom_rejection(self):
self.assertFalse(os.path.exists(self.MO_FILE))

def test_no_write_access(self):
mo_file_en = 'locale/en/LC_MESSAGES/django.mo'
mo_file_en = Path(self.MO_FILE_EN)
err_buffer = StringIO()
# put file in read-only mode
old_mode = os.stat(mo_file_en).st_mode
os.chmod(mo_file_en, stat.S_IREAD)
# Put file in read-only mode.
old_mode = mo_file_en.stat().st_mode
mo_file_en.chmod(stat.S_IREAD)
try:
with self.assertRaisesMessage(CommandError, 'compilemessages generated one or more errors.'):
call_command('compilemessages', locale=['en'], stderr=err_buffer, verbosity=0)
self.assertIn('not writable location', err_buffer.getvalue())
finally:
os.chmod(mo_file_en, old_mode)
mo_file_en.chmod(old_mode)


class PoFileContentsTests(MessageCompilationTests):
Expand Down

0 comments on commit ed0a040

Please sign in to comment.