Skip to content

Commit 2b4db47

Browse files
authored
Merge pull request #3705 from branfosj/strip
correctly strip extension from filename in extract_cmd and back_up_file functions
2 parents 9991d5b + 30713da commit 2b4db47

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

easybuild/tools/filetools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ def extract_cmd(filepath, overwrite=False):
13201320
"""
13211321
filename = os.path.basename(filepath)
13221322
ext = find_extension(filename)
1323-
target = filename.rstrip(ext)
1323+
target = filename[:-len(ext)]
13241324

13251325
cmd_tmpl = EXTRACT_CMDS[ext.lower()]
13261326
if overwrite:
@@ -2005,8 +2005,8 @@ def back_up_file(src_file, backup_extension='bak', hidden=False, strip_fn=None):
20052005
fn_suffix = '.%s' % backup_extension
20062006

20072007
src_dir, src_fn = os.path.split(src_file)
2008-
if strip_fn:
2009-
src_fn = src_fn.rstrip(strip_fn)
2008+
if strip_fn and src_fn.endswith(strip_fn):
2009+
src_fn = src_fn[:-len(strip_fn)]
20102010

20112011
backup_fp = find_backup_name_candidate(os.path.join(src_dir, fn_prefix + src_fn + fn_suffix))
20122012

test/framework/filetools.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def test_extract_cmd(self):
9797
('test.iso', "7z x test.iso"),
9898
('test.tar.Z', "tar xzf test.tar.Z"),
9999
('test.foo.bar.sh', "cp -a test.foo.bar.sh ."),
100+
# check whether extension is stripped correct to determine name of target file
101+
# cfr. https://github.com/easybuilders/easybuild-framework/pull/3705
102+
('testbz2.bz2', "bunzip2 -c testbz2.bz2 > testbz2"),
103+
('testgz.gz', "gunzip -c testgz.gz > testgz"),
100104
]
101105
for (fn, expected_cmd) in tests:
102106
cmd = ft.extract_cmd(fn)
@@ -926,14 +930,18 @@ def test_back_up_file(self):
926930
self.assertEqual(ft.read_file(fp), new_txt)
927931

928932
# check whether strip_fn works as expected
929-
fp2 = fp + '.lua'
933+
fp2 = fp + 'a.lua'
930934
ft.copy_file(fp, fp2)
931935
res = ft.back_up_file(fp2)
932936
self.assertTrue(fp2.endswith('.lua'))
933937
self.assertTrue('.lua' in os.path.basename(res))
934938

935939
res = ft.back_up_file(fp2, strip_fn='.lua')
936940
self.assertFalse('.lua' in os.path.basename(res))
941+
# strip_fn should not remove the first a in 'a.lua'
942+
expected = os.path.basename(fp) + 'a.bak_'
943+
res_fn = os.path.basename(res)
944+
self.assertTrue(res_fn.startswith(expected), "'%s' should start with with '%s'" % (res_fn, expected))
937945

938946
def test_move_logs(self):
939947
"""Test move_logs function."""

0 commit comments

Comments
 (0)