-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·141 lines (114 loc) · 4.12 KB
/
setup.py
File metadata and controls
executable file
·141 lines (114 loc) · 4.12 KB
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
#! /usr/bin/env python
from setuptools import setup, Extension
from sys import platform, prefix, executable, exit
from subprocess import Popen, PIPE
from os.path import dirname, exists, join, normpath
from tempfile import mkstemp
from os import write, close, remove, environ
import logging
import re
ON_WINDOWS = platform == 'win32'
VERSION = '0.12.3'
include_dirs = []
lib_dirs = []
compilation_search_paths = [
dirname(executable),
dirname(dirname(executable)),
]
if ON_WINDOWS:
compilation_search_paths.append(environ.get("FREETDS", "c:\\freetds"))
for _path in compilation_search_paths:
include_path = join(_path, "include")
lib_path = join(_path, "lib")
if exists(lib_path):
lib_dirs[:0] = [lib_path]
if exists(include_path):
include_dirs[:0] = [include_path]
def extract_constants(freetds_include="sybdb.h", constants_file="bcp_constants.py"):
""" Extract constant names from sybdb.h to use as python constants """
fileno, source_file = mkstemp(suffix=".c", text=True)
write(fileno, "#include <{}>".format(freetds_include).encode())
close(fileno)
fileno, include_directives = mkstemp(suffix=".txt")
close(fileno)
if ON_WINDOWS:
cmd_template = "cl /E {includes} {source} > {output}"
else:
cmd_template = "cpp {includes} '{source}' > '{output}'"
cmd = cmd_template.format(
output=normpath(include_directives),
source=normpath(source_file),
includes=" ".join(
"-I{}".format(normpath(_include)) for _include in include_dirs
)
)
fifo = Popen(cmd, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
fifo.communicate()
fifo.wait()
remove(source_file)
if fifo.returncode < 0:
raise Exception("Cannot run preprocessor step")
row_regex = re.compile('[\r\n]+')
field_regex = re.compile('[\s]+')
with open(include_directives, "r") as fd:
include_paths = list(
_filename
for contents in [fd.read()]
for _row in row_regex.split(contents) if _row.find(freetds_include) > -1
for _index, _word in enumerate(field_regex.split(_row)) if _index == 2
for _filename in [_word.strip('"')] if exists(_filename)
)
remove(include_directives)
for include_file in include_paths:
with open(include_file, "r") as fd:
definition_pairs = [
(_values[1], int(_values[2]))
for contents in [fd.read()]
for _row in row_regex.split(contents)
for _values in [field_regex.split(_row)] if len(_values) == 3 and _values[0] == "#define" and _values[2].isdigit()
]
if len(definition_pairs):
with open(constants_file, "w") as output_fd:
output_fd.write("\n".join("%s=%d" % _row for _row in definition_pairs))
break
else:
raise Exception("Couldn't find a freetds include file")
extract_constants(
freetds_include="sybdb.h",
constants_file="bcp_constants.py",
)
if ON_WINDOWS:
bcp_module = Extension(
'bcp',
sources = ['pythonbcp.c'],
include_dirs = include_dirs,
library_dirs = lib_dirs,
libraries = ['sybdb'],
)
setup(
name = 'bcp',
version = VERSION,
description = 'This package supports bulk transfers to MS SQL and Sybase databases',
# data_files = [(prefix, ['win32/FreeTDS.dll', 'win32/dblib.dll'])],
ext_modules = [bcp_module],
py_modules = ['bcp_constants'],
)
else:
bcp_module = Extension(
'bcp',
sources = ['pythonbcp.c'],
include_dirs = include_dirs,
library_dirs = lib_dirs,
libraries = ['sybdb',
# 'iconv', # not needed on ubuntu
],
# extra_compile_args=['-m32', '-march=i386'],
# extra_link_args=['-m32', '-march=i386'],
)
setup(
name = 'bcp',
version = VERSION,
description = 'This package supports bulk transfers to MS SQL and Sybase databases',
ext_modules = [bcp_module],
py_modules = ['bcp_constants'],
)