File: setup.py

package info (click to toggle)
python-lap 0.5.12-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,684 kB
  • sloc: python: 1,408; cpp: 872; sh: 17; makefile: 3
file content (132 lines) | stat: -rw-r--r-- 4,982 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from setuptools import Command
from setuptools import Extension, setup

LICENSE = "BSD-2-Clause"
DESCRIPTION = "Linear Assignment Problem solver (LAPJV/LAPMOD)."
LONG_DESCRIPTION = open("README.md", encoding="utf-8").read()

package_name = "lap"
package_path = "lap"
_lapjv_src = "_lapjv_cpp"

def get_version_string():
    with open("lap/__init__.py") as version_file:
        for line in version_file.read().splitlines():
            if line.startswith('__version__'):
                delim = '"' if '"' in line else "'"
                return line.split(delim)[1]

def include_numpy():
    import numpy as np
    try:
        numpy_include = np.get_include()
    except AttributeError:
        numpy_include = np.get_numpy_include()
    return numpy_include

def compile_cpp(cython_file):
    """Compile a C++ file from Cython's pyx or py.
    """
    import os
    import subprocess
    cpp_file = os.path.splitext(cython_file)[0] + ".cpp"
    flags = ['--fast-fail', '--cplus']
    rc = subprocess.call(['cython'] + flags + ['-o', cpp_file, cython_file])
    if rc != 0:
        raise Exception('Cythonizing %s failed' % cython_file)
    else:
        return cpp_file

class ExportCythonCommand(Command):
    description = "Export _lapjv binary from source."
    def run(self):
        super().run()
        import os
        import shutil
        this_dir = os.path.dirname(os.path.realpath(__file__))
        lap = os.path.join(this_dir, package_path)
        _lapjv_src = os.path.join(this_dir, _lapjv_src)
        for file in os.listdir(_lapjv_src):
            if file[-2:].lower() in "soyd":
                print(f"Moving {_lapjv_src}/{file} to {lap}/")
                shutil.copy2(os.path.join(_lapjv_src, file), lap)
                break

def main_setup():
    """Use modern setup() by setuptools.
    """
    import os
    from Cython.Build import cythonize
    _lapjvpyx = os.path.join(_lapjv_src, "_lapjv.pyx")
    _lapjvcpp = compile_cpp(_lapjvpyx)
    lapjvcpp = os.path.join(_lapjv_src, "lapjv.cpp")
    lapmodcpp = os.path.join(_lapjv_src, "lapmod.cpp")

    ext_modules = [
        Extension(
            name='lap._lapjv',
            sources=[_lapjvcpp, lapjvcpp, lapmodcpp],
            include_dirs=[include_numpy(), _lapjv_src, package_path],
        )
    ]

    package_data = {}
    tests_package = package_path + ".tests"
    packages = [package_path, tests_package]
    for p in packages: package_data.update({p: ["*"]})

    setup(
        name=package_name,
        version=get_version_string(),
        description=DESCRIPTION,
        license=LICENSE,
        long_description=LONG_DESCRIPTION,
        long_description_content_type="text/markdown",
        keywords=['Linear Assignment', 'LAPJV', 'LAPMOD', 'lap', 'lapx'],
        url="https://github.com/gatagat/lap",
        author="gatagat, rathaROG, and co.",
        packages=packages,
        package_data=package_data,
        include_package_data=True,
        install_requires=['numpy>=1.21.6',],
        python_requires=">=3.7",
        classifiers=['Development Status :: 4 - Beta',
                     'Environment :: Console',
                     'Intended Audience :: Developers',
                     'Intended Audience :: Education',
                     'Intended Audience :: Science/Research',
                     'License :: OSI Approved :: BSD License',
                     'Programming Language :: Python :: 3',
                     'Programming Language :: Python :: 3.7',
                     'Programming Language :: Python :: 3.8',
                     'Programming Language :: Python :: 3.9',
                     'Programming Language :: Python :: 3.10',
                     'Programming Language :: Python :: 3.11',
                     'Programming Language :: Python :: 3.12',
                     'Programming Language :: Python :: 3.13',
                     'Topic :: Education',
                     'Topic :: Education :: Testing',
                     'Topic :: Scientific/Engineering',
                     'Topic :: Scientific/Engineering :: Mathematics',
                     'Topic :: Software Development',
                     'Topic :: Scientific/Engineering',
                     'Topic :: Scientific/Engineering :: Artificial Intelligence',
                     'Topic :: Scientific/Engineering :: Image Recognition',
                     'Operating System :: Microsoft :: Windows',                                  
                     'Operating System :: POSIX',
                     'Operating System :: Unix',
                     'Operating System :: MacOS',],
        ext_modules=cythonize(ext_modules),
        cmdclass={'cmdexport': ExportCythonCommand,},
    )

if __name__ == "__main__":
    """
    Recommend using :py:mod:`build` to build the package as it does not 
    disrupt your current environment.

    >>> pip install wheel build
    >>> python -m build --sdist
    >>> python -m build --wheel
    """ 
    main_setup()