summaryrefslogtreecommitdiff
path: root/compressor/management/commands/compress.py
blob: 23c35a2b162b7d6ade8462e0ad81535a7ae1c948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# flake8: noqa
import os
import sys
import concurrent.futures
from threading import Lock

from collections import OrderedDict, defaultdict
from fnmatch import fnmatch
from importlib import import_module

import django
from django.core.management.base import BaseCommand, CommandError
import django.template
from django.template import Context
from django.utils.encoding import smart_str
from django.template.loader import (
    get_template,
)  # noqa Leave this in to preload template locations
from django.template import engines

from compressor.cache import (
    get_offline_hexdigest,
    write_offline_manifest,
    get_offline_manifest,
)
from compressor.conf import settings
from compressor.exceptions import (
    OfflineGenerationError,
    TemplateSyntaxError,
    TemplateDoesNotExist,
)
from compressor.utils import get_mod_func

offline_manifest_lock = Lock()


class Command(BaseCommand):
    help = "Compress content outside of the request/response cycle"

    if django.VERSION >= (3, 2):
        requires_system_checks = []
    else:
        requires_system_checks = False

    def add_arguments(self, parser):
        parser.add_argument(
            "--extension",
            "-e",
            action="append",
            dest="extensions",
            help='The file extension(s) to examine (default: ".html", '
            "separate multiple extensions with commas, or use -e "
            "multiple times)",
        )
        parser.add_argument(
            "-f",
            "--force",
            default=False,
            action="store_true",
            help="Force the generation of compressed content even if the "
            "COMPRESS_ENABLED setting is not True.",
            dest="force",
        )
        parser.add_argument(
            "--follow-links",
            default=False,
            action="store_true",
            help="Follow symlinks when traversing the COMPRESS_ROOT "
            "(which defaults to STATIC_ROOT). Be aware that using this "
            "can lead to infinite recursion if a link points to a parent "
            "directory of itself.",
            dest="follow_links",
        )
        parser.add_argument(
            "--engine",
            default=[],
            action="append",
            help="Specifies the templating engine. jinja2 and django are "
            "supported. It may be a specified more than once for "
            "multiple engines. If not specified, django engine is used.",
            dest="engines",
        )

    def get_loaders(self):
        template_source_loaders = []
        for e in engines.all():
            if hasattr(e, "engine"):
                template_source_loaders.extend(
                    e.engine.get_template_loaders(e.engine.loaders)
                )
        loaders = []
        # If template loader is CachedTemplateLoader, return the loaders
        # that it wraps around. So if we have
        # TEMPLATE_LOADERS = (
        #    ('django.template.loaders.cached.Loader', (
        #        'django.template.loaders.filesystem.Loader',
        #        'django.template.loaders.app_directories.Loader',
        #    )),
        # )
        # The loaders will return django.template.loaders.filesystem.Loader
        # and django.template.loaders.app_directories.Loader
        # The cached Loader and similar ones include a 'loaders' attribute
        # so we look for that.
        for loader in template_source_loaders:
            if hasattr(loader, "loaders"):
                loaders.extend(loader.loaders)
            else:
                loaders.append(loader)
        return loaders

    def __get_parser(self, engine):
        charset = (
            settings.FILE_CHARSET if settings.is_overridden("FILE_CHARSET") else "utf-8"
        )
        if engine == "jinja2":
            from compressor.offline.jinja2 import Jinja2Parser

            env = settings.COMPRESS_JINJA2_GET_ENVIRONMENT()
            parser = Jinja2Parser(charset=charset, env=env)
        elif engine == "django":
            from compressor.offline.django import DjangoParser

            parser = DjangoParser(charset=charset)
        else:
            raise OfflineGenerationError("Invalid templating engine specified.")

        return parser

    def compress(self, engine, extensions, verbosity, follow_links, log):
        """
        Searches templates containing 'compress' nodes and compresses them
        "offline" -- outside of the request/response cycle.

        The result is cached with a cache-key derived from the content of the
        compress nodes (not the content of the possibly linked files!).
        """

        if not self.get_loaders():
            raise OfflineGenerationError(
                "No template loaders defined. You "
                "must set TEMPLATE_LOADERS in your "
                "settings or set 'loaders' in your "
                "TEMPLATES dictionary."
            )
        templates = set()
        if engine == "django":
            paths = set()
            for loader in self.get_loaders():
                try:
                    module = import_module(loader.__module__)
                    get_template_sources = getattr(module, "get_template_sources", None)
                    if get_template_sources is None:
                        get_template_sources = loader.get_template_sources
                    paths.update(
                        smart_str(origin) for origin in get_template_sources("")
                    )
                except (ImportError, AttributeError, TypeError):
                    # Yeah, this didn't work out so well, let's move on
                    pass

            if not paths:
                raise OfflineGenerationError(
                    "No template paths found. None of "
                    "the configured template loaders "
                    "provided template paths. See "
                    "https://docs.djangoproject.com/en/2.1/topics/templates/ "
                    "for more information on template "
                    "loaders."
                )
            if verbosity >= 2:
                log.write("Considering paths:\n\t" + "\n\t".join(paths) + "\n")

            for path in paths:
                for root, dirs, files in os.walk(path, followlinks=follow_links):
                    templates.update(
                        os.path.relpath(os.path.join(root, name), path)
                        for name in files
                        if not name.startswith(".")
                        and any(fnmatch(name, "*%s" % glob) for glob in extensions)
                    )
        elif engine == "jinja2":
            env = settings.COMPRESS_JINJA2_GET_ENVIRONMENT()
            if env and hasattr(env, "list_templates"):
                templates |= set(
                    [
                        env.loader.get_source(env, template)[1]
                        for template in env.list_templates(
                            filter_func=lambda _path: os.path.splitext(_path)[-1]
                            in extensions
                        )
                    ]
                )

        if not templates:
            raise OfflineGenerationError(
                "No templates found. Make sure your "
                "TEMPLATE_LOADERS and TEMPLATE_DIRS "
                "settings are correct."
            )
        if verbosity >= 2:
            log.write("Found templates:\n\t" + "\n\t".join(templates) + "\n")

        contexts = settings.COMPRESS_OFFLINE_CONTEXT
        if isinstance(contexts, str):
            try:
                module, function = get_mod_func(contexts)
                contexts = getattr(import_module(module), function)()
            except (AttributeError, ImportError, TypeError) as e:
                raise ImportError(
                    "Couldn't import offline context function %s: %s"
                    % (settings.COMPRESS_OFFLINE_CONTEXT, e)
                )
        elif not isinstance(contexts, (list, tuple)):
            contexts = [contexts]

        parser = self.__get_parser(engine)
        fine_templates = []

        if verbosity >= 1:
            log.write("Compressing... ")

        for template_name in templates:
            try:
                template = parser.parse(template_name)
                template.template_name = template_name
                fine_templates.append(template)
            except IOError:  # unreadable file -> ignore
                if verbosity >= 1:
                    log.write("Unreadable template at: %s\n" % template_name)
                continue
            except TemplateSyntaxError as e:  # broken template -> ignore
                if verbosity >= 1:
                    log.write(
                        "Invalid template %s: %s\n" % (template_name, smart_str(e))
                    )
                continue
            except TemplateDoesNotExist:  # non existent template -> ignore
                if verbosity >= 1:
                    log.write("Non-existent template at: %s\n" % template_name)
                continue
            except UnicodeDecodeError:
                if verbosity >= 1:
                    log.write(
                        "UnicodeDecodeError while trying to read "
                        "template %s\n" % template_name
                    )
                continue

        contexts_count = 0
        nodes_count = 0
        offline_manifest = OrderedDict()
        errors = []

        for context_dict in contexts:
            compressor_nodes = OrderedDict()
            for template in fine_templates:
                context = Context(parser.get_init_context(context_dict))

                try:
                    nodes = list(parser.walk_nodes(template, context=context))
                except (TemplateDoesNotExist, TemplateSyntaxError) as e:
                    # Could be an error in some base template
                    if verbosity >= 1:
                        log.write(
                            "Error parsing template %s: %s\n"
                            % (template.template_name, smart_str(e))
                        )
                    continue

                if nodes:
                    template_nodes = compressor_nodes.setdefault(
                        template, OrderedDict()
                    )
                    for node in nodes:
                        nodes_count += 1
                        template_nodes.setdefault(node, []).append(context)

            pool = concurrent.futures.ThreadPoolExecutor(max_workers=4)
            for template, nodes in compressor_nodes.items():
                template._log = log
                template._log_verbosity = verbosity

                pool.submit(
                    self._compress_template,
                    offline_manifest,
                    nodes,
                    parser,
                    template,
                    errors,
                )

            pool.shutdown(wait=True)
            contexts_count += 1

        # If errors exist, raise the first one in the list
        if errors:
            raise errors[0]
        elif not nodes_count:
            raise OfflineGenerationError(
                "No 'compress' template tags found in templates."
                "Try running compress command with --follow-links and/or"
                "--extension=EXTENSIONS"
            )

        if verbosity >= 1:
            log.write(
                "done\nCompressed %d block(s) from %d template(s) for %d context(s).\n"
                % (len(offline_manifest), nodes_count, contexts_count)
            )
        return offline_manifest, len(offline_manifest), offline_manifest.values()

    @staticmethod
    def _compress_template(offline_manifest, nodes, parser, template, errors):
        for node, node_contexts in nodes.items():
            for context in node_contexts:
                context.push()
                if not parser.process_template(template, context):
                    continue

                parser.process_node(template, context, node)
                rendered = parser.render_nodelist(template, context, node)
                key = get_offline_hexdigest(rendered)

                # Atomically check if the key exists in offline manifest.
                # If it doesn't, set a placeholder key (None). This is to prevent
                # concurrent _compress_template instances from rendering the
                # same node, and then writing to the same file.
                with offline_manifest_lock:
                    if key in offline_manifest:
                        continue

                    offline_manifest[key] = None

                try:
                    result = parser.render_node(template, context, node)
                except Exception as e:
                    errors.append(
                        CommandError(
                            "An error occurred during rendering %s: "
                            "%s" % (template.template_name, smart_str(e))
                        )
                    )
                    del offline_manifest[key]
                    return
                result = result.replace(
                    settings.COMPRESS_URL, settings.COMPRESS_URL_PLACEHOLDER
                )
                offline_manifest[key] = result
                context.pop()

    def handle_extensions(self, extensions=("html",)):
        """
        organizes multiple extensions that are separated with commas or
        passed by using --extension/-e multiple times.

        for example: running 'django-admin compress -e js,txt -e xhtml -a'
        would result in an extension list: ['.js', '.txt', '.xhtml']

        >>> handle_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py'])
        ['.html', '.js']
        >>> handle_extensions(['.html, txt,.tpl'])
        ['.html', '.tpl', '.txt']
        """
        ext_list = []
        for ext in extensions:
            ext_list.extend(ext.replace(" ", "").split(","))
        for i, ext in enumerate(ext_list):
            if not ext.startswith("."):
                ext_list[i] = ".%s" % ext_list[i]
        return set(ext_list)

    def handle(self, **options):
        self.handle_inner(**options)

    def handle_inner(self, **options):
        if not settings.COMPRESS_ENABLED and not options.get("force"):
            raise CommandError(
                "Compressor is disabled. Set the COMPRESS_ENABLED "
                "setting or use --force to override."
            )
        if not settings.COMPRESS_OFFLINE:
            if not options.get("force"):
                raise CommandError(
                    "Offline compression is disabled. Set "
                    "COMPRESS_OFFLINE or use the --force to override."
                )

        log = options.get("log", sys.stdout)
        verbosity = options.get("verbosity", 1)
        follow_links = options.get("follow_links", False)
        extensions = self.handle_extensions(options.get("extensions") or ["html"])
        engines = [e.strip() for e in options.get("engines", [])] or ["django"]

        final_offline_manifest = {}
        final_block_count = 0
        final_results = []
        for engine in engines:
            offline_manifest, block_count, results = self.compress(
                engine, extensions, verbosity, follow_links, log
            )
            final_results.extend(results)
            final_block_count += block_count
            final_offline_manifest.update(offline_manifest)
        write_offline_manifest(final_offline_manifest)
        return final_block_count, final_results