Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/cdefs.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2017 The Bitcoin developers | # Copyright (c) 2017 The Bitcoin developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
""" | """ | ||||
Imports some application default values from source files outside the test | Imports some application default values from source files outside the test | ||||
framework, and defines equivalents of consensus parameters for the test | framework, and defines equivalents of consensus parameters for the test | ||||
framework. | framework. | ||||
""" | """ | ||||
import os | import os | ||||
import re | import re | ||||
def get_srcdir(calling_script=None): | def get_srcdir(): | ||||
""" | """ | ||||
Try to find out the base folder containing the 'src' folder. | Try to find out the base folder containing the 'src' folder. | ||||
If SRCDIR is set it does a sanity check and returns that. | If SRCDIR is set it does a sanity check and returns that. | ||||
Otherwise it goes on a search and rescue mission. | Otherwise it goes on a search and rescue mission. | ||||
Returns None if it cannot find a suitable folder. | Returns None if it cannot find a suitable folder. | ||||
""" | """ | ||||
def contains_src(path_to_check): | def contains_src(path_to_check): | ||||
if not path_to_check: | if not path_to_check: | ||||
return False | return False | ||||
else: | else: | ||||
cand_path = os.path.join(path_to_check, 'src') | cand_path = os.path.join(path_to_check, 'src') | ||||
return os.path.exists(cand_path) and os.path.isdir(cand_path) | return os.path.exists(cand_path) and os.path.isdir(cand_path) | ||||
srcdir = os.environ.get('SRCDIR', '') | srcdir = os.environ.get('SRCDIR', '') | ||||
if contains_src(srcdir): | if contains_src(srcdir): | ||||
return srcdir | return srcdir | ||||
# If we have a caller, try to guess from its location where the | |||||
# top level might be. | |||||
if calling_script: | |||||
caller_basedir = os.path.dirname( | |||||
os.path.dirname(os.path.dirname(calling_script))) | |||||
if caller_basedir != '' and contains_src(os.path.abspath(caller_basedir)): | |||||
return os.path.abspath(caller_basedir) | |||||
# Try to work it based out on main module | # Try to work it based out on main module | ||||
# We might expect the caller to be rpc-tests.py or a test script | |||||
# itself. | |||||
import sys | import sys | ||||
mainmod = sys.modules['__main__'] | mainmod = sys.modules['__main__'] | ||||
mainmod_path = getattr(mainmod, '__file__', '') | mainmod_path = getattr(mainmod, '__file__', '') | ||||
if mainmod_path and mainmod_path.endswith('.py'): | if mainmod_path and mainmod_path.endswith('.py'): | ||||
maybe_top = os.path.dirname( | maybe_top = mainmod_path | ||||
os.path.dirname(os.path.dirname(mainmod_path))) | while maybe_top != '/': | ||||
if contains_src(os.path.abspath(maybe_top)): | maybe_top = os.path.abspath(os.path.dirname(maybe_top)) | ||||
return os.path.abspath(maybe_top) | if contains_src(maybe_top): | ||||
return maybe_top | |||||
# No luck, give up. | # No luck, give up. | ||||
return None | return None | ||||
# Slurp in consensus.h contents | # Slurp in consensus.h contents | ||||
_consensus_h_fh = open(os.path.join(get_srcdir(), 'src', 'consensus', | _consensus_h_fh = open(os.path.join(get_srcdir(), 'src', 'consensus', | ||||
'consensus.h'), 'rt') | 'consensus.h'), 'rt') | ||||
▲ Show 20 Lines • Show All 45 Lines • Show Last 20 Lines |