Skip to content

Instantly share code, notes, and snippets.

@JacquesLucke
Created November 1, 2017 17:04
Show Gist options
  • Select an option

  • Save JacquesLucke/5b339d4fd9315d44aa527cfc2d9844c6 to your computer and use it in GitHub Desktop.

Select an option

Save JacquesLucke/5b339d4fd9315d44aa527cfc2d9844c6 to your computer and use it in GitHub Desktop.

Revisions

  1. JacquesLucke created this gist Nov 1, 2017.
    54 changes: 54 additions & 0 deletions setup.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    sources = [
    ("test1.pyx", []),
    ("test2.pyx", ["extra.c"])
    ]

    import os
    import sys

    def main():
    if cython_exists():
    cythonize_paths()
    compile_c()

    def cythonize_paths():
    pyx_sources = [source[0] for source in sources]

    from Cython.Build import cythonize
    for path in pyx_sources:
    cythonize(path)

    def cython_exists():
    try:
    import Cython
    return True
    except:
    return False

    def compile_c():
    c_source_groups = {}
    for source in sources:
    module_name = get_file_name_without_extension(source[0])
    source_files = [change_file_extension(source[0], ".c")] + source[1]
    c_source_groups[module_name] = source_files

    from distutils.core import Extension
    for module_name, c_file_paths in c_source_groups.items():
    extension = Extension(module_name, sources = c_file_paths)
    compile_extension(extension)

    def compile_extension(extension):
    from distutils.core import setup
    old_args = sys.argv
    sys.argv = [old_args[0], "build_ext", "--inplace"]
    setup(ext_modules = [extension])
    sys.argv = old_args

    def change_file_extension(path, newExtension):
    return os.path.splitext(path)[0] + newExtension

    def get_file_name_without_extension(path):
    return os.path.basename(os.path.splitext(path)[0])

    if __name__ == '__main__':
    main()