Skip to content

Instantly share code, notes, and snippets.

@JacquesLucke
Created November 1, 2017 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacquesLucke/5b339d4fd9315d44aa527cfc2d9844c6 to your computer and use it in GitHub Desktop.
Save JacquesLucke/5b339d4fd9315d44aa527cfc2d9844c6 to your computer and use it in GitHub Desktop.
Simple setup.py that also checks if cython exists or not. Also allows for extra .c files.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment