forked from frescobaldi/python-poppler-qt5
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
393 lines (315 loc) · 13 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#! python
project = dict(
name = 'python-poppler-qt5',
version = '0.75.0',
description = 'A Python binding to Poppler-Qt5',
long_description = (
'A Python binding to Poppler-Qt5 that aims for '
'completeness and for being actively maintained. '
'Using this module you can access the contents of PDF files '
'inside PyQt5 applications.'
),
maintainer = 'Wilbert Berendsen',
maintainer_email = '[email protected]',
url = 'https://github.com/frescobaldi/python-poppler-qt5',
license = 'LGPL',
classifiers = [
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Programming Language :: Python :: 3',
'Topic :: Multimedia :: Graphics :: Viewers',
],
cmdclass={}
)
import os
import re
import shlex
import subprocess
import sys
import platform
try:
from setuptools import setup, Extension
except ImportError:
from distutils.core import setup, Extension
import sipdistutils
### this circumvents a bug in sip < 4.14.2, where the file() builtin is used
### instead of open()
try:
import builtins
try:
builtins.file
except AttributeError:
builtins.file = open
except ImportError:
pass
### end
def check_qtxml():
"""Return True if PyQt5.QtXml can be imported.
in some early releases of PyQt5, QtXml was missing because it was
thought QtXml was deprecated.
"""
import PyQt5
try:
import PyQt5.QtXml
except ImportError:
return False
return True
def pkg_config(package, attrs=None, include_only=False):
"""parse the output of pkg-config for a package.
returns the given or a new dictionary with one or more of these keys
'include_dirs', 'library_dirs', 'libraries'. Every key is a list of paths,
so that it can be used with distutils Extension objects.
"""
if attrs is None:
attrs = {}
cmd = ['pkg-config']
if include_only:
cmd += ['--cflags-only-I']
else:
cmd += ['--cflags', '--libs']
cmd.append(package)
try:
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
except OSError:
return attrs
flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}
# for python3 turn bytes back to string
if sys.version_info[0] > 2:
output = output.decode('utf-8')
for flag in shlex.split(output):
option, path = flag[:2], flag[2:]
if option in flag_map:
l = attrs.setdefault(flag_map[option], [])
if path not in l:
l.append(path)
return attrs
def pkg_config_version(package):
"""Returns the version of the given package as a tuple of ints."""
cmd = ['pkg-config', '--modversion', package]
try:
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
# for python3 turn bytes back to string
if sys.version_info[0] > 2:
output = output.decode('utf-8')
return tuple(map(int, re.findall(r'\d+', output)))
except OSError:
sys.stderr.write("Can't determine version of %s\n" % package)
ext_args = {}
ext_args['extra_compile_args'] = ['-std=c++11']
pkg_config('poppler-qt5', ext_args)
if 'libraries' not in ext_args:
ext_args['libraries'] = ['poppler-qt5']
# hack to provide our options to sip on its invocation:
build_ext_base = sipdistutils.build_ext
class build_ext(build_ext_base):
description = "Builds the popplerqt5 module."
user_options = build_ext_base.user_options + [
('poppler-version=', None, "version of the poppler library"),
('qmake-bin=', None, "Path to qmake binary"),
('sip-bin=', None, "Path to sip binary"),
('qt-include-dir=', None, "Path to Qt headers"),
('pyqt-sip-dir=', None, "Path to PyQt's SIP files"),
('pyqt-sip-flags=', None, "SIP flags used to generate PyQt bindings")
]
def initialize_options (self):
build_ext_base.initialize_options(self)
self.poppler_version = None
self.qmake_bin = 'qmake'
self.sip_bin = None
self.qt_include_dir = None
self.pyqt_sip_dir = None
self.pyqt_sip_flags = None
def finalize_options (self):
build_ext_base.finalize_options(self)
if not self.qt_include_dir:
self.qt_include_dir = self.__find_qt_include_dir()
if not self.pyqt_sip_dir:
self.pyqt_sip_dir = self.__find_pyqt_sip_dir()
if not self.pyqt_sip_flags:
self.pyqt_sip_flags = self.__find_pyqt_sip_flags()
if not self.qt_include_dir:
raise SystemExit('Could not find Qt5 headers. '
'Please specify via --qt-include-dir=')
if not self.pyqt_sip_dir:
raise SystemExit('Could not find PyQt SIP files. '
'Please specify containing directory via '
'--pyqt-sip-dir=')
if not self.pyqt_sip_flags:
raise SystemExit('Could not find PyQt SIP flags. '
'Please specify via --pyqt-sip-flags=')
self.include_dirs += (self.qt_include_dir,
os.path.join(self.qt_include_dir, 'QtCore'),
os.path.join(self.qt_include_dir, 'QtGui'),
os.path.join(self.qt_include_dir, 'QtXml'))
if self.poppler_version is not None:
self.poppler_version = tuple(map(int, re.findall(r'\d+', self.poppler_version)))
def __find_qt_include_dir(self):
if self.pyqtconfig:
return self.pyqtconfig.qt_inc_dir
try:
qt_version = subprocess.check_output(" ".join([self.qmake_bin,
'-query',
'QT_VERSION']), shell=True)
qt_version = qt_version.strip().decode("ascii")
except (OSError, subprocess.CalledProcessError) as e:
raise SystemExit('Failed to determine Qt version (%s).' % e)
if not qt_version.startswith("5."):
raise SystemExit('Unsupported Qt version (%s). '
'Try specifying the path to qmake manually via '
'--qmake-bin=' % qt_version)
try:
result = subprocess.check_output(" ".join([self.qmake_bin,
'-query',
'QT_INSTALL_HEADERS']), shell=True)
return result.strip().decode(sys.getfilesystemencoding())
except (OSError, subprocess.CalledProcessError) as e:
raise SystemExit('Failed to determine location of Qt headers (%s).' % e)
def __find_pyqt_sip_dir(self):
if self.pyqtconfig:
return self.pyqtconfig.pyqt_sip_dir
import sipconfig
return os.path.join(sipconfig.Configuration().default_sip_dir, 'PyQt5')
def __find_pyqt_sip_flags(self):
if self.pyqtconfig:
return self.pyqtconfig.pyqt_sip_flags
from PyQt5 import QtCore
return QtCore.PYQT_CONFIGURATION.get('sip_flags', '')
@property
def pyqtconfig(self):
if not hasattr(self, '_pyqtconfig'):
try:
from PyQt5 import pyqtconfig
self._pyqtconfig = pyqtconfig.Configuration()
except ImportError:
self._pyqtconfig = None
return self._pyqtconfig
def write_version_sip(self, poppler_qt5_version, python_poppler_qt5_version):
"""Write a version.sip file.
The file contains code to make version information accessible from
the popplerqt5 Python module.
"""
with open('version.sip', 'w') as f:
f.write(version_sip_template.format(
vlen = 'i' * len(python_poppler_qt5_version),
vargs = ', '.join(map(format, python_poppler_qt5_version)),
pvlen = 'i' * len(poppler_qt5_version),
pvargs = ', '.join(map(format, poppler_qt5_version))))
def _find_sip(self):
"""override _find_sip to allow for manually speficied sip path."""
return self.sip_bin or build_ext_base._find_sip(self)
def _sip_compile(self, sip_bin, source, sbf):
# First check manually specified poppler version
ver = self.poppler_version or pkg_config_version('poppler-qt5') or ()
# our own version:
version = tuple(map(int, re.findall(r'\d+', project['version'])))
# make those accessible from the popplerqt5 module:
self.write_version_sip(ver, version)
# Disable features if older poppler-qt5 version is found.
# See the defined tags in %Timeline{} in timeline.sip.
tag = 'POPPLER_V0_20_0'
if ver:
with open("timeline.sip", "r") as f:
for m in re.finditer(r'POPPLER_V(\d+)_(\d+)_(\d+)', f.read()):
if ver < tuple(map(int, m.group(1, 2, 3))):
break
tag = m.group()
cmd = [sip_bin]
if hasattr(self, 'sip_opts'):
cmd += self.sip_opts
if hasattr(self, '_sip_sipfiles_dir'):
cmd += ['-I', self._sip_sipfiles_dir()]
if tag:
cmd += ['-t', tag]
cmd += ["-x", "QTXML_AVAILABLE"] # mark QtXml not supported
cmd += [
"-c", self.build_temp,
"-b", sbf,
"-I", self.pyqt_sip_dir] # find the PyQt5 stuff
cmd += shlex.split(self.pyqt_sip_flags) # use same SIP flags as for PyQt5
cmd.append(source)
self.spawn(cmd)
def get_libraries(self, ext):
if sys.platform == "win32":
return super(build_ext, self).get_libraries(ext)
else:
return ext.libraries
if platform.system() == 'Windows':
# Enforce libraries to link against on Windows
ext_args['libraries'] = ['poppler-qt5', 'Qt5Core', 'Qt5Gui', 'Qt5Xml']
class bdist_support():
def __find_poppler_dll(self):
paths = os.environ['PATH'].split(";")
poppler_dll = None
for path in paths:
dll_path_candidate = os.path.join(path, "poppler-qt5.dll")
if os.path.exists(dll_path_candidate):
return dll_path_candidate
return None
def _copy_poppler_dll(self):
poppler_dll = self.__find_poppler_dll()
if poppler_dll is None:
self.warn("Could not find poppler-qt5.dll in any of the folders listed in the PATH environment variable.")
return False
self.mkpath(self.bdist_dir)
self.copy_file(poppler_dll, os.path.join(self.bdist_dir, "python-poppler5.dll"))
return True
import distutils.command.bdist_msi
class bdist_msi(distutils.command.bdist_msi.bdist_msi, bdist_support):
def run(self):
if not self._copy_poppler_dll():
return
distutils.command.bdist_msi.bdist_msi.run(self)
project['cmdclass']['bdist_msi'] = bdist_msi
import distutils.command.bdist_wininst
class bdist_wininst(distutils.command.bdist_wininst.bdist_wininst, bdist_support):
def run(self):
if not self._copy_poppler_dll():
return
distutils.command.bdist_wininst.bdist_wininst.run(self)
project['cmdclass']['bdist_wininst'] = bdist_wininst
import distutils.command.bdist_dumb
class bdist_dumb(distutils.command.bdist_dumb.bdist_dumb, bdist_support):
def run(self):
if not self._copy_poppler_dll():
return
distutils.command.bdist_dumb.bdist_dumb.run(self)
project['cmdclass']['bdist_dumb'] = bdist_dumb
try:
# Attempt to patch bdist_egg if the setuptools/distribute extension is installed
import setuptools.command.bdist_egg
class bdist_egg(setuptools.command.bdist_egg.bdist_egg, bdist_support):
def run(self):
if not self._copy_poppler_dll():
return
setuptools.command.bdist_egg.bdist_egg.run(self)
project['cmdclass']['bdist_egg'] = bdist_egg
except ImportError:
pass
version_sip_template = r"""// Generated by setup.py -- Do not edit
PyObject *version();
%Docstring
The version of the popplerqt5 python module.
%End
PyObject *poppler_version();
%Docstring
The version of the Poppler library.
%End
%ModuleCode
PyObject *version()
{{ return Py_BuildValue("({vlen})", {vargs}); }};
PyObject *poppler_version()
{{ return Py_BuildValue("({pvlen})", {pvargs}); }};
%End
"""
### use full README.rst as long description
with open('README.rst', 'rb') as f:
project["long_description"] = f.read().decode('utf-8')
project['cmdclass']['build_ext'] = build_ext
setup(
ext_modules = [Extension("popplerqt5", ["poppler-qt5.sip"], **ext_args)],
**project
)