Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# try using adjust_permissions on a file not owned by current user,
# using permissions that are actually already correct;
# actual chmod should be skipped, otherwise it fails (you need to own a file to change permissions on it)
# use /bin/ls, which should always be there, has read/exec permissions for anyone (755), and is owned by root
ls_path = '/bin/ls'
# try adding read/exec permissions for current user (which is already there)
ft.adjust_permissions(ls_path, stat.S_IRUSR | stat.S_IXUSR, add=True)
# try removing write permissions for others (which are not set already)
ft.adjust_permissions(ls_path, stat.S_IWOTH, add=False)
# try hard setting permissions using current permissions
current_ls_perms = os.stat(ls_path)[stat.ST_MODE]
ft.adjust_permissions(ls_path, current_ls_perms, relative=False)
# restore original umask
os.umask(orig_umask)
def test_toolchain_prepare_rpath(self):
"""Test toolchain.prepare under --rpath"""
# put fake 'g++' command in place that just echos its arguments
fake_gxx = os.path.join(self.test_prefix, 'fake', 'g++')
write_file(fake_gxx, '#!/bin/bash\necho "$@"')
adjust_permissions(fake_gxx, stat.S_IXUSR)
os.environ['PATH'] = '%s:%s' % (os.path.join(self.test_prefix, 'fake'), os.getenv('PATH', ''))
# enable --rpath and prepare toolchain
init_config(build_options={'rpath': True, 'rpath_filter': ['/ba.*'], 'silent': True})
tc = self.get_toolchain('gompi', version='2018a')
# preparing RPATH wrappers requires --experimental, need to bypass that here
tc.log.experimental = lambda x: x
# 'rpath' toolchain option gives control to disable use of RPATH wrappers
tc.set_options({})
self.assertTrue(tc.options['rpath']) # enabled by default
# setting 'rpath' toolchain option to false implies no RPATH wrappers being used
tc.set_options({'rpath': False})
tc.prepare()
def test_find_eb_script(self):
"""Test find_eb_script function."""
self.assertTrue(os.path.exists(ft.find_eb_script('rpath_args.py')))
self.assertTrue(os.path.exists(ft.find_eb_script('rpath_wrapper_template.sh.in')))
self.assertErrorRegex(EasyBuildError, "Script 'no_such_script' not found", ft.find_eb_script, 'no_such_script')
# put test script in place relative to location of 'eb'
ft.write_file(os.path.join(self.test_prefix, 'bin', 'eb'), '#!/bin/bash\necho "fake eb"')
ft.adjust_permissions(os.path.join(self.test_prefix, 'bin', 'eb'), stat.S_IXUSR)
os.environ['PATH'] = '%s:%s' % (os.path.join(self.test_prefix, 'bin'), os.getenv('PATH', ''))
justatest = os.path.join(self.test_prefix, 'easybuild', 'scripts', 'justatest.sh')
ft.write_file(justatest, '#!/bin/bash')
self.assertTrue(os.path.samefile(ft.find_eb_script('justatest.sh'), justatest))
self.assertErrorRegex(EasyBuildError, err_msg, ft.adjust_permissions, nosuchdir, stat.S_IWOTH)
nosuchfile = os.path.join(self.test_prefix, 'nosuchfile')
self.assertErrorRegex(EasyBuildError, err_msg, ft.adjust_permissions, nosuchfile, stat.S_IWUSR, recursive=False)
# try using adjust_permissions on a file not owned by current user,
# using permissions that are actually already correct;
# actual chmod should be skipped, otherwise it fails (you need to own a file to change permissions on it)
# use /bin/ls, which should always be there, has read/exec permissions for anyone (755), and is owned by root
ls_path = '/bin/ls'
# try adding read/exec permissions for current user (which is already there)
ft.adjust_permissions(ls_path, stat.S_IRUSR | stat.S_IXUSR, add=True)
# try removing write permissions for others (which are not set already)
ft.adjust_permissions(ls_path, stat.S_IWOTH, add=False)
# try hard setting permissions using current permissions
current_ls_perms = os.stat(ls_path)[stat.ST_MODE]
ft.adjust_permissions(ls_path, current_ls_perms, relative=False)
# restore original umask
os.umask(orig_umask)
stat.S_IROTH, stat.S_IXOTH]:
self.assertTrue(bar_perms & bit)
self.assertFalse(bar_perms & stat.S_IWOTH)
# foo/foobar file: rwxrw-r--
for path in [os.path.join(self.test_prefix, 'bar', 'foobar'), os.path.join(self.test_prefix, 'foobar_symlink')]:
perms = os.stat(path)[stat.ST_MODE]
for bit in [stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR, stat.S_IRGRP, stat.S_IWGRP, stat.S_IROTH]:
self.assertTrue(perms & bit)
for bit in [stat.S_IXGRP, stat.S_IWOTH, stat.S_IXOTH]:
self.assertFalse(perms & bit)
# check error reporting when changing permissions fails
nosuchdir = os.path.join(self.test_prefix, 'nosuchdir')
err_msg = "Failed to chmod/chown several paths.*No such file or directory"
self.assertErrorRegex(EasyBuildError, err_msg, ft.adjust_permissions, nosuchdir, stat.S_IWOTH)
nosuchfile = os.path.join(self.test_prefix, 'nosuchfile')
self.assertErrorRegex(EasyBuildError, err_msg, ft.adjust_permissions, nosuchfile, stat.S_IWUSR, recursive=False)
# try using adjust_permissions on a file not owned by current user,
# using permissions that are actually already correct;
# actual chmod should be skipped, otherwise it fails (you need to own a file to change permissions on it)
# use /bin/ls, which should always be there, has read/exec permissions for anyone (755), and is owned by root
ls_path = '/bin/ls'
# try adding read/exec permissions for current user (which is already there)
ft.adjust_permissions(ls_path, stat.S_IRUSR | stat.S_IXUSR, add=True)
# try removing write permissions for others (which are not set already)
ft.adjust_permissions(ls_path, stat.S_IWOTH, add=False)
for bit in [stat.S_IXUSR, stat.S_IWGRP, stat.S_IXGRP, stat.S_IWOTH, stat.S_IXOTH]:
self.assertFalse(foobar_perms & bit)
# include symlink
os.symlink(foobar_path, os.path.join(self.test_prefix, 'foobar_symlink'))
# include broken symlink (symlinks are skipped, so this shouldn't cause problems)
tmpfile = os.path.join(self.test_prefix, 'thiswontbetherelong')
ft.write_file(tmpfile, 'poof!')
os.symlink(tmpfile, os.path.join(self.test_prefix, 'broken_symlink'))
os.remove(tmpfile)
# test default behaviour:
# recursive, add permissions, relative to existing permissions, both files and dirs, skip symlinks
# add user execution, group write permissions
ft.adjust_permissions(self.test_prefix, stat.S_IXUSR|stat.S_IWGRP)
# foo file: rwxrw-r--
foo_perms = os.stat(os.path.join(self.test_prefix, 'foo'))[stat.ST_MODE]
for bit in [stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR, stat.S_IRGRP, stat.S_IWGRP, stat.S_IROTH]:
self.assertTrue(foo_perms & bit)
for bit in [stat.S_IXGRP, stat.S_IWOTH, stat.S_IXOTH]:
self.assertFalse(foo_perms & bit)
# bar dir: rwxrwxr-x
bar_perms = os.stat(os.path.join(self.test_prefix, 'bar'))[stat.ST_MODE]
for bit in [stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR, stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP,
stat.S_IROTH, stat.S_IXOTH]:
self.assertTrue(bar_perms & bit)
self.assertFalse(bar_perms & stat.S_IWOTH)
# foo/foobar file: rwxrw-r--
# add another location to 'bar', which should only return the first location by default
barbis = os.path.join(self.test_prefix, 'more', 'bar')
ft.write_file(barbis, '#!/bin/bash')
ft.adjust_permissions(barbis, stat.S_IRUSR | stat.S_IXUSR)
os.environ['PATH'] = '%s:%s' % (os.environ['PATH'], os.path.dirname(barbis))
self.assertTrue(os.path.samefile(ft.which('bar'), bar))
# test getting *all* locations to specified command
res = ft.which('bar', retain_all=True)
self.assertEqual(len(res), 2)
self.assertTrue(os.path.samefile(res[0], bar))
self.assertTrue(os.path.samefile(res[1], barbis))
# both read/exec permissions must be available
# if read permissions are removed for first hit, second hit is found instead
ft.adjust_permissions(bar, stat.S_IRUSR, add=False)
self.assertTrue(os.path.samefile(ft.which('bar'), barbis))
# likewise for read permissions
ft.adjust_permissions(bar, stat.S_IRUSR, add=True)
self.assertTrue(os.path.samefile(ft.which('bar'), bar))
ft.adjust_permissions(bar, stat.S_IXUSR, add=False)
self.assertTrue(os.path.samefile(ft.which('bar'), barbis))
# if read permission on other 'bar' are also removed, nothing is found anymore
ft.adjust_permissions(barbis, stat.S_IRUSR, add=False)
self.assertEqual(ft.which('bar'), None)
# checking of read/exec permissions can be disabled via 'check_perms'
self.assertTrue(os.path.samefile(ft.which('bar', check_perms=False), bar))
'--container-base=ubuntu:16.04',
'--container-build-image',
]
if not which('docker'):
error_pattern = "docker not found on your system."
self.assertErrorRegex(EasyBuildError, error_pattern, self.run_main, args, raise_error=True)
# install mocked versions of 'sudo' and 'docker' commands
docker = os.path.join(self.test_prefix, 'bin', 'docker')
write_file(docker, MOCKED_DOCKER)
adjust_permissions(docker, stat.S_IXUSR, add=True)
sudo = os.path.join(self.test_prefix, 'bin', 'sudo')
write_file(sudo, '#!/bin/bash\necho "running command \'$@\' with sudo..."\neval "$@"\n')
adjust_permissions(sudo, stat.S_IXUSR, add=True)
os.environ['PATH'] = os.path.pathsep.join([os.path.join(self.test_prefix, 'bin'), os.getenv('PATH')])
stdout, stderr = self.run_main(args)
self.assertFalse(stderr)
regexs = [
"^== docker tool found at %s/bin/docker" % self.test_prefix,
"^== Dockerfile definition file created at %s/containers/Dockerfile\.toy-0.0" % self.test_prefix,
"^== Running 'sudo docker build -f .* -t .* \.', you may need to enter your 'sudo' password...",
"^== Docker image created at toy-0.0:latest",
]
self.check_regexs(regexs, stdout)
args.extend(['--force', '--extended-dry-run'])
stdout, stderr = self.run_main(args)
self.assertFalse(stderr)
def write_wrapper(self, wrapper_dir, compiler, i_mpi_root):
"""Helper function to write a compiler wrapper."""
wrapper_txt = INTEL_COMPILER_WRAPPER % {
'compiler_path': which(compiler),
'intel_mpi_root': i_mpi_root,
'cpath': os.getenv('CPATH'),
'intel_license_file': os.getenv('INTEL_LICENSE_FILE', os.getenv('LM_LICENSE_FILE')),
'wrapper_dir': wrapper_dir,
}
wrapper = os.path.join(wrapper_dir, compiler)
write_file(wrapper, wrapper_txt)
if self.dry_run:
self.dry_run_msg("Wrapper for '%s' was put in place: %s", compiler, wrapper)
else:
adjust_permissions(wrapper, stat.S_IXUSR)
self.log.info("Using wrapper script for '%s': %s", compiler, which(compiler))
if self.cfg['group']:
gid = grp.getgrnam(self.cfg['group'])[2]
# rwx for owner, r-x for group, --- for other
try:
adjust_permissions(self.installdir, 0750, recursive=True, group_id=gid, relative=False,
ignore_errors=True)
except EasyBuildError, err:
self.log.error("Unable to change group permissions of file(s). " \
"Are you a member of this group?\n%s" % err)
self.log.info("Successfully made software only available for group %s" % self.cfg['group'])
else:
# remove write permissions for group and other
perms = stat.S_IWGRP | stat.S_IWOTH
adjust_permissions(self.installdir, perms, add=False, recursive=True, relative=True, ignore_errors=True)
self.log.info("Successfully removed write permissions recursively for group/other on install dir.")
if read_only_installdir():
# remove write permissions for everyone
perms = stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH
adjust_permissions(self.installdir, perms, add=False, recursive=True, relative=True, ignore_errors=True)
self.log.info("Successfully removed write permissions recursively for *EVERYONE* on install dir.")