Skip to content

Commit e02861e

Browse files
committed
cleanup
1 parent 11e69c0 commit e02861e

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

cwltool/singularity.py

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,33 @@ def get_image(dockerRequirement, pull_image, dry_run=False):
3030
if match:
3131
dockerRequirement["dockerImageId"] = re.sub(pattern=r'([a-z]*://)', repl=r'',
3232
string=dockerRequirement["dockerPull"])
33-
dockerRequirement["dockerImageId"] = re.sub(pattern=r'[:/]', repl=r'-',
34-
string=dockerRequirement["dockerImageId"]) + ".img"
33+
dockerRequirement["dockerImageId"] = re.sub(
34+
pattern=r'[:/]', repl=r'-', string=dockerRequirement["dockerImageId"]) + ".img"
3535
else:
36-
dockerRequirement["dockerImageId"] = re.sub(pattern=r'[:/]', repl=r'-',
37-
string=dockerRequirement["dockerPull"]) + ".img"
36+
dockerRequirement["dockerImageId"] = re.sub(
37+
pattern=r'[:/]', repl=r'-', string=dockerRequirement["dockerPull"]) + ".img"
3838
dockerRequirement["dockerPull"] = "docker://" + dockerRequirement["dockerPull"]
3939

4040
# check if Singularity image is available in $SINGULARITY_CACHEDIR
41-
if "SINGULARITY_CACHEDIR" in os.environ \
42-
and os.path.isfile(os.path.join(os.environ["SINGULARITY_CACHEDIR"], dockerRequirement["dockerImageId"])):
43-
_logger.info("Using local copy of Singularity image found in $SINGULARITY_CACHEDIR")
44-
dockerRequirement["dockerImageId"] = os.path.join(os.environ["SINGULARITY_CACHEDIR"], dockerRequirement["dockerImageId"])
41+
if "SINGULARITY_CACHEDIR" in os.environ and os.path.isfile(
42+
os.path.join(os.environ["SINGULARITY_CACHEDIR"],
43+
dockerRequirement["dockerImageId"])):
44+
_logger.info("Using local copy of Singularity image found in "
45+
"$SINGULARITY_CACHEDIR")
46+
dockerRequirement["dockerImageId"] = os.path.join(
47+
os.environ["SINGULARITY_CACHEDIR"],
48+
dockerRequirement["dockerImageId"])
4549
found = True
4650

4751
# check if Singularity image is available in $SINGULARITY_PULLFOLDER
48-
elif "SINGULARITY_PULLFOLDER" in os.environ \
49-
and os.path.isfile(os.path.join(os.environ["SINGULARITY_PULLFOLDER"], dockerRequirement["dockerImageId"])):
50-
_logger.info("Using local copy of Singularity image found in $SINGULARITY_PULLFOLDER")
51-
dockerRequirement["dockerImageId"] = os.path.join(os.environ["SINGULARITY_PULLFOLDER"], dockerRequirement["dockerImageId"])
52+
elif "SINGULARITY_PULLFOLDER" in os.environ and os.path.isfile(
53+
os.path.join(os.environ["SINGULARITY_PULLFOLDER"],
54+
dockerRequirement["dockerImageId"])):
55+
_logger.info("Using local copy of Singularity image found in "
56+
"$SINGULARITY_PULLFOLDER")
57+
dockerRequirement["dockerImageId"] = os.path.join(
58+
os.environ["SINGULARITY_PULLFOLDER"],
59+
dockerRequirement["dockerImageId"])
5260
found = True
5361

