compilemessages.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import codecs
  2. import concurrent.futures
  3. import glob
  4. import os
  5. from pathlib import Path
  6. from django.core.management.base import BaseCommand, CommandError
  7. from django.core.management.utils import (
  8. find_command, is_ignored_path, popen_wrapper,
  9. )
  10. def has_bom(fn):
  11. with fn.open('rb') as f:
  12. sample = f.read(4)
  13. return sample.startswith((codecs.BOM_UTF8, codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE))
  14. def is_writable(path):
  15. # Known side effect: updating file access/modified time to current time if
  16. # it is writable.
  17. try:
  18. with open(path, 'a'):
  19. os.utime(path, None)
  20. except OSError:
  21. return False
  22. return True
  23. class Command(BaseCommand):
  24. help = 'Compiles .po files to .mo files for use with builtin gettext support.'
  25. requires_system_checks = []
  26. program = 'msgfmt'
  27. program_options = ['--check-format']
  28. def add_arguments(self, parser):
  29. parser.add_argument(
  30. '--locale', '-l', action='append', default=[],
  31. help='Locale(s) to process (e.g. de_AT). Default is to process all. '
  32. 'Can be used multiple times.',
  33. )
  34. parser.add_argument(
  35. '--exclude', '-x', action='append', default=[],
  36. help='Locales to exclude. Default is none. Can be used multiple times.',
  37. )
  38. parser.add_argument(
  39. '--use-fuzzy', '-f', dest='fuzzy', action='store_true',
  40. help='Use fuzzy translations.',
  41. )
  42. parser.add_argument(
  43. '--ignore', '-i', action='append', dest='ignore_patterns',
  44. default=[], metavar='PATTERN',
  45. help='Ignore directories matching this glob-style pattern. '
  46. 'Use multiple times to ignore more.',
  47. )
  48. def handle(self, **options):
  49. locale = options['locale']
  50. exclude = options['exclude']
  51. ignore_patterns = set(options['ignore_patterns'])
  52. self.verbosity = options['verbosity']
  53. if options['fuzzy']:
  54. self.program_options = self.program_options + ['-f']
  55. if find_command(self.program) is None:
  56. raise CommandError("Can't find %s. Make sure you have GNU gettext "
  57. "tools 0.15 or newer installed." % self.program)
  58. basedirs = [os.path.join('conf', 'locale'), 'locale']
  59. if os.environ.get('DJANGO_SETTINGS_MODULE'):
  60. from django.conf import settings
  61. basedirs.extend(settings.LOCALE_PATHS)
  62. # Walk entire tree, looking for locale directories
  63. for dirpath, dirnames, filenames in os.walk('.', topdown=True):
  64. for dirname in dirnames:
  65. if is_ignored_path(os.path.normpath(os.path.join(dirpath, dirname)), ignore_patterns):
  66. dirnames.remove(dirname)
  67. elif dirname == 'locale':
  68. basedirs.append(os.path.join(dirpath, dirname))
  69. # Gather existing directories.
  70. basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs)))
  71. if not basedirs:
  72. raise CommandError("This script should be run from the Django Git "
  73. "checkout or your project or app tree, or with "
  74. "the settings module specified.")
  75. # Build locale list
  76. all_locales = []
  77. for basedir in basedirs:
  78. locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % basedir))
  79. all_locales.extend(map(os.path.basename, locale_dirs))
  80. # Account for excluded locales
  81. locales = locale or all_locales
  82. locales = set(locales).difference(exclude)
  83. self.has_errors = False
  84. for basedir in basedirs:
  85. if locales:
  86. dirs = [os.path.join(basedir, locale, 'LC_MESSAGES') for locale in locales]
  87. else:
  88. dirs = [basedir]
  89. locations = []
  90. for ldir in dirs:
  91. for dirpath, dirnames, filenames in os.walk(ldir):
  92. locations.extend((dirpath, f) for f in filenames if f.endswith('.po'))
  93. if locations:
  94. self.compile_messages(locations)
  95. if self.has_errors:
  96. raise CommandError('compilemessages generated one or more errors.')
  97. def compile_messages(self, locations):
  98. """
  99. Locations is a list of tuples: [(directory, file), ...]
  100. """
  101. with concurrent.futures.ThreadPoolExecutor() as executor:
  102. futures = []
  103. for i, (dirpath, f) in enumerate(locations):
  104. po_path = Path(dirpath) / f
  105. mo_path = po_path.with_suffix('.mo')
  106. try:
  107. if mo_path.stat().st_mtime >= po_path.stat().st_mtime:
  108. if self.verbosity > 0:
  109. self.stdout.write(
  110. 'File “%s” is already compiled and up to date.'
  111. % po_path
  112. )
  113. continue
  114. except FileNotFoundError:
  115. pass
  116. if self.verbosity > 0:
  117. self.stdout.write('processing file %s in %s' % (f, dirpath))
  118. if has_bom(po_path):
  119. self.stderr.write(
  120. 'The %s file has a BOM (Byte Order Mark). Django only '
  121. 'supports .po files encoded in UTF-8 and without any BOM.' % po_path
  122. )
  123. self.has_errors = True
  124. continue
  125. # Check writability on first location
  126. if i == 0 and not is_writable(mo_path):
  127. self.stderr.write(
  128. 'The po files under %s are in a seemingly not writable location. '
  129. 'mo files will not be updated/created.' % dirpath
  130. )
  131. self.has_errors = True
  132. return
  133. args = [self.program, *self.program_options, '-o', mo_path, po_path]
  134. futures.append(executor.submit(popen_wrapper, args))
  135. for future in concurrent.futures.as_completed(futures):
  136. output, errors, status = future.result()
  137. if status:
  138. if self.verbosity > 0:
  139. if errors:
  140. self.stderr.write("Execution of %s failed: %s" % (self.program, errors))
  141. else:
  142. self.stderr.write("Execution of %s failed" % self.program)
  143. self.has_errors = True