Skip to content

Commit a3f3873

Browse files
committed
start adopting run_shell_cmd in easyblock.py (+ use f-strings)
1 parent 95e752c commit a3f3873

File tree

1 file changed

+61
-63
lines changed

1 file changed

+61
-63
lines changed

easybuild/framework/easyblock.py

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
from easybuild.tools.hooks import MODULE_STEP, MODULE_WRITE, PACKAGE_STEP, PATCH_STEP, PERMISSIONS_STEP, POSTITER_STEP
8888
from easybuild.tools.hooks import POSTPROC_STEP, PREPARE_STEP, READY_STEP, SANITYCHECK_STEP, SOURCE_STEP
8989
from easybuild.tools.hooks import SINGLE_EXTENSION, TEST_STEP, TESTCASES_STEP, load_hooks, run_hook
90-
from easybuild.tools.run import RunShellCmdError, check_async_cmd, run_cmd
90+
from easybuild.tools.run import RunShellCmdError, check_async_cmd, run_cmd, run_shell_cmd
9191
from easybuild.tools.jenkins import write_to_xml
9292
from easybuild.tools.module_generator import ModuleGeneratorLua, ModuleGeneratorTcl, module_generator, dependencies_for
9393
from easybuild.tools.module_naming_scheme.utilities import det_full_ec_version
@@ -1780,21 +1780,20 @@ def skip_extensions_sequential(self, exts_filter):
17801780

17811781
exts_cnt = len(self.ext_instances)
17821782

1783-
res = []
1783+
exts = []
17841784
for idx, ext_inst in enumerate(self.ext_instances):
17851785
cmd, stdin = resolve_exts_filter_template(exts_filter, ext_inst)
1786-
(out, ec) = run_cmd(cmd, log_all=False, log_ok=False, simple=False, inp=stdin,
1787-
regexp=False, trace=False)
1788-
self.log.info("exts_filter result for %s: exit code %s; output: %s", ext_inst.name, ec, out)
1789-
if ec == 0:
1790-
print_msg("skipping extension %s" % ext_inst.name, silent=self.silent, log=self.log)
1786+
res = run_shell_cmd(cmd, stdin=stdin, fail_on_error=False, hidden=True)
1787+
self.log.info(f"exts_filter result for {ext_inst.name}: exit code {res.exit_code}; output: {res.output}")
1788+
if res.exit_code == 0:
1789+
print_msg(f"skipping extension {ext_inst.name}", silent=self.silent, log=self.log)
17911790
else:
1792-
self.log.info("Not skipping %s", ext_inst.name)
1793-
res.append(ext_inst)
1791+
self.log.info(f"Not skipping {ext_inst.name}")
1792+
exts.append(ext_inst)
17941793

1795-
self.update_exts_progress_bar("skipping installed extensions (%d/%d checked)" % (idx + 1, exts_cnt))
1794+
self.update_exts_progress_bar(f"skipping installed extensions ({idx + 1}/{exts_cnt} checked)")
17961795

1797-
self.ext_instances = res
1796+
self.ext_instances = exts
17981797
self.update_exts_progress_bar("already installed extensions filtered out", total=len(self.ext_instances))
17991798

18001799
def skip_extensions_parallel(self, exts_filter):
@@ -2714,17 +2713,15 @@ def test_step(self):
27142713
"""Run unit tests provided by software (if any)."""
27152714
unit_test_cmd = self.cfg['runtest']
27162715
if unit_test_cmd:
2717-
2718-
self.log.debug("Trying to execute %s as a command for running unit tests...", unit_test_cmd)
2719-
(out, _) = run_cmd(unit_test_cmd, log_all=True, simple=False)
2720-
2721-
return out
2716+
self.log.debug(f"Trying to execute {unit_test_cmd} as a command for running unit tests...")
2717+
res = run_shell_cmd(unit_test_cmd)
2718+
return res.output
27222719

27232720
def _test_step(self):
27242721
"""Run the test_step and handles failures"""
27252722
try:
27262723
self.test_step()
2727-
except EasyBuildError as err:
2724+
except RunShellCmdError as err:
27282725
self.report_test_failure(err)
27292726

27302727
def stage_install_step(self):
@@ -2977,17 +2974,17 @@ def run_post_install_commands(self, commands=None):
29772974
commands = self.cfg['postinstallcmds']
29782975

29792976
if commands:
2980-
self.log.debug("Specified post install commands: %s", commands)
2977+
self.log.debug(f"Specified post install commands: {commands}")
29812978

29822979
# make sure we have a list of commands
29832980
if not isinstance(commands, (list, tuple)):
2984-
error_msg = "Invalid value for 'postinstallcmds', should be list or tuple of strings: %s"
2985-
raise EasyBuildError(error_msg, commands)
2981+
error_msg = f"Invalid value for 'postinstallcmds', should be list or tuple of strings: {commands}"
2982+
raise EasyBuildError(error_msg)
29862983

29872984
for cmd in commands:
29882985
if not isinstance(cmd, str):
2989-
raise EasyBuildError("Invalid element in 'postinstallcmds', not a string: %s", cmd)
2990-
run_cmd(cmd, simple=True, log_ok=True, log_all=True)
2986+
raise EasyBuildError(f"Invalid element in 'postinstallcmds', not a string: {cmd}")
2987+
run_shell_cmd(cmd)
29912988

