This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
precompile_templates.py
116 lines (103 loc) · 3.77 KB
/
precompile_templates.py
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
#!/usr/bin/python
"""Script to pre-compile chameleon templates to the cache.
This script is useful if the time to compile chameleon templates is
unacceptably long. It finds and compiles all templates within a directory,
saving the result in the cache configured via the CHAMELEON_CACHE environment
variable.
"""
# This script is copied from: https://github.com/Pylons/pyramid_chameleon
import logging
import optparse
import os
import sys
import chameleon.config
from pyramid_chameleon.zpt import PyramidPageTemplateFile
def compile_one(fullpath, template_factory=PyramidPageTemplateFile):
assert chameleon.config.CACHE_DIRECTORY is not None
template = template_factory(fullpath, macro=None)
template.cook_check()
def walk_dir(
directory,
extensions=frozenset(['.pt']),
template_factory=PyramidPageTemplateFile,
fail_fast=False):
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
if filename.startswith('.'):
continue
_, ext = os.path.splitext(filename)
if ext not in extensions:
continue
fullpath = os.path.join(dirpath, filename)
try:
compile_one(fullpath, template_factory=template_factory)
except Exception:
if fail_fast:
raise
yield dict(
path=fullpath,
success=False)
logging.warn('Failed to compile: %s' % fullpath)
continue
logging.debug('Compiled: %s' % fullpath)
yield dict(
path=fullpath,
success=True)
def precompile(argv=sys.argv):
parser = optparse.OptionParser(usage="""usage: %prog [options]
Compile chameleon templates, saving the results in the chameleon cache.
The CACHE_DIRECTORY environment variable MUST be set to the directory where the
templates will be stored.
By default the exit code of this script will be 0 if one template was found and
compiled.
""")
parser.add_option(
"--fail-fast",
dest="fail_fast",
default=False,
action="store_true",
help="Exit with non-zero exit code on the first "
"template which fails compillation.")
parser.add_option(
"--dir",
dest="dir",
help="The directory to search for tempaltes. "
"Will be recursively searched")
parser.add_option(
"--ext",
dest="exts",
action="append",
help="The file extensions to search for, "
"can be specified more than once."
"The default is to look only for the .pt extension.")
parser.add_option(
"--loglevel",
dest="loglevel",
help="set the loglevel, see the logging module for possible values",
default='INFO')
options, args = parser.parse_args(argv)
loglevel = getattr(logging, options.loglevel)
logging.basicConfig(level=loglevel)
if chameleon.config.CACHE_DIRECTORY is None:
logging.error('The CHAMELEON_CACHE environment variable must be specified')
return 1
if len(args) > 1:
msg = ' '.join(args[1:])
logging.error('This command takes only keyword arguments, got: %s' % msg)
return 1
exts = options.exts
if not exts:
exts = ['.pt']
exts = set(exts)
success = total = 0
for f in walk_dir(options.dir, extensions=exts, fail_fast=options.fail_fast):
total += 1
if f['success']:
success += 1
logging.info('Compiled %s out of %s found templates' % (success, total))
if not success:
logging.error("No templates successfully compiled out of %s found" % total)
return 1
return 0
if __name__ == '__main__':
sys.exit(precompile())