Skip to content

Commit 50a6485

Browse files
authored
Merge pull request #1424 from nsoranzo/fix_CondaTarget_package_name
Fix use of `package_name` attribute on `CondaTarget` objects
2 parents 0b5df63 + 30c60d3 commit 50a6485

File tree

4 files changed

+78
-29
lines changed

4 files changed

+78
-29
lines changed

planemo/commands/cmd_container_register.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Module describing the planemo ``container_register`` command."""
22
import os
33
import string
4+
from typing import List
45

56
import click
67
from galaxy.tool_util.deps.container_resolvers.mulled import targets_to_mulled_name
@@ -10,11 +11,15 @@
1011
)
1112
from galaxy.tool_util.deps.mulled.util import (
1213
conda_build_target_str,
14+
CondaTarget,
1315
v2_image_name,
1416
)
1517

1618
from planemo import options
17-
from planemo.cli import command_function
19+
from planemo.cli import (
20+
command_function,
21+
PlanemoCliContext,
22+
)
1823
from planemo.conda import (
1924
best_practice_search,
2025
build_conda_context,
@@ -78,7 +83,7 @@
7883
help="Force push branch for pull request in case it already exists.",
7984
)
8085
@command_function
81-
def cli(ctx, paths, **kwds):
86+
def cli(ctx: "PlanemoCliContext", paths, **kwds) -> None:
8287
"""Register multi-requirement containers as needed.
8388
8489
BioContainers publishes all Bioconda packages automatically as individual
@@ -154,7 +159,7 @@ def cli(ctx, paths, **kwds):
154159
class RegistryTarget:
155160
"""Abstraction around mulled container registry (both directory and Github repo)."""
156161

157-
def __init__(self, ctx, **kwds):
162+
def __init__(self, ctx: "PlanemoCliContext", **kwds):
158163
output_directory = kwds["output_directory"]
159164
pr_titles = []
160165
target_repository = None
@@ -179,15 +184,24 @@ def __init__(self, ctx, **kwds):
179184
self.output_directory = output_directory
180185
self.target_repository = target_repository
181186

182-
def has_pull_request_for(self, name):
187+
def has_pull_request_for(self, name: str) -> bool:
183188
has_pr = False
184189
if self.do_pull_request:
185190
if any([name in t for t in self.pr_titles]):
186191
has_pr = True
187192

188193
return has_pr
189194

190-
def handle_pull_request(self, ctx, name, target_filename, packages_str, tools_str, base_image, **kwds):
195+
def handle_pull_request(
196+
self,
197+
ctx: "PlanemoCliContext",
198+
name: str,
199+
target_filename: str,
200+
packages_str: str,
201+
tools_str: str,
202+
base_image: str,
203+
**kwds,
204+
) -> None:
191205
if self.do_pull_request:
192206
message = kwds["message"]
193207
message = string.Template(message).safe_substitute(
@@ -199,32 +213,40 @@ def handle_pull_request(self, ctx, name, target_filename, packages_str, tools_st
199213
}
200214
)
201215
branch_name = name.replace(":", "-")
216+
assert self.target_repository
202217
branch(ctx, self.target_repository, branch_name, from_branch="master")
203218
add(ctx, self.target_repository, target_filename)
204219
commit(ctx, self.target_repository, message=message)
205220
force_push = kwds.get("force_push", False)
206221
push(ctx, repo_path=self.target_repository, to=self.remote_name, branch=branch_name, force=force_push)
207222
pull_request(ctx, self.target_repository, message=message, repo=REGISTRY_REPOSITORY)
208223

209-
def write_targets(self, ctx, target_filename, mulled_targets, tag, base_image):
224+
def write_targets(
225+
self,
226+
ctx: "PlanemoCliContext",
227+
target_filename: str,
228+
mulled_targets: List[CondaTarget],
229+
tag: str,
230+
base_image: str,
231+
) -> None:
210232
with open(target_filename, "w") as f:
211233
targets = to_target_str(mulled_targets)
212234
f.write(string.Template(CONTENTS).safe_substitute(targets=targets, base_image=base_image, image_build=tag))
213235
ctx.log(f"Wrote requirements [{targets}] to file [{target_filename}]")
214236

