Skip to content

Commit

Permalink
Add setup.py bundle_imagemagick command
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Aug 2, 2012
1 parent d3d97e8 commit 7cca4fd
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
ImageMagick-*
docs/_build
build
dist
wand/lib
.tox
*.pyc
*.egg
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[aliases]
bdist = bundle_imagemagick bdist
bdist_egg = bundle_imagemagick bdist_egg
upload_doc = build_sphinx upload_doc
release = sdist github_upload -R dahlia/wand register build_sphinx upload_doc
95 changes: 90 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import os
import os.path
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
import distutils.cmd
import os
import os.path
import shutil
import subprocess
import tarfile
import tempfile
import traceback
import urllib2

from wand.version import VERSION

Expand All @@ -16,6 +20,84 @@ def readme():
return f.read()


class bundle_imagemagick(distutils.cmd.Command):
"""Bundle ImageMagick library into the distribution."""

# url = 'http://www.imagemagick.org/download/ImageMagick-6.7.8-7.tar.bz2'
url = 'https://github.com/downloads/dahlia/wand/ImageMagick-6.7.8-7.tar.bz2'
description = __doc__
user_options = [
('url=', 'u', 'ImageMagick source tarball url [default: ' + url + ']')
]

def initialize_options(self):
self.url = self.__class__.url

def finalize_options(self):
pass

def main(self):
log = distutils.log
for name in os.listdir('.'):
if name.startswith('ImageMagick-') and os.path.isdir(name):
dirname = name
log.info('ImageMagick source seems to already exist')
break
else:
log.info('Downloading ImageMagick source tarball...')
log.info(self.url)
response = urllib2.urlopen(self.url)
tmp = tempfile.TemporaryFile()
self.copy(response, tmp)
response.close()
tmp.seek(0)
log.info('Extracting ImageMagick source tarball...')
tar = tarfile.open(fileobj=tmp)
dirname = tar.getnames()[0]
tar.extractall()
libdir = os.path.join(dirname, 'lib')
if os.path.isdir(libdir):
log.info('ImageMagick seems already built')
else:
log.info('Getting configured ImageMagick...')
subprocess.call([
'./configure', '--prefix=' + os.path.abspath(dirname),
'--disable-installed', '--without-magick-plus-plus'
], cwd=dirname)
log.info('Building ImageMagick...')
subprocess.call(['make', 'install'], cwd=dirname)
dstdir = os.path.join('wand', 'lib')
if not os.path.isdir(dstdir):
os.mkdir(dstdir)
data = []
for libname in os.listdir(libdir):
if not libname.endswith(('.so', '.dylib')):
continue
with open(os.path.join(libdir, libname), 'rb') as src:
dstname = os.path.join('lib', libname)
with open(os.path.join('wand', dstname), 'wb') as dst:
self.copy(src, dst)
data.append(dstname)
self.distribution.package_data.setdefault('wand', []).extend(data)
self.distribution.has_ext_modules = lambda: True
self.distribution.zip_safe = False

def run(self):
try:
self.main()
except:
traceback.print_exc()
raise

def copy(self, source, destination):
while 1:
chunk = source.read(4096)
if chunk:
destination.write(chunk)
else:
break


class upload_doc(distutils.cmd.Command):
"""Uploads the documentation to GitHub pages."""

Expand Down Expand Up @@ -47,7 +129,6 @@ def run(self):
setup(
name='Wand',
packages=['wand'],
data_files=[('', ['README.rst'])],
version=VERSION,
description='Ctypes-based simple MagickWand API binding for Python',
long_description=readme(),
Expand All @@ -57,6 +138,7 @@ def run(self):
maintainer='Hong Minhee',
maintainer_email='minhee@dahlia.kr',
url='http://dahlia.github.com/wand/',
data_files=[('', ['README.rst'])],
tests_require=['Attest'],
test_loader='attest:auto_reporter.test_loader',
test_suite='wandtests.tests',
Expand All @@ -70,6 +152,9 @@ def run(self):
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Topic :: Multimedia :: Graphics'
],
cmdclass={'upload_doc': upload_doc}
],
cmdclass={
'bundle_imagemagick': bundle_imagemagick,
'upload_doc': upload_doc
}
)
22 changes: 11 additions & 11 deletions wand/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ def load_library():
libpath = None
system = platform.system()

magick_home = os.environ.get('MAGICK_HOME')
if magick_home:
if system == 'Windows':
libpath = 'CORE_RL_wand_.dll',
elif system == 'Darwin':
libpath = 'lib', 'libMagickWand.dylib',
else:
libpath = 'lib', 'libMagickWand.so',
libpath = os.path.join(magick_home, *libpath)
magick_home = os.environ.get('MAGICK_HOME', os.path.dirname(__file__))
if system == 'Windows':
libpath = 'CORE_RL_wand_.dll',
elif system == 'Darwin':
libpath = 'lib', 'libMagickWand.dylib',
else:
libpath = 'lib', 'libMagickWand.so',
libpath = os.path.join(magick_home, *libpath)
if not os.path.isfile(libpath):
if system == 'Windows':
libpath = ctypes.util.find_library('CORE_RL_wand_')
else:
Expand Down Expand Up @@ -249,8 +248,9 @@ class MagickPixelPacket(ctypes.Structure):

libmagick.GetMagickReleaseDate.argtypes = []
libmagick.GetMagickReleaseDate.restype = ctypes.c_char_p
except AttributeError:
raise ImportError('MagickWand shared library not found or incompatible')
except AttributeError as e:
raise ImportError('MagickWand shared library not found or incompatible; '
+ str(e))

#: (:class:`ctypes.CDLL`) The C standard library.
libc = None
Expand Down

0 comments on commit 7cca4fd

Please sign in to comment.