File: utils.py

package info (click to toggle)
datalad-container 1.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 604 kB
  • sloc: python: 3,940; makefile: 188; sh: 43
file content (46 lines) | stat: -rw-r--r-- 1,292 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import os.path as op
import sys

from datalad.api import containers_add
from datalad.interface.common_cfg import dirs as appdirs
from datalad.tests.utils_pytest import SkipTest
from datalad.utils import chpwd


def add_pyscript_image(ds, container_name, file_name):
    """Set up simple Python script as image.

    Parameters
    ----------
    ds : Dataset
    container_name : str
        Add container with this name.
    file_name : str
        Write script to this file and use it as the image.
    """
    ds_file = (ds.pathobj / file_name)
    ds_file.write_text("import sys\nprint(sys.argv)\n")
    ds.save(ds_file, message="Add dummy container")
    containers_add(container_name, image=str(ds_file),
                   call_fmt=sys.executable + " {img} {cmd}",
                   dataset=ds)


def get_singularity_image():
    imgname = 'datalad_container_singularity_testimg.simg'
    targetpath = op.join(
        appdirs.user_cache_dir,
        imgname)
    if op.exists(targetpath):
        return targetpath

    with chpwd(appdirs.user_cache_dir):
        os.system(
            'singularity pull --name "{}" shub://datalad/datalad-container:testhelper'.format(
                imgname))

    if op.exists(targetpath):
        return targetpath

    raise SkipTest