Skip to content

Commit 02f838f

Browse files
committed
(feat) wrapper to subprocess calls?
1 parent d492d2f commit 02f838f

File tree

1 file changed

+63
-33
lines changed

1 file changed

+63
-33
lines changed

setup_support.py

+63-33
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import glob
2+
import logging
23
import os
3-
import shutil
44
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()
3333

3434

3535
def absolute(*paths):
@@ -47,9 +47,13 @@ def build_flags(library, type_, path):
4747
pkg_config_path.append(os.environ['LIB_DIR'])
4848
pkg_config_path.append(os.path.join(os.environ['LIB_DIR'], 'pkgconfig'))
4949

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+
5054
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)
5357
flags = list(flags.decode('UTF-8').split())
5458

5559
return [flag.strip(f'-{type_}') for flag in flags]
@@ -62,9 +66,11 @@ def _find_lib():
6266
from cffi import FFI
6367

6468
try:
65-
subprocess.check_output(['pkg-config', '--exists', 'libsecp256k1']) # noqa S603
69+
cmd = ['pkg-config', '--exists', 'libsecp256k1']
70+
execute_command_with_temp_log(cmd)
6671

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)
6874
includes = includes.strip().decode('utf-8')
6975

7076
return os.path.exists(os.path.join(includes[2:], 'secp256k1_ecdh.h'))
@@ -95,3 +101,27 @@ def detect_dll():
95101
if fn.endswith('.dll'):
96102
return True
97103
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

Comments
 (0)