Debian Codegraph @

django/core/management/commands/compilemessages.py

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