Skip to content

Commit 988de1d

Browse files
committed
Add --from_workflow to shed_init.
Builds out a repository_dependencies.xml from the specified workflow. If needed, this also copies the workflow into the target repository. Closes #118.
1 parent cc1a447 commit 988de1d

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

planemo/commands/cmd_shed_init.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99

1010
@click.command("shed_init")
1111
@options.optional_project_arg()
12-
# TODO:
13-
# @click.option(
14-
# "--from_workflow",
15-
# type=click.Path(exists=True, file_okay=True, resolve_path=True),
16-
# help=('Attempt to generate repository dependencies from specified '
17-
# 'workflow.')
18-
# )
12+
@click.option(
13+
"--from_workflow",
14+
type=click.Path(exists=True, file_okay=True, resolve_path=True),
15+
help=('Attempt to generate repository dependencies from specified '
16+
'workflow.')
17+
)
1918
@click.option(
2019
"--description",
2120
help='Specify repository description for .shed.yml.'

planemo/shed.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fnmatch
22
import glob
33
import hashlib
4+
import json
45
import os
56
import tarfile
67
from tempfile import (
@@ -106,6 +107,26 @@ def shed_init(ctx, path, **kwds):
106107
return 1
107108

108109
_create_shed_config(ctx, shed_config_path, **kwds)
110+
repo_dependencies_path = os.path.join(path, "repository_dependencies.xml")
111+
from_workflow = kwds.get("from_workflow", None)
112+
113+
if from_workflow:
114+
workflow_name = os.path.basename(from_workflow)
115+
workflow_target = os.path.join(path, workflow_name)
116+
if not os.path.exists(workflow_target):
117+
shutil.copyfile(from_workflow, workflow_target)
118+
119+
if not can_write_to_path(repo_dependencies_path, **kwds):
120+
return 1
121+
122+
repo_pairs = _parse_repos_from_workflow(from_workflow)
123+
contents = '<repositories description="">'
124+
line_template = ' <repository owner="%s" name="%s" />'
125+
for (owner, name) in repo_pairs:
126+
contents += line_template % (owner, name)
127+
contents += "</repositories>"
128+
open(repo_dependencies_path, "w").write(contents)
129+
109130
return 0
110131

111132

@@ -369,6 +390,29 @@ def _create_shed_config(ctx, path, **kwds):
369390
yaml.dump(config, f)
370391

371392

393+
def _parse_repos_from_workflow(path):
394+
workflow_json = json.load(open(path, "r"))
395+
steps = workflow_json["steps"]
396+
tool_ids = set()
397+
for value in steps.values():
398+
step_type = value["type"]
399+
if step_type != "tool":
400+
continue
401+
tool_id = value["tool_id"]
402+
if "/repos/" in tool_id:
403+
tool_ids.add(tool_id)
404+
405+
repo_pairs = set()
406+
for tool_id in tool_ids:
407+
tool_repo_info = tool_id.split("/repos/", 1)[1]
408+
tool_repo_parts = tool_repo_info.split("/")
409+
owner = tool_repo_parts[0]
410+
name = tool_repo_parts[1]
411+
repo_pairs.add((owner, name))
412+
413+
return repo_pairs
414+
415+
372416
def _find_raw_repositories(path, **kwds):
373417
name = kwds.get("name", None)
374418
recursive = kwds.get("recursive", False)

tests/test_shed_init.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
import yaml
44

5-
from .test_utils import CliShedTestCase
5+
from .test_utils import (
6+
CliShedTestCase,
7+
TEST_REPOS_DIR,
8+
)
9+
10+
TEST_WORKFLOW_PATH = os.path.join(
11+
TEST_REPOS_DIR,
12+
"workflow_1",
13+
"blast_top_hit_species.ga"
14+
)
615

716

817
class ShedInitTestCase(CliShedTestCase):
@@ -50,3 +59,19 @@ def test_more_options(self):
5059
assert len(categories) == 3
5160
assert "SAM" in categories
5261
assert "Statistics" in categories
62+
63+
def test_from_workflow(self):
64+
with self._isolate() as f:
65+
init_command = ["shed_init", "--owner", "iuc"]
66+
init_command += ["--category", "Sequence Analysis"]
67+
init_command += ["--name", "blasthits"]
68+
init_command += ["--from_workflow", TEST_WORKFLOW_PATH]
69+
self._check_exit_code(init_command)
70+
71+
repo_deps_path = os.path.join(f, "repository_dependencies.xml")
72+
wf_path = os.path.join(f, "blast_top_hit_species.ga")
73+
assert os.path.exists(repo_deps_path)
74+
assert os.path.exists(wf_path)
75+
76+
# lint repository as a way of verifying repository_dependencies
77+
self._check_exit_code(["shed_lint"])

0 commit comments

Comments
 (0)