# coding: utf-8
import os
import fnmatch
import sysconfig
from pathlib import Path

from setuptools import setup, find_packages
from setuptools.command.build_py import build_py as _build_py

from Cython.Build import cythonize


EXCLUDE_FILES = [
    "PAI/main.py",
]


def get_ext_paths(root_dir, exclude_files):
    """get filepaths for compilation"""
    paths = []

    for root, dirs, files in os.walk(root_dir):
        for filename in files:
            if os.path.splitext(filename)[1] != ".py":
                continue

            file_path = os.path.join(root, filename)
            if file_path in exclude_files:
                continue

            paths.append(file_path)
    return paths


# noinspection PyPep8Naming
class build_py(_build_py):

    def find_package_modules(self, package, package_dir):
        ext_suffix = sysconfig.get_config_var("EXT_SUFFIX")
        modules = super().find_package_modules(package, package_dir)
        filtered_modules = []
        for pkg, mod, filepath in modules:
            if os.path.exists(filepath.replace(".py", ext_suffix)):
                continue
            filtered_modules.append(
                (
                    pkg,
                    mod,
                    filepath,
                )
            )
        return filtered_modules

readme_path = Path(__file__).resolve().parent / "README.md"
if readme_path.exists():
    long_description = readme_path.read_text(encoding="utf-8")
else:
    long_description = "Official package for the artificial dendrite library for PyTorch, Perforated AI"

setup(
    name="perforatedai",
    version="3.2.9",
    packages=find_packages(),
    package_data={"perforatedai": ["*.json"]},
    include_package_data=True,
    author="PerforatedAI",
    author_email="rorry@perforatedai.com",
    description="Official package for the artificial dendrite library for PyTorch, Perforated AI",
    long_description=long_description,          
    long_description_content_type="text/markdown",
    license="Apache 2.0",
    python_requires=">=3.6",
    ext_modules=cythonize(
        get_ext_paths("perforatedai", EXCLUDE_FILES),
        compiler_directives={"language_level": 3},
    ),
    cmdclass={"build_py": build_py},
    install_requires=[
        "torch",
        "torchvision",
        "matplotlib",
        "pandas",
        "cryptography",
        "pyyaml",
        "safetensors",
        "requests",
    ],
    # may need setuptools upgraded
)
