forked from msitt/blpapi-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
219 lines (184 loc) · 8.42 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
"""
setup.py file for Bloomberg Python SDK
"""
import os
import platform as plat
import re
import codecs
from sys import version
from setuptools import setup, Extension
os.chdir(os.path.dirname(os.path.realpath(__file__)))
platform = plat.system().lower()
def find_version_number():
"""Load the version number from blpapi/version.py"""
version_path = os.path.abspath(os.path.join('blpapi', 'version.py'))
version_file = None
with codecs.open(version_path, 'r') as fp:
version_file = fp.read()
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
version_file, re.M)
if version_match:
return version_match.group(1)
raise RuntimeError("Unable to find version string.")
if version < '2.6':
raise Exception(
"Python versions before 2.6 are not supported (current version is "
+ version + ")")
blpapiRoot = os.environ.get('BLPAPI_ROOT')
blpapiIncludesVar = os.environ.get('BLPAPI_INCDIR')
blpapiLibVar = os.environ.get('BLPAPI_LIBDIR')
assert blpapiRoot or (blpapiIncludesVar and blpapiLibVar), \
"BLPAPI_ROOT environment variable isn't defined"
is64bit = plat.architecture()[0] == '64bit'
if is64bit:
blpapiLibraryName = 'blpapi3_64'
else:
blpapiLibraryName = 'blpapi3_32'
cmdclass = {}
extraLinkArgs = []
if platform == 'windows':
from distutils.command import build_ext
from ctypes import windll, create_string_buffer, c_uint, byref, string_at, wstring_at, c_void_p
import shutil
import array
import re
class BuildBlpExtension(build_ext.build_ext):
@staticmethod
def get_file_info(filename, info):
"""
Extract information from a file.
"""
if version >= '3.0':
GetFileVersionInfoSize = windll.version.GetFileVersionInfoSizeW
GetFileVersionInfo = windll.version.GetFileVersionInfoW
VerQueryValue = windll.version.VerQueryValueW
tstring_at = wstring_at
else:
GetFileVersionInfoSize = windll.version.GetFileVersionInfoSizeA
GetFileVersionInfo = windll.version.GetFileVersionInfoA
VerQueryValue = windll.version.VerQueryValueA
tstring_at = string_at
# Get size needed for buffer (0 if no info)
size = GetFileVersionInfoSize(filename, None)
if not size:
return ''
# Create buffer
res = create_string_buffer(size)
# Load file informations into buffer res
GetFileVersionInfo(filename, None, size, res)
r = c_void_p()
l = c_uint()
# Look for codepages
VerQueryValue(res, '\\VarFileInfo\\Translation', byref(r), byref(l))
# If no codepage -> empty string
if not l.value:
return ''
# Take the first codepage
codepages = array.array('H', string_at(r.value, l.value))
codepage = tuple(codepages[:2].tolist())
# Extract information
VerQueryValue(res, ('\\StringFileInfo\\%04x%04x\\' + info) % codepage, byref(r), byref(l))
return tstring_at(r.value, l.value-1).replace(" ", "").replace(",", ".")
def build_extension(self, ext):
"""
Builds the C extension libraries, but replaces blpapi dll dependency with
a versioned copy so it doesn't conflict with other Bloomberg tools.
"""
# Replace blpapiLibraryName with the version specific one
if blpapiLibraryName in ext.libraries:
srcdll = os.path.join(blpapiLibraryPath, blpapiLibraryName + ".dll")
version = self.get_file_info(srcdll, "FileVersion")
versionedLibName = blpapiLibraryName + "_" + version
build_dir = os.path.dirname(self.get_ext_fullpath(ext.name))
if self.force or not os.path.exists(os.path.join(self.build_temp, versionedLibName + ".lib")):
if not self.compiler.initialized:
self.compiler.initialize()
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
# dump the def file for the dll to rebuild the lib file
dumpbinfile = os.path.join(self.build_temp, blpapiLibraryName + "_" + version + ".dumpbin")
dumpbin = os.path.join(os.path.dirname(self.compiler.lib), "DUMPBIN.EXE")
self.compiler.spawn([dumpbin, "/EXPORTS", "/OUT:" + dumpbinfile, srcdll])
# get all the function definitions
deffile = os.path.join(self.build_temp, blpapiLibraryName + "_" + version + ".def")
with open(deffile, "wt") as out_fh:
exports = []
for line in open(dumpbinfile).readlines():
matches = re.search(r'^\s*(\d+)\s+[A-Z0-9]+\s+[A-Z0-9]{8}\s+([^ ]+)', line)
if matches:
exports.append(matches.group(2) + "\n")
out_fh.writelines(["EXPORTS\n"] + exports)
# rebuild the lib file with the new dll name
libfile = os.path.join(self.build_temp, blpapiLibraryName + "_" + version + ".lib")
machine = "/MACHINE:" + ("X64" if is64bit else "X86")
self.compiler.spawn([self.compiler.lib, machine, "/DEF:" + deffile, "/OUT:" + libfile])
# copy the versioned dll to the build output
if self.force or not os.path.exists(os.path.join(build_dir, versionedLibName + ".dll")):
if not os.path.exists(build_dir):
os.makedirs(build_dir)
shutil.copy(os.path.join(blpapiLibraryPath, blpapiLibraryName + ".dll"),
os.path.join(build_dir, versionedLibName + ".dll"))
# replace blpapi.lib with the versioned one
ext.libraries = [versionedLibName if x == blpapiLibraryName else x for x in ext.libraries]
ext.library_dirs.insert(0, self.build_temp)
build_ext.build_ext.build_extension(self, ext)
cmdclass.update({
"build_ext": BuildBlpExtension,
})
blpapiLibraryPath = os.path.join(blpapiRoot, 'lib')
extraLinkArgs = ['/MANIFEST']
# Handle the very frequent case when user need to use Visual C++ 2010
# with Python that wants to use Visual C++ 2008.
if plat.python_compiler().startswith('MSC v.1500'):
if (not 'VS90COMNTOOLS' in os.environ) and \
('VS100COMNTOOLS' in os.environ):
os.environ['VS90COMNTOOLS'] = os.environ['VS100COMNTOOLS']
elif platform == 'linux':
blpapiLibraryPath = os.path.join(blpapiRoot, 'Linux')
elif platform == 'sunos':
lib = "lib64" if is64bit else "lib"
blpapiLibraryPath = os.path.join(blpapiRoot, lib)
elif platform == 'aix':
lib = "lib64" if is64bit else "lib"
blpapiLibraryPath = os.path.join(blpapiRoot, lib)
elif platform == 'darwin':
blpapiLibraryPath = os.path.join(blpapiRoot, 'Darwin')
else:
raise Exception("Platform '" + platform + "' isn't supported")
blpapiLibraryPath = blpapiLibVar or blpapiLibraryPath
blpapiIncludes = blpapiIncludesVar or os.path.join(blpapiRoot, 'include')
blpapi_wrap = Extension(
'blpapi._internals',
sources=['blpapi/internals_wrap.c'],
include_dirs=[blpapiIncludes],
library_dirs=[blpapiLibraryPath],
libraries=[blpapiLibraryName],
extra_link_args=extraLinkArgs
)
versionhelper_wrap = Extension(
'blpapi._versionhelper',
sources=['blpapi/versionhelper_wrap.c'],
include_dirs=[blpapiIncludes],
library_dirs=[blpapiLibraryPath],
libraries=[blpapiLibraryName],
extra_link_args=extraLinkArgs
)
setup(
name='blpapi',
version=find_version_number(),
author='Bloomberg L.P.',
author_email='[email protected]',
description='Python SDK for Bloomberg BLPAPI',
ext_modules=[blpapi_wrap, versionhelper_wrap],
url='http://www.bloomberglabs.com/api/',
packages=["blpapi"],
cmdclass=cmdclass,
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Intended Audience :: Financial and Insurance Industry',
'License :: Other/Proprietary License',
'Topic :: Office/Business :: Financial',
],
)