annotate piecrust/processing/requirejs.py @ 215:a47580a0955b

bake: Better error handling for the processing pipeline. Pipeline jobs now keep track of whether they've seen any errors. This is aggregated into an overall "success" flag for the processing record. Also, jobs keep going as long as there's no critical (i.e. internal) failure happening. Errors raised by processors are also better tracked: the actual processor that failed, along with the input file, are tracks in the processing record. The `bake` command returns a failure exit code if processing saw any error.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 31 Jan 2015 17:08:02 -0800
parents e5f048799d61
children 4850f8c21b6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
117
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import json
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import hashlib
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import logging
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 import platform
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 import subprocess
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 from piecrust.processing.base import Processor, PRIORITY_FIRST
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 from piecrust.processing.tree import FORCE_BUILD
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 logger = logging.getLogger(__name__)
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 class RequireJSProcessor(Processor):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 PROCESSOR_NAME = 'requirejs'
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 def __init__(self):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 super(RequireJSProcessor, self).__init__()
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 self.is_bypassing_structured_processing = True
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 self._conf = None
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 def initialize(self, app):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 super(RequireJSProcessor, self).initialize(app)
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 self._conf = app.config.get('requirejs')
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 if self._conf is None:
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 return
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 if 'build_path' not in self._conf:
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 raise Exception("You need to specify `requirejs/build_path` "
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 "for RequireJS.")
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 self._conf.setdefault('bin', 'r.js')
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 self._conf.setdefault('out_path', self._conf['build_path'])
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 def onPipelineStart(self, pipeline):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 super(RequireJSProcessor, self).onPipelineStart(pipeline)
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
118
e5f048799d61 Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents: 117
diff changeset
39 if self._conf is None:
e5f048799d61 Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents: 117
diff changeset
40 return
e5f048799d61 Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents: 117
diff changeset
41
117
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 logger.debug("Adding Javascript suppressor to build pipeline.")
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 skip = _JavascriptSkipProcessor(self._conf['build_path'])
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 pipeline.processors.append(skip)
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 def matches(self, path):
118
e5f048799d61 Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents: 117
diff changeset
47 if self._conf is None:
e5f048799d61 Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents: 117
diff changeset
48 return False
117
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 return path == self._conf['build_path']
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 def getDependencies(self, path):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 return FORCE_BUILD
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 def process(self, path, out_dir):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 args = [self._conf['bin'], '-o', path]
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 shell = (platform.system() == 'Windows')
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 cwd = self.app.root_dir
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 logger.debug("Running RequireJS: %s" % ' '.join(args))
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 try:
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 retcode = subprocess.call(args, shell=shell, cwd=cwd)
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 except FileNotFoundError as ex:
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 logger.error("Tried running RequireJS processor "
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 "with command: %s" % args)
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 raise Exception("Error running RequireJS. "
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 "Did you install it?") from ex
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 if retcode != 0:
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 raise Exception("Error occured in RequireJS compiler. "
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 "Please check log messages above for "
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 "more information.")
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 return True
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 class _JavascriptSkipProcessor(Processor):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 PROCESSOR_NAME = 'requirejs_javascript_skip'
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 def __init__(self, except_path=None):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 super(_JavascriptSkipProcessor, self).__init__()
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 self.priority = PRIORITY_FIRST
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 self.is_bypassing_structured_processing = True
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 self._except_path = except_path
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 def matches(self, path):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 _, ext = os.path.splitext(path)
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 return ext == '.js' and path != self._except_path
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 def process(self, in_path, out_path):
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 return False
6827dcc9d3fb Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88