|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import pathlib |
| 5 | +import sys |
| 6 | + |
| 7 | +# Always prefer setuptools over distutils |
| 8 | +from setuptools import Extension, setup |
| 9 | +import Cython.Compiler.Options |
| 10 | +from Cython.Build import cythonize |
| 11 | +from Cython.Distutils import build_ext |
| 12 | + |
| 13 | +logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s") |
| 14 | + |
| 15 | + |
| 16 | +class cythonizer: |
| 17 | + def __init__(self) -> None: |
| 18 | + self.__extension: list = [] |
| 19 | + self.__args = self.__Arguments() |
| 20 | + self.__Checking() |
| 21 | + self.__Compiling() |
| 22 | + # self.__Cleanup() |
| 23 | + |
| 24 | + # Argumebts Parser ↓ |
| 25 | + def __Arguments(self): |
| 26 | + parser = argparse.ArgumentParser( |
| 27 | + description="A script that will attempt to automatically convert one or more .py and .pyx files into the corresponding compiled .pyd or .so binary modules files." |
| 28 | + ) |
| 29 | + |
| 30 | + # Taking Files ↓ |
| 31 | + parser.add_argument( |
| 32 | + "filenames", type=pathlib.Path, nargs="+", help=".py and .pyx files only" |
| 33 | + ) |
| 34 | + |
| 35 | + # Cython Annotation ↓ |
| 36 | + parser.add_argument( |
| 37 | + "--annotation", |
| 38 | + default=False, |
| 39 | + action="store_true", |
| 40 | + help="(default: False)", |
| 41 | + ) |
| 42 | + |
| 43 | + # Numpy Includes ↓ |
| 44 | + parser.add_argument( |
| 45 | + "--numpy-includes", |
| 46 | + default=False, |
| 47 | + action="store_true", |
| 48 | + help="(default: False)", |
| 49 | + ) |
| 50 | + |
| 51 | + # Cython Debugmode ↓ |
| 52 | + parser.add_argument( |
| 53 | + "--debugmode", |
| 54 | + default=False, |
| 55 | + action="store_true", |
| 56 | + help="(default: False)", |
| 57 | + ) |
| 58 | + |
| 59 | + return parser.parse_args().__dict__ |
| 60 | + |
| 61 | + # Received File Checking ↓ |
| 62 | + def __Checking(self): |
| 63 | + for file in self.__args.get("filenames"): |
| 64 | + if not os.path.exists(file): |
| 65 | + logging.error(f'"{file}" doesn\'t exist.') |
| 66 | + sys.exit(-1) |
| 67 | + if not os.path.isfile(file): |
| 68 | + logging.error(f'"{file}" is not a file') |
| 69 | + sys.exit(-1) |
| 70 | + if os.path.splitext(file)[-1] not in (".py", ".pyx"): |
| 71 | + logging.error(f'"{file}" is not a valid file') |
| 72 | + sys.exit(-1) |
| 73 | + |
| 74 | + # Includes Directory ↓ |
| 75 | + def __Includes_Dir(self): |
| 76 | + # Extra include folders. Mainly for numpy. |
| 77 | + if self.__args.get("numpy_includes"): |
| 78 | + try: |
| 79 | + import numpy as np |
| 80 | + |
| 81 | + except ModuleNotFoundError: |
| 82 | + logging.error("Numpy is required, but not found. Please install it") |
| 83 | + sys.exit(-1) |
| 84 | + return [np.get_include()] |
| 85 | + else: |
| 86 | + return [] |
| 87 | + |
| 88 | + # Cython Compilation ↓ |
| 89 | + def __Compiling(self): |
| 90 | + Cython.Compiler.Options.annotate = self.__args.get("annotation") |
| 91 | + |
| 92 | + ext_modules: list = [] |
| 93 | + for _ in self.__args.get("filenames"): |
| 94 | + file: str = str(_) |
| 95 | + # The name must be plain, no path |
| 96 | + module_name = os.path.basename(file) |
| 97 | + module_name = os.path.splitext(module_name)[0] |
| 98 | + ext_modules.append( |
| 99 | + Extension( |
| 100 | + module_name, [file], extra_compile_args=["-O2", "-march=native"] |
| 101 | + ) |
| 102 | + ) |
| 103 | + |
| 104 | + sys.argv = [sys.argv[0], "build_ext", "--inplace"] |
| 105 | + |
| 106 | + setup( |
| 107 | + cmdclass={"build_ext": build_ext}, |
| 108 | + include_dirs=self.__Includes_Dir(), |
| 109 | + ext_modules=cythonize(module_list=ext_modules, language_level="3"), |
| 110 | + ) |
| 111 | + |
| 112 | + def __Cleanup(self): |
| 113 | + # Delete intermediate C files. |
| 114 | + for _ in self.__args.get("filenames"): |
| 115 | + filename = str(_) |
| 116 | + filename = f"{filename}.c" |
| 117 | + if os.path.exists(filename): |
| 118 | + os.remove(filename) |
| 119 | + |
| 120 | + |
| 121 | +def main(): |
| 122 | + try: |
| 123 | + cythonizer() |
| 124 | + except Exception as e: |
| 125 | + logging.error(e) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + print("Hello World") |
0 commit comments