File: setup.py

package info (click to toggle)
python-pgmagick 0.6.4-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 756 kB
  • sloc: cpp: 3,555; python: 1,755; makefile: 153
file content (205 lines) | stat: -rw-r--r-- 8,252 bytes parent folder | download
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
from setuptools import setup, find_packages, Extension
from distutils.sysconfig import get_python_inc
import glob
import os
import re
import sys
import ast
import io

GMCPP_PC = 'GraphicsMagick++.pc'
IMCPP_PC = 'ImageMagick++.pc'
LIBRARY = 'GraphicsMagick'  # default value
include_dirs = [get_python_inc()]
library_dirs = []

search_include_dirs = ['/usr/local/include/GraphicsMagick/',
                       '/usr/include/GraphicsMagick/']
search_library_dirs = ['/usr/local/lib64/', '/usr/lib64/',
                       '/usr/local/lib/', '/usr/lib/']
search_pkgconfig_dirs = ['/usr/local/lib/pkgconfig/', '/usr/local/lib64/pkgconfig/',
                         '/usr/lib/pkgconfig/', '/usr/lib64/pkgconfig']
if sys.platform.lower() == 'darwin':
    if os.path.exists('/opt/local/include'):
        include_dirs.append('/opt/local/include/')
    else:
        include_dirs.append('/usr/local/include/')
    search_include_dirs.extend(['/opt/local/include/GraphicsMagick/',
                                '/opt/local/include/',
                                '/usr/local/Cellar/graphicsmagick'])
    search_library_dirs.extend(['/opt/local/lib/',
                                '/usr/local/Cellar/graphicsmagick'])
# for ImageMagick
search_include_dirs.extend(['/usr/local/include/ImageMagick/',
                            '/usr/include/ImageMagick/'])
if sys.platform.lower() == 'darwin':
    search_include_dirs.append('/opt/local/include/ImageMagick/')


def _grep(regex, filename):
    for line in open(filename):
        if re.search(regex, line):
            return line


def get_version_from_devheaders(search_dirs):
    target_api_name = "addNoiseChannel"
    for dirname in search_dirs:
        for root, dirs, files in os.walk(dirname):
            for f in files:
                if f == 'Image.h':
                    if _grep(target_api_name, os.path.join(root, 'Image.h')):
                        return '1.2.0'


def get_version_from_pc(search_dirs, target):
    """similar to 'pkg-config --modversion GraphicsMagick++'"""
    for dirname in search_dirs:
        for root, dirs, files in os.walk(dirname):
            for f in files:
                if f == target:
                    _tmp = _grep("Version: ", os.path.join(root, target))
                    return _tmp.split()[1]


def find_file(filename, search_dirs):
    for dirname in search_dirs:
        for root, dirs, files in os.walk(dirname):
            for f in files:
                if filename in f:
                    return root
            for d in dirs:
                if filename in d:
                    return root
            if filename in root:
                return root
    return False

def library_supports_api(library_version, api_version, different_major_breaks_support=True):
    """
    Returns whether api_version is supported by given library version.
    E. g.  library_version (1,3,21) returns True for api_version (1,3,21), (1,3,19), (1,3,'x'), (1,2,'x'), (1, 'x')
           False for (1,3,24), (1,4,'x'), (2,'x')

    different_major_breaks_support - if enabled and library and api major versions are different always return False
           ex) with library_version (2,0,0) and for api_version(1,3,24) returns False if enabled, True if disabled
    """
    assert isinstance(library_version, (tuple, list))  # won't work with e.g. generators
    assert len(library_version) == 3
    sequence_type = type(library_version)  # assure we will compare same types
    api_version = sequence_type(0 if num == 'x' else num for num in api_version)
    if different_major_breaks_support and library_version[0] != api_version[0]:
        return False
    assert len(api_version) <= 3     # otherwise following comparision won't work as intended, e.g. (2, 0, 0) > (2, 0, 0, 0)
    return library_version >= api_version

# find to header path
header_path = find_file('Magick++.h', search_include_dirs)
if not header_path:
    raise Exception("Magick++ not found")
print("include header path: %s" % header_path)
include_dirs.append(header_path)

# find to library path for boost_python
# TODO: only test on Ubuntu11.10
_python_version = sys.version_info