5462
# check if Singularity image is available in current working directory
@@ -80,10 +88,10 @@ def get_from_requirements(self, r, req, pull_image, dry_run=False, force_pull=Fa
8088
errmsg = None
8189
try:
8290
subprocess.check_output(["singularity", "--version"])
83-
except subprocess.CalledProcessError as e:
84-
errmsg = "Cannot execute 'singularity --version' " + Text(e)
85-
except OSError as e:
86-
errmsg = "'singularity' executable not found: " + Text(e)
91+
except subprocess.CalledProcessError as err:
92+
errmsg = "Cannot execute 'singularity --version' {}".format(err)
93+
except OSError as err:
94+
errmsg = "'singularity' executable not found: {}".format(err)
8795

8896
if errmsg:
8997
if req:
@@ -95,7 +103,8 @@ def get_from_requirements(self, r, req, pull_image, dry_run=False, force_pull=Fa
95103
return os.path.abspath(r["dockerImageId"])
96104
else:
97105
if req:
98-
raise WorkflowException(u"Container image %s not found" % r["dockerImageId"])
106+
raise WorkflowException(u"Container image {} not "
107+
"found".format(r["dockerImageId"]))
99108

100109
return None
101110

@@ -104,7 +113,7 @@ def add_volumes(self, pathmapper, runtime, stage_output):
104113

105114
host_outdir = self.outdir
106115
container_outdir = self.builder.outdir
107-
for src, vol in pathmapper.items():
116+
for _, vol in pathmapper.items():
108117
if not vol.staged:
109118
continue
110119
if stage_output:
@@ -119,13 +128,15 @@ def add_volumes(self, pathmapper, runtime, stage_output):
119128
if vol.type in ("File", "Directory"):
120129
if not vol.resolved.startswith("_:"):
121130
runtime.append(u"--bind")
122-
runtime.append("%s:%s:ro" % (
123-
docker_windows_path_adjust(vol.resolved), docker_windows_path_adjust(containertgt)))
131+
runtime.append("{}:{}:ro".format(
132+
docker_windows_path_adjust(vol.resolved),
133+
docker_windows_path_adjust(containertgt)))
124134
elif vol.type == "WritableFile":
125135
if self.inplace_update:
126136
runtime.append(u"--bind")
127-
runtime.append("%s:%s:rw" % (
128-
docker_windows_path_adjust(vol.resolved), docker_windows_path_adjust(containertgt)))
137+
runtime.append("{}:{}:rw".format(
138+
docker_windows_path_adjust(vol.resolved),
139+
docker_windows_path_adjust(containertgt)))
129140
else:
130141
shutil.copy(vol.resolved, host_outdir_tgt)
131142
ensure_writable(host_outdir_tgt)
@@ -135,28 +146,32 @@ def add_volumes(self, pathmapper, runtime, stage_output):
135146
else:
136147
if self.inplace_update:
137148
runtime.append(u"--bind")
138-
runtime.append("%s:%s:rw" % (
139-
docker_windows_path_adjust(vol.resolved), docker_windows_path_adjust(containertgt)))
149+
runtime.append("{}:{}:rw".format(
150+
docker_windows_path_adjust(vol.resolved),
151+
docker_windows_path_adjust(containertgt)))
140152
else:
141153
shutil.copytree(vol.resolved, vol.target)
142154
elif vol.type == "CreateFile":
143155
createtmp = os.path.join(host_outdir, os.path.basename(vol.target))
144-
with open(createtmp, "wb") as f:
145-
f.write(vol.resolved.encode("utf-8"))
156+
with open(createtmp, "wb") as tmp:
157+
tmp.write(vol.resolved.encode("utf-8"))
146158
runtime.append(u"--bind")
147-
runtime.append(
148-
"%s:%s:ro" % (docker_windows_path_adjust(createtmp), docker_windows_path_adjust(vol.target)))
159+
runtime.append("{}:{}:ro".format(
160+
docker_windows_path_adjust(createtmp),
161+
docker_windows_path_adjust(vol.target)))
149162

150163
def create_runtime(self, env, rm_container=True, record_container_id=False, cidfile_dir="",
151164
cidfile_prefix="", **kwargs):
152165
# type: (MutableMapping[Text, Text], bool, bool, Text, Text, **Any) -> List
153166

154167
runtime = [u"singularity", u"--quiet", u"exec", u"--contain"]
155168
runtime.append(u"--bind")
156-
runtime.append(
157-
u"%s:%s:rw" % (docker_windows_path_adjust(os.path.realpath(self.outdir)), self.builder.outdir))
169+
runtime.append(u"{}:{}:rw".format(
170+
docker_windows_path_adjust(os.path.realpath(self.outdir)),
171+
self.builder.outdir))
158172
runtime.append(u"--bind")
159-
runtime.append(u"%s:%s:rw" % (docker_windows_path_adjust(os.path.realpath(self.tmpdir)), "/tmp"))
173+
runtime.append(u"{}:{}:rw".format(
174+
docker_windows_path_adjust(os.path.realpath(self.tmpdir)), "/tmp"))
160175

161176
self.add_volumes(self.pathmapper, runtime, stage_output=False)
162177
if self.generatemapper:
@@ -172,6 +187,6 @@ def create_runtime(self, env, rm_container=True, record_container_id=False, cidf
172187
env["SINGULARITYENV_TMPDIR"] = "/tmp"
173188
env["SINGULARITYENV_HOME"] = self.builder.outdir
174189

175-
for t, v in self.environment.items():
176-
env["SINGULARITYENV_" + t] = v
190+
for name, value in self.environment.items():
191+
env["SINGULARITYENV_{}".format(name)] = value
177192
return runtime

0 commit comments

Comments
 (0)