Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CmdCp easier to use #3549

Open
wants to merge 1 commit into
base: 5.0.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions easybuild/easyblocks/generic/cmdcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def extra_options(extra_vars=None):
extra_vars = MakeCp.extra_options(extra_vars=extra_vars)
extra_vars['cmds_map'] = [
[('.*', "$CC $CFLAGS %(source)s -o %(target)s")],
"List of regex/template command (with 'source'/'target' fields) tuples",
"List of commands to run. ",
"Entries are tuples of a regex to a command (may use 'source'&'target' templates) "
"to be executed on sources matching the regex.\n",
"Any element that is not a tuple, i.e. a string, "
"is a command to be executed after all tuple entries are handled.",
CUSTOM,
]
return extra_vars
Expand All @@ -60,24 +64,29 @@ def build_step(self):
except OSError as err:
raise EasyBuildError("Failed to move (back) to %s: %s", self.cfg['start_dir'], err)

for src in self.src:
src = src['path']
target, _ = os.path.splitext(os.path.basename(src))
cmds_map = self.cfg['cmds_map']
template_cmds = [cmd for cmd in cmds_map if isinstance(cmd, tuple)]
additional_cmds = [cmd for cmd in cmds_map if not isinstance(cmd, tuple)]

# determine command to use
# find (first) regex match, then complete matching command template
cmd = None
for pattern, regex_cmd in self.cfg['cmds_map']:
if template_cmds:
# Verify regexes
regex_cmds = []
for pattern, cmd in template_cmds:
try:
regex = re.compile(pattern)
except re.error as err:
raise EasyBuildError("Failed to compile regular expression '%s': %s", pattern, err)
regex_cmds.append((regex, cmd))

if regex.match(os.path.basename(src)):
cmd = regex_cmd % {'source': src, 'target': target}
break
if cmd is None:
raise EasyBuildError("No match for %s in %s, don't know which command to use.",
src, self.cfg['cmds_map'])
for src in self.src:
src = src['path']
target = os.path.splitext(os.path.basename(src))[0]

# determine command to use by finding first regex match
cmd = next((cur_cmd for regex, cur_cmd in regex_cmds if regex.match(os.path.basename(src))), None)
if cmd is None:
raise EasyBuildError("No match for %s in %s, don't know which command to use.",
src, self.cfg['cmds_map'])
run_shell_cmd(cmd % {'source': src, 'target': target})
for cmd in additional_cmds:
run_shell_cmd(cmd)