|
| 1 | +## |
| 2 | +# copyright 2009, James William Pye |
| 3 | +# http://python.projects.postgresql.org |
| 4 | +## |
| 5 | +""" |
| 6 | +Python distutils data provisions module. |
| 7 | +
|
| 8 | +For sub-packagers, the `prefixed_packages` and `prefixed_extensions` functions |
| 9 | +should be of particular interest. It is not recommended that sub-packagers |
| 10 | +include the `scripts` keyword from py-postgresql in their setup() call. |
| 11 | +If the distribution including ``py-postgresql`` uses the standard layout, |
| 12 | +chances are that `prefixed_extensions` and `prefixed_packages` will supply the |
| 13 | +appropriate information by default as they use `default_prefix` which is derived |
| 14 | +from the module's `__package__`. |
| 15 | +""" |
| 16 | +import sys |
| 17 | +import os |
| 18 | +from .. import \ |
| 19 | + __version__ as version, \ |
| 20 | + __project__ as name, \ |
| 21 | + __project_id__ as url |
| 22 | +from distutils.core import Extension |
| 23 | + |
| 24 | +LONG_DESCRIPTION = """ |
| 25 | +py-postgresql is a set of Python modules providing interfaces to various parts |
| 26 | +of PostgreSQL. Notably, it provides a driver for querying a PostgreSQL database. |
| 27 | +
|
| 28 | +Sample PG-API Code |
| 29 | +------------------ |
| 30 | +
|
| 31 | + >>> import postresql.driver as pg_driver |
| 32 | + >>> db = pg_driver.connect(user = 'mydbuser', host = 'localhost', port = 5432, database = 'mydbname') |
| 33 | + >>> db.execute("CREATE TABLE emp (emp_first_name text, emp_last_name text, emp_salary numeric)") |
| 34 | + >>> make_emp = db.prepare("INSERT INTO emp VALUES ($1, $2, $3)") |
| 35 | + >>> make_emp("John", "Doe", "75,322") |
| 36 | + >>> with db.xact: |
| 37 | + ... make_emp("Jane", "Doe", "75,322") |
| 38 | + ... make_emp("Edward", "Johnson", "82,744") |
| 39 | + ... |
| 40 | +
|
| 41 | +There is a DB-API 2.0 module as well:: |
| 42 | +
|
| 43 | + postgresql.driver.dbapi20 |
| 44 | +
|
| 45 | +However, PG-API is recommended as it provides greater utility. |
| 46 | +
|
| 47 | +Once you get it installed, try out the ``pg_python`` command:: |
| 48 | +
|
| 49 | + $ pg_python -h localhost -U theuser -d database_name |
| 50 | +
|
| 51 | +That should give you a Python console with the database connection bound to the |
| 52 | +`db` name. |
| 53 | +
|
| 54 | +History |
| 55 | +------- |
| 56 | +
|
| 57 | +py-postgresql is not yet another PostgreSQL driver, it's been in development for |
| 58 | +years. py-postgresql is the Python 3.0 port of the ``pg_proboscis`` driver and |
| 59 | +integration of the other ``pg/python`` projects. |
| 60 | +
|
| 61 | +
|
| 62 | +More Information |
| 63 | +---------------- |
| 64 | +
|
| 65 | +http://python.projects.postgresql.org |
| 66 | +""" |
| 67 | + |
| 68 | +CLASSIFIERS = [ |
| 69 | + 'Development Status :: 5 - Production/Stable', |
| 70 | + 'Intended Audience :: Developers', |
| 71 | + 'License :: OSI Approved :: BSD License', |
| 72 | + 'License :: OSI Approved :: MIT License', |
| 73 | + 'License :: OSI Approved :: Attribution Assurance License', |
| 74 | + 'License :: OSI Approved :: Python Software Foundation License', |
| 75 | + 'Natural Language :: English', |
| 76 | + 'Operating System :: OS Independent', |
| 77 | + 'Programming Language :: Python', |
| 78 | + 'Programming Language :: Python :: 3', |
| 79 | + 'Topic :: Database', |
| 80 | +] |
| 81 | + |
| 82 | +subpackages = [ |
| 83 | + 'bin', |
| 84 | + 'encodings', |
| 85 | + 'protocol', |
| 86 | + 'driver', |
| 87 | + 'test', |
| 88 | + 'documentation', |
| 89 | + 'python', |
| 90 | + 'release', |
| 91 | + # Modules imported from other packages. |
| 92 | + 'resolved', |
| 93 | +] |
| 94 | +extensions_data = { |
| 95 | + 'protocol.cbuffer' : { |
| 96 | + 'sources' : [os.path.join('protocol', 'buffer.c')], |
| 97 | + 'libraries' : (sys.platform == 'win32' and ['ws2_32'] or []), |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +scripts = [ |
| 102 | + 'postgresql/bin/pg_dotconf', |
| 103 | + 'postgresql/bin/pg_python', |
| 104 | + 'postgresql/bin/pg_tin', |
| 105 | + 'postgresql/bin/pg_withcluster' |
| 106 | +] |
| 107 | + |
| 108 | +try: |
| 109 | + # :) |
| 110 | + if __package__ is not None: |
| 111 | + default_prefix = __package__.split('.')[:-1] |
| 112 | + else: |
| 113 | + default_prefix = __name__.split('.')[:-2] |
| 114 | +except NameError: |
| 115 | + default_prefix = ['postgresql'] |
| 116 | + |
| 117 | +def prefixed_extensions( |
| 118 | + prefix : "prefix to prepend to paths" = default_prefix, |
| 119 | + extensions_data : "`extensions_data`" = extensions_data, |
| 120 | +) -> [Extension]: |
| 121 | + """ |
| 122 | + Generator producing the `distutils` `Extension` objects. |
| 123 | + """ |
| 124 | + pkg_prefix = '.'.join(prefix) + '.' |
| 125 | + path_prefix = os.path.sep.join(prefix) |
| 126 | + for mod, data in extensions_data.items(): |
| 127 | + yield Extension( |
| 128 | + pkg_prefix + mod, |
| 129 | + [os.path.join(path_prefix, src) for src in data['sources']], |
| 130 | + libraries = data['libraries'] |
| 131 | + ) |
| 132 | + |
| 133 | +def prefixed_packages( |
| 134 | + prefix : "prefix to prepend to source paths" = default_prefix, |
| 135 | + packages = subpackages, |
| 136 | +): |
| 137 | + """ |
| 138 | + Generator producing the standard `package` list prefixed with `prefix`. |
| 139 | + """ |
| 140 | + prefix = '.'.join(prefix) |
| 141 | + yield prefix |
| 142 | + prefix = prefix + '.' |
| 143 | + for pkg in packages: |
| 144 | + yield prefix + pkg |
| 145 | + |
| 146 | +def standard_setup_keywords(build_extensions = True, prefix = default_prefix): |
| 147 | + """ |
| 148 | + Used by the py-postgresql distribution. |
| 149 | + """ |
| 150 | + d = { |
| 151 | + 'name' : name, |
| 152 | + 'version' : version, |
| 153 | + 'description' : 'PostgreSQL tools pacakges. Driver, API specifications, and cluster tools.', |
| 154 | + 'long_description' : LONG_DESCRIPTION, |
| 155 | + 'author' : 'James William Pye', |
| 156 | + 'author_email' : 'x@jwp.name', |
| 157 | + 'maintainer' : 'James William Pye', |
| 158 | + 'maintainer_email' : 'python-general@pgfoundry.org', |
| 159 | + 'url' : url, |
| 160 | + 'classifiers' : CLASSIFIERS, |
| 161 | + 'packages' : list(prefixed_packages(prefix = prefix)), |
| 162 | + 'scripts' : scripts, |
| 163 | + } |
| 164 | + if build_extensions: |
| 165 | + d['ext_modules'] = list(prefixed_extensions(prefix = prefix)) |
| 166 | + return d |
0 commit comments