This repository has been archived by the owner on Dec 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsetup.py
executable file
·198 lines (149 loc) · 6.23 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
# Copyright (C) 2011 Smarkets Limited <[email protected]>
#
# This module is released under the MIT License:
# http://www.opensource.org/licenses/mit-license.php
import glob
import io
import os
import shutil
import subprocess
import sys
from distutils.command import build, clean
from distutils.spawn import find_executable
from itertools import chain
from os.path import abspath, dirname, join
from setuptools import setup
PROJECT_ROOT = abspath(dirname(__file__))
sys.path.insert(0, PROJECT_ROOT)
def check_call(*args, **kwargs):
print('Calling %s, %s' % (args, kwargs,))
subprocess.check_call(*args, **kwargs)
ETO_PIQI_URL = 'https://raw.github.com/smarkets/eto_common/v1.2.3/eto.piqi'
SETO_PIQI_URL = 'https://raw.github.com/smarkets/smk_api_common/v6.6.0/seto.piqi'
def _safe_glob(pathname):
"Do a safe version of glob which copes with win32"
is_win32 = sys.platform == 'win32'
for source in glob.glob(pathname):
yield source.replace('/', '\\') if is_win32 else source
protobuf_modules = ['eto', 'seto']
def protobuf_module_file(name):
return join(PROJECT_ROOT, 'smarkets', 'streaming_api', '%s_pb2.py' % (name,))
class SmarketsProtocolBuild(build.build):
"Class to build the protobuf output"
description = "build the protocol buffer output with protobuf-compiler"
def download(self, url):
check_call((self.find('wget'), url))
def find(self, name):
result = find_executable(name)
if result is None:
raise Exception("*** Cannot find %s; make sure it's installed" % (name,))
return result
def run(self):
"Get the .piqi definitions and run the 'protoc' compiler command"
eto_piqi = join(PROJECT_ROOT, 'eto.piqi')
if not os.path.exists(eto_piqi):
self.download(ETO_PIQI_URL)
seto_piqi = join(PROJECT_ROOT, 'seto.piqi')
if not os.path.exists(seto_piqi):
self.download(SETO_PIQI_URL)
eto_proto = join(PROJECT_ROOT, 'smarkets.streaming_api.eto.proto')
if not os.path.exists(eto_proto):
check_call((self.find('piqi'), 'to-proto', eto_piqi, '-o', eto_proto))
seto_proto = join(PROJECT_ROOT, 'smarkets.streaming_api.seto.proto')
if not os.path.exists(seto_proto):
check_call((self.find('piqi'), 'to-proto', seto_piqi, '-o', seto_proto))
self.replace_file(seto_proto,
lambda line: line.replace(
'import "eto.piqi.proto"',
'import "smarkets.streaming_api.eto.proto"'))
for pkg in protobuf_modules:
dst_pkg_file = protobuf_module_file(pkg)
if not os.path.exists(dst_pkg_file):
check_call((self.find('protoc'),
'--python_out=.', 'smarkets.streaming_api.%s.proto' % (pkg,)))
build.build.run(self)
@staticmethod
def replace_file(filename, line_map):
"Map line_map for each line in filename"
with open(filename, "r") as sources:
lines = sources.readlines()
with open(filename, "w") as sources:
for line in lines:
sources.write(line_map(line))
class SmarketsProtocolClean(clean.clean):
"""Class to clean up the built protobuf files."""
description = "clean up files generated by protobuf-compiler"
def run(self):
"""Do the clean up"""
for src_dir in [
join('build', 'pb'),
]:
src_dir = join(PROJECT_ROOT, src_dir)
if os.path.exists(src_dir):
shutil.rmtree(src_dir)
for filename in chain(
_safe_glob('*.proto'),
_safe_glob('*.piqi'),
(join(PROJECT_ROOT, 'smarkets', 'streaming_api', '%s_pb2.py' % key)
for key in ('eto', 'seto'))):
if os.path.exists(filename):
os.unlink(filename)
# Call the parent class clean command
clean.clean.run(self)
readme_path = join(PROJECT_ROOT, 'README.rst')
with io.open(readme_path, encoding='utf-8') as f:
long_description = f.read()
# this is not ideal but at at least we're not repeating ourselved when updating package version
with open(join(PROJECT_ROOT, 'smarkets', '__init__.py')) as f:
version_line = [line for line in f if line.startswith('__version__')][0]
__version__ = version_line.split('=')[1].strip().strip("'").strip('"')
sdict = {
'name': 'smk_python_sdk',
'version': __version__,
'description': 'Smarkets Python SDK - API clients and utility library',
'long_description': long_description,
'url': 'https://github.com/smarkets/smk_python_sdk',
'download_url': 'https://github.com/smarkets/smk_python_sdk/downloads/smk_python_sdk-%s.tar.gz' % (
__version__,),
'author': 'Smarkets Limited',
'author_email': '[email protected]',
'maintainer': 'Smarkets Limited',
'maintainer_email': '[email protected]',
'keywords': ['Smarkets', 'betting exchange'],
'license': 'MIT',
'packages': ['smarkets', 'smarkets.streaming_api', 'smarkets.tests'],
'classifiers': [
'Development Status :: 3 - Alpha',
'Environment :: Console',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python'],
'install_requires': [
'decorator',
'iso8601',
'protobuf',
'pytz',
'six',
],
'zip_safe': False,
'cmdclass': {
'build': SmarketsProtocolBuild,
'clean': SmarketsProtocolClean,
},
}
def creating_a_distribution():
command_line = ' '.join(sys.argv)
return 'sdist' in command_line or 'bdist' in command_line
def make_sure_the_package_is_built():
# It used to be *very* easy to create a sdist/bdist without building
# the package first and the resulting distribution would be incomplete,
# this is to prevent that from happening.
for name in protobuf_modules:
file_name = protobuf_module_file(name)
assert os.path.isfile(file_name), '%r not built' % (file_name,)
if __name__ == '__main__':
if creating_a_distribution():
make_sure_the_package_is_built()
setup(**sdict)