Skip to content

Commit 4542532

Browse files
cceydaCeyda Cinarel
andauthored
Add sudo_options & 'env' parameters to run_command (#167)
* add sudo_options kwarg, add env kwarg to run_command * add sudo_options to instance stop * add environment parameter to run_command * lint code * rename env to environ * lint Co-authored-by: Ceyda Cinarel <snu-ceyda@users.noreply.github.com>
1 parent fb33495 commit 4542532

File tree

8 files changed

+43
-9
lines changed

8 files changed

+43
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ The client here will eventually be released as "spython" (and eventually to
1717
singularity on pypi), and the versions here will coincide with these releases.
1818

1919
## [master](https://github.com/singularityhub/singularity-cli/tree/master)
20+
- add sudo_options option to Instance.start/stop and Client.execute (0.0.85)
21+
- add environ option to Instance.start and Client.execute for passing env variables
2022
- Small bugfix for docker writer and adding pyflakes for unused imports (0.0.84)
2123
- Adding support for multistage build parsing (0.0.83)
2224
- Singularity Python does not yet support multistage builds (0.0.82)

spython/instance/cmd/start.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ def start(
1414
name=None,
1515
args=None,
1616
sudo=False,
17+
sudo_options=None,
1718
options=None,
1819
capture=False,
1920
singularity_options=None,
21+
environ=None,
2022
):
2123
"""start an instance. This is done by default when an instance is created.
2224
@@ -77,7 +79,14 @@ def start(
7779
self.args = args
7880
self.cmd = cmd
7981

80-
output = run_command(cmd, sudo=sudo, quiet=True, capture=capture)
82+
output = run_command(
83+
cmd,
84+
sudo=sudo,
85+
sudo_options=sudo_options,
86+
quiet=True,
87+
capture=capture,
88+
environ=environ,
89+
)
8190

8291
if output["return_code"] == 0:
8392
self._update_metadata()

spython/instance/cmd/stop.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
from spython.logger import bot
99

1010

11-
def stop(self, name=None, sudo=False, timeout=None, singularity_options=None):
11+
def stop(
12+
self,
13+
name=None,
14+
sudo=False,
15+
sudo_options=None,
16+
timeout=None,
17+
singularity_options=None,
18+
):
1219
"""stop an instance. This is done by default when an instance is created.
1320
1421
Parameters
@@ -42,7 +49,7 @@ def stop(self, name=None, sudo=False, timeout=None, singularity_options=None):
4249
instance_name = name
4350
cmd = cmd + [instance_name]
4451

45-
output = run_command(cmd, sudo=sudo, quiet=True)
52+
output = run_command(cmd, sudo=sudo, sudo_options=sudo_options, quiet=True)
4653

4754
if output["return_code"] != 0:
4855
message = "%s : return code %s" % (output["message"], output["return_code"])

spython/main/base/command.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def run_command(
108108
quiet=None,
109109
return_result=False,
110110
sudo_options=None,
111+
environ=None,
111112
):
112113

113114
"""run_command is a wrapper for the global run_command, checking first
@@ -130,7 +131,12 @@ def run_command(
130131
quiet = self.quiet
131132

132133
result = run_cmd(
133-
cmd, sudo=sudo, capture=capture, quiet=quiet, sudo_options=sudo_options
134+
cmd,
135+
sudo=sudo,
136+
capture=capture,
137+
quiet=quiet,
138+
sudo_options=sudo_options,
139+
environ=environ,
134140
)
135141

136142
# If one line is returned, squash dimension

spython/main/execute.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ def execute(
2525
options=None,
2626
singularity_options=None,
2727
sudo=False,
28+
sudo_options=None,
2829
quiet=True,
30+
environ=None,
2931
):
3032
"""execute: send a command to a container
3133
@@ -99,9 +101,14 @@ def execute(
99101

100102
if not stream:
101103
return self._run_command(
102-
cmd, sudo=sudo, return_result=return_result, quiet=quiet
104+
cmd,
105+
sudo=sudo,
106+
sudo_options=sudo_options,
107+
return_result=return_result,
108+
quiet=quiet,
109+
environ=environ,
103110
)
104-
return stream_command(cmd, sudo=sudo)
111+
return stream_command(cmd, sudo=sudo, sudo_options=sudo_options)
105112

106113
bot.exit("Please include a command (list) to execute.")
107114

spython/main/instances.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def list_instances(
1515
return_json=False,
1616
quiet=False,
1717
sudo=False,
18+
sudo_options=None,
1819
singularity_options=None,
1920
):
2021
"""list instances. For Singularity, this is provided as a command sub
@@ -53,7 +54,7 @@ def list_instances(
5354
if name is not None:
5455
cmd.append(name)
5556

56-
output = run_command(cmd, quiet=True, sudo=sudo)
57+
output = run_command(cmd, quiet=True, sudo=sudo, sudo_options=sudo_options)
5758
instances = []
5859

5960
# Success, we have instances

spython/utils/terminal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def run_command(
149149
no_newline_regexp="Progess",
150150
quiet=False,
151151
sudo_options=None,
152+
environ=None,
152153
):
153154

154155
"""run_command uses subprocess to send a command to the terminal. If
@@ -174,7 +175,8 @@ def run_command(
174175
stdout = subprocess.PIPE
175176

176177
# Use the parent stdout and stderr
177-
process = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=stdout)
178+
179+
process = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=stdout, env=environ)
178180
lines = []
179181
found_match = False
180182

spython/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
66

77

8-
__version__ = "0.0.84"
8+
__version__ = "0.0.85"
99
AUTHOR = "Vanessa Sochat"
1010
AUTHOR_EMAIL = "vsochat@stanford.edu"
1111
NAME = "spython"

0 commit comments

Comments
 (0)