Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Spack
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
eic_tools
Spack
Commits
2d9dac9a
Commit
2d9dac9a
authored
8 years ago
by
Todd Gamblin
Committed by
GitHub
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix Python3 issue with sbang checking; add tests. (#4017)
parent
58567a21
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/spack/spack/hooks/sbang.py
+5
-2
5 additions, 2 deletions
lib/spack/spack/hooks/sbang.py
lib/spack/spack/test/sbang.py
+71
-47
71 additions, 47 deletions
lib/spack/spack/test/sbang.py
with
76 additions
and
49 deletions
lib/spack/spack/hooks/sbang.py
+
5
−
2
View file @
2d9dac9a
...
@@ -38,9 +38,12 @@
...
@@ -38,9 +38,12 @@
def
shebang_too_long
(
path
):
def
shebang_too_long
(
path
):
"""
Detects whether a file has a shebang line that is too long.
"""
"""
Detects whether a file has a shebang line that is too long.
"""
with
open
(
path
,
'
r
'
)
as
script
:
if
not
os
.
path
.
isfile
(
path
):
return
False
with
open
(
path
,
'
rb
'
)
as
script
:
bytes
=
script
.
read
(
2
)
bytes
=
script
.
read
(
2
)
if
bytes
!=
'
#!
'
:
if
bytes
!=
b
'
#!
'
:
return
False
return
False
line
=
bytes
+
script
.
readline
()
line
=
bytes
+
script
.
readline
()
...
...
This diff is collapsed.
Click to expand it.
lib/spack/spack/test/sbang.py
+
71
−
47
View file @
2d9dac9a
...
@@ -27,13 +27,16 @@
...
@@ -27,13 +27,16 @@
"""
"""
import
os
import
os
import
stat
import
stat
import
unit
test
import
py
test
import
tempfile
import
tempfile
import
shutil
import
shutil
from
llnl.util.filesystem
import
*
from
llnl.util.filesystem
import
*
from
spack.hooks.sbang
import
filter_shebangs_in_directory
import
spack
import
spack
from
spack.hooks.sbang
import
*
from
spack.util.executable
import
which
short_line
=
"
#!/this/is/short/bin/bash
\n
"
short_line
=
"
#!/this/is/short/bin/bash
\n
"
long_line
=
"
#!/this/
"
+
(
'
x
'
*
200
)
+
"
/is/long
\n
"
long_line
=
"
#!/this/
"
+
(
'
x
'
*
200
)
+
"
/is/long
\n
"
...
@@ -43,14 +46,13 @@
...
@@ -43,14 +46,13 @@
last_line
=
"
last!
\n
"
last_line
=
"
last!
\n
"
class
S
bangTest
(
unittest
.
TestCase
):
class
S
criptDirectory
(
object
):
"""
Directory full of test scripts to run sbang instrumentation on.
"""
def
setUp
(
self
):
def
__init__
(
self
):
self
.
tempdir
=
tempfile
.
mkdtemp
()
self
.
tempdir
=
tempfile
.
mkdtemp
()
# make sure we can ignore non-files
self
.
directory
=
os
.
path
.
join
(
self
.
tempdir
,
'
dir
'
)
directory
=
os
.
path
.
join
(
self
.
tempdir
,
'
dir
'
)
mkdirp
(
self
.
directory
)
mkdirp
(
directory
)
# Script with short shebang
# Script with short shebang
self
.
short_shebang
=
os
.
path
.
join
(
self
.
tempdir
,
'
short
'
)
self
.
short_shebang
=
os
.
path
.
join
(
self
.
tempdir
,
'
short
'
)
...
@@ -71,48 +73,70 @@ def setUp(self):
...
@@ -71,48 +73,70 @@ def setUp(self):
f
.
write
(
last_line
)
f
.
write
(
last_line
)
# Script already using sbang.
# Script already using sbang.
self
.
has_s
he
bang
=
os
.
path
.
join
(
self
.
tempdir
,
'
shebang
'
)
self
.
has_sbang
=
os
.
path
.
join
(
self
.
tempdir
,
'
shebang
'
)
with
open
(
self
.
has_s
he
bang
,
'
w
'
)
as
f
:
with
open
(
self
.
has_sbang
,
'
w
'
)
as
f
:
f
.
write
(
sbang_line
)
f
.
write
(
sbang_line
)
f
.
write
(
long_line
)
f
.
write
(
long_line
)
f
.
write
(
last_line
)
f
.
write
(
last_line
)
def
tearDown
(
self
):
# Fake binary file.
self
.
binary
=
os
.
path
.
join
(
self
.
tempdir
,
'
binary
'
)
tar
=
which
(
'
tar
'
,
required
=
True
)
tar
(
'
czf
'
,
self
.
binary
,
self
.
has_sbang
)
def
destroy
(
self
):
shutil
.
rmtree
(
self
.
tempdir
,
ignore_errors
=
True
)
shutil
.
rmtree
(
self
.
tempdir
,
ignore_errors
=
True
)
def
test_shebang_handling
(
self
):
filter_shebangs_in_directory
(
self
.
tempdir
)
@pytest.fixture
def
script_dir
():
# Make sure this is untouched
sdir
=
ScriptDirectory
()
with
open
(
self
.
short_shebang
,
'
r
'
)
as
f
:
yield
sdir
self
.
assertEqual
(
f
.
readline
(),
short_line
)
sdir
.
destroy
()
self
.
assertEqual
(
f
.
readline
(),
last_line
)
# Make sure this got patched.
def
test_shebang_handling
(
script_dir
):
with
open
(
self
.
long_shebang
,
'
r
'
)
as
f
:
assert
shebang_too_long
(
script_dir
.
lua_shebang
)
self
.
assertEqual
(
f
.
readline
(),
sbang_line
)
assert
shebang_too_long
(
script_dir
.
long_shebang
)
self
.
assertEqual
(
f
.
readline
(),
long_line
)
self
.
assertEqual
(
f
.
readline
(),
last_line
)
assert
not
shebang_too_long
(
script_dir
.
short_shebang
)
assert
not
shebang_too_long
(
script_dir
.
has_sbang
)
# Make sure this got patched.
assert
not
shebang_too_long
(
script_dir
.
binary
)
with
open
(
self
.
lua_shebang
,
'
r
'
)
as
f
:
assert
not
shebang_too_long
(
script_dir
.
directory
)
self
.
assertEqual
(
f
.
readline
(),
sbang_line
)
self
.
assertEqual
(
f
.
readline
(),
lua_line_patched
)
filter_shebangs_in_directory
(
script_dir
.
tempdir
)
self
.
assertEqual
(
f
.
readline
(),
last_line
)
# Make sure this is untouched
# Make sure this is untouched
with
open
(
script_dir
.
short_shebang
,
'
r
'
)
as
f
:
with
open
(
self
.
has_shebang
,
'
r
'
)
as
f
:
assert
f
.
readline
()
==
short_line
self
.
assertEqual
(
f
.
readline
(),
sbang_line
)
assert
f
.
readline
()
==
last_line
self
.
assertEqual
(
f
.
readline
(),
long_line
)
self
.
assertEqual
(
f
.
readline
(),
last_line
)
# Make sure this got patched.
with
open
(
script_dir
.
long_shebang
,
'
r
'
)
as
f
:
def
test_shebang_handles_non_writable_files
(
self
):
assert
f
.
readline
()
==
sbang_line
# make a file non-writable
assert
f
.
readline
()
==
long_line
st
=
os
.
stat
(
self
.
long_shebang
)
assert
f
.
readline
()
==
last_line
not_writable_mode
=
st
.
st_mode
&
~
stat
.
S_IWRITE
os
.
chmod
(
self
.
long_shebang
,
not_writable_mode
)
# Make sure this got patched.
with
open
(
script_dir
.
lua_shebang
,
'
r
'
)
as
f
:
self
.
test_shebang_handling
()
assert
f
.
readline
()
==
sbang_line
assert
f
.
readline
()
==
lua_line_patched
st
=
os
.
stat
(
self
.
long_shebang
)
assert
f
.
readline
()
==
last_line
self
.
assertEqual
(
oct
(
not_writable_mode
),
oct
(
st
.
st_mode
))
# Make sure this is untouched
with
open
(
script_dir
.
has_sbang
,
'
r
'
)
as
f
:
assert
f
.
readline
()
==
sbang_line
assert
f
.
readline
()
==
long_line
assert
f
.
readline
()
==
last_line
def
test_shebang_handles_non_writable_files
(
script_dir
):
# make a file non-writable
st
=
os
.
stat
(
script_dir
.
long_shebang
)
not_writable_mode
=
st
.
st_mode
&
~
stat
.
S_IWRITE
os
.
chmod
(
script_dir
.
long_shebang
,
not_writable_mode
)
test_shebang_handling
(
script_dir
)
st
=
os
.
stat
(
script_dir
.
long_shebang
)
assert
oct
(
not_writable_mode
)
==
oct
(
st
.
st_mode
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment