Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions planemo/commands/cmd_shed_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
type=tar_path,
default=None,
)
@click.option(
'--force_repository_creation',
help="If a repository cannot be found for the specified user/repo name "
"pair, then automatically create the repository in the toolshed.",
is_flag=True,
default=False
)
@click.option(
'-r', '--recursive',
is_flag=True,
Expand Down Expand Up @@ -103,16 +110,21 @@ def __handle_upload(ctx, path, **kwds):
message = kwds.get("message", None)
if message:
update_kwds["commit_message"] = message

repo_id = __find_repository(ctx, tsi, path, **kwds)
if repo_id is None and kwds["force_repository_creation"]:
repo_id = __create_repository(ctx, tsi, path, **kwds)
# failing to create the repo, give up
if repo_id is None:
return -1
# TODO: support updating repo information if it changes in the config file

try:
tsi.repositories.update_repository(repo_id, tar_path, **update_kwds)
except Exception as e:
exception_content = e.read()
try:
# Galaxy passes nice JSON messages as their errors, which bioblend
# blindly returns. Attempt to parse those.
upstream_error = json.loads(exception_content)
error(upstream_error['err_msg'])
except Exception as e2:
Expand All @@ -124,9 +136,7 @@ def __handle_upload(ctx, path, **kwds):


def __find_repository(ctx, tsi, path, **kwds):
"""Extracted into separate function to reduce complexity though it's not a
really sensible change since we now have to catch the error code separately
whenever we call this function elsewhere.
"""More advanced error handling for finding a repository by ID
"""
try:
repo_id = shed.find_repository_id(ctx, tsi, path, **kwds)
Expand All @@ -140,3 +150,20 @@ def __find_repository(ctx, tsi, path, **kwds):
# wrapped in try/except
error("Could not find repository in toolshed")
return None


def __create_repository(ctx, tsi, path, **kwds):
"""Wrapper for creating the endpoint if it doesn't exist
"""
try:
repo = shed.create_repository(ctx, tsi, path, **kwds)
return repo['id']
# Have to catch missing snyopsis/bioblend exceptions
except Exception as e:
# TODO: galaxyproject/bioblend#126
try:
upstream_error = json.loads(e.read())
error(upstream_error['err_msg'])
except Exception:
error(str(e))
return None
41 changes: 41 additions & 0 deletions planemo/shed.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,47 @@ def matches(r):
return repo_id


def create_repository(ctx, tsi, path, **kwds):
repo_config = shed_repo_config(path)
name = kwds.get("name", None) or repo_config.get("name", None)

description = repo_config.get("description", None)
long_description = repo_config.get("long_description", None)
type = repo_config.get("type", "unrestricted")
remote_repository_url = repo_config.get("remote_repository_url", None)
homepage_url = repo_config.get("homepage_url", None)
categories = repo_config.get("categories", [])
# Translate human readable category names into their associated IDs
category_list = tsi.repositories.get_categories()

category_ids = []
for cat in categories:
matching_cats = [x for x in category_list if x['name'] == cat]
if not matching_cats:
message = "Failed to find category %s" % cat
raise Exception(message)
category_ids.append(matching_cats[0]['id'])

if name is None:
name = os.path.basename(os.path.abspath(path))

# description is required, as is name.
if description is None:
message = "description required for automatic creation of repositories"
raise Exception(message)

repo = tsi.repositories.create_repository(
name=name,
synopsis=description,
description=long_description,
type=type,
remote_repository_url=remote_repository_url,
homepage_url=homepage_url,
category_ids=category_ids
)
return repo


def download_tarball(ctx, tsi, path, **kwds):
destination = kwds.get('destination', 'shed_download.tar.gz')
repo_id = find_repository_id(ctx, tsi, path, **kwds)
Expand Down