source: trunk/module/setup.py @ 691

Last change on this file since 691 was 691, checked in by cito, 4 years ago

Print a warning when the PostgreSQL version is too old

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 7.5 KB
Line 
1#! /usr/bin/python
2# $Id: setup.py 691 2016-01-02 23:35:15Z cito $
3
4"""Setup script for PyGreSQL version 5.0
5
6PyGreSQL is an open-source Python module that interfaces to a
7PostgreSQL database. It embeds the PostgreSQL query library to allow
8easy use of the powerful PostgreSQL features from a Python script.
9
10Authors and history:
11* PyGreSQL written 1997 by D'Arcy J.M. Cain <darcy@druid.net>
12* based on code written 1995 by Pascal Andre <andre@chimay.via.ecp.fr>
13* setup script created 2000/04 Mark Alexander <mwa@gate.net>
14* tweaked 2000/05 Jeremy Hylton <jeremy@cnri.reston.va.us>
15* win32 support 2001/01 by Gerhard Haering <gerhard@bigfoot.de>
16* tweaked 2006/02-2010/02 by Christoph Zwerschke <cito@online.de>
17
18Prerequisites to be installed:
19* Python including devel package (header files and distutils)
20* PostgreSQL libs and devel packages (header file of the libpq client)
21* PostgreSQL pg_config tool (usually included in the devel package)
22  (the Windows installer has it as part of the database server feature)
23
24The supported versions are Python 2.6-2.7, 3.3-3.5 and PostgreSQL 9.0-9.5.
25
26Use as follows:
27python setup.py build   # to build the module
28python setup.py install # to install it
29
30You can use MinGW or MinGW-w64 for building on Windows:
31python setup.py build -c mingw32 install
32
33See docs.python.org/doc/install/ for more information on
34using distutils to install Python programs.
35
36"""
37
38version = '5.0a0'
39
40
41import sys
42
43if (not (2, 6) <= sys.version_info[:2] <= (2, 7)
44        and not (3, 3) <= sys.version_info[:2] <= (3, 5)):
45    raise Exception("Sorry, PyGreSQL %s"
46        " does not support this Python version" % version)
47
48
49import os
50import platform
51import warnings
52try:
53    from setuptools import setup
54except ImportError:
55    from distutils.core import setup
56from distutils.extension import Extension
57from distutils.command.build_ext import build_ext
58from distutils.ccompiler import get_default_compiler
59from distutils.sysconfig import get_python_inc, get_python_lib
60
61
62def pg_config(s):
63    """Retrieve information about installed version of PostgreSQL."""
64    f = os.popen('pg_config --%s' % s)
65    d = f.readline().strip()
66    if f.close() is not None:
67        raise Exception("pg_config tool is not available.")
68    if not d:
69        raise Exception("Could not get %s information." % s)
70    return d
71
72
73def pg_version():
74    """Return the PostgreSQL version as a tuple of integers."""
75    parts = []
76    for part in pg_config('version').split()[-1].split('.'):
77        if part.isdigit():
78            part = int(part)
79        parts.append(part)
80    return tuple(parts or [9, 0])
81
82
83pg_version = pg_version()
84py_modules = ['pg', 'pgdb']
85libraries = ['pq']
86# Make sure that the Python header files are searched before
87# those of PostgreSQL, because PostgreSQL can have its own Python.h
88include_dirs = [get_python_inc(), pg_config('includedir')]
89library_dirs = [get_python_lib(), pg_config('libdir')]
90define_macros = [('PYGRESQL_VERSION', version)]
91undef_macros = []
92extra_compile_args = ['-O2', '-Wall', '-Werror', '-funsigned-char']
93
94
95class build_pg_ext(build_ext):
96    """Customized build_ext command for PyGreSQL."""
97
98    description = "build the PyGreSQL C extension"
99
100    user_options = build_ext.user_options + [
101        ('direct-access', None,
102            "enable direct access functions"),
103        ('large-objects', None,
104            "enable large object support"),
105        ('default-vars', None,
106            "enable default variables use"),
107        ('escaping-funcs', None,
108            "enable string escaping functions")]
109
110    boolean_options = build_ext.boolean_options + [
111        'direct-access', 'large-objects', 'default-vars', 'escaping-funcs']
112
113    def get_compiler(self):
114        """Return the C compiler used for building the extension."""
115        return self.compiler or get_default_compiler()
116
117    def initialize_options(self):
118        build_ext.initialize_options(self)
119        self.direct_access = True
120        self.large_objects = True
121        self.default_vars = True
122        self.escaping_funcs = pg_version[0] >= 9
123        if pg_version < (9, 0):
124            warnings.warn("PygreSQL does not support this PostgreSQL version.")
125
126    def finalize_options(self):
127        """Set final values for all build_pg options."""
128        build_ext.finalize_options(self)
129        if self.direct_access:
130            define_macros.append(('DIRECT_ACCESS', None))
131        if self.large_objects:
132            define_macros.append(('LARGE_OBJECTS', None))
133        if self.default_vars:
134            define_macros.append(('DEFAULT_VARS', None))
135        if self.escaping_funcs and pg_version[0] >= 9:
136            define_macros.append(('ESCAPING_FUNCS', None))
137        if sys.platform == 'win32':
138            bits = platform.architecture()[0]
139            if bits == '64bit':  # we need to find libpq64
140                for path in os.environ['PATH'].split(os.pathsep) + [
141                        r'C:\Program Files\PostgreSQL\libpq64']:
142                    library_dir = os.path.join(path, 'lib')
143                    if not os.path.isdir(library_dir):
144                        continue
145                    lib = os.path.join(library_dir, 'libpqdll.')
146                    if not (os.path.exists(lib + 'lib')
147                            or os.path.exists(lib + 'a')):
148                        continue
149                    include_dir = os.path.join(path, 'include')
150                    if not os.path.isdir(include_dir):
151                        continue
152                    if library_dir not in library_dirs:
153                        library_dirs.insert(1, library_dir)
154                    if include_dir not in include_dirs:
155                        include_dirs.insert(1, include_dir)
156                    libraries[0] += 'dll'  # libpqdll instead of libpq
157                    break
158            compiler = self.get_compiler()
159            if compiler == 'mingw32':  # MinGW
160                if bits == '64bit':  # needs MinGW-w64
161                    define_macros.append(('MS_WIN64', None))
162            elif compiler == 'msvc':  # Microsoft Visual C++
163                libraries[0] = 'lib' + libraries[0]
164
165
166setup(
167    name="PyGreSQL",
168    version=version,
169    description="Python PostgreSQL Interfaces",
170    long_description=__doc__.split('\n\n', 2)[1],  # first passage
171    keywords="pygresql postgresql database api dbapi",
172    author="D'Arcy J. M. Cain",
173    author_email="darcy@PyGreSQL.org",
174    url="http://www.pygresql.org",
175    download_url="ftp://ftp.pygresql.org/pub/distrib/",
176    platforms=["any"],
177    license="Python",
178    py_modules=py_modules,
179    ext_modules=[Extension('_pg', ['pgmodule.c'],
180        include_dirs=include_dirs, library_dirs=library_dirs,
181        define_macros=define_macros, undef_macros=undef_macros,
182        libraries=libraries, extra_compile_args=extra_compile_args)],
183    zip_safe=False,
184    cmdclass=dict(build_ext=build_pg_ext),
185    test_suite='tests.discover',
186    classifiers=[
187        "Development Status :: 6 - Mature",
188        "Intended Audience :: Developers",
189        "License :: OSI Approved :: Python Software Foundation License",
190        "Operating System :: OS Independent",
191        "Programming Language :: C",
192        'Programming Language :: Python',
193        'Programming Language :: Python :: 2',
194        'Programming Language :: Python :: 2.6',
195        'Programming Language :: Python :: 2.7',
196        'Programming Language :: Python :: 3',
197        'Programming Language :: Python :: 3.3',
198        'Programming Language :: Python :: 3.4',
199        'Programming Language :: Python :: 3.5',
200        "Programming Language :: SQL",
201        "Topic :: Database",
202        "Topic :: Database :: Front-Ends",
203        "Topic :: Software Development :: Libraries :: Python Modules"]
204)
Note: See TracBrowser for help on using the repository browser.