Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
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
FSL
fslpy
Commits
e7f70760
Commit
e7f70760
authored
7 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
tweaks to run module, asd added DRY_RUN flag/dryrun context manager.
parent
c355eaa4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/utils/run.py
+55
-17
55 additions, 17 deletions
fsl/utils/run.py
with
55 additions
and
17 deletions
fsl/utils/run.py
+
55
−
17
View file @
e7f70760
...
@@ -16,33 +16,74 @@
...
@@ -16,33 +16,74 @@
import
logging
import
logging
import
contextlib
import
subprocess
as
sp
import
subprocess
as
sp
import
os.path
as
op
import
os.path
as
op
import
six
from
fsl.utils.platform
import
platform
as
fslplatform
from
fsl.utils.platform
import
platform
as
fslplatform
log
=
logging
.
getLogger
(
__name__
)
log
=
logging
.
getLogger
(
__name__
)
def
run
(
*
args
):
DRY_RUN
=
False
"""
Call a command and return its output. You can pass the command and
"""
If ``True``, the :func:`run` function will only log commands, but will not
arguments as a single string, or as an unpacked sequence.
execute them.
"""
@contextlib.contextmanager
def
dryrun
(
*
args
):
"""
Context manager which causes all calls to :func:`run` to be logged but
not executed. See the :data:`DRY_RUN` flag.
"""
global
DRY_RUN
oldval
=
DRY_RUN
DRY_RUN
=
True
try
:
yield
finally
:
DRY_RUN
=
oldval
def
_prepareArgs
(
args
):
"""
Used by the :func:`run` function. Ensures that the given arguments is a
list of strings.
"""
"""
# If we've been given a single argument,
# assume it is a string containing the
# command and its arguments. Otherwise,
# assume it is a sequence containing
# separate command and arguments.
if
len
(
args
)
==
1
:
if
len
(
args
)
==
1
:
args
=
args
[
0
].
split
()
args
=
list
(
args
)
# Argument was a command string
if
isinstance
(
args
[
0
],
six
.
string_types
):
args
=
args
[
0
].
split
()
log
.
debug
(
'
run: {}
'
.
format
(
'
'
.
join
(
args
)))
# Argument was an unpacked sequence
else
:
args
=
args
[
0
]
result
=
sp
.
check_output
(
args
).
decode
(
'
utf-8
'
).
strip
()
return
list
(
args
)
def
run
(
*
args
):
"""
Call a command and return its output. You can pass the command and
arguments as a single string, or as a regular or unpacked sequence.
"""
args
=
_prepareArgs
(
args
)
if
DRY_RUN
:
log
.
debug
(
'
dryrun: {}
'
.
format
(
'
'
.
join
(
args
)))
else
:
log
.
debug
(
'
run: {}
'
.
format
(
'
'
.
join
(
args
)))
if
DRY_RUN
:
result
=
'
<dryrun>
'
else
:
result
=
sp
.
check_output
(
args
).
decode
(
'
utf-8
'
).
strip
()
log
.
debug
(
'
result: {}
'
.
format
(
result
))
log
.
debug
(
'
result: {}
'
.
format
(
result
))
...
@@ -51,16 +92,13 @@ def run(*args):
...
@@ -51,16 +92,13 @@ def run(*args):
def
runfsl
(
*
args
):
def
runfsl
(
*
args
):
"""
Call a FSL command and return its output. This function simply prepends
"""
Call a FSL command and return its output. This function simply prepends
$FSLDIR/bin/ to the command before passing it to :func:`run`.
``
$FSLDIR/bin/
``
to the command before passing it to :func:`run`.
"""
"""
if
fslplatform
.
fsldir
is
None
:
if
fslplatform
.
fsldir
is
None
:
raise
RuntimeError
(
'
$FSLDIR is not set - FSL cannot be found!
'
)
raise
RuntimeError
(
'
$FSLDIR is not set - FSL cannot be found!
'
)
if
len
(
args
)
==
1
:
args
=
_prepareArgs
(
args
)
args
=
args
[
0
].
split
()
args
=
list
(
args
)
args
[
0
]
=
op
.
join
(
fslplatform
.
fsldir
,
'
bin
'
,
args
[
0
])
args
[
0
]
=
op
.
join
(
fslplatform
.
fsldir
,
'
bin
'
,
args
[
0
])
return
run
(
*
args
)
return
run
(
*
args
)
...
...
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