Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
allocbench
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
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Fischer
allocbench
Commits
05473cb4
Commit
05473cb4
authored
5 years ago
by
Florian Fischer
Browse files
Options
Downloads
Patches
Plain Diff
add callable subprocess wrapper Cmd
parent
432ae05b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/cmd.py
+87
-0
87 additions, 0 deletions
src/cmd.py
with
87 additions
and
0 deletions
src/cmd.py
0 → 100644
+
87
−
0
View file @
05473cb4
# Copyright 2018-2019 Florian Fischer <florian.fl.fischer@fau.de>
#
# This file is part of allocbench.
#
# allocbench is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# allocbench is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with allocbench. If not, see <http://www.gnu.org/licenses/>.
"""
Callable wrapper classes for subprocess.run
"""
import
subprocess
import
src.globalvars
class
Cmd
:
"""
subprocess.run wrapper which cares about verbosity
"""
def
__init__
(
self
,
cmd
,
output_verbosity
=
2
,
capture
=
False
,
shell
=
False
,
check
=
True
,
cwd
=
None
,
input
=
None
):
if
isinstance
(
cmd
,
str
):
cmd
=
cmd
.
split
()
self
.
cmd
=
cmd
self
.
output_verbosity
=
output_verbosity
self
.
capture
=
capture
self
.
shell
=
shell
self
.
check
=
check
self
.
cwd
=
cwd
self
.
inpt
=
input
def
__run
(
self
,
inpt
,
cwd
):
"""
execute the cmd in cwd and pass input to it
"""
if
self
.
capture
:
stdout
=
subprocess
.
PIPE
stderr
=
stdout
elif
src
.
globalvars
.
verbosity
<
self
.
output_verbosity
:
stdout
=
subprocess
.
DEVNULL
stderr
=
stdout
else
:
stdout
=
None
stderr
=
stdout
return
subprocess
.
run
(
self
.
cmd
,
stdout
=
stdout
,
stderr
=
stderr
,
shell
=
self
.
shell
,
check
=
self
.
check
,
input
=
inpt
,
cwd
=
cwd
,
universal_newlines
=
True
)
def
__call__
(
self
):
"""
default execution with the arguments passed with __init__
"""
return
self
.
__run
(
self
.
inpt
,
self
.
cwd
)
def
run_in_dir
(
self
,
cwd
):
"""
execute cmd in a specific directory
"""
return
self
.
__run
(
self
.
inpt
,
cwd
)
class
ShellCmd
(
Cmd
):
"""
A shell cmd and printing its output acording to our verbosity level
"""
def
__init__
(
self
,
cmd
,
output_verbosity
=
2
,
capture
=
False
,
check
=
True
,
cwd
=
None
,
input
=
None
):
super
().
__init__
(
cmd
,
output_verbosity
,
capture
,
True
,
check
,
cwd
,
input
)
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