boost_lib_target_files = []
if _python_version >= (3, ):
    boost_lib_target_files.append("boost_python-py%s%s" % (_python_version[0], _python_version[1]))
    # ArchLinux uses boost_python3
    boost_lib_target_files.append("boost_python3")
boost_lib_target_files.append("boost_python-mt-py%s%s" % (_python_version[0], _python_version[1]))
# gentoo appends the python version numbers to the boost_python libraries
boost_lib_target_files.append("boost_python-%s.%s" % (_python_version[0], _python_version[1]))
boost_lib_target_files.append("boost_python-mt")

for boost_lib in boost_lib_target_files:
    lib_path = find_file('lib' + boost_lib, search_library_dirs)
    if lib_path:
        break

if not lib_path:
    boost_lib = "boost_python"
print("boost lib: %s" % boost_lib)

libraries = [boost_lib]

# find to library path for Magick
lib_path = find_file('libGraphicsMagick++', search_library_dirs)
if lib_path:
    libraries.append('GraphicsMagick++')
    print("library path: %s" % (os.path.join(lib_path, "libGraphicsMagick++")))
else:
    lib_path = find_file('libMagick++', search_library_dirs)
    if lib_path:
        LIBRARY = 'ImageMagick'
        libraries.append('Magick++')
        print("library path: %s" % (os.path.join(lib_path, "libMagick++")))
    else:
        raise Exception("libGraphicsMagick++ (or libMagick++) not found")
library_dirs.append(lib_path)

# get version and extra compile argument
ext_compile_args = []
if LIBRARY == 'GraphicsMagick':
    _version = get_version_from_pc(search_pkgconfig_dirs + search_include_dirs, GMCPP_PC)
else:
    _version = get_version_from_pc(search_pkgconfig_dirs + search_include_dirs, IMCPP_PC)
if not _version:
    _version = get_version_from_devheaders(include_dirs)
if _version:
    _str_version = _version
    print("%s version: %s" % (LIBRARY, _version))
    _version = list(map(int, _version.split('.')))
    if len(_version) == 2:
        # ex) 1.2 -> 1.2.0
        _version.append(0)
    if LIBRARY == 'GraphicsMagick':
        # 1.3.6 for not Ubuntu10.04
        _tested_api_versions = ((1,3,26), (1,3,24), (1,3,22), (1,3,20), (1,3,19), (1,3,6))
        _supportedApiVersions = (v for v in _tested_api_versions if library_supports_api(_version, v))
        ext_compile_args = ["-DPGMAGICK_LIB_GRAPHICSMAGICK_" + '_'.join(map(str, version)) for version in _supportedApiVersions]
        if not (_version[0] == 1 and _version[1] == 1):
            # for GM version 1.3.x and higher
            ext_compile_args.append("-DPGMAGICK_LIB_GRAPHICSMAGICK_1_3_x")
    elif LIBRARY == 'ImageMagick':
        ext_compile_args = ["-DPGMAGICK_LIB_IMAGEMAGICK"]
    ext_compile_args.append("-D_LIBRARY_VERSION=\"%s\"" % (_str_version))
else:
    _version = '%s version: ???' % (LIBRARY)


def version():
    """Return version string."""
    with io.open('pgmagick/_version.py') as input_file:
        for line in input_file:
            if line.startswith('__version__'):
                return ast.parse(line).body[0].value.s

setup(name='pgmagick',
      version=version(),
      description="Yet Another Python wrapper for GraphicsMagick",
      long_description=open('README.rst').read(),
      author='Hideo Hattori',
      author_email='hhatto.jp@gmail.com',
      url='https://github.com/hhatto/pgmagick',
      license='MIT',
      packages=find_packages(),
      ext_modules=[
          Extension('pgmagick._pgmagick',
                    sources=glob.glob('./src/*.cpp'),
                    include_dirs=include_dirs,
                    library_dirs=library_dirs,
                    libraries=libraries,
                    extra_compile_args=ext_compile_args)],
      classifiers=[
          'Development Status :: 4 - Beta',
          'Intended Audience :: Developers',
          'License :: OSI Approved :: MIT License',
          'Operating System :: POSIX',
          'Programming Language :: C++',
          'Programming Language :: Python',
          'Programming Language :: Python :: 3',
          'Topic :: Multimedia :: Graphics'],
      keywords="GraphicsMagick ImageMagick graphics boost image")