215237

216-
def to_target_str(targets):
238+
def to_target_str(targets: List[CondaTarget]) -> str:
217239
target_strings = []
218240
for target in targets:
219241
if target.version:
220-
target_str = f"{target.package_name}={target.version}"
242+
target_str = f"{target.package}={target.version}"
221243
else:
222-
target_str = target.package_name
244+
target_str = target.package
223245
target_strings.append(target_str)
224246
return ",".join(target_strings)
225247

226248

227-
def open_prs(ctx):
249+
def open_prs(ctx: "PlanemoCliContext") -> List:
228250
repo = get_repository_object(ctx, REGISTRY_REPOSITORY)
229251
prs = [pr for pr in repo.get_pulls()]
230252
return prs

planemo/conda.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,24 @@
88
import os
99
import threading
1010
from copy import deepcopy
11-
from typing import TYPE_CHECKING
11+
from typing import (
12+
Any,
13+
Dict,
14+
FrozenSet,
15+
Iterable,
16+
List,
17+
Optional,
18+
Set,
19+
Tuple,
20+
TYPE_CHECKING,
21+
Union,
22+
)
1223

1324
from galaxy.tool_util.deps import conda_util
14-
from galaxy.tool_util.deps.conda_util import CondaContext
25+
from galaxy.tool_util.deps.conda_util import (
26+
CondaContext,
27+
CondaTarget,
28+
)
1529
from galaxy.util import unicodify
1630

