Skip to content

Commit 4929b7b

Browse files
committed
added configuration option to define the env command to use for shebangs
1 parent f4a9479 commit 4929b7b

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

easybuild/framework/easyblock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2367,7 +2367,7 @@ def fix_shebang(self):
23672367
if isinstance(fix_shebang_for, string_type):
23682368
fix_shebang_for = [fix_shebang_for]
23692369

2370-
shebang = '#!/usr/bin/env %s' % lang
2370+
shebang = '#!%s %s' % (build_option('env_for_shebang'), lang)
23712371
for glob_pattern in fix_shebang_for:
23722372
paths = glob.glob(os.path.join(self.installdir, glob_pattern))
23732373
self.log.info("Fixing '%s' shebang to '%s' for files that match '%s': %s",

easybuild/tools/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
DEFAULT_CONT_TYPE = CONT_TYPE_SINGULARITY
8080

8181
DEFAULT_BRANCH = 'develop'
82+
DEFAULT_ENV_FOR_SHEBANG = '/usr/bin/env'
8283
DEFAULT_ENVVAR_USERS_MODULES = 'HOME'
8384
DEFAULT_INDEX_MAX_AGE = 7 * 24 * 60 * 60 # 1 week (in seconds)
8485
DEFAULT_JOB_BACKEND = 'GC3Pie'
@@ -295,6 +296,9 @@ def mk_full_default_path(name, prefix=DEFAULT_PREFIX):
295296
DEFAULT_BRANCH: [
296297
'pr_target_branch',
297298
],
299+
DEFAULT_ENV_FOR_SHEBANG: [
300+
'env_for_shebang',
301+
],
298302
DEFAULT_INDEX_MAX_AGE: [
299303
'index_max_age',
300304
],

easybuild/tools/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ def override_options(self):
375375
None, 'store', None, 'e', {'metavar': 'CLASS'}),
376376
'enforce-checksums': ("Enforce availability of checksums for all sources/patches, so they can be verified",
377377
None, 'store_true', False),
378+
'env-for-shebang': ("Define the env command to use when fixing shebangs", None, 'store', '/usr/bin/env'),
378379
'experimental': ("Allow experimental code (with behaviour that can be changed/removed at any given time).",
379380
None, 'store_true', False),
380381
'extra-modules': ("List of extra modules to load after setting up the build environment",

test/framework/toy_build.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import sys
4141
import tempfile
4242
from distutils.version import LooseVersion
43-
from test.framework.utilities import EnhancedTestCase, TestLoaderFiltered
43+
from test.framework.utilities import EnhancedTestCase, TestLoaderFiltered, init_config
4444
from test.framework.package import mock_fpm
4545
from unittest import TextTestRunner
4646

@@ -2854,6 +2854,47 @@ def test_fix_shebang(self):
28542854
self.assertTrue(bash_shebang_regex.match(bashbin_txt),
28552855
"Pattern '%s' found in %s: %s" % (bash_shebang_regex.pattern, bashbin_path, bashbin_txt))
28562856

2857+
# now test with a custom env command
2858+
init_config(build_options={'env_for_shebang': "/usr/bin/env -S"})
2859+
2860+
self.test_toy_build(ec_file=test_ec, raise_error=True)
2861+
2862+
toy_bindir = os.path.join(self.test_installpath, 'software', 'toy', '0.0', 'bin')
2863+
2864+
# bin/toy and bin/toy2 should *not* be patched, since they're binary files
2865+
toy_txt = read_file(os.path.join(toy_bindir, 'toy'), mode='rb')
2866+
for fn in ['toy.perl', 'toy.python']:
2867+
fn_txt = read_file(os.path.join(toy_bindir, fn), mode='rb')
2868+
# no shebang added
2869+
self.assertFalse(fn_txt.startswith(b"#!/"))
2870+
# exact same file as original binary (untouched)
2871+
self.assertEqual(toy_txt, fn_txt)
2872+
2873+
# no re.M, this should match at start of file!
2874+
py_shebang_regex = re.compile(r'^#!/usr/bin/env -S python\n# test$')
2875+
for pybin in ['t1.py', 't2.py', 't3.py', 't4.py', 't5.py', 't6.py', 't7.py']:
2876+
pybin_path = os.path.join(toy_bindir, pybin)
2877+
pybin_txt = read_file(pybin_path)
2878+
self.assertTrue(py_shebang_regex.match(pybin_txt),
2879+
"Pattern '%s' found in %s: %s" % (py_shebang_regex.pattern, pybin_path, pybin_txt))
2880+
2881+
# no re.M, this should match at start of file!
2882+
perl_shebang_regex = re.compile(r'^#!/usr/bin/env -S perl\n# test$')
2883+
for perlbin in ['t1.pl', 't2.pl', 't3.pl', 't4.pl', 't5.pl', 't6.pl', 't7.pl']:
2884+
perlbin_path = os.path.join(toy_bindir, perlbin)
2885+
perlbin_txt = read_file(perlbin_path)
2886+
self.assertTrue(perl_shebang_regex.match(perlbin_txt),
2887+
"Pattern '%s' found in %s: %s" % (perl_shebang_regex.pattern, perlbin_path, perlbin_txt))
2888+
2889+
# There are 2 bash files which shouldn't be influenced by fix_shebang
2890+
bash_shebang_regex = re.compile(r'^#!/usr/bin/env bash\n# test$')
2891+
for bashbin in ['b1.sh', 'b2.sh']:
2892+
bashbin_path = os.path.join(toy_bindir, bashbin)
2893+
bashbin_txt = read_file(bashbin_path)
2894+
self.assertTrue(bash_shebang_regex.match(bashbin_txt),
2895+
"Pattern '%s' found in %s: %s" % (bash_shebang_regex.pattern, bashbin_path, bashbin_txt))
2896+
2897+
28572898
def test_toy_system_toolchain_alias(self):
28582899
"""Test use of 'system' toolchain alias."""
28592900
toy_ec = os.path.join(os.path.dirname(__file__), 'easyconfigs', 'test_ecs', 't', 'toy', 'toy-0.0.eb')

0 commit comments

Comments
 (0)