forked from openzim/python-libzim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·161 lines (137 loc) · 6.12 KB
/
setup.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
"""
python-libzim (the openzim/libzim bindings for Python)
The project is compiled in two steps:
1. Cython: compile the cython format files (.pyx, .pyd) to C++ (.cpp and .h)
2. Cythonize: compile the generated C++ to a python-importable binary extension .so
The Cython and Cythonize compilation is done automatically with setup.py:
$ python3 setup.py build_ext
$ python3 setup.py sdist bdist_wheel
To compile or run this project, you must first get the libzim headers & binary:
- You can get the headers here and build and install the binary from source:
https://github.com/openzim/libzim
- Or you can download a full prebuilt release (if one exists for your platform):
https://download.openzim.org/release/libzim/
Either place the `libzim.so` and `zim/*.h` files in `./lib/` and `./include/`,
or set these environment variables to use custom libzim header and dylib paths:
$ export CFLAGS="-I/tmp/libzim_linux-x86_64-6.1.1/include"
$ export LDFLAGS="-L/tmp/libzim_linux-x86_64-6.1.1/lib/x86_64-linux-gnu"
$ export LD_LIBRARY_PATH+=":/tmp/libzim_linux-x86_64-6.1.1/lib/x86_64-linux-gnu"
"""
import os
import platform
from pathlib import Path
from ctypes.util import find_library
from setuptools import setup, Extension
from Cython.Build import cythonize
from Cython.Distutils.build_ext import new_build_ext as build_ext
GITHUB_URL = "https://github.com/openzim/python-libzim"
BASE_DIR = Path(__file__).parent
LIBZIM_INCLUDE_DIR = "include" # the libzim C++ header src dir (containing zim/*.h)
LIBZIM_LIBRARY_DIR = "lib" # the libzim .so binary lib dir (containing libzim.so)
LIBZIM_DYLIB = "libzim.{ext}".format(
ext="dylib" if platform.system() == "Darwin" else "so"
)
# set PROFILE env to `1` to enable profile info on build (used for coverage reporting)
PROFILE = os.getenv("PROFILE", "") == "1"
class fixed_build_ext(build_ext):
"""Workaround for rpath bug in distutils for OSX."""
def finalize_options(self):
super().finalize_options()
# Special treatment of rpath in case of OSX, to work around python
# distutils bug 36353. This constructs proper rpath arguments for clang.
# See https://bugs.python.org/issue36353
if platform.system() == "Darwin":
for path in self.rpath:
for ext in self.extensions:
ext.extra_link_args.append("-Wl,-rpath," + path)
self.rpath[:] = []
# Check for the CPP Libzim library headers in expected directory
if not (BASE_DIR / LIBZIM_INCLUDE_DIR / "zim/zim.h").exists():
print(
f"[!] Warning: Couldn't find zim/*.h in ./{LIBZIM_INCLUDE_DIR}!\n"
f" Hint: install from source using from https://github.com/openzim/libzim\n"
f" or download a prebuilt release's headers into ./include/zim/*.h\n"
f" (or set CFLAGS='-I<library_path>/include')"
)
# Check for the CPP Libzim shared library in expected directory or system paths
if not ((BASE_DIR / LIBZIM_LIBRARY_DIR / LIBZIM_DYLIB).exists() or find_library("zim")):
print(
f"[!] Warning: Couldn't find {LIBZIM_DYLIB} in ./{LIBZIM_LIBRARY_DIR} or system"
f" Hint: install from source using https://github.com/openzim/libzim\n"
f" or download a prebuilt {LIBZIM_DYLIB} release into ./lib.\n"
f" (or set LDFLAGS='-L<library_path>/lib/[x86_64-linux-gnu]')"
)
def get_long_description():
return (BASE_DIR / "README.md").read_text()
wrapper_extension = Extension(
name="libzim.wrapper",
sources=["libzim/wrapper.pyx", "libzim/lib.cxx"],
include_dirs=["libzim", LIBZIM_INCLUDE_DIR],
libraries=["zim"],
library_dirs=[LIBZIM_LIBRARY_DIR],
extra_compile_args=["-std=c++11", "-Wall", "-Wextra"],
language="c++",
define_macros=[("CYTHON_TRACE", "1"), ("CYTHON_TRACE_NOGIL", "1")]
if PROFILE
else [],
)
compiler_directives = {"language_level": "3"}
if PROFILE:
compiler_directives.update({"linetrace": "True"})
setup(
# Basic information about libzim module
name="libzim",
version="1.0.0.dev0",
url=GITHUB_URL,
project_urls={
"Source": GITHUB_URL,
"Bug Tracker": f"{GITHUB_URL}/issues",
"Changelog": f"{GITHUB_URL}/releases",
"Documentation": f"{GITHUB_URL}/blob/master/README.md",
"Donate": "https://www.kiwix.org/en/support-us/",
},
author="Monadical Inc.",
author_email="[email protected]",
license="GPL-3.0-or-later",
description="A python-facing API for creating and interacting with ZIM files",
long_description=get_long_description(),
long_description_content_type="text/markdown",
python_requires=">=3.6",
# Content
packages=["libzim"],
cmdclass={"build_ext": fixed_build_ext},
ext_modules=cythonize([wrapper_extension], compiler_directives=compiler_directives),
# Packaging
include_package_data=True,
zip_safe=False,
# Extra
classifiers=[
"Development Status :: 3 - Alpha",
"Topic :: Utilities",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: System :: Archiving",
"Topic :: System :: Archiving :: Compression",
"Topic :: System :: Archiving :: Mirroring",
"Topic :: System :: Archiving :: Backup",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: Indexing/Search",
"Topic :: Sociology :: History",
"Intended Audience :: Developers",
"Intended Audience :: Education",
"Intended Audience :: End Users/Desktop",
"Intended Audience :: Information Technology",
"Intended Audience :: System Administrators",
"Programming Language :: Cython",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
# "Typing :: Typed",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Natural Language :: English",
"Operating System :: OS Independent",
],
)