1731
from planemo.exit_codes import (
@@ -43,7 +57,7 @@ def build_conda_context(ctx: "PlanemoCliContext", **kwds) -> CondaContext:
4357
condarc_override = kwds.get("condarc", condarc_override_default)
4458
use_local = kwds.get("conda_use_local", False)
4559
shell_exec = shell if use_planemo_shell else None
46-
conda_context = conda_util.CondaContext(
60+
conda_context = CondaContext(
4761
conda_prefix=conda_prefix,
4862
ensure_channels=ensure_channels,
4963
condarc_override=condarc_override,
@@ -100,21 +114,23 @@ def collect_conda_targets(ctx, paths, recursive=False, found_tool_callback=None)
100114

101115

102116
# Copied and modified from mulled stuff - need to syncronize these concepts.
103-
def target_str_to_targets(targets_raw):
104-
def parse_target(target_str):
117+
def target_str_to_targets(targets_raw: str) -> List[CondaTarget]:
118+
def parse_target(target_str: str) -> CondaTarget:
105119
if "=" in target_str:
106120
package_name, version = target_str.split("=", 1)
107121
else:
108122
package_name = target_str
109123
version = None
110-
target = conda_util.CondaTarget(package_name, version)
124+
target = CondaTarget(package_name, version)
111125
return target
112126

113127
targets = [parse_target(_) for _ in targets_raw.split(",")]
114128
return targets
115129

116130

117-
def collect_conda_target_lists(ctx, paths, recursive=False, found_tool_callback=None):
131+
def collect_conda_target_lists(
132+
ctx: "PlanemoCliContext", paths: Iterable[str], recursive: bool = False, found_tool_callback=None
133+
) -> List[FrozenSet[CondaTarget]]:
118134
"""Load CondaTarget lists from supplied artifact sources.
119135
120136
If a tool contains more than one requirement, the requirements will all
@@ -126,26 +142,28 @@ def collect_conda_target_lists(ctx, paths, recursive=False, found_tool_callback=
126142
return conda_target_lists
127143

128144

129-
def collect_conda_target_lists_and_tool_paths(ctx, paths, recursive=False, found_tool_callback=None):
145+
def collect_conda_target_lists_and_tool_paths(
146+
ctx: "PlanemoCliContext", paths: Iterable[str], recursive: bool = False, found_tool_callback=None
147+
) -> Tuple[List[FrozenSet[CondaTarget]], List[List[str]]]:
130148
"""Load CondaTarget lists from supplied artifact sources.
131149
132150
If a tool contains more than one requirement, the requirements will all
133151
appear together as one list element of the output list.
134152
"""
135-
conda_target_lists = set()
153+
conda_target_sets: Set[FrozenSet[CondaTarget]] = set()
136154
tool_paths = collections.defaultdict(list)
137155
for tool_path, tool_source in yield_tool_sources_on_paths(ctx, paths, recursive=recursive, yield_load_errors=False):
138156
try:
139157
if found_tool_callback:
140158
found_tool_callback(tool_path)
141159
targets = frozenset(tool_source_conda_targets(tool_source))
142-
conda_target_lists.add(targets)
160+
conda_target_sets.add(targets)
143161
tool_paths[targets].append(tool_path)
144162
except Exception as e:
145163
ctx.log(f"Error while collecting list of conda targets for '{tool_path}': {unicodify(e)}")
146164

147165
# Turn them into lists so the order matches before returning...
148-
conda_target_lists = list(conda_target_lists)
166+
conda_target_lists = list(conda_target_sets)
149167
conda_target_tool_paths = [tool_paths[c] for c in conda_target_lists]
150168

151169
return conda_target_lists, conda_target_tool_paths
@@ -160,7 +178,9 @@ def tool_source_conda_targets(tool_source):
160178
best_practice_search_first = threading.local()
161179

162180

163-
def best_practice_search(conda_target, conda_context=None, platform=None):
181+
def best_practice_search(
182+
conda_target: CondaTarget, conda_context: Optional[CondaContext] = None, platform: Optional[str] = None
183+
) -> Union[Tuple[None, None], Tuple[Dict[str, Any], bool]]:
164184
# Call it in offline mode after the first time.
165185
try:
166186
best_practice_search_first.previously_called
@@ -175,7 +195,7 @@ def best_practice_search(conda_target, conda_context=None, platform=None):
175195
conda_context = deepcopy(conda_context)
176196
conda_context.ensure_channels = BEST_PRACTICE_CHANNELS
177197
else:
178-
conda_context = conda_util.CondaContext(ensure_channels=BEST_PRACTICE_CHANNELS)
198+
conda_context = CondaContext(ensure_channels=BEST_PRACTICE_CHANNELS)
179199
return conda_util.best_search_result(
180200
conda_target,
181201
conda_context=conda_context,

planemo/mulled.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,30 @@
44
"""
55

66
import os
7+
from typing import (
8+
Iterable,
9+
List,
10+
)
711

812
from galaxy.tool_util.deps.mulled.mulled_build import (
913
DEFAULT_CHANNELS,
1014
ensure_installed,
1115
InvolucroContext,
1216
)
13-
from galaxy.tool_util.deps.mulled.util import build_target
17+
from galaxy.tool_util.deps.mulled.util import (
18+
build_target,
19+
CondaTarget,
20+
)
1421

1522
from planemo.conda import collect_conda_target_lists
1623
from planemo.io import shell
1724

1825

19-
def conda_to_mulled_targets(conda_targets):
26+
def conda_to_mulled_targets(conda_targets: Iterable[CondaTarget]) -> List[CondaTarget]:
2027
return list(map(lambda c: build_target(c.package, c.version), conda_targets))
2128

2229

23-
def collect_mulled_target_lists(ctx, paths, recursive=False):
30+
def collect_mulled_target_lists(ctx, paths: Iterable[str], recursive: bool = False) -> List[List[CondaTarget]]:
2431
return list(map(conda_to_mulled_targets, collect_conda_target_lists(ctx, paths, recursive=recursive)))
2532

2633

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ bioblend>=1.0.0
33
click!=8.0.2
44
cwltool>=1.0.20191225192155
55
ephemeris>=0.10.3
6-
galaxy-tool-util>=23.0,<24.0
7-
galaxy-util[template]>=23.0,<24.0
6+
galaxy-tool-util>=23.1,<24.0
7+
galaxy-util[template]>=23.1,<24.0
88
glob2
99
gxformat2>=0.14.0
1010
h5py
@@ -15,4 +15,4 @@ pathvalidate
1515
pyyaml
1616
virtualenv
1717
stdlib_list; python_version < '3.10'
18-
tabulate
18+
tabulate

0 commit comments

Comments
 (0)