forked from ofek/coincurve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_support.py
106 lines (85 loc) · 2.75 KB
/
setup_support.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
import glob
import os
import shutil
import subprocess
from contextlib import contextmanager
from tempfile import mkdtemp
@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 _find_lib():
if 'COINCURVE_IGNORE_SYSTEM_LIB' in os.environ:
return False
from cffi import FFI
ffi = FFI()
try:
ffi.dlopen('secp256k1')
if os.path.exists('/usr/include/secp256k1_ecdh.h'):
return True
else:
# The system library lacks the ecdh module
return False
except OSError:
if 'LIB_DIR' in os.environ:
for path in glob.glob(os.path.join(os.environ['LIB_DIR'], '*secp256k1*')):
try:
FFI().dlopen(path)
return True
except OSError:
pass
# We couldn't locate libsecp256k1 so we'll use the bundled one
return False
else:
# If we got this far then the system library should be good enough
return True
_has_system_lib = None
def has_system_lib():
global _has_system_lib
if _has_system_lib is None:
_has_system_lib = _find_lib()
return _has_system_lib
def detect_dll():
here = os.path.dirname(os.path.abspath(__file__))
for fn in os.listdir(os.path.join(here, 'coincurve')):
if fn.endswith('.dll'):
return True
return False