-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup_tools.py
152 lines (122 loc) · 4.21 KB
/
setup_tools.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
import glob
import platform
import os
import shutil
from contextlib import contextmanager
from tempfile import mkdtemp
from setuptools import __version__ as setuptools_version
import subprocess
import tarfile
from distutils import log
from io import BytesIO
from distutils import log
from setuptools import Distribution as _Distribution
from setuptools.command.sdist import sdist as _sdist
from setuptools.command.egg_info import egg_info as _egg_info
try:
from urllib2 import urlopen, URLError
except ImportError:
from urllib.request import urlopen
from urllib.error import URLError
try:
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
except ImportError:
_bdist_wheel = None
pass
# We require setuptools >= 3.3
if [int(i) for i in setuptools_version.split('.', 2)[:2]] < [3, 3]:
raise SystemExit(
'Your setuptools version ({}) is too old to correctly install this '
'package. Please upgrade to a newer version (>= 3.3).'.format(setuptools_version)
)
MAKE = 'gmake' if platform.system() in ['FreeBSD'] else 'make'
LIB_URL = 'https://github.com/bitaps-com/secp256k1/tarball/master'
class Distribution(_Distribution):
def has_c_libraries(self):
return True
def download_library(command):
if command.dry_run:
return
libdir = absolute("libsecp256k1")
if os.path.exists(os.path.join(libdir, "autogen.sh")):
# Library already downloaded
return
if not os.path.exists(libdir):
command.announce("downloading libsecp256k1 source code", level=log.INFO)
try:
r = urlopen(LIB_URL)
if r.getcode() == 200:
content = BytesIO(r.read())
content.seek(0)
with tarfile.open(fileobj=content) as tf:
dirname = tf.getnames()[0].partition('/')[0]
tf.extractall()
shutil.move(dirname, libdir)
else:
raise SystemExit( "Unable to download secp256k1 library: HTTP-Status: %d", r.getcode())
except URLError as ex:
raise SystemExit("Unable to download secp256k1 library: %s", str(ex))
class egg_info(_egg_info):
def run(self):
# Ensure library has been downloaded (sdist might have been skipped)
download_library(self)
_egg_info.run(self)
class sdist(_sdist):
def run(self):
download_library(self)
_sdist.run(self)
if _bdist_wheel:
class bdist_wheel(_bdist_wheel):
def run(self):
download_library(self)
_bdist_wheel.run(self)
else:
bdist_wheel = None
@contextmanager
def workdir():
cwd = os.getcwd()
tmpdir = mkdtemp()
os.chdir(tmpdir)
try:
yield
finally:
os.chdir(cwd)
shutil.rmtree(tmpdir)
@contextmanager
def redirect(stdchannel, dest_filename):
oldstdchannel = os.dup(stdchannel.fileno())
dest_file = open(dest_filename, 'w')
os.dup2(dest_file.fileno(), stdchannel.fileno())
try:
yield
finally:
if oldstdchannel is not None:
os.dup2(oldstdchannel, stdchannel.fileno())
if dest_file is not None:
dest_file.close()
def absolute(*paths):
op = os.path
return op.realpath(op.abspath(op.join(op.dirname(__file__), *paths)))
def build_flags(library, type_, path):
"""Return separated build flags from pkg-config output"""
pkg_config_path = [path]
if "PKG_CONFIG_PATH" in os.environ:
pkg_config_path.append(os.environ['PKG_CONFIG_PATH'])
if "LIB_DIR" in os.environ:
pkg_config_path.append(os.environ['LIB_DIR'])
pkg_config_path.append(os.path.join(os.environ['LIB_DIR'], "pkgconfig"))
options = ["--static", {'I': "--cflags-only-I", 'L': "--libs-only-L", 'l': "--libs-only-l"}[type_]]
return [
flag.strip("-{}".format(type_))
for flag in subprocess.check_output(
["pkg-config"] + options + [library], env=dict(os.environ, PKG_CONFIG_PATH=":".join(pkg_config_path))
)
.decode("UTF-8")
.split()
]
def detect_dll():
here = os.path.dirname(os.path.abspath(__file__))
for fn in os.listdir(os.path.join(here, 'libsecp256k1')):
if fn.endswith('.dll'):
return True
return False