Skip to content

Commit e999189

Browse files
committed
adding pull command for shub, and testing get_files command
1 parent 28b05cc commit e999189

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

singularity/analysis/classify.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from singularity.package import package as make_package
2323
from singularity.utils import (
2424
get_installdir,
25+
remove_uri,
26+
read_file,
2527
update_dict,
2628
update_dict_sum
2729
)
@@ -175,6 +177,33 @@ def get_tags(container=None,image_package=None,sudopw=None,search_folders=None,d
175177
###################################################################################
176178

177179

180+
def get_files(container,S=None):
181+
'''get_files will return a list of files inside a container, sorted by name
182+
:param container: the container to use, either shub:// or docker:// or actual
183+
'''
184+
files = None
185+
tmpdir = tempfile.mkdtemp()
186+
tmpfile = "%s/files.txt" %tmpdir
187+
container_name = remove_uri(container)
188+
command = 'ls -LR >> %s 2>/dev/null' %(tmpfile)
189+
if S==None:
190+
S = Singularity(sudo=None)
191+
result = S.execute(container,command)
192+
if os.path.exists(tmpfile):
193+
os.system("sed -i '/^$/d' %s" %(tmpfile))
194+
os.system('sort %s -or %s' %(tmpfile,tmpfile))
195+
files = read_file(tmpfile)
196+
rootfs=None
197+
if len(files) > 0:
198+
files = [x for x in files if x.startswith('.')]
199+
files = [x.split(container_name)[1:] for x in files]
200+
files = [x for x in files if len(x) > 0]
201+
files = [x[0] for x in files]
202+
shutil.rmtree(tmpdir)
203+
return files
204+
205+
206+
178207
def file_counts(container=None,patterns=None,image_package=None,sudopw=None,diff=None):
179208
'''file counts will return a list of files that match one or more regular expressions.
180209
if no patterns is defined, a default of readme is used. All patterns and files are made

singularity/build/converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def get_mapping():
218218
workdir_command = {"section": "%post","fun": parse_workdir, "json": False }
219219
entry_command = {"section": "%runscript", "fun": parse_entry, "json": True }
220220

221-
return {"ADD": add_command,
221+
return {"ADD":add_command,
222222
"COPY":copy_command,
223223
"CMD":cmd_command,
224224
"ENTRYPOINT":entry_command,
@@ -228,7 +228,7 @@ def get_mapping():
228228
"WORKDIR":workdir_command,
229229
"MAINTAINER":comment_command,
230230
"VOLUME":comment_command,
231-
"PORT":port_command,
231+
"EXPOSE":port_command,
232232
"LABEL":label_command}
233233

234234

singularity/cli.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
class Singularity:
2929

30-
def __init__(self,sudo=True,sudopw=None,debug=False):
30+
31+
def __init__(self,sudo=False,sudopw=None,debug=False):
3132
'''upon init, store user password to not ask for it again'''
3233

3334
self.sudopw = sudopw
@@ -117,7 +118,7 @@ def execute(self,image_path,command,writable=False,contain=False):
117118
if self.debug == True:
118119
cmd = ["singularity",'--debug',"exec"]
119120
else:
120-
cmd = ["singularity","exec"]
121+
cmd = ["singularity",'--quiet',"exec"]
121122

122123
cmd = self.add_flags(cmd,writable=writable,contain=contain)
123124

@@ -194,6 +195,21 @@ def importcmd(self,image_path,input_source,import_type=None,command=None):
194195
return None
195196

196197

198+
def pull(self,image_path):
199+
'''pull will pull a singularity hub image
200+
:param image_path: full path to image
201+
'''
202+
if not image_path.startswit('shub://'):
203+
bot.logger.error("pull is only valid for the shub://uri, %s is invalid.",image_name)
204+
sys.exit(1)
205+
206+
if self.debug == True:
207+
cmd = ['singularity','--debug','pull',image_path]
208+
else:
209+
cmd = ['singularity','pull',image_path]
210+
return self.run_command(cmd)
211+
212+
197213

198214
def run(self,image_path,command,writable=False,contain=False):
199215
'''run will run a command inside the container, probably not intended for within python
@@ -213,6 +229,7 @@ def run(self,image_path,command,writable=False,contain=False):
213229
# Run the command
214230
return self.run_command(cmd,sudo=sudo)
215231

232+
216233
def start(self,image_path,writable=False,contain=False):
217234
'''start will start a container
218235
'''
@@ -276,7 +293,7 @@ def get_image(image,return_existed=False,sudopw=None,size=None,debug=False):
276293
cli = Singularity(debug=debug) # This command will ask the user for sudo
277294

278295
tmpdir = tempfile.mkdtemp()
279-
image_name = "%s.img" %image.replace("docker://","")
296+
image_name = "%s.img" %image.replace("docker://","").replace("/","-")
280297
bot.logger.info("Found docker image %s, creating and importing...",image_name)
281298
image_path = "%s/%s" %(tmpdir,image_name)
282299
cli.create(image_path,size=size)

singularity/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ def format_container_name(name,special_characters=None):
304304
return ''.join(e.lower() for e in name if e.isalnum() or e in special_characters)
305305

306306

307+
def remove_uri(container):
308+
'''remove_uri will remove docker:// or shub:// from the uri
309+
'''
310+
return container.replace('docker://','').replace('shub://','')
311+
312+
307313
def download_repo(repo_url,destination,commit=None):
308314
'''download_repo
309315
:param repo_url: the url of the repo to clone from

singularity/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.0.10"
1+
__version__ = "1.1.0"
22
AUTHOR = 'Vanessa Sochat'
33
AUTHOR_EMAIL = '[email protected]'
44
NAME = 'singularity'

0 commit comments

Comments
 (0)