-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
executable file
·183 lines (163 loc) · 4.91 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
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
#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
import os
import logging
import sys
from sys import platform
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from subprocess import Popen, CalledProcessError, check_output
project_dir = os.path.dirname(os.path.realpath(__file__))
# Excluding project directory from sys.path so that Sphinx
# wouldn't get confused about where to load the sources.
# This, however, implies that you _must_ install the project
# before you generate documentation.
sys.path = [x for x in sys.path if not x == project_dir]
class BuildWithYacc(build_ext):
yacc = 'bison', (3, 0, 4),
lex = 'flex', (2, 6, 4),
def has_yacc_and_lex(self):
finder = None
if platform == 'win32':
finder = 'where'
else:
finder = 'which'
result = 1
for (p, v) in (self.yacc, self.lex):
try:
out = check_output([finder, p]).strip()
if not out:
logging.warning('Did not find {}'.format(p))
result = 0
continue
out = check_output([p, '--version']).strip()
out = out.split(b'\n')[0].split(b' ').pop().split(b'.')
out = tuple(map(int, out))
if out < v:
logging.warning(
'Version of {} ({}) is too old, need {}'.format(
p, out, v,
),
)
result = 0
except: # noqa: E722
logging.warning('Did not find {}'.format(p))
result = 0
return result
def run_proc_with_setup(self, cwd, env, args):
try:
print(' '.join(args))
proc = Popen(args, env=env, cwd=cwd)
stdout, stderr = proc.communicate()
if stdout:
print(stdout)
if proc.returncode != 0:
raise Exception(str(stderr))
except CalledProcessError as e:
print(e.stderr)
raise
def generate_parser(self):
yacc_args = [
self.yacc[0],
'-t',
'-v',
'-d',
'-o',
'protopy.tab.c',
'protopy.y',
]
lex_args = [
self.lex[0],
'-d',
'--header-file=protopy.lex.h',
'-o',
'protopy.lex.c',
'protopy.l'
]
cwd = os.path.join(project_dir, 'protopy', 'lib')
env = dict(os.environ)
self.run_proc_with_setup(cwd, env, yacc_args)
self.run_proc_with_setup(cwd, env, lex_args)
def run(self):
if self.has_yacc_and_lex():
self.generate_parser()
else:
logging.warning(
'Did not find yacc and or lex, will use precompiled parser',
)
super().run()
apr_lib_candidates = (
'/usr/include/apr-1.0',
'/usr/include/apr-1',
)
apr_lib = None
extra_compile_args = None
package_data = {}
library_dirs = None
data_files = None
if platform == 'win32':
apr_lib = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'include/apr',
)
package_data = {
'lib': ['lib/libapr-1.dll'],
}
library_dirs = ['lib/apr']
data_files = [('lib', ['lib/libapr-1.dll'])]
else:
extra_compile_args = ['-std=c11']
for lib in apr_lib_candidates:
if os.path.isdir(lib):
apr_lib = lib
break
if not apr_lib:
raise Exception('Cannot find Apache Portable Runtime headers')
setup(
cmdclass={
'build_ext': BuildWithYacc,
},
packages=['protopy'],
name='protopy',
version='0.0.4',
description='Parser for Google Protobuf',
author='Oleg Sivokon',
author_email='olegs@traiana.com',
url='https://github.com/wvxvw-traiana/protopy',
license='LGPL v3',
scripts=['bin/protopy'],
package_data=package_data,
data_files=data_files,
ext_package='protopy',
command_options={
'build_sphinx': {
'source_dir': ('setup.py', 'source')
}
},
ext_modules=[
Extension(
'wrapped',
[
'protopy/lib/helpers.c',
'protopy/lib/descriptors.c',
'protopy/lib/binparser.c',
'protopy/lib/serializer.c',
'protopy/lib/defparser.c',
'protopy/lib/protopy.lex.c',
'protopy/lib/protopy.tab.c',
'protopy/lib/pyhelpers.c',
'protopy/lib/apr_adaptor.c',
'protopy/wrapper.c',
],
include_dirs=[apr_lib],
library_dirs=library_dirs,
libraries=['apr-1'],
py_limited_api=False,
extra_compile_args=extra_compile_args,
)
],
setup_requires=['sphinx'],
install_requires=[
'pytest >= 3.4.2',
],
)