mirror of
https://github.com/django/django.git
synced 2025-01-10 18:36:05 +00:00
Refs #31692 -- Updated compilemessages and tests to use pathlib.
This commit is contained in:
parent
f386454d13
commit
ed0a040773
@ -2,6 +2,7 @@ import codecs
|
|||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand, CommandError
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
from django.core.management.utils import (
|
from django.core.management.utils import (
|
||||||
@ -10,7 +11,7 @@ from django.core.management.utils import (
|
|||||||
|
|
||||||
|
|
||||||
def has_bom(fn):
|
def has_bom(fn):
|
||||||
with open(fn, 'rb') as f:
|
with fn.open('rb') as f:
|
||||||
sample = f.read(4)
|
sample = f.read(4)
|
||||||
return sample.startswith((codecs.BOM_UTF8, codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE))
|
return sample.startswith((codecs.BOM_UTF8, codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE))
|
||||||
|
|
||||||
@ -123,7 +124,9 @@ class Command(BaseCommand):
|
|||||||
for i, (dirpath, f) in enumerate(locations):
|
for i, (dirpath, f) in enumerate(locations):
|
||||||
if self.verbosity > 0:
|
if self.verbosity > 0:
|
||||||
self.stdout.write('processing file %s in %s' % (f, dirpath))
|
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):
|
if has_bom(po_path):
|
||||||
self.stderr.write(
|
self.stderr.write(
|
||||||
'The %s file has a BOM (Byte Order Mark). Django only '
|
'The %s file has a BOM (Byte Order Mark). Django only '
|
||||||
@ -131,10 +134,9 @@ class Command(BaseCommand):
|
|||||||
)
|
)
|
||||||
self.has_errors = True
|
self.has_errors = True
|
||||||
continue
|
continue
|
||||||
base_path = os.path.splitext(po_path)[0]
|
|
||||||
|
|
||||||
# Check writability on first location
|
# 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(
|
self.stderr.write(
|
||||||
'The po files under %s are in a seemingly not writable location. '
|
'The po files under %s are in a seemingly not writable location. '
|
||||||
'mo files will not be updated/created.' % dirpath
|
'mo files will not be updated/created.' % dirpath
|
||||||
@ -142,9 +144,7 @@ class Command(BaseCommand):
|
|||||||
self.has_errors = True
|
self.has_errors = True
|
||||||
return
|
return
|
||||||
|
|
||||||
args = [self.program] + self.program_options + [
|
args = [self.program, *self.program_options, '-o', mo_path, po_path]
|
||||||
'-o', base_path + '.mo', base_path + '.po'
|
|
||||||
]
|
|
||||||
futures.append(executor.submit(popen_wrapper, args))
|
futures.append(executor.submit(popen_wrapper, args))
|
||||||
|
|
||||||
for future in concurrent.futures.as_completed(futures):
|
for future in concurrent.futures.as_completed(futures):
|
||||||
|
@ -34,6 +34,7 @@ class PoFileTests(MessageCompilationTests):
|
|||||||
|
|
||||||
LOCALE = 'es_AR'
|
LOCALE = 'es_AR'
|
||||||
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
||||||
|
MO_FILE_EN = 'locale/en/LC_MESSAGES/django.mo'
|
||||||
|
|
||||||
def test_bom_rejection(self):
|
def test_bom_rejection(self):
|
||||||
stderr = StringIO()
|
stderr = StringIO()
|
||||||
@ -43,17 +44,17 @@ class PoFileTests(MessageCompilationTests):
|
|||||||
self.assertFalse(os.path.exists(self.MO_FILE))
|
self.assertFalse(os.path.exists(self.MO_FILE))
|
||||||
|
|
||||||
def test_no_write_access(self):
|
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()
|
err_buffer = StringIO()
|
||||||
# put file in read-only mode
|
# Put file in read-only mode.
|
||||||
old_mode = os.stat(mo_file_en).st_mode
|
old_mode = mo_file_en.stat().st_mode
|
||||||
os.chmod(mo_file_en, stat.S_IREAD)
|
mo_file_en.chmod(stat.S_IREAD)
|
||||||
try:
|
try:
|
||||||
with self.assertRaisesMessage(CommandError, 'compilemessages generated one or more errors.'):
|
with self.assertRaisesMessage(CommandError, 'compilemessages generated one or more errors.'):
|
||||||
call_command('compilemessages', locale=['en'], stderr=err_buffer, verbosity=0)
|
call_command('compilemessages', locale=['en'], stderr=err_buffer, verbosity=0)
|
||||||
self.assertIn('not writable location', err_buffer.getvalue())
|
self.assertIn('not writable location', err_buffer.getvalue())
|
||||||
finally:
|
finally:
|
||||||
os.chmod(mo_file_en, old_mode)
|
mo_file_en.chmod(old_mode)
|
||||||
|
|
||||||
|
|
||||||
class PoFileContentsTests(MessageCompilationTests):
|
class PoFileContentsTests(MessageCompilationTests):
|
||||||
|
Loading…
Reference in New Issue
Block a user