source: branches/4.x/setup.py @ 711

Last change on this file since 711 was 711, checked in by cito, 3 years ago

Flatten the directory structure of the project

Simplified the directory structure by flattening the "module" subdirectory out
to the root directory. That way, the setup.py script can now also access the
top-level docs subdirectory, so it could also install or build the docs.
There was nothing else in the root directory anyway, except the mkdocs and
mktar scripts which could be made unnecessary through setup.py.

Also made the setup script a bit clearer. Removed the note about MinGW for
Windows since the Microsoft compiler for Python 2.7 and Visual Studio Community
are now freely available including 64bit compilers, and produce less problems.

Note that the usual structure would have been to use a "pygresql" package
directory instead of the "module" directory. But since we install PyGreSQL
as two top-level modules "pg" and "pgdb" instead of a package "pygresql",
the flattened structure reflects that much better. For historical reasons
and people don't want to rewrite import statements, we will keep it that way.

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