1
1
import glob
2
+ import logging
2
3
import os
3
- import shutil
4
4
import subprocess
5
- from contextlib import contextmanager , suppress
6
- from tempfile import mkdtemp
7
-
8
-
9
- @contextmanager
10
- def workdir ():
11
- cwd = os .getcwd ()
12
- tmpdir = mkdtemp ()
13
- os .chdir (tmpdir )
14
- try :
15
- yield
16
- finally :
17
- os .chdir (cwd )
18
- shutil .rmtree (tmpdir )
19
-
20
-
21
- @contextmanager
22
- def redirect (stdchannel , dest_filename ):
23
- oldstdchannel = os .dup (stdchannel .fileno ())
24
- dest_file = open (dest_filename , 'w' )
25
- os .dup2 (dest_file .fileno (), stdchannel .fileno ())
26
- try :
27
- yield
28
- finally :
29
- if oldstdchannel is not None :
30
- os .dup2 (oldstdchannel , stdchannel .fileno ())
31
- if dest_file is not None :
32
- dest_file .close ()
5
+ import tempfile
6
+ from contextlib import suppress
7
+
8
+ # Is this needed?
9
+ # @contextmanager
10
+ # def workdir():
11
+ # cwd = os.getcwd()
12
+ # tmpdir = mkdtemp()
13
+ # os.chdir(tmpdir)
14
+ # try:
15
+ # yield
16
+ # finally:
17
+ # os.chdir(cwd)
18
+ # shutil.rmtree(tmpdir)
19
+ #
20
+ #
21
+ # @contextmanager
22
+ # def redirect(stdchannel, dest_filename):
23
+ # oldstdchannel = os.dup(stdchannel.fileno())
24
+ # dest_file = open(dest_filename, 'w')
25
+ # os.dup2(dest_file.fileno(), stdchannel.fileno())
26
+ # try:
27
+ # yield
28
+ # finally:
29
+ # if oldstdchannel is not None:
30
+ # os.dup2(oldstdchannel, stdchannel.fileno())
31
+ # if dest_file is not None:
32
+ # dest_file.close()
33
33
34
34
35
35
def absolute (* paths ):
@@ -47,9 +47,13 @@ def build_flags(library, type_, path):
47
47
pkg_config_path .append (os .environ ['LIB_DIR' ])
48
48
pkg_config_path .append (os .path .join (os .environ ['LIB_DIR' ], 'pkgconfig' ))
49
49
50
+ # Update PKG_CONFIG_PATH, it may be needed in later stages
51
+ new_path = str (os .pathsep ).join (pkg_config_path )
52
+ os .environ ['PKG_CONFIG_PATH' ] = new_path + os .pathsep + os .environ .get ('PKG_CONFIG_PATH' , '' )
53
+
50
54
options = {'I' : '--cflags-only-I' , 'L' : '--libs-only-L' , 'l' : '--libs-only-l' }
51
- env = dict ( os . environ , PKG_CONFIG_PATH = ':' . join ( pkg_config_path ))
52
- flags = subprocess . check_output ([ 'pkg-config' , '--static' , options [ type_ ], library ], env = env ) # noqa S603
55
+ cmd = [ 'pkg-config' , options [ type_ ], library ]
56
+ flags = execute_command_with_temp_log ( cmd , capture_output = True )
53
57
flags = list (flags .decode ('UTF-8' ).split ())
54
58
55
59
return [flag .strip (f'-{ type_ } ' ) for flag in flags ]
@@ -62,9 +66,11 @@ def _find_lib():
62
66
from cffi import FFI
63
67
64
68
try :
65
- subprocess .check_output (['pkg-config' , '--exists' , 'libsecp256k1' ]) # noqa S603
69
+ cmd = ['pkg-config' , '--exists' , 'libsecp256k1' ]
70
+ execute_command_with_temp_log (cmd )
66
71
67
- includes = subprocess .check_output (['pkg-config' , '--cflags-only-I' , 'libsecp256k1' ]) # noqa S603
72
+ cmd = ['pkg-config' , '--cflags-only-I' , 'libsecp256k1' ]
73
+ includes = execute_command_with_temp_log (cmd , capture_output = True )
68
74
includes = includes .strip ().decode ('utf-8' )
69
75
70
76
return os .path .exists (os .path .join (includes [2 :], 'secp256k1_ecdh.h' ))
@@ -95,3 +101,27 @@ def detect_dll():
95
101
if fn .endswith ('.dll' ):
96
102
return True
97
103
return False
104
+
105
+
106
+ def execute_command_with_temp_log (cmd , * , debug = False , capture_output = False , ** kwargs ):
107
+ with tempfile .NamedTemporaryFile (mode = 'w+' ) as temp_log :
108
+ try :
109
+ if capture_output :
110
+ ret = subprocess .check_output (cmd , stderr = temp_log , ** kwargs ) # noqa S603
111
+ else :
112
+ subprocess .check_call (cmd , stdout = temp_log , stderr = temp_log , ** kwargs ) # noqa S603
113
+
114
+ if debug :
115
+ temp_log .seek (0 )
116
+ log_contents = temp_log .read ()
117
+ logging .info (f'Command log:\n { log_contents } ' )
118
+
119
+ if capture_output :
120
+ return ret
121
+
122
+ except subprocess .CalledProcessError as e :
123
+ logging .error (f'An error occurred during the command execution: { e } ' )
124
+ temp_log .seek (0 )
125
+ log_contents = temp_log .read ()
126
+ logging .error (f'Command log:\n { log_contents } ' )
127
+ raise e
0 commit comments