11#!/usr/bin/env python3
2- from abc import ABC , abstractmethod
32import argparse
4- import logging
53import os
6- import subprocess
74import sys
85
6+ from cwl_utils .image_puller import DockerImagePuller , SingularityImagePuller
97import cwl_utils .parser_v1_0 as cwl
108
11- logging .basicConfig (level = logging .INFO )
12- _LOGGER = logging .getLogger (__name__ )
13-
14-
15- class ImagePuller (ABC ):
16-
17- def __init__ (self , req , save_directory ):
18- self .req = req
19- self .save_directory = save_directory
20-
21- @abstractmethod
22- def get_image_name (self ):
23- pass
24-
25- @abstractmethod
26- def save_docker_image (self ):
27- pass
28-
29- @staticmethod
30- def _run_command_pull (cmd_pull ):
31- try :
32- subprocess .run (cmd_pull , check = True , stdout = subprocess .PIPE ,
33- stderr = subprocess .STDOUT )
34- except subprocess .CalledProcessError as err :
35- if err .output :
36- raise subprocess .SubprocessError (err .output )
37- raise err
38-
39-
40- class DockerImagePuller (ImagePuller ):
41- """
42- Pull docker image with Docker
43- """
44-
45- def get_image_name (self ):
46- return '' .join (self .req .split ('/' )) + '.tar'
47-
48- def generate_udocker_loading_command (self ):
49- return f'udocker load -i { self .get_image_name ()} '
50-
51- def save_docker_image (self ):
52- _LOGGER .info (f"Pulling { self .req } with Docker..." )
53- cmd_pull = ['docker' , 'pull' , self .req ]
54- ImagePuller ._run_command_pull (cmd_pull )
55- cmd_save = ['docker' , 'save' , '-o' , os .path .join (self .save_directory ,
56- self .get_image_name ()),
57- self .req ]
58- subprocess .run (cmd_save , check = True )
59- _LOGGER .info (f"Image successfully pulled: { self .save_directory } /{ self .get_image_name ()} " )
60- print (self .generate_udocker_loading_command ())
61-
62-
63- class SingularityImagePuller (ImagePuller ):
64- """
65- Pull docker image with Singularity
66- """
67- CHARS_TO_REPLACE = ['/' , ':' ]
68- NEW_CHAR = '-'
69-
70- def get_image_name (self ):
71- image_name = self .req
72- for char in self .CHARS_TO_REPLACE :
73- image_name = image_name .replace (char , self .NEW_CHAR )
74- return f'{ image_name } .img'
75-
76- def save_docker_image (self ):
77- _LOGGER .info (f"Pulling { self .req } with Singularity..." )
78- cmd_pull = ['singularity' , 'pull' , os .path .join (self .save_directory ,
79- self .get_image_name ()),
80- f'docker:{ self .req } ' ]
81- ImagePuller ._run_command_pull (cmd_pull )
82- _LOGGER .info (f"Image successfully pulled: { self .save_directory } /{ self .get_image_name ()} " )
83-
849
8510def parse_args ():
8611 parser = argparse .ArgumentParser (
@@ -100,56 +25,11 @@ def main():
10025 for req in set (traverse (top )):
10126 if args .singularity :
10227 image_puller = SingularityImagePuller (req , args .dir )
103- # image_name = get_image_name_singularity(req)
104- # save_docker_image_singularity(req, image_name, args.dir)
10528 else :
10629 image_puller = DockerImagePuller (req , args .dir )
107- # image_name = get_image_name(req)
108- # save_docker_image(req, image_name, args.dir)
109- # print(load_docker_image(image_name))
11030 image_puller .save_docker_image ()
11131
11232
113- # def get_image_name(req):
114- # return ''.join(req.split('/')) + '.tar'
115- #
116- #
117- # def get_image_name_singularity(req):
118- # CHARS_TO_REPLACE = ['/', ':']
119- # NEW_CHAR = '-'
120- # image_name = req
121- # for char in CHARS_TO_REPLACE:
122- # image_name = image_name.replace(char, NEW_CHAR)
123- # return f'{image_name}.img'
124- #
125- #
126- # def load_docker_image(image_name):
127- # return f'udocker load -i {image_name}'
128-
129-
130- # def _run_command_pull(cmd_pull):
131- # try:
132- # subprocess.run(cmd_pull, check=True, stdout=subprocess.PIPE,
133- # stderr=subprocess.STDOUT)
134- # except subprocess.CalledProcessError as err:
135- # if err.output:
136- # raise subprocess.SubprocessError(err.output)
137- # raise err
138-
139-
140- # def save_docker_image(req, image_name, image_dir):
141- # cmd_pull = ['docker', 'pull', req]
142- # _run_command_pull(cmd_pull)
143- # cmd_save = ['docker', 'save', '-o', os.path.join(image_dir, image_name),
144- # req]
145- # subprocess.run(cmd_save, check=True)
146- #
147- #
148- # def save_docker_image_singularity(req, image_name, image_dir):
149- # cmd_pull = ['singularity', 'pull', os.path.join(image_dir, image_name), f'docker:{req}']
150- # _run_command_pull(cmd_pull)
151-
152-
15333def extract_docker_requirements (process : cwl .Process ):
15434 for req in extract_docker_reqs (process ):
15535 if ':' not in req .dockerPull :
0 commit comments