-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
executable file
·105 lines (82 loc) · 4.2 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
#!/usr/bin/env python3
from setuptools import setup, Extension
from glob import glob
import subprocess
import sysconfig
import sys
import os
#import re
NAME = next(filter(lambda x: x.startswith('name = '), open('setup.cfg').readlines())).strip().split()[-1]
VERSION = next(filter(lambda x: x.startswith('__version__ = '), open(NAME+'/__init__.py').readlines())).strip().replace("'","").split()[-1]
CMDCLASS = {}
## build_sphinx target ##
try:
from sphinx.setup_command import BuildDoc as _BuildDoc
class BuildDoc(_BuildDoc):
def run(self):
__import__(NAME)
super().run()
CMDCLASS['build_sphinx'] = BuildDoc
except Exception:
print('warning: sphinx package not found, build_sphinx target will not be available.')
#https://stackoverflow.com/questions/17666018/using-distutils-where-swig-interface-file-is-in-src-folder
from distutils.command.build import build as _build
#Define custom build order, so that the python interface module
#created by SWIG is staged in build_py.
class build(_build):
# different order: build_ext *before* build_py
sub_commands = [('build_ext', _build.has_ext_modules),
('build_py', _build.has_pure_modules),
('build_clib', _build.has_c_libraries),
('build_scripts', _build.has_scripts)]
CMDCLASS['build'] = build
from setuptools.command.build_ext import build_ext as build_ext_orig
class build_ext(build_ext_orig):
def run(self):
super().run()
subprocess.check_call(['sed', '-i', '/^# Import the low-level C.C++ module/{ N;N;N;N; s|.*\\n *import |import |; }', 'textfeat/swigTextFeatExtractor.py'])
print('warning: applied circular import patch to textfeat/swigTextFeatExtractor.py')
CMDCLASS['build_ext'] = build_ext
from setuptools import Distribution as _Distribution
class Distribution(_Distribution):
global_options = _Distribution.global_options + [('magick', None, 'Compile textfeat extension with __PAGEXML_IMG_MAGICK__')]
def textfeat_Extension(magick=False):
import pkgconfig
opencv = 'opencv' if sys.version_info[:2] == (3, 6) else 'opencv4'
libs = [opencv,'libxml-2.0','Magick++']
compile_args = ['-std=c++11']
link_args = []
for lib in libs:
if not pkgconfig.exists(lib):
raise FileNotFoundError('pkgconfig did not find '+lib)
compile_args += pkgconfig.cflags(lib).split()
link_args += pkgconfig.libs(lib).split()
#compile_args += pkgconfig.cflags('opencv').split()
#cvre = re.compile('^-L|^-lopencv_core|^-lopencv_imgproc|^-lopencv_imgcodecs|^-lopencv_flann')
#link_args += [x for x in pkgconfig.libs('opencv').split() if cvre.match(x)]
cvinc = pkgconfig.cflags(opencv).split()[0]
defimage = '__PAGEXML_IMG_MAGICK__' if magick else '__PAGEXML_IMG_CV__'
pageimage = 'Magick::Image' if magick else 'cv::Mat'
define_macros = [(defimage,''),('__PAGEXML_MAGICK__','')]
swig_opts = ['-D'+defimage,'-DPageImage='+pageimage,'-D__PAGEXML_MAGICK__']
print('textfeat_Extension configured with '+defimage)
return Extension('_swigTextFeatExtractor',
define_macros = define_macros + [('SWIG_PYTHON_SILENT_MEMLEAK','')],
extra_compile_args = compile_args,
extra_link_args = link_args,
swig_opts = swig_opts + [cvinc,'-modern','-keyword','-c++'],
sources = ['textfeat/TextFeatExtractor.i','textfeat/TextFeatExtractor.cc','textfeat/intimg.cc','textfeat/mem.cc'])
def distutils_dir_name(dname):
"""Returns the name of a distutils build directory"""
f = "{dirname}.{platform}-{version[0]}.{version[1]}"
return f.format(dirname=dname,
platform=sysconfig.get_platform(),
version=sys.version_info)
sys.path = [ os.path.join(os.path.dirname(os.path.realpath(__file__)), 'build', distutils_dir_name('lib'))] + sys.path
## Run setuptools setup ##
setup(version=VERSION,
name=NAME+('_magick' if '--magick' in sys.argv else ''),
ext_modules=[textfeat_Extension(True if '--magick' in sys.argv else False)],
scripts=[x for x in glob(NAME+'/bin/*.py') if not x.endswith('__.py')],
distclass=Distribution,
cmdclass=CMDCLASS)