URL: https://github.com/freeipa/freeipa/pull/1795
Author: tiran
 Title: #1795: [Backport][ipa-4-6] Defer import of ipaclient.csrgen
Action: opened

PR body:
"""
This PR was opened automatically because PR #1779 was pushed to master and 
backport to ipa-4-6 is required.
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/1795/head:pr1795
git checkout pr1795
From 927830c1b788792fa6748925081fc2f5b052f36a Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Thu, 5 Apr 2018 08:35:15 +0200
Subject: [PATCH] Defer import of ipaclient.csrgen

The modules ipaclient.csrgen and ipaclient.csrgen_ffi are expensive to load,
but rarely used. On demand loading speeds up ipa CLI by about 200ms.

Fixes: https://pagure.io/freeipa/issue/7484
Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipaclient/plugins/cert.py   |  5 ++++-
 ipaclient/plugins/csrgen.py | 12 +++++-------
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/ipaclient/plugins/cert.py b/ipaclient/plugins/cert.py
index 50549cf590..edc132dfae 100644
--- a/ipaclient/plugins/cert.py
+++ b/ipaclient/plugins/cert.py
@@ -23,7 +23,6 @@
 
 import six
 
-from ipaclient import csrgen
 from ipaclient.frontend import MethodOverride
 from ipalib import errors
 from ipalib import x509
@@ -111,6 +110,10 @@ def forward(self, csr=None, **options):
         password_file = options.pop('password_file', None)
 
         if csr is None:
+            # Deferred import, ipaclient.csrgen is expensive to load.
+            # see https://pagure.io/freeipa/issue/7484
+            from ipaclient import csrgen
+
             if database:
                 adaptor = csrgen.NSSAdaptor(database, password_file)
             elif private_key:
diff --git a/ipaclient/plugins/csrgen.py b/ipaclient/plugins/csrgen.py
index 01c75f4bcc..5aad63605f 100644
--- a/ipaclient/plugins/csrgen.py
+++ b/ipaclient/plugins/csrgen.py
@@ -16,13 +16,6 @@
 from ipalib.text import _
 from ipapython import dogtag
 
-try:
-    import jinja2  # pylint: disable=unused-import
-except ImportError:
-    raise errors.SkipPluginModule(reason=_("jinja2 is not installed."))
-else:
-    from ipaclient import csrgen
-    from ipaclient import csrgen_ffi
 
 if six.PY3:
     unicode = str
@@ -79,6 +72,11 @@ class cert_get_requestdata(Local):
     )
 
     def execute(self, *args, **options):
+        # Deferred import, ipaclient.csrgen is expensive to load.
+        # see https://pagure.io/freeipa/issue/7484
+        from ipaclient import csrgen
+        from ipaclient import csrgen_ffi
+
         if 'out' in options:
             util.check_writable_file(options['out'])
 
_______________________________________________
FreeIPA-devel mailing list -- freeipa-devel@lists.fedorahosted.org
To unsubscribe send an email to freeipa-devel-le...@lists.fedorahosted.org

Reply via email to