29922989
def apply_post_install_patches(self, patches=None):
29932990
"""
@@ -3104,8 +3101,10 @@ def sanity_check_rpath(self, rpath_dirs=None, check_readelf_rpath=True):
31043101
# hard reset $LD_LIBRARY_PATH before running RPATH sanity check
31053102
orig_env = env.unset_env_vars(['LD_LIBRARY_PATH'])
31063103

3107-
self.log.debug("$LD_LIBRARY_PATH during RPATH sanity check: %s", os.getenv('LD_LIBRARY_PATH', '(empty)'))
3108-
self.log.debug("List of loaded modules: %s", self.modules_tool.list())
3104+
ld_library_path = os.getenv('LD_LIBRARY_PATH', '(empty)')
3105+
self.log.debug(f"$LD_LIBRARY_PATH during RPATH sanity check: {ld_library_path}")
3106+
modules_list = self.modules_tool.list()
3107+
self.log.debug(f"List of loaded modules: {modules_list}")
31093108

31103109
not_found_regex = re.compile(r'(\S+)\s*\=\>\s*not found')
31113110
readelf_rpath_regex = re.compile('(RPATH)', re.M)
@@ -3114,68 +3113,66 @@ def sanity_check_rpath(self, rpath_dirs=None, check_readelf_rpath=True):
31143113
# For example, libcuda.so.1 should never be RPATH-ed by design,
31153114
# see https://github.com/easybuilders/easybuild-framework/issues/4095
31163115
filter_rpath_sanity_libs = build_option('filter_rpath_sanity_libs')
3117-
msg = "Ignoring the following libraries if they are not found by RPATH sanity check: %s"
3118-
self.log.info(msg, filter_rpath_sanity_libs)
3116+
msg = "Ignoring the following libraries if they are not found by RPATH sanity check: {filter_rpath_sanity_libs}"
3117+
self.log.info(msg)
31193118

31203119
if rpath_dirs is None:
31213120
rpath_dirs = self.cfg['bin_lib_subdirs'] or self.bin_lib_subdirs()
31223121

31233122
if not rpath_dirs:
31243123
rpath_dirs = DEFAULT_BIN_LIB_SUBDIRS
3125-
self.log.info("Using default subdirectories for binaries/libraries to verify RPATH linking: %s",
3126-
rpath_dirs)
3124+
self.log.info(f"Using default subdirs for binaries/libraries to verify RPATH linking: {rpath_dirs}")
31273125
else:
3128-
self.log.info("Using specified subdirectories for binaries/libraries to verify RPATH linking: %s",
3129-
rpath_dirs)
3126+
self.log.info(f"Using specified subdirs for binaries/libraries to verify RPATH linking: {rpath_dirs}")
31303127

31313128
for dirpath in [os.path.join(self.installdir, d) for d in rpath_dirs]:
31323129
if os.path.exists(dirpath):
3133-
self.log.debug("Sanity checking RPATH for files in %s", dirpath)
3130+
self.log.debug(f"Sanity checking RPATH for files in {dirpath}")
31343131

31353132
for path in [os.path.join(dirpath, x) for x in os.listdir(dirpath)]:
3136-
self.log.debug("Sanity checking RPATH for %s", path)
3133+
self.log.debug(f"Sanity checking RPATH for {path}")
31373134

31383135
out = get_linked_libs_raw(path)
31393136

31403137
if out is None:
3141-
msg = "Failed to determine dynamically linked libraries for %s, "
3138+
msg = "Failed to determine dynamically linked libraries for {path}, "
31423139
msg += "so skipping it in RPATH sanity check"
3143-
self.log.debug(msg, path)
3140+
self.log.debug(msg)
31443141
else:
31453142
# check whether all required libraries are found via 'ldd'
31463143
matches = re.findall(not_found_regex, out)
31473144
if len(matches) > 0: # Some libraries are not found via 'ldd'
31483145
# For each match, check if the library is in the exception list
31493146
for match in matches:
31503147
if match in filter_rpath_sanity_libs:
3151-
msg = "Library %s not found for %s, but ignored "
3152-
msg += "since it is on the rpath exception list: %s"
3153-
self.log.info(msg, match, path, filter_rpath_sanity_libs)
3148+
msg = f"Library {match} not found for {path}, but ignored "
3149+
msg += f"since it is on the rpath exception list: {filter_rpath_sanity_libs}"
3150+
self.log.info(msg)
31543151
else:
3155-
fail_msg = "Library %s not found for %s" % (match, path)
3152+
fail_msg = f"Library {match} not found for {path}"
31563153
self.log.warning(fail_msg)
31573154
fails.append(fail_msg)
31583155
else:
3159-
self.log.debug("Output of 'ldd %s' checked, looks OK", path)
3156+
self.log.debug(f"Output of 'ldd {path}' checked, looks OK")
31603157

31613158
# check whether RPATH section in 'readelf -d' output is there
31623159
if check_readelf_rpath:
31633160
fail_msg = None
3164-
out, ec = run_cmd("readelf -d %s" % path, simple=False, trace=False)
3165-
if ec:
3166-
fail_msg = "Failed to run 'readelf %s': %s" % (path, out)
3167-
elif not readelf_rpath_regex.search(out):
3168-
fail_msg = "No '(RPATH)' found in 'readelf -d' output for %s: %s" % (path, out)
3161+
res = run_shell_cmd(f"readelf -d {path}", fail_on_error=False)
3162+
if res.exit_code:
3163+
fail_msg = f"Failed to run 'readelf -d {path}': {res.output}"
3164+
elif not readelf_rpath_regex.search(res.output):
3165+
fail_msg = f"No '(RPATH)' found in 'readelf -d' output for {path}: {out}"
31693166

31703167
if fail_msg:
31713168
self.log.warning(fail_msg)
31723169
fails.append(fail_msg)
31733170
else:
3174-
self.log.debug("Output of 'readelf -d %s' checked, looks OK", path)
3171+
self.log.debug(f"Output of 'readelf -d {path}' checked, looks OK")
31753172
else:
31763173
self.log.debug("Skipping the RPATH section check with 'readelf -d', as requested")
31773174
else:
3178-
self.log.debug("Not sanity checking files in non-existing directory %s", dirpath)
3175+
self.log.debug(f"Not sanity checking files in non-existing directory {dirpath}")
31793176

31803177
env.restore_env_vars(orig_env)
31813178

@@ -3604,19 +3601,20 @@ def xs2str(xs):
36043601
change_dir(self.installdir)
36053602

36063603
# run sanity check commands
3607-
for command in commands:
3604+
for cmd in commands:
36083605

3609-
trace_msg("running command '%s' ..." % command)
3606+
trace_msg(f"running command '{cmd}' ...")
36103607

3611-
out, ec = run_cmd(command, simple=False, log_ok=False, log_all=False, trace=False)
3612-
if ec != 0:
3613-
fail_msg = "sanity check command %s exited with code %s (output: %s)" % (command, ec, out)
3608+
res = run_shell_cmd(cmd, fail_on_error=False, hidden=True)
3609+
if res.exit_code != 0:
3610+
fail_msg = f"sanity check command {cmd} exited with code {res.exit_code} (output: {res.output})"
36143611
self.sanity_check_fail_msgs.append(fail_msg)
3615-
self.log.warning("Sanity check: %s" % self.sanity_check_fail_msgs[-1])
3612+
self.log.warning(f"Sanity check: {fail_msg}")
36163613
else:
3617-
self.log.info("sanity check command %s ran successfully! (output: %s)" % (command, out))
3614+
self.log.info(f"sanity check command {cmd} ran successfully! (output: {res.output})")
36183615

3619-
trace_msg("result for command '%s': %s" % (command, ('FAILED', 'OK')[ec == 0]))
3616+
cmd_result_str = ('FAILED', 'OK')[res.exit_code == 0]
3617+
trace_msg(f"result for command '{cmd}': {cmd_result_str}")
36203618

36213619
# also run sanity check for extensions (unless we are an extension ourselves)
36223620
if not extension:
@@ -3643,7 +3641,7 @@ def xs2str(xs):
36433641

36443642
# pass or fail
36453643
if self.sanity_check_fail_msgs:
3646-
raise EasyBuildError("Sanity check failed: %s", '\n'.join(self.sanity_check_fail_msgs))
3644+
raise EasyBuildError("Sanity check failed: " + '\n'.join(self.sanity_check_fail_msgs))
36473645
else:
36483646
self.log.debug("Sanity check passed!")
36493647

@@ -3854,20 +3852,20 @@ def test_cases_step(self):
38543852
for test in self.cfg['tests']:
38553853
change_dir(self.orig_workdir)
38563854
if os.path.isabs(test):
3857-
path = test
3855+
test_cmd = test
38583856
else:
38593857
for source_path in source_paths():
3860-
path = os.path.join(source_path, self.name, test)
3861-
if os.path.exists(path):
3858+
test_cmd = os.path.join(source_path, self.name, test)
3859+
if os.path.exists(test_cmd):
38623860
break
3863-
if not os.path.exists(path):
3864-
raise EasyBuildError("Test specifies invalid path: %s", path)
3861+
if not os.path.exists(test_cmd):
3862+
raise EasyBuildError(f"Test specifies invalid path: {test_cmd}")
38653863

38663864
try:
3867-
self.log.debug("Running test %s" % path)
3868-
run_cmd(path, log_all=True, simple=True)
3865+
self.log.debug(f"Running test {test_cmd}")
3866+
run_shell_cmd(test_cmd)
38693867
except EasyBuildError as err:
3870-
raise EasyBuildError("Running test %s failed: %s", path, err)
3868+
raise EasyBuildError(f"Running test {test_cmd} failed: {err}")
38713869

38723870
def update_config_template_run_step(self):
38733871
"""Update the the easyconfig template dictionary with easyconfig.TEMPLATE_NAMES_EASYBLOCK_RUN_STEP names"""

0 commit comments

Comments
 (0)