From 02ab805cbc87e622810922aab8a290e4d3c9c936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Jena?= Date: Tue, 20 Sep 2022 22:16:59 +0200 Subject: [PATCH] simplifiy setup.py --- .github/workflows/build.yaml | 39 +++++++ pyoptools/raytrace/mat_lib/mat_eq.py | 2 +- pyoptools/wavefront/field/field.pyx | 7 +- pyproject.toml | 3 + setup.py | 150 +++------------------------ 5 files changed, 60 insertions(+), 141 deletions(-) create mode 100644 .github/workflows/build.yaml create mode 100644 pyproject.toml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 00000000..e6f100d8 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,39 @@ +name: Build Package + +on: push + +jobs: + build-wheels: + name: Build wheels on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-20.04, windows-2019, macos-11] + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.10" + - name: Install dependencies + run: | + python -m pip install --upgrade pip setuptools wheel twine + pip install Cython==0.29.30 --install-option="--no-cython-compile" + pip install numpy==1.21.6 cibuildwheel + - name: Build sdist + run: python setup.py sdist --formats=gztar,zip + - name: Build wheels + run: | + python -m cibuildwheel --output-dir dist/ + env: + CIBW_BUILD: "cp37* cp38* cp39* cp310*" + CIBW_SKIP: "*_i686 *musllinux* pp*" + - name: Upload Distributions + uses: actions/upload-artifact@v3 + with: + name: dist + path: | + dist/*.whl + dist/*.tar.gz + dist/*.zip + retention-days: 7 diff --git a/pyoptools/raytrace/mat_lib/mat_eq.py b/pyoptools/raytrace/mat_lib/mat_eq.py index e9c4edd7..d159981e 100644 --- a/pyoptools/raytrace/mat_lib/mat_eq.py +++ b/pyoptools/raytrace/mat_lib/mat_eq.py @@ -188,7 +188,7 @@ def from_yml(filename): """Create a material instance from a YML file as defined at https://refractiveindex.info/about """ - with open(filename) as f: + with open(filename, encoding='utf-8') as f: mat = yaml.load(f, Loader=yaml.FullLoader) for c in mat["DATA"]: diff --git a/pyoptools/wavefront/field/field.pyx b/pyoptools/wavefront/field/field.pyx index 8706967e..14fb58cb 100644 --- a/pyoptools/wavefront/field/field.pyx +++ b/pyoptools/wavefront/field/field.pyx @@ -50,9 +50,6 @@ from pyoptools.misc.cmisc.cmisc import unwrap cdef extern from "math.h": double M_PI double sqrt(double) -cdef extern from "complex.h": - double complex cexp(double complex) - double complex I cimport numpy as np cimport cython @@ -307,8 +304,8 @@ cdef class Field: R2=xd*xd+yd*yd+z*z R=sqrt(R2) R3=R2*R - ikR=I*k*R - result[i, j]=((z/(2.*M_PI*R3))*cexp(ikR)*(1.-ikR)) + ikR=1j*k*R + result[i, j]=((z/(2.*M_PI*R3))*exp(ikR)*(1.-ikR)) return result diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..29c1e9e3 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools", "wheel", "numpy==1.21.6", "Cython==0.29.30"] +build-backend = "setuptools.build_meta" diff --git a/setup.py b/setup.py index f48c1d99..65bd6666 100644 --- a/setup.py +++ b/setup.py @@ -1,140 +1,29 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- - import sys -import os -from setuptools import setup -from setuptools.extension import Extension - -try: - from Cython.Build import cythonize - from Cython.Distutils import build_ext -except ImportError: - print("You don't seem to have Cython installed. Please get a") - print("copy from www.cython.org and install it") - sys.exit(1) - - -# Look for paths containing arrayobject.h -# Necessary for non-unix systems -def contains_arrayobject_h(path): - """ - Returns True if the python path string contains the arrayobject.h - include file where it is supposed to be. - """ - f = False - try: - _s = os.stat( - os.path.join(path, "numpy", "core", "include", "numpy", "arrayobject.h") - ) - f = True - except OSError: - pass - return f - - -# scan the directory for extension files, converting -# them to extension names in dotted notation -def scandir(dir_, files=[]): - for file in os.listdir(dir_): - if file == "venv": - continue - path = os.path.join(dir_, file) - if os.path.isfile(path) and path.endswith(".pyx"): - files.append(path.replace(os.path.sep, ".")[2:-4]) - elif os.path.isdir(path): - scandir(path, files) - return files - - -def findpackages(dir_, files=[]): - for file in os.listdir(dir_): - if file in ["build", "venv"]: - continue - path = os.path.join(dir_, file) - if os.path.isdir(path): # and path.endswith(".py"): - for file1 in os.listdir(path): - if file1 == "__init__.py": - files.append(path.replace(os.path.sep, ".")[2:]) - findpackages(path, files) - return files - - -# generate an Extension object from its dotted name -def makeExtension(extName): - extPath = extName.replace(".", os.path.sep) + ".pyx" - return Extension( - extName, - [extPath], - # adding the '.' to include_dirs is CRUCIAL!! - include_dirs=[".", include_numpy_array], - extra_compile_args=["-O3", "-Wall"], - # extra_link_args = ['-g'], - # libraries = ["dv",], - ) - - -# Check the availability of arrayobject.h -valid_paths = list(filter(contains_arrayobject_h, sys.path)) -if len(valid_paths) == 0: - print("No paths in the python path contain numpy/arrayobject.h") - sys.exit(0) - -# The base path is by default the first python path with arrayobject.h in it. -include_numpy_array = valid_paths[0] -if len(valid_paths) > 1: - print( - "There are several valid include directories" "containing numpy/arrayobject.h" - ) - l = [("%d: %s" % (i + 1, valid_paths[i])) for i in range(0, len(valid_paths))] - s = -1 - print("\n".join(l)) - # Prompt the user with a list of selections. +from setuptools import setup, find_packages +from Cython.Build import cythonize +from Cython.Build.Dependencies import default_create_extension - # Fix input for Python 3-3 - try: - input = raw_input - except NameError: - pass +import numpy - while not (s >= 1 and s <= len(valid_paths)): - try: - s = input("Selection [default=1]: ") - except EOFError: - s = 1 - print("Selection is", s) - continue - if s == "": - s = 1 - else: - s = int(s) - include_numpy_array = valid_paths[s - 1] -# Add the children directory path suffix to the base path. -include_numpy_array = os.path.join(include_numpy_array, "numpy", "core", "include") +def create_extension(template, kwds: dict): + define_macros = kwds.get("define_macros", []) -# Need to create a pyOpTools package -extNames = scandir("./") + if sys.platform in ("darwin", "win32"): + define_macros.append(("CYTHON_INLINE", "")) + + kwds["define_macros"] = define_macros + return default_create_extension(template, kwds) -# and build up the set of Extension objects -extensions = [makeExtension(name) for name in extNames] setup( name="pyoptools", version="0.1.1", - packages=findpackages("./"), + packages=find_packages(exclude=["tests"]), scripts=["ipyoptools"], - # The names from pipy are used, not the deb package names - # requires=['numpy', - # 'cython', - # 'PyOpenGl', - # 'ipython', - # 'scipy', - # 'six', - # ], package_data={ - # 'pyoptools.raytrace.mat_lib': ['data/*.mat'], "pyoptools.raytrace.mat_lib": [ "data/glass/*", "data/glass/*/*", @@ -151,16 +40,7 @@ def makeExtension(extName): license="GPLv3", url="https://github.com/cihologramas/pyoptools/", download_url="https://github.com/cihologramas/pyoptools/archive/v0.1.1.zip", - ext_modules=cythonize(extensions), - cmdclass={"build_ext": build_ext}, - # data_files=[("share/doc/pyoptools/examples/basic_course", - # ["examples/basic_course/00-Introducción.ipynb", - # "examples/basic_course/03-SimpleComponents.ipynb", - # "examples/basic_course/05-Autocollimator.ipynb", - # "examples/basic_course/01-IntroPython.ipynb", - # "examples/basic_course/04-PredefinedComponents.ipynb", - # "examples/basic_course/06-GeomWF.ipynb", - # "examples/basic_course/02-Surfaces.ipynb", - # "examples/basic_course/04-Simple RayTraces.ipynb", - # "examples/basic_course/07-SimpleEODs.ipynb"])] + ext_modules=cythonize("pyoptools/**/*.pyx", language_level="2", create_extension=create_extension), + include_dirs=[numpy.get_include()], + install_requires=['numpy', 'scipy', 'imageio', 'PyYAML', 'matplotlib'], )