Skip to content

Commit 45a642e

Browse files
committed
refacto: extract classes into dedicated module
1 parent dba437c commit 45a642e

File tree

2 files changed

+78
-121
lines changed

2 files changed

+78
-121
lines changed

cwl_utils/docker_extract.py

Lines changed: 1 addition & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,11 @@
11
#!/usr/bin/env python3
2-
from abc import ABC, abstractmethod
32
import argparse
4-
import logging
53
import os
6-
import subprocess
74
import sys
85

6+
from cwl_utils.image_puller import DockerImagePuller, SingularityImagePuller
97
import 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

8510
def 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-
15333
def extract_docker_requirements(process: cwl.Process):
15434
for req in extract_docker_reqs(process):
15535
if ':' not in req.dockerPull:

cwl_utils/image_puller.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from abc import ABC, abstractmethod
2+
import logging
3+
import os
4+
import subprocess
5+
6+
logging.basicConfig(level=logging.INFO)
7+
_LOGGER = logging.getLogger(__name__)
8+
9+
10+
class ImagePuller(ABC):
11+
12+
def __init__(self, req, save_directory):
13+
self.req = req
14+
self.save_directory = save_directory
15+
16+
@abstractmethod
17+
def get_image_name(self):
18+
pass
19+
20+
@abstractmethod
21+
def save_docker_image(self):
22+
pass
23+
24+
@staticmethod
25+
def _run_command_pull(cmd_pull):
26+
try:
27+
subprocess.run(cmd_pull, check=True, stdout=subprocess.PIPE,
28+
stderr=subprocess.STDOUT)
29+
except subprocess.CalledProcessError as err:
30+
if err.output:
31+
raise subprocess.SubprocessError(err.output)
32+
raise err
33+
34+
35+
class DockerImagePuller(ImagePuller):
36+
"""
37+
Pull docker image with Docker
38+
"""
39+
40+
def get_image_name(self):
41+
return ''.join(self.req.split('/')) + '.tar'
42+
43+
def generate_udocker_loading_command(self):
44+
return f'udocker load -i {self.get_image_name()}'
45+
46+
def save_docker_image(self):
47+
_LOGGER.info(f"Pulling {self.req} with Docker...")
48+
cmd_pull = ['docker', 'pull', self.req]
49+
ImagePuller._run_command_pull(cmd_pull)
50+
cmd_save = ['docker', 'save', '-o', os.path.join(self.save_directory,
51+
self.get_image_name()),
52+
self.req]
53+
subprocess.run(cmd_save, check=True)
54+
_LOGGER.info(f"Image successfully pulled: {self.save_directory}/{self.get_image_name()}")
55+
print(self.generate_udocker_loading_command())
56+
57+
58+
class SingularityImagePuller(ImagePuller):
59+
"""
60+
Pull docker image with Singularity
61+
"""
62+
CHARS_TO_REPLACE = ['/', ':']
63+
NEW_CHAR = '-'
64+
65+
def get_image_name(self):
66+
image_name = self.req
67+
for char in self.CHARS_TO_REPLACE:
68+
image_name = image_name.replace(char, self.NEW_CHAR)
69+
return f'{image_name}.img'
70+
71+
def save_docker_image(self):
72+
_LOGGER.info(f"Pulling {self.req} with Singularity...")
73+
cmd_pull = ['singularity', 'pull', os.path.join(self.save_directory,
74+
self.get_image_name()),
75+
f'docker:{self.req}']
76+
ImagePuller._run_command_pull(cmd_pull)
77+
_LOGGER.info(f"Image successfully pulled: {self.save_directory}/{self.get_image_name()}")

0 commit comments

Comments
 (0)