setup.py 4.79 KB
Newer Older
Christoph Groth's avatar
Christoph Groth committed
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python

# Copyright 2012-2013 Tinyarray authors.
#
# This file is part of Tinyarray.  It is subject to the license terms in the
# LICENSE file found in the top-level directory of this distribution and at
# http://kwant-project.org/tinyarray/license.  A list of Tinyarray authors can
# be found in the README file at the top-level directory of this distribution
# and at http://kwant-project.org/tinyarray.

import subprocess
import os
13
14
15
16
import sys
from distutils.core import setup, Extension, Command
from distutils.util import get_platform
from distutils.errors import DistutilsError, DistutilsModuleError
Christoph Groth's avatar
Christoph Groth committed
17
18
19

README_FILE = 'README'
STATIC_VERSION_FILE = 'src/version.hh'
20
TEST_MODULE = 'test_tinyarray.py'
Christoph Groth's avatar
Christoph Groth committed
21

Christoph Groth's avatar
Christoph Groth committed
22
23
24
25
26
27
28
29
30
31
32
33
34
CLASSIFIERS = """\
Development Status :: 5 - Production/Stable
Intended Audience :: Science/Research
Intended Audience :: Developers
License :: OSI Approved :: BSD License
Programming Language :: C++
Topic :: Software Development
Topic :: Scientific/Engineering
Operating System :: POSIX
Operating System :: Unix
Operating System :: MacOS
Operating System :: Microsoft :: Windows"""

Christoph Groth's avatar
Christoph Groth committed
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
tinyarray_dir = os.path.dirname(os.path.abspath(__file__))


def get_version_from_git():
    try:
        p = subprocess.Popen(['git', 'describe'], cwd=tinyarray_dir,
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except OSError:
        return

    if p.wait() != 0:
        return
    version = p.communicate()[0].strip()

    if version[0] == 'v':
        version = version[1:]

    try:
        p = subprocess.Popen(['git', 'diff', '--quiet'], cwd=tinyarray_dir)
    except OSError:
        version += '-confused'  # This should never happen.
    else:
        if p.wait() == 1:
            version += '-dirty'
    return version


def get_static_version():
    """Return the version as recorded inside a file."""
    try:
        with open(STATIC_VERSION_FILE) as f:
            contents = f.read()
            assert contents[:17] == '#define VERSION "'
            assert contents[-2:] == '"\n'
            return contents[17:-2]
    except:
        return None


def version():
    """Determine the version of Tinyarray.  Return it and save it in a file."""
    git_version = get_version_from_git()
    static_version = get_static_version()
    if git_version is not None:
        version = git_version
        if static_version != git_version:
            with open(STATIC_VERSION_FILE, 'w') as f:
                f.write('#define VERSION "{}"\n'.format(version))
    elif static_version is not None:
        version = static_version
    else:
        version = 'unknown'
    return version


def long_description():
    text = []
    skip = True
    try:
        with open(README_FILE) as f:
            for line in f:
                if line == "\n":
                    if skip:
                        skip = False
                        continue
                    else:
                        break
                if not skip:
                    text.append(line.rstrip())
    except:
        return ''
    return '\n'.join(text)


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
class test(Command):
    description = "run the unit tests"
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        try:
            from nose.core import run
        except ImportError:
            raise DistutilsModuleError('nose <http://nose.readthedocs.org/> '
                                       'is needed to run the tests')
        self.run_command('build')
        major, minor = sys.version_info[:2]
        lib_dir = "build/lib.{0}-{1}.{2}".format(get_platform(), major, minor)
        print '**************** Tests ****************'
        if not run(argv=[__file__, '-v', '-w', lib_dir,
                         '-w', '../../' + TEST_MODULE]):
            raise DistutilsError('at least one of the tests failed')


Christoph Groth's avatar
Christoph Groth committed
134
135
136
137
138
139
140
141
142
143
144
module = Extension('tinyarray',
                   language='c++',
                   sources=['src/arithmetic.cc', 'src/array.cc',
                            'src/functions.cc'],
                   depends=['src/arithmetic.hh', 'src/array.hh',
                            'src/conversion.hh', 'src/functions.hh'])


def main():
    setup(name='tinyarray',
          version=version(),
Christoph Groth's avatar
Christoph Groth committed
145
          author='Christoph Groth (CEA) and others',
Christoph Groth's avatar
Christoph Groth committed
146
147
148
149
          author_email='christoph.groth@cea.fr',
          description="Arrays of numbers for Python, optimized for small sizes",
          long_description=long_description(),
          url="http://kwant-project.org/tinyarray/",
Christoph Groth's avatar
Christoph Groth committed
150
151
          download_url="http://downloads.kwant-project.org/tinyarray/",
          license="Simplified BSD license",
Christoph Groth's avatar
Christoph Groth committed
152
          platforms=["Unix", "Linux", "Mac OS-X", "Windows"],
Christoph Groth's avatar
Christoph Groth committed
153
          classifiers=CLASSIFIERS.split('\n'),
154
          cmdclass={'test': test},
Christoph Groth's avatar
Christoph Groth committed
155
156
157
158
159
          ext_modules=[module])


if __name__ == '__main__':
    main()