@@ -27,14 +27,15 @@ def _process_sudo_cmd(cmd, sudo, sudo_options):
2727 if sudo and sudo_options is not None :
2828 if isinstance (sudo_options , str ):
2929 sudo_options = shlex .split (sudo_options )
30- cmd = ["sudo" ] + sudo_options + cmd
30+ cmd = ["sudo" , "-E" ] + sudo_options + cmd
3131 elif sudo :
32- cmd = ["sudo" ] + cmd
33- return cmd
32+ cmd = ["sudo" , "-E" ] + cmd
33+ return [ x for x in cmd if x ]
3434
3535
3636def check_install (software = "singularity" , quiet = True ):
37- """check_install will attempt to run the singularity command, and
37+ """
38+ check_install will attempt to run the singularity command, and
3839 return True if installed. The command line utils will not run
3940 without this check.
4041 """
@@ -66,7 +67,8 @@ def which(software="singularity"):
6667
6768
6869def get_singularity_version ():
69- """get the full singularity client version as reported by
70+ """
71+ get the full singularity client version as reported by
7072 singularity --version [...]. For Singularity 3.x, this means:
7173 "singularity version 3.0.1-1"
7274 """
@@ -85,17 +87,23 @@ def get_singularity_version():
8587
8688
8789def get_userhome ():
88- """get the user home based on the effective uid"""
90+ """
91+ Get the user home based on the effective uid
92+ """
8993 return pwd .getpwuid (os .getuid ())[5 ]
9094
9195
9296def get_username ():
93- """get the user name based on the effective uid"""
97+ """
98+ Get the user name based on the effective uid
99+ """
94100 return pwd .getpwuid (os .getuid ())[0 ]
95101
96102
97103def get_singularity_version_info ():
98- """get the full singularity client version as a semantic version" """
104+ """
105+ Get the full singularity client version as a semantic version"
106+ """
99107 version_string = get_singularity_version ()
100108 prefix = "singularity version "
101109 if version_string .startswith (prefix ):
@@ -106,7 +114,9 @@ def get_singularity_version_info():
106114
107115
108116def get_installdir ():
109- """get_installdir returns the installation directory of the application"""
117+ """
118+ Get_installdir returns the installation directory of the application
119+ """
110120 return os .path .abspath (os .path .dirname (os .path .dirname (__file__ )))
111121
112122
@@ -117,7 +127,8 @@ def stream_command(
117127 sudo_options = None ,
118128 output_type = "stdout" ,
119129):
120- """stream a command (yield) back to the user, as each line is available.
130+ """
131+ Stream a command (yield) back to the user, as each line is available.
121132
122133 # Example usage:
123134 results = []
@@ -167,9 +178,11 @@ def run_command(
167178 quiet = False ,
168179 sudo_options = None ,
169180 environ = None ,
181+ background = False ,
170182):
171183
172- """run_command uses subprocess to send a command to the terminal. If
184+ """
185+ run_command uses subprocess to send a command to the terminal. If
173186 capture is True, we use the parent stdout, so the progress bar (and
174187 other commands of interest) are piped to the user. This means we
175188 don't return the output to parse.
@@ -184,6 +197,7 @@ def run_command(
184197 option can print a progress bar, but won't return the lines
185198 as output.
186199 sudo_options: string or list of strings that will be passed as options to sudo
200+ background: run in background and don't try to get output.
187201 """
188202 cmd = _process_sudo_cmd (cmd , sudo , sudo_options )
189203
@@ -192,8 +206,12 @@ def run_command(
192206 stdout = subprocess .PIPE
193207
194208 # Use the parent stdout and stderr
209+ if background :
210+ subprocess .Popen (cmd , env = environ )
211+ return
195212
196213 process = subprocess .Popen (cmd , stderr = subprocess .PIPE , stdout = stdout , env = environ )
214+
197215 lines = []
198216 found_match = False
199217
@@ -222,7 +240,8 @@ def run_command(
222240
223241
224242def format_container_name (name , special_characters = None ):
225- """format_container_name will take a name supplied by the user,
243+ """
244+ format_container_name will take a name supplied by the user,
226245 remove all special characters (except for those defined by "special-characters"
227246 and return the new image name.
228247 """
@@ -232,7 +251,8 @@ def format_container_name(name, special_characters=None):
232251
233252
234253def split_uri (container ):
235- """Split the uri of a container into the protocol and image part
254+ """
255+ Split the uri of a container into the protocol and image part
236256
237257 An empty protocol is returned if none found.
238258 A trailing slash is removed from the image part.
@@ -247,5 +267,7 @@ def split_uri(container):
247267
248268
249269def remove_uri (container ):
250- """remove_uri will remove docker:// or shub:// or library:// from the uri"""
270+ """
271+ remove_uri will remove docker:// or shub:// or library:// from the uri
272+ """
251273 return split_uri (container )[1 ]
0 commit comments