1#!/usr/bin/env python
2# Copyright (c) 2015-2019 by Farsight Security, Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16NAME = 'pydnstable'
17VERSION = '0.7.2'
18LICENSE = 'Apache License 2.0'
19DESCRIPTION = 'Python extension module for the dnstable C library'
20URL = 'https://github.com/farsightsec/pydnstable'
21AUTHOR = 'Farsight Security, Inc.'
22AUTHOR_EMAIL = 'software@farsightsecurity.com'
23
24
25import os
26from distutils.core import setup, Command
27from distutils.extension import Extension
28from distutils.command.clean import clean
29import unittest
30
31class Test(Command):
32    user_options = []
33    def initialize_options(self):
34        pass
35
36    def finalize_options(self):
37        pass
38
39    def run(self):
40        unittest.TextTestRunner(verbosity=1).run(
41            unittest.TestLoader().discover('tests'))
42
43class Cleaner(clean):
44    def run(self):
45        clean.run(self)
46        for i in ["dnstable.c"]:
47            if os.path.isfile(i):
48                print("Cleaning ", i)
49                os.unlink(i)
50
51def pkgconfig(*packages, **kw):
52    import subprocess
53    flag_map = {
54            '-I': 'include_dirs',
55            '-L': 'library_dirs',
56            '-l': 'libraries'
57    }
58
59    pkg_config_cmd = (
60        'pkg-config',
61        '--cflags',
62        '--libs',
63        ' '.join(packages),
64    )
65
66    pkg_config_output = subprocess.check_output(pkg_config_cmd,
67                                                universal_newlines=True)
68
69    for token in pkg_config_output.split():
70        flag = token[:2]
71        arg = token[2:]
72        if flag in flag_map:
73            kw.setdefault(flag_map[flag], []).append(arg)
74    return kw
75
76try:
77    from Cython.Distutils import build_ext
78    setup(
79        name = NAME,
80        version = VERSION,
81        license = LICENSE,
82        description = DESCRIPTION,
83        url = URL,
84        author = AUTHOR,
85        author_email = AUTHOR_EMAIL,
86        ext_modules = [ Extension('dnstable', ['dnstable.pyx'], **pkgconfig('libdnstable >= 0.11.2')) ],
87        cmdclass = {'build_ext': build_ext, 'clean': Cleaner, 'test': Test}
88    )
89except ImportError:
90    import os
91    if os.path.isfile('dnstable.c'):
92        setup(
93            name = NAME,
94            version = VERSION,
95            license = LICENSE,
96            description = DESCRIPTION,
97            url = URL,
98            author = AUTHOR,
99            author_email = AUTHOR_EMAIL,
100            ext_modules = [ Extension('dnstable', ['dnstable.c'], **pkgconfig('libdnstable >= 0.11.0')) ],
101        )
102    else:
103        raise
104