aboutsummaryrefslogtreecommitdiff
path: root/setup.py
blob: 7cae721b771418a9c44b6886df29384e546d7938 (plain)
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
#
#    Copyright 2010 Leonard Thomas
#
#    This file is part of Dodai.
#
#    Dodai is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Dodai is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Dodai.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import platform
from setuptools import setup
from setuptools import find_packages
from ctypes.util import find_library
from ctypes.util import _findLib_ldconfig as find_library_ldconfig
from subprocess import Popen, PIPE
from copy import deepcopy

ARGS = {
    'name': 'dodai',
    'version': '0.4.1',
    'install_requires': [
        'sqlalchemy',
        'chardet',
        'couchdb',
        'ordereddict',
        'psycopg2',
        'mysql-python',
        'cx_oracle'
    ],
    'platforms': [
        'Linux',
        'Darwin',
    ],
    'author': 'Leonard Thomas',
    'author_email': 'six@choushi.net',
    'url': 'http://code.google.com/p/dodai',
    'download_url': 'http://code.google.com/p/dodai/downloads/list',
    'license': 'GPLv3',
    'classifiers': [
        'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU Affero General Public License v3',
        'Natural Language :: English',
        'Operating System :: POSIX',
        'Operating System :: POSIX :: Linux',
        'Operating System :: MacOS',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Topic :: Database',
        'Topic :: Software Development',
        'Topic :: Software Development :: Libraries',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],
    'description': "Module code for quick easy access to parsed text based "\
        "config file data and configured database engines.",
    'long_description': "This module provides code for quick easy access to "\
        "parsed text based config file data and configured database "\
        "engines.  All config file data is returned ordered and transformed "\
        "to unicode objects and database connections are returned as "\
        "sqlalchemy objects.",
    'entry_points': {
        'distutils.commands': [
            'devstrap = dodai.build:devstrap',
        ]
    },
    'zip_safe': False,
    'packages': find_packages('lib'),
    'package_dir': {'':'lib'},
}
PYTHON_VERSION_LOW = '2.6'
PYTHON_VERSION_HIGH = '3.0'

class BaseLibCheck(object):

    def _has_library(self, lib):
        if find_library(lib):
            return True
        return False

    def _which(self, name):
        cmd = ['which', name]
        return Popen(cmd, stdout=PIPE).communicate()[0]


class PostgressLibCheck(BaseLibCheck):

    PACKAGE = 'psycopg2'
    LIB = 'pq'

    def __call__(self, package):
        if package.lower().startswith(self.PACKAGE):
            if self._has_library(self.LIB):
                return True
            return False


class MysqlLibCheck(BaseLibCheck):

    PACKAGE = 'mysql-python'
    LIB = 'mysqlpp'

    def __call__(self, package):
        if package.lower().startswith(self.PACKAGE):
            if self._has_library(self.LIB):
                if self._which('mysql_config'):
                    return True
            return False


class OracleLibCheck(BaseLibCheck):

    PACKAGE = 'cx_oracle'
    LIB = 'clntsh'

    def __call__(self, package):
        if package.lower().startswith(self.PACKAGE):
            if 'ORACLE_HOME' in os.environ:
                if os.environ['ORACLE_HOME']:
                    return True
            else:
                if self._has_library(self.LIB):
                    self._set_oracle_home()
                    return True
            return False

    def _set_oracle_home(self):
        path = find_library_ldconfig(self.LIB)
        os.environ['ORACLE_HOME'] = os.path.dirname(path)


class OrderedDictLibCheck(BaseLibCheck):

    PACKAGE = 'ordereddict'

    def __call__(self, package):
        if package.lower().startswith(self.PACKAGE):
            if sys.version < '2.7':
                return True
            return False


class ArgparseLibCheck(BaseLibCheck):

    PACKAGE = 'argparse'

    def __call__(self, package):
        if package.lower().startswith(self.PACKAGE):
            if sys.version < '2.7':
                return True
            return False


class PassLibCheck(BaseLibCheck):

    def __init__(self, chain):
        self._packages = []
        for obj in chain:
            self._packages.append(obj.PACKAGE.lower())

    def __call__(self, package):
        if package.lower() not in self._packages:
            return True

def get_install_requires():
    out = []
    chain = [
        PostgressLibCheck(),
        MysqlLibCheck(),
        OracleLibCheck(),
        OrderedDictLibCheck(),
        ArgparseLibCheck(),
    ]
    chain.append(PassLibCheck(chain))
    for package in ARGS['install_requires']:
        for can_install in chain:
            if can_install(package):
                out.append(package)
    return out

def valid_python_version():
    if sys.version < PYTHON_VERSION_LOW:
        return False
    if sys.version >= PYTHON_VERSION_HIGH:
        return False
    return True

def build_args():
    args = deepcopy(ARGS)
    args['install_requires'] = get_install_requires()
    return args

if not valid_python_version():
    message = """
        {package} is not able to install:  Because the version of
        Python that is being used, '{version}', is not compatable
        with {package}.  {package} will only install with Python
        versions {low} through {high}\n
    """.format(package=ARGS['name'].lower().capitalize(),
               low=PYTHON_VERSION_LOW, high=PYTHON_VERSION_HIGH)
    sys.stderr.write(message)
    sys.exit(1)
else:
    setup(**build_args())