diff --git a/sklearn/_build_utils/__init__.py b/sklearn/_build_utils/__init__.py index a2e69c6978efe..1d6c1eaf607a6 100644 --- a/sklearn/_build_utils/__init__.py +++ b/sklearn/_build_utils/__init__.py @@ -75,3 +75,30 @@ def maybe_cythonize_extensions(top_path, config): nthreads=n_jobs, compile_time_env={'SKLEARN_OPENMP_SUPPORTED': with_openmp}, compiler_directives={'language_level': 3}) + + +def gen_from_templates(templates, top_path): + """Generate cython files from a list of templates""" + is_release = os.path.exists(os.path.join(top_path, 'PKG-INFO')) + # Files are already cythonized, nothing to do. + if is_release: + return + + # Lazy import because cython is not a dependency when building from + # source distribution. + from Cython import Tempita # noqa + + for template in templates: + outfile = template.replace('.tp', '') + + # if the template is not updated, no need to output the cython file + if not (os.path.exists(outfile) and + os.stat(template).st_mtime < os.stat(outfile).st_mtime): + + with open(template, "r") as f: + tmpl = f.read() + + tmpl_ = Tempita.sub(tmpl) + + with open(outfile, "w") as f: + f.write(tmpl_) diff --git a/sklearn/linear_model/setup.py b/sklearn/linear_model/setup.py index b7ef0a7aa39f5..121b449d673d0 100644 --- a/sklearn/linear_model/setup.py +++ b/sklearn/linear_model/setup.py @@ -1,7 +1,9 @@ import os - import numpy +from sklearn._build_utils import gen_from_templates + + def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration @@ -22,19 +24,8 @@ def configuration(parent_package='', top_path=None): libraries=libraries) # generate sag_fast from template - sag_cython_file = 'sklearn/linear_model/_sag_fast.pyx.tp' - sag_file = sag_cython_file.replace('.tp', '') - - if not (os.path.exists(sag_file) and - os.stat(sag_cython_file).st_mtime < os.stat(sag_file).st_mtime): - - with open(sag_cython_file, "r") as f: - tmpl = f.read() - from Cython import Tempita # noqa - tmpl_ = Tempita.sub(tmpl) - - with open(sag_file, "w") as f: - f.write(tmpl_) + templates = ['sklearn/linear_model/_sag_fast.pyx.tp'] + gen_from_templates(templates, top_path) config.add_extension('_sag_fast', sources=['_sag_fast.pyx'], diff --git a/sklearn/utils/setup.py b/sklearn/utils/setup.py index 370bd20e01d69..098adeeccab09 100644 --- a/sklearn/utils/setup.py +++ b/sklearn/utils/setup.py @@ -1,11 +1,12 @@ import os from os.path import join +from sklearn._build_utils import gen_from_templates + def configuration(parent_package='', top_path=None): import numpy from numpy.distutils.misc_util import Configuration - from Cython import Tempita config = Configuration('utils', parent_package, top_path) @@ -45,23 +46,10 @@ def configuration(parent_package='', top_path=None): sources=['_openmp_helpers.pyx'], libraries=libraries) - # generate files from a template - pyx_templates = ['sklearn/utils/_seq_dataset.pyx.tp', - 'sklearn/utils/_seq_dataset.pxd.tp'] - - for pyxfiles in pyx_templates: - outfile = pyxfiles.replace('.tp', '') - # if .pyx.tp is not updated, no need to output .pyx - if (os.path.exists(outfile) and - os.stat(pyxfiles).st_mtime < os.stat(outfile).st_mtime): - continue - - with open(pyxfiles, "r") as f: - tmpl = f.read() - pyxcontent = Tempita.sub(tmpl) - - with open(outfile, "w") as f: - f.write(pyxcontent) + # generate _seq_dataset from template + templates = ['sklearn/utils/_seq_dataset.pyx.tp', + 'sklearn/utils/_seq_dataset.pxd.tp'] + gen_from_templates(templates, top_path) config.add_extension('_seq_dataset', sources=['_seq_dataset.pyx'],