From a629d1d8b90bf98392741a15f6f617d51d305618 Mon Sep 17 00:00:00 2001 From: Diego Rabatone Oliveira Date: Mon, 24 Apr 2017 17:38:29 -0300 Subject: [PATCH 1/3] Add Jinja2 to sys.path and load it on setup. FIX kytos/kytos#370 --- setup.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e7f3c4665..287b5b127 100644 --- a/setup.py +++ b/setup.py @@ -9,6 +9,7 @@ from abc import abstractmethod # Disabling checks due to https://github.com/PyCQA/pylint/issues/73 from distutils.command.clean import clean # pylint: disable=E0401,E0611 +from pathlib import Path, PurePosixPath from subprocess import call from setuptools import Command, find_packages, setup @@ -96,6 +97,26 @@ def run(self): class CommonInstall: """Class with common method used by children classes.""" + @staticmethod + def add_jinja2_to_path(): + """Add Jinja2 into sys.path. + + As Jinja2 may be installed during this setup, it will not be available + on runtime to be used here. So, we need to look for it and append it on + the sys.path. + """ + #: First we find the 'site_pkg' directory + site_pkg = None + for path in sys.path: + if PurePosixPath(path).stem == 'site-packages': + site_pkg = Path(path) + break + + #: Then we get the 'Jinja2' egg directory and append it into the + #: current sys.path. + jinja2path = site_pkg.glob('Jinja2*').__next__() + sys.path.append(str(jinja2path)) + @staticmethod def generate_file_from_template(templates, destination=os.path.dirname(__file__), @@ -110,7 +131,9 @@ def generate_file_from_template(templates, destination (string): Directory in which the config file will be placed. """ - from jinja2 import Template + CommonInstall.add_jinja2_to_path() + from jinja2 import Template # pylint: disable=import-error + for path in templates: with open(path, 'r', encoding='utf-8') as src_file: content = Template(src_file.read()).render(**kwargs) From d8a210df169b45e746680c053b4fb06160c0f90e Mon Sep 17 00:00:00 2001 From: Diego Rabatone Oliveira Date: Tue, 25 Apr 2017 10:03:52 -0300 Subject: [PATCH 2/3] Adrressing issues pointed out by @cemsbr --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 287b5b127..fb07fe717 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ from abc import abstractmethod # Disabling checks due to https://github.com/PyCQA/pylint/issues/73 from distutils.command.clean import clean # pylint: disable=E0401,E0611 -from pathlib import Path, PurePosixPath +from pathlib import Path from subprocess import call from setuptools import Command, find_packages, setup @@ -108,13 +108,13 @@ def add_jinja2_to_path(): #: First we find the 'site_pkg' directory site_pkg = None for path in sys.path: - if PurePosixPath(path).stem == 'site-packages': + if Path(path).stem == 'site-packages': site_pkg = Path(path) break #: Then we get the 'Jinja2' egg directory and append it into the #: current sys.path. - jinja2path = site_pkg.glob('Jinja2*').__next__() + jinja2path = next(site_pkg.glob('Jinja2*')) sys.path.append(str(jinja2path)) @staticmethod From f3bb20274a3ab0d6313f98ad708e52697ee72f27 Mon Sep 17 00:00:00 2001 From: Diego Rabatone Oliveira Date: Tue, 25 Apr 2017 10:17:32 -0300 Subject: [PATCH 3/3] Adding a try/except to jinja import --- setup.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index fb07fe717..7bf5ec9c8 100644 --- a/setup.py +++ b/setup.py @@ -131,8 +131,11 @@ def generate_file_from_template(templates, destination (string): Directory in which the config file will be placed. """ - CommonInstall.add_jinja2_to_path() - from jinja2 import Template # pylint: disable=import-error + try: + from jinja2 import Template # pylint: disable=import-error + except ImportError: + CommonInstall.add_jinja2_to_path() + from jinja2 import Template # pylint: disable=import-error for path in templates: with open(path, 'r', encoding='utf-8') as src_file: