forked from jwilk-archive/python-djvulibre
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
268 lines (225 loc) · 7.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Copyright © 2007-2022 Jakub Wilk <jwilk@jwilk.net>
# Copyright © 2022-2024 FriedrichFroebel
#
# This file is part of djvulibre-python.
#
# djvulibre-python is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# djvulibre-python is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
"""
*djvulibre-python* is a set of Python bindings for
the `DjVuLibre <https://djvu.sourceforge.net/>`_ library,
an open source implementation of `DjVu <http://djvu.org/>`_.
"""
import glob
import logging
import os
import subprocess as ipc
import sys
import setuptools
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.sdist import sdist as _sdist
from wheel.bdist_wheel import bdist_wheel
logger = logging.getLogger(__name__)
del logging
class PackageVersionError(Exception):
pass
def get_ext_modules():
for pyx_file in glob.iglob(os.path.join('djvu', '*.pyx')):
module, _ = os.path.splitext(os.path.basename(pyx_file))
yield module
ext_modules = list(get_ext_modules())
def get_version():
path = os.path.join(os.path.dirname(__file__), 'doc', 'changelog')
with open(path, encoding='UTF-8') as fd:
line = fd.readline()
return line.split()[1].strip('()')
py_version = get_version()
def run_pkgconfig(*cmdline):
cmdline = ['pkg-config'] + list(cmdline)
try:
pkgconfig = ipc.Popen(
cmdline,
stdout=ipc.PIPE, stderr=ipc.PIPE
)
except EnvironmentError as exc:
msg = f'cannot execute pkg-config: {exc.strerror}'
logger.warning(msg)
return
stdout, stderr = pkgconfig.communicate()
stdout = stdout.decode('ASCII')
stderr = stderr.decode('ASCII', 'replace')
if pkgconfig.returncode != 0:
logger.warning('pkg-config failed:')
for line in stderr.splitlines():
logger.warning(' ' + line)
return
return stdout
def pkgconfig_build_flags(*packages, **kwargs):
flag_map = {
'-I': 'include_dirs',
'-L': 'library_dirs',
'-l': 'libraries',
}
fallback = dict(
libraries=['djvulibre'],
)
stdout = run_pkgconfig('--libs', '--cflags', *packages)
if stdout is None:
return fallback
kwargs.setdefault('extra_link_args', [])
kwargs.setdefault('extra_compile_args', [])
for argument in stdout.split():
key = argument[:2]
try:
value = argument[2:]
kwargs.setdefault(flag_map[key], []).append(value)
except KeyError:
kwargs['extra_link_args'].append(argument)
kwargs['extra_compile_args'].append(argument)
return kwargs
def pkgconfig_version(package):
stdout = run_pkgconfig('--modversion', package)
if stdout is None:
return
return stdout.strip()
def get_djvulibre_version():
version = pkgconfig_version('ddjvuapi')
if version is None:
raise PackageVersionError('cannot determine DjVuLibre version')
version = version or '0'
from packaging.version import Version
return Version(version)
CONFIG_TEMPLATE = """
cdef extern from *:
\"\"\"
#define PYTHON_DJVULIBRE_VERSION "{py_version}"
\"\"\"
extern const char* PYTHON_DJVULIBRE_VERSION
"""
class BuildExtension(_build_ext):
name = 'build_ext'
def run(self):
djvulibre_version = get_djvulibre_version()
from packaging.version import Version
if djvulibre_version != Version('0') and djvulibre_version < Version('3.5.26'):
raise PackageVersionError('DjVuLibre >= 3.5.26 is required')
compiler_flags = pkgconfig_build_flags('ddjvuapi')
for extension in self.extensions:
for attr, flags in compiler_flags.items():
getattr(extension, attr)
setattr(extension, attr, flags)
new_config = CONFIG_TEMPLATE.format(
py_version=py_version,
)
self.src_dir = src_dir = os.path.join(self.build_temp, 'src')
os.makedirs(src_dir, exist_ok=True)
self.config_path = os.path.join(src_dir, 'config.pxi')
try:
with open(self.config_path, 'rt') as fp:
old_config = fp.read()
except IOError:
old_config = ''
if new_config.strip() != old_config.strip():
logger.info(f'creating {self.config_path!r}')
with open(self.config_path, mode='w') as fd:
fd.write(new_config)
_build_ext.run(self)
def build_extensions(self):
self.check_extensions_list(self.extensions)
for ext in self.extensions:
ext.sources = list(self.cython_sources(ext))
self.build_extension(ext)
def cython_sources(self, ext):
for source in ext.sources:
source_base = os.path.basename(source)
target = os.path.join(
self.src_dir,
f'{source_base[:-4]}.c'
)
yield target
depends = [source, self.config_path] + ext.depends
logger.debug(f'cythonizing {ext.name!r} extension')
def build_c(source_, target_):
ipc.run([
sys.executable, '-m', 'cython',
'-I', os.path.dirname(self.config_path),
'-o', target_,
source_,
])
self.make_file(depends, target, build_c, [source, target])
class Sdist(_sdist):
name = 'sdist'
def maybe_move_file(self, base_dir, src, dst):
src = os.path.join(base_dir, src)
dst = os.path.join(base_dir, dst)
if os.path.exists(src):
self.move_file(src, dst)
def make_release_tree(self, base_dir, files):
_sdist.make_release_tree(self, base_dir, files)
self.maybe_move_file(base_dir, 'COPYING', 'doc/COPYING')
classifiers = '''
Development Status :: 4 - Beta
Intended Audience :: Developers
License :: OSI Approved :: GNU General Public License v2 (GPLv2)
Operating System :: POSIX
Programming Language :: Cython
Programming Language :: Python
Programming Language :: Python :: 3
Topic :: Multimedia :: Graphics
Topic :: Multimedia :: Graphics :: Graphics Conversion
Topic :: Text Processing
'''.strip().splitlines()
meta = dict(
name='djvulibre-python',
version=py_version,
author='Jakub Wilk',
maintainer='FriedrichFröbel',
license='GPL-2.0-only',
description='Python support for the DjVu image format',
long_description=__doc__.strip(),
long_description_content_type='text/x-rst',
classifiers=classifiers,
url='https://github.com/FriedrichFroebel/python-djvulibre',
)
setup_params = dict(
packages=['djvu'],
ext_modules=[
setuptools.Extension(
f'djvu.{name}',
[f'djvu/{name}.pyx'],
depends=(['djvu/common.pxi'] + glob.glob('djvu/*.pxd')),
)
for name in ext_modules
],
cmdclass=dict(
(cmd.__name__ if not hasattr(cmd, 'name') else cmd.name, cmd)
for cmd in (BuildExtension, Sdist, bdist_wheel)
if cmd is not None
),
py_modules=['djvu.const'],
extras_require={
'dev': [
'flake8',
'pep8-naming',
],
'docs': [
'sphinx',
],
'examples': [
# djvu2png
# 'cairocffi', # Broken: https://github.com/Kozea/cairocffi/issues/223
'pycairo',
'numpy',
]
},
**meta
)
if __name__ == '__main__':
# Required for Sphinx.
setuptools.setup(**setup_params)