Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.mock_stdout(True)
self.mock_stderr(True)
dry_run_msg(msg, *args, **kwargs)
stdout = self.get_stdout()
stderr = self.get_stderr()
self.mock_stdout(False)
self.mock_stderr(False)
self.assertEqual(stdout, expected_stdout)
self.assertEqual(stderr, '')
run_check("test 123", [], expected_stdout="test 123\n")
run_check("test %s", ['123'], expected_stdout="test 123\n")
run_check("test 123", [], silent=True)
run_check("test %s", ['123'], silent=True)
self.assertErrorRegex(EasyBuildError, "Unknown named arguments", dry_run_msg, 'foo', unknown_arg='bar')
def run_check(msg, args, expected_stdout='', **kwargs):
"""Helper function to check stdout/stderr produced via dry_run_msg."""
self.mock_stdout(True)
self.mock_stderr(True)
dry_run_msg(msg, *args, **kwargs)
stdout = self.get_stdout()
stderr = self.get_stderr()
self.mock_stdout(False)
self.mock_stderr(False)
self.assertEqual(stdout, expected_stdout)
self.assertEqual(stderr, '')
def _load_dependencies_modules(self, silent=False):
"""Load modules for dependencies, and handle special cases like external modules."""
dep_mods = [dep['short_mod_name'] for dep in self.dependencies]
if self.dry_run:
dry_run_msg("\nLoading modules for dependencies...\n", silent=silent)
mods_exist = self.modules_tool.exist(dep_mods)
# load available modules for dependencies, simulate load for others
for dep, dep_mod_exists in zip(self.dependencies, mods_exist):
mod_name = dep['short_mod_name']
if dep_mod_exists:
self.modules_tool.load([mod_name])
dry_run_msg("module load %s" % mod_name, silent=silent)
else:
dry_run_msg("module load %s [SIMULATED]" % mod_name, silent=silent)
# 'use '$EBROOTNAME' as value for dep install prefix (looks nice in dry run output)
if not dep['external_module']:
deproot = '$%s' % get_software_root_env_var_name(dep['name'])
self._simulated_load_dependency_module(dep['name'], dep['version'], {'prefix': deproot})
else:
# load modules for all dependencies
self.log.debug("Loading modules for dependencies: %s", dep_mods)
self.modules_tool.load(dep_mods)
if self.dependencies:
build_dep_mods = [dep['short_mod_name'] for dep in self.dependencies if dep['build_only']]
if build_dep_mods:
trace_msg("loading modules for build dependencies:")
for dep_mod in build_dep_mods:
:param simple: if True, just return True/False to indicate success, else return a tuple: (output, exit_code)
:param inp: the input given to the command via stdin
:param regex: regex used to check the output for errors; if True it will use the default (see parse_log_for_error)
:param log_output: indicate whether all output of command should be logged to a separate temporary logfile
:param path: path to execute the command in; current working directory is used if unspecified
:param force_in_dry_run: force running the command during dry run
:param verbose: include message on running the command in dry run output
"""
cwd = os.getcwd()
# early exit in 'dry run' mode, after printing the command that would be run (unless running the command is forced)
if not force_in_dry_run and build_option('extended_dry_run'):
if path is None:
path = cwd
if verbose:
dry_run_msg(" running command \"%s\"" % cmd, silent=build_option('silent'))
dry_run_msg(" (in %s)" % path, silent=build_option('silent'))
# make sure we get the type of the return value right
if simple:
return True
else:
# output, exit code
return ('', 0)
try:
if path:
os.chdir(path)
_log.debug("run_cmd: running cmd %s (in %s)" % (cmd, os.getcwd()))
except OSError, err:
_log.warning("Failed to change to %s: %s" % (path, err))
"""
Unset the keys given in the environment
Returns a dict with the old values of the unset keys
"""
old_environ = {}
if keys and verbose and build_option('extended_dry_run'):
dry_run_msg("Undefining environment variables:\n", silent=build_option('silent'))
for key in list(keys):
if key in os.environ:
_log.info("Unsetting environment variable %s (value: %s)" % (key, os.environ[key]))
old_environ[key] = os.environ[key]
del os.environ[key]
if verbose and build_option('extended_dry_run'):
dry_run_msg(" unset %s # value was: %s" % (key, old_environ[key]), silent=build_option('silent'))
return old_environ
def move_file(path, target_path, force_in_dry_run=False):
"""
Move a file from path to target_path
:param path: the original filepath
:param target_path: path to move the file to
:param force_in_dry_run: force running the command during dry run
"""
if not force_in_dry_run and build_option('extended_dry_run'):
dry_run_msg("moved file %s to %s" % (path, target_path))
else:
# remove first to ensure portability (shutil.move might fail when overwriting files in some systems)
remove_file(target_path)
try:
mkdir(os.path.dirname(target_path), parents=True)
shutil.move(path, target_path)
_log.info("%s moved to %s", path, target_path)
except (IOError, OSError) as err:
raise EasyBuildError("Failed to move %s to %s: %s", path, target_path, err)
def copy_file(path, target_path, force_in_dry_run=False):
"""
Copy a file from specified location to specified location
:param path: the original filepath
:param target_path: path to copy the file to
:param force_in_dry_run: force copying of file during dry run
"""
if not force_in_dry_run and build_option('extended_dry_run'):
dry_run_msg("copied file %s to %s" % (path, target_path))
else:
try:
target_exists = os.path.exists(target_path)
if target_exists and os.path.samefile(path, target_path):
_log.debug("Not copying %s to %s since files are identical", path, target_path)
# if target file exists and is owned by someone else than the current user,
# try using shutil.copyfile to just copy the file contents
# since shutil.copy2 will fail when trying to copy over file metadata (since chown requires file ownership)
elif target_exists and os.stat(target_path).st_uid != os.getuid():
shutil.copyfile(path, target_path)
_log.info("Copied contents of file %s to %s", path, target_path)
else:
mkdir(os.path.dirname(target_path), parents=True)
shutil.copy2(path, target_path)
_log.info("%s copied to %s", path, target_path)
except (IOError, OSError, shutil.Error) as err:
def remove_file(path):
"""Remove file at specified path."""
# early exit in 'dry run' mode
if build_option('extended_dry_run'):
dry_run_msg("file %s removed" % path, silent=build_option('silent'))
return
try:
# note: file may also be a broken symlink...
if os.path.exists(path) or os.path.islink(path):
os.remove(path)
except OSError as err:
raise EasyBuildError("Failed to remove file %s: %s", path, err)
def _setenv_variables(self, donotset=None, verbose=True):
"""Actually set the environment variables"""
self.log.devel("_setenv_variables: setting variables: donotset=%s", donotset)
if self.dry_run:
dry_run_msg("Defining build environment...\n", silent=not verbose)
donotsetlist = []
if isinstance(donotset, str):
# TODO : more legacy code that should be using proper type
raise EasyBuildError("_setenv_variables: using commas-separated list. should be deprecated.")
elif isinstance(donotset, list):
donotsetlist = donotset
for key, val in sorted(self.vars.items()):
if key in donotsetlist:
self.log.debug("_setenv_variables: not setting environment variable %s (value: %s).", key, val)
continue
self.log.debug("_setenv_variables: setting environment variable %s to %s", key, val)
setvar(key, val, verbose=verbose)