summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2019-12-28 14:43:23 -0800
committerGitHub <noreply@github.com>2019-12-28 14:43:23 -0800
commit4d6462247eedee6a57053dd791f8292593e6f54f (patch)
tree2c81d684173060ce37bc8da12d3590ef4c5acecd /lib
parent2dafeaf81995614374a3da5c6b3fb3cf5d96771b (diff)
downloadspack-4d6462247eedee6a57053dd791f8292593e6f54f.tar.gz
spack-4d6462247eedee6a57053dd791f8292593e6f54f.tar.bz2
spack-4d6462247eedee6a57053dd791f8292593e6f54f.tar.xz
spack-4d6462247eedee6a57053dd791f8292593e6f54f.zip
externals: avoid importing jinja2 on startup (#14308)
Jinja2 costs a tenth to a few tenths of a second to import, so we should avoid importing it on startup. - [x] only import jinja2 within functions
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/modules/common.py3
-rw-r--r--lib/spack/spack/tengine.py8
2 files changed, 6 insertions, 5 deletions
diff --git a/lib/spack/spack/modules/common.py b/lib/spack/spack/modules/common.py
index 2e0090a449..5cacea561a 100644
--- a/lib/spack/spack/modules/common.py
+++ b/lib/spack/spack/modules/common.py
@@ -804,10 +804,11 @@ class BaseModuleFileWriter(object):
# Get the template for the module
template_name = self._get_template()
+ import jinja2
try:
env = tengine.make_environment()
template = env.get_template(template_name)
- except tengine.TemplateNotFound:
+ except jinja2.TemplateNotFound:
# If the template was not found raise an exception with a little
# more information
msg = 'template \'{0}\' was not found for \'{1}\''
diff --git a/lib/spack/spack/tengine.py b/lib/spack/spack/tengine.py
index 6aceb391cd..f872ea49ad 100644
--- a/lib/spack/spack/tengine.py
+++ b/lib/spack/spack/tengine.py
@@ -5,7 +5,6 @@
import itertools
import textwrap
-import jinja2
import llnl.util.lang
import six
@@ -13,9 +12,6 @@ import spack.config
from spack.util.path import canonicalize_path
-TemplateNotFound = jinja2.TemplateNotFound
-
-
class ContextMeta(type):
"""Meta class for Context. It helps reducing the boilerplate in
client code.
@@ -77,6 +73,10 @@ def make_environment(dirs=None):
dirs = [canonicalize_path(d)
for d in itertools.chain(builtins, extensions)]
+ # avoid importing this at the top level as it's used infrequently and
+ # slows down startup a bit.
+ import jinja2
+
# Loader for the templates
loader = jinja2.FileSystemLoader(dirs)
# Environment of the template engine