Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 275 lines (229 sloc) 9.384 kb
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
# On Linux, install libreadline6-dev before attempting to build rl.
# On Mac OS X, make sure you have Xcode Tools installed.

from __future__ import with_statement

import sys
import os
import re

from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext
from distutils.sysconfig import get_config_var
from distutils.spawn import find_executable
from distutils import log
from os.path import join, exists

version = '2.5'


def sys_path_contains(string):
    for dir in sys.path:
        if dir.startswith(string):
            return True


class ReadlineExtension(Extension):

    def __init__(self, name):
        # Describe the extension
        sources = [
            'rl/readline.c',
            'rl/stringarray.c',
            'rl/unicode.c',
            'rl/iterator.c',
        ]
        libraries = ['readline']
        Extension.__init__(self, name, sources, libraries=libraries)

        # Use include and library dirs from Python build
        self.use_include_dirs()
        self.use_library_dirs()

        # Force static build if environment variable is set
        if os.environ.get('RL_BUILD_STATIC_READLINE') and self.have_curl():
            self.use_static_readline()

        # Mac OS X ships with libedit which we cannot use
        elif sys.platform == 'darwin':
            # System Python
            if sys_path_contains('/System/Library/Frameworks/Python.framework'):
                self.use_static_readline()
            # Mac Python
            elif sys_path_contains('/Library/Frameworks/Python.framework'):
                self.use_static_readline()
                self.library_dirs.append(
                    '/Library/Frameworks/Python.framework/Versions/%s/lib' % sys.version[:3])
            # MacPorts Python
            elif '/opt/local/include' in self.include_dirs:
                pass
            # Fink Python
            elif '/sw/include' in self.include_dirs:
                pass
            # Other Python
            else:
                self.use_static_readline()

    def have_curl(self):
        if not find_executable('curl'):
            log.warn('WARNING: Cannot build statically. Command not found: curl')
            return False
        return True

    def use_include_dirs(self):
        cppflags = get_config_var('CPPFLAGS')

        for match in re.finditer(r'-I\s*(\S+)', cppflags):
            if match.group(1) not in ['.', 'Include', './Include']:
                self.include_dirs.append(match.group(1))

    def use_library_dirs(self):
        ldflags = get_config_var('LDFLAGS')

        for match in re.finditer(r'-L\s*(\S+)', ldflags):
            self.library_dirs.append(match.group(1))

    def suppress_warnings(self):
        cflags = get_config_var('CFLAGS')
        cflags = cflags.split()

        # -Wno-all is not supported by gcc < 4.2
        if sys.platform == 'darwin':
            self.extra_compile_args.append('-Wno-all')

        if '-Wstrict-prototypes' in cflags:
            self.extra_compile_args.append('-Wno-strict-prototypes')

    def use_static_readline(self):
        self.sources.extend([
            'build/readline/bind.c',
            'build/readline/callback.c',
            'build/readline/compat.c',
            'build/readline/complete.c',
            'build/readline/display.c',
            'build/readline/funmap.c',
            'build/readline/histexpand.c',
            'build/readline/histfile.c',
            'build/readline/history.c',
            'build/readline/histsearch.c',
            'build/readline/input.c',
            'build/readline/isearch.c',
            'build/readline/keymaps.c',
            'build/readline/kill.c',
            'build/readline/macro.c',
            'build/readline/mbutil.c',
            'build/readline/misc.c',
            'build/readline/nls.c',
            'build/readline/parens.c',
            'build/readline/readline.c',
            'build/readline/rltty.c',
            'build/readline/savestring.c',
            'build/readline/search.c',
            'build/readline/shell.c',
            'build/readline/signals.c',
            'build/readline/terminal.c',
            'build/readline/text.c',
            'build/readline/tilde.c',
            'build/readline/undo.c',
            'build/readline/util.c',
            'build/readline/vi_mode.c',
            'build/readline/xmalloc.c',
            'build/readline/xfree.c',
        ])

        self.define_macros.extend([
            ('HAVE_CONFIG_H', None),
            ('RL_LIBRARY_VERSION', '"6.2"'),
        ])

        self.include_dirs = ['build', 'build/readline'] + self.include_dirs
        self.libraries.remove('readline')

        self.suppress_warnings()


