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
|
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
TESTED_API_VERSIONS = (
(1, 3, 27),
(1, 3, 26),
(1, 3, 24),
(1, 3, 22),
(1, 3, 20),
(1, 3, 19),
(1, 3, 6),
(1, 3, 'x')
)
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 = [
'/opt/homebrew/lib/pkgconfig',
'/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/',
'/opt/homebrew/include/GraphicsMagick',
'/usr/local/Cellar/graphicsmagick'])
search_library_dirs.extend(['/opt/local/lib/',
'/opt/homebrew/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/')
if 'MAGICK_HOME' in os.environ:
extra_include_dir = os.path.join(os.environ['MAGICK_HOME'], 'include')
print('adding extra include directory %s' % extra_include_dir)
search_include_dirs.insert(0, extra_include_dir)
extra_library_dir = os.path.join(os.environ['MAGICK_HOME'], 'lib')
print('adding extra library directory %s' % extra_library_dir)
search_library_dirs.insert(0, extra_library_dir)
extra_pkgconfig_dir = os.path.join(extra_library_dir, 'pkgconfig')
print('adding extra pkgconfig directory %s' % extra_pkgconfig_dir)
search_pkgconfig_dirs.insert(0, extra_pkgconfig_dir)
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:
file_path = os.path.join(root, target)
_tmp = _grep("Version: ", file_path)
version = _tmp.split()[1]
print("Found version %s in file %s" % (version, file_path))
return version
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_python3-mt")
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")
# Homebrew's boost_python
boost_lib_target_files.append("boost_python%s%s" % (_python_version[0], _python_version[1]))
boost_lib_target_files.append("boost_python%s%s-mt" % (_python_version[0], _python_version[1]))
for boost_lib in boost_lib_target_files:
lib_path = find_file('lib%s.' % 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
_supported_api_versions = (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 _supported_api_versions]
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=sorted(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")
|