setup.py 5.76 KB
Newer Older
André Anjos's avatar
André Anjos committed
1 2 3 4 5
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Andre Anjos <andre.anjos@idiap.ch>
# Mon 16 Apr 08:18:08 2012 CEST

6 7
bob_packages = ['bob.core', 'bob.io.base']

André Anjos's avatar
André Anjos committed
8
from setuptools import setup, find_packages, dist
9
dist.Distribution(dict(setup_requires=['bob.extension', 'bob.blitz'] + bob_packages))
André Anjos's avatar
André Anjos committed
10
from bob.extension.utils import egrep, find_header, find_library
11 12
from bob.blitz.extension import Extension, Library, build_ext

13 14 15
from bob.extension.utils import load_requirements
build_requires = load_requirements()

16 17 18
# Define package version
version = open("version.txt").read().rstrip()

19
packages = ['boost']
20
boost_modules = ['system', 'filesystem']
André Anjos's avatar
André Anjos committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

# process libsvm requirement
import os
from distutils.version import LooseVersion

def libsvm_version(svm_h):

  matches = egrep(svm_h, r"#\s*define\s+LIBSVM_VERSION\s+(\d+)")
  if not len(matches): return None

  # we have a match, produce a string version of the version number
  version_int = int(matches[0].group(1))
  version_tuple = (version_int // 100, version_int % 100)
  return '.'.join([str(k) for k in version_tuple])

class libsvm:
  """A class for capturing configuration information from libsvm

  Example usage:

  .. doctest::
     :options: +NORMALIZE_WHITESPACE +ELLIPSIS

André Anjos's avatar
André Anjos committed
44
     >>> from bob.learn.libsvm import libsvm
André Anjos's avatar
André Anjos committed
45 46 47 48 49 50 51 52 53 54 55 56
     >>> l = libsvm('>= 3.12')
     >>> l.include_directory
     '...'
     >>> l.version
     '...'
     >>> l.library_directory
     '...'
     >>> l.libraries
     [...]

  """

57
  def __init__ (self):
André Anjos's avatar
André Anjos committed
58
    """
59
    Searches for libsvm in stock locations.
André Anjos's avatar
André Anjos committed
60

André Anjos's avatar
André Anjos committed
61
    If the user sets the environment variable BOB_PREFIX_PATH, that prefixes
André Anjos's avatar
André Anjos committed
62 63 64
    the standard path locations.
    """

65
    candidates = find_header('svm.h', subpaths=['', 'libsvm', 'libsvm-*/libsvm'])
André Anjos's avatar
André Anjos committed
66 67 68 69

    if not candidates:
      raise RuntimeError("could not find libsvm's `svm.h' - have you installed libsvm on this machine?")

70 71
    self.include_directory = os.path.dirname(candidates[0])
    self.version = libsvm_version(candidates[0])
André Anjos's avatar
André Anjos committed
72 73 74 75 76 77

    # normalize
    self.include_directory = os.path.normpath(self.include_directory)

    # find library
    prefix = os.path.dirname(os.path.dirname(self.include_directory))
André Anjos's avatar
André Anjos committed
78 79
    module = 'svm'
    candidates = find_library(module, version=self.version,
80
            prefixes=[prefix], only_static=False)
André Anjos's avatar
André Anjos committed
81 82 83 84 85 86 87 88 89 90

    if not candidates:
      raise RuntimeError("cannot find required libsvm binary module `%s' - make sure libsvm is installed on `%s'" % (module, prefix))

    # libraries
    self.libraries = []
    name, ext = os.path.splitext(os.path.basename(candidates[0]))
    if ext in ['.so', '.a', '.dylib', '.dll']:
      self.libraries.append(name[3:]) #strip 'lib' from the name
    else: #link against the whole thing
91
      self.libraries.append(':' + os.path.basename(candidates[0]))
André Anjos's avatar
André Anjos committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105

    # library path
    self.library_directory = os.path.dirname(candidates[0])

  def macros(self):
    """Returns package availability and version number macros

    This method returns a python list with 2 macros indicating package
    availability and a version number, using standard GNU compatible names.
    Example:

    .. doctest::
       :options: +NORMALIZE_WHITESPACE +ELLIPSIS

André Anjos's avatar
André Anjos committed
106
       >>> from bob.learn.libsvm import libsvm
André Anjos's avatar
André Anjos committed
107 108
       >>> pkg = libsvm()
       >>> pkg.macros()
109
       [('HAVE_LIBSVM', '1')]
André Anjos's avatar
André Anjos committed
110 111

    """
112
    return [('HAVE_LIBSVM', '1')]
André Anjos's avatar
André Anjos committed
113 114

pkg = libsvm()
115
system_include_dirs = [pkg.include_directory]
André Anjos's avatar
André Anjos committed
116 117 118 119 120 121
library_dirs = [pkg.library_directory]
libraries = pkg.libraries
define_macros = pkg.macros()

setup(

André Anjos's avatar
André Anjos committed
122
    name='bob.learn.libsvm',
André Anjos's avatar
André Anjos committed
123
    version=version,
André Anjos's avatar
André Anjos committed
124 125
    description='Python bindings to LIBSVM for Bob',
    url='http://gitlab.idiap.ch/bob/bob.learn.libsvm',
André Anjos's avatar
André Anjos committed
126 127 128 129 130 131 132 133
    license='BSD',
    author='Andre Anjos',
    author_email='andre.anjos@idiap.ch',

    long_description=open('README.rst').read(),

    packages=find_packages(),
    include_package_data=True,
André Anjos's avatar
André Anjos committed
134
    zip_safe=False,
André Anjos's avatar
André Anjos committed
135

136 137
    setup_requires = build_requires,
    install_requires = build_requires,
André Anjos's avatar
André Anjos committed
138

139

André Anjos's avatar
André Anjos committed
140 141

    ext_modules = [
André Anjos's avatar
André Anjos committed
142
      Extension("bob.learn.libsvm.version",
André Anjos's avatar
André Anjos committed
143
        [
André Anjos's avatar
André Anjos committed
144
          "bob/learn/libsvm/version.cpp",
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        ],
        bob_packages = bob_packages,
        version = version,
        system_include_dirs = system_include_dirs,
        define_macros = define_macros,
        library_dirs = library_dirs,
        libraries = libraries,
      ),

      Library("bob.learn.libsvm.bob_learn_libsvm",
        [
          "bob/learn/libsvm/cpp/file.cpp",
          "bob/learn/libsvm/cpp/machine.cpp",
          "bob/learn/libsvm/cpp/trainer.cpp",
        ],
        bob_packages = bob_packages,
André Anjos's avatar
André Anjos committed
161
        version = version,
162
        system_include_dirs = system_include_dirs,
André Anjos's avatar
André Anjos committed
163 164 165
        define_macros = define_macros,
        library_dirs = library_dirs,
        libraries = libraries,
166 167 168 169
        packages = packages,
        boost_modules = boost_modules,
      ),

André Anjos's avatar
André Anjos committed
170
      Extension("bob.learn.libsvm._library",
André Anjos's avatar
André Anjos committed
171
        [
172
          "bob/learn/libsvm/utils.cpp",
André Anjos's avatar
André Anjos committed
173 174 175 176
          "bob/learn/libsvm/file.cpp",
          "bob/learn/libsvm/machine.cpp",
          "bob/learn/libsvm/trainer.cpp",
          "bob/learn/libsvm/main.cpp",
177 178
        ],
        bob_packages = bob_packages,
André Anjos's avatar
André Anjos committed
179
        version = version,
180
        system_include_dirs = system_include_dirs,
André Anjos's avatar
André Anjos committed
181 182 183
        define_macros = define_macros,
        library_dirs = library_dirs,
        libraries = libraries,
184 185 186 187 188 189 190 191
        packages = packages,
        boost_modules = boost_modules,
      ),
    ],

    cmdclass = {
      'build_ext': build_ext
    },
André Anjos's avatar
André Anjos committed
192 193 194

    entry_points={
      'console_scripts': [
195 196
      ],
    },
André Anjos's avatar
André Anjos committed
197 198

    classifiers = [
199 200
      'Framework :: Bob',
      'Development Status :: 4 - Beta',
André Anjos's avatar
André Anjos committed
201 202 203 204 205 206
      'Intended Audience :: Developers',
      'License :: OSI Approved :: BSD License',
      'Natural Language :: English',
      'Programming Language :: Python',
      'Programming Language :: Python :: 3',
      'Topic :: Software Development :: Libraries :: Python Modules',
207
    ],
André Anjos's avatar
André Anjos committed
208

209
)