class ReadlineExtensionBuilder(build_ext):

    def build_extension(self, ext):
        lib_dynload = join(sys.exec_prefix, 'lib', 'python%s' % sys.version[:3], 'lib-dynload')
        lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib', '/usr/local/lib']
        lib_dirs = ext.library_dirs + self.compiler.library_dirs + lib_dirs

        # Find a termcap library
        termcap = ''

        if self.can_inspect_libraries():

            if 'readline' in ext.libraries:
                readline = self.compiler.find_library_file(lib_dirs, 'readline')
                termcap = self.get_termcap_from(readline)

            if not termcap:
                pyreadline = join(lib_dynload, 'readline.so')
                termcap = self.get_termcap_from(pyreadline)

            if not termcap:
                pycurses = join(lib_dynload, '_curses.so')
                termcap = self.get_termcap_from(pycurses)

            if termcap and not self.compiler.find_library_file(lib_dirs, termcap):
                termcap = ''

        if not termcap:
            for name in ['tinfo', 'ncursesw', 'ncurses', 'cursesw', 'curses', 'termcap']:
                if self.compiler.find_library_file(lib_dirs, name):
                    termcap = name
                    break

        if termcap:
            ext.libraries.append(termcap)
        else:
            log.warn('WARNING: Failed to find a termcap library')

        # Prepare the source tree
        if 'readline' not in ext.libraries:
            self.configure_static_readline()

        return build_ext.build_extension(self, ext)

    def can_inspect_libraries(self):
        if sys.platform == 'darwin':
            cmd = 'otool'
        else:
            cmd = 'ldd'
        if not find_executable(cmd):
            log.warn('WARNING: Command not found: %s' % cmd)
            return False
        return True

    def get_termcap_from(self, module):
        if module and exists(module):
            if sys.platform == 'darwin':
                cmd = 'otool -L "%s"' % module
            else:
                cmd = 'ldd "%s"' % module
            with os.popen(cmd) as fp:
                libraries = fp.read()
            for name in ['tinfo', 'ncursesw', 'ncurses', 'cursesw', 'curses', 'termcap']:
                 if 'lib%s.' % name in libraries:
                    return name
        return ''

    def configure_static_readline(self):
        tarball = 'http://ftp.gnu.org/gnu/readline/readline-6.2.tar.gz'
        patches = 'http://ftp.gnu.org/gnu/readline/readline-6.2-patches'
        have_patch = find_executable('patch') and 'True' or 'False'
        stdout = ''

        if not self.distribution.verbose:
            stdout = '>%s' % os.devnull

        if not exists(join('build', 'readline', 'config.h')):
            os.system("""\
mkdir -p build
cd build
rm -rf readline-6.2 readline
echo downloading %(tarball)s %(stdout)s
curl --connect-timeout 30 -s %(tarball)s | tar zx
mv readline-6.2 readline
cd readline
if [ "%(have_patch)s" = "True" ]; then
curl --connect-timeout 30 -s %(patches)s/readline62-001 | patch -p0 %(stdout)s
curl --connect-timeout 30 -s %(patches)s/readline62-002 | patch -p0 %(stdout)s
fi
./configure %(stdout)s
""" % locals())


setup(name='rl',
      version=version,
      description='Alternative Python bindings for GNU Readline',
      long_description=open('README.txt').read() + '\n' +
                       open('CHANGES.txt').read(),
      classifiers=[
          'Development Status :: 5 - Production/Stable',
          'Intended Audience :: Developers',
          'License :: OSI Approved :: GNU General Public License (GPL)',
          'License :: OSI Approved :: Python Software Foundation License',
          'Operating System :: MacOS :: MacOS X',
          'Operating System :: POSIX',
          'Programming Language :: C',
          'Programming Language :: Python :: 2',
          'Programming Language :: Python :: 3',
      ],
      keywords='gnu readline bindings',
      author='Stefan H. Holek',
      author_email='stefan@epy.co.at',
      url='http://pypi.python.org/pypi/rl',
      license='GPL or PSF',
      packages=find_packages(),
      include_package_data=True,
      zip_safe=False,
      use_2to3=True,
      test_suite='rl.tests',
      ext_modules=[
          ReadlineExtension(name='rl.readline'),
      ],
      cmdclass={
          'build_ext': ReadlineExtensionBuilder,
      },
      install_requires=[
          'setuptools',
      ],
)

Something went wrong with that request. Please try again.