forked from appnexus/pyrobuf
/
setup.py
158 lines (129 loc) · 5.61 KB
/
setup.py
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
from distutils.command.clean import clean as _clean
from distutils.dir_util import remove_tree
from distutils import log
from setuptools import setup, find_packages, Command, Distribution
from setuptools.command.test import test as _test
import os
import os.path
import sys
VERSION = "0.5.7"
HERE = os.path.dirname(os.path.abspath(__file__))
PYROBUF_LIST_PXD = "pyrobuf_list.pxd"
PYROBUF_LIST_PYX = "pyrobuf_list.pyx"
class clean(_clean):
def __clean_tree(self, tree):
for dirpath, dirnames, filenames in os.walk(tree):
for dirname in dirnames:
if dirname in ('__pycache__', 'build'):
remove_tree(os.path.join(dirpath, dirname), verbose=self.verbose, dry_run=self.dry_run)
for dirpath, dirnames, filenames in os.walk(tree):
for filename in filenames:
if any(filename.endswith(suffix) for suffix in (".so", ".pyd", ".dll", ".pyc")):
self.__remove_file(os.path.join(dirpath, filename))
continue
extension = os.path.splitext(filename)[1]
if extension in ['.c', '.h']:
pyx_file = str.replace(filename, extension, '.pyx')
if os.path.exists(os.path.join(dirpath, pyx_file)):
self.__remove_file(os.path.join(dirpath, filename))
def __remove_file(self, full_path):
if os.path.exists(full_path):
if not self.dry_run:
os.unlink(full_path)
if self.verbose >= 1:
log.info("removing '%s'" % full_path)
def run(self):
_clean.run(self)
self.__clean_tree(os.path.join(HERE, 'pyrobuf'))
self.__clean_tree(os.path.join(HERE, 'tests'))
tests_out = os.path.join(HERE, 'tests', 'out')
if os.path.isdir(tests_out):
remove_tree(tests_out, verbose=self.verbose, dry_run=self.dry_run)
self.__remove_file(os.path.join(HERE, 'pyrobuf', 'src', PYROBUF_LIST_PXD))
self.__remove_file(os.path.join(HERE, 'pyrobuf', 'src', PYROBUF_LIST_PYX))
for suffix in (".so", ".pyd"):
self.__remove_file(os.path.join(HERE, 'pyrobuf_list' + suffix))
self.__remove_file(os.path.join(HERE, 'pyrobuf_util' + suffix))
class test(_test):
def initialize_options(self):
_test.initialize_options(self)
self.pytest_args = []
def run_tests(self):
import pytest
errno = pytest.main(self.pytest_args)
sys.exit(errno)
class PyrobufDistribution(Distribution):
def run_commands(self):
# By now the setup_requires deps have been fetched.
if not self.ext_modules:
self.ext_modules = list()
self.ext_modules.extend(self.pyrobufize_builtins())
Distribution.run_commands(self)
def pyrobufize_builtins(self):
from jinja2 import Environment, PackageLoader
from Cython.Build import cythonize
env = Environment(loader=PackageLoader('pyrobuf.protobuf', 'templates'))
templ_pyx = env.get_template('pyrobuf_list_pyx.tmpl')
templ_pxd = env.get_template('pyrobuf_list_pxd.tmpl')
listdict = {
'DoubleList': 'double',
'FloatList': 'float',
'IntList': 'int',
'Int32List': 'int32_t',
'Uint32List': 'uint32_t',
'Int64List': 'int64_t',
'Uint64List': 'uint64_t',
'CharList': 'char'
}
format_map = {
'double': 'd',
'float': 'f',
'int': 'i',
'int32_t': 'i',
'uint32_t': 'I',
'int64_t': 'L',
'uint64_t': 'K',
'char': 'c',
}
path = os.path.join(HERE, 'pyrobuf', 'src', PYROBUF_LIST_PYX)
if not os.path.exists(path) or os.path.getmtime(path) < os.path.getmtime(templ_pyx.filename):
if not self.dry_run:
with open(path, 'w') as fp:
fp.write(templ_pyx.render(
{'def': listdict, 'version_major': sys.version_info.major, 'format_map': format_map}
))
if self.verbose >= 1:
log.info("rendering '%s' from '%s'" % (PYROBUF_LIST_PYX, templ_pyx.filename))
path = os.path.join(HERE, 'pyrobuf', 'src', PYROBUF_LIST_PXD)
if not os.path.exists(path) or os.path.getmtime(path) < os.path.getmtime(templ_pxd.filename):
if not self.dry_run:
with open(path, 'w') as fp:
fp.write(templ_pxd.render(
{'def': listdict, 'version_major': sys.version_info.major, 'format_map': format_map}
))
if self.verbose >= 1:
log.info("rendering '%s' from '%s'" % (PYROBUF_LIST_PXD, templ_pxd.filename))
return cythonize(['pyrobuf/src/*.pyx'],
include_path=['pyrobuf/src'])
setup(
distclass=PyrobufDistribution,
name="pyrobuf",
version=VERSION,
packages=find_packages(),
include_package_data=True,
cmdclass={'clean': clean, 'test': test},
entry_points={
'console_scripts': ['pyrobuf = pyrobuf.__main__:main'],
'distutils.setup_keywords': [
'pyrobuf_modules = pyrobuf.setuptools_ext:pyrobuf_modules',
],
},
description='A Cython based protobuf compiler',
long_description=open(os.path.join(HERE, 'README.md')).read(),
url='https://github.com/appnexus/pyrobuf',
author='AppNexus',
tests_require=['pytest', 'protobuf >= 2.6.0, <3'],
setup_requires=['jinja2', 'cython >= 0.23'],
install_requires=['jinja2', 'cython >= 0.23'],
zip_safe=False,
)