source: trunk/setup.py @ 754

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