Mercurial > piecrust2
annotate piecrust/processing/requirejs.py @ 117:6827dcc9d3fb
Changes to the asset processing pipeline:
* Add semi-functional RequireJS processor.
* Processors now match on the relative path.
* Support for processors that add more processors of their own.
* A couple of related fixes.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 28 Oct 2014 08:20:38 -0700 |
parents | |
children | e5f048799d61 |
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 |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 logger.debug("Adding Javascript suppressor to build pipeline.") |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 skip = _JavascriptSkipProcessor(self._conf['build_path']) |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 pipeline.processors.append(skip) |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 def matches(self, path): |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 return path == self._conf['build_path'] |
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 getDependencies(self, path): |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 return FORCE_BUILD |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 def process(self, path, out_dir): |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 args = [self._conf['bin'], '-o', path] |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 shell = (platform.system() == 'Windows') |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 cwd = self.app.root_dir |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 logger.debug("Running RequireJS: %s" % ' '.join(args)) |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 try: |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 retcode = subprocess.call(args, shell=shell, cwd=cwd) |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 except FileNotFoundError as ex: |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 logger.error("Tried running RequireJS processor " |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 "with command: %s" % args) |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 raise Exception("Error running RequireJS. " |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 "Did you install it?") from ex |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 if retcode != 0: |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 raise Exception("Error occured in RequireJS compiler. " |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 "Please check log messages above for " |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 "more information.") |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 return True |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 class _JavascriptSkipProcessor(Processor): |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 PROCESSOR_NAME = 'requirejs_javascript_skip' |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 def __init__(self, except_path=None): |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 super(_JavascriptSkipProcessor, self).__init__() |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 self.priority = PRIORITY_FIRST |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 self.is_bypassing_structured_processing = True |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 self._except_path = except_path |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 def matches(self, path): |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 _, ext = os.path.splitext(path) |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 return ext == '.js' and path != self._except_path |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 def process(self, in_path, out_path): |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 return False |
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 |