diff --git a/planemo/commands/cmd_shed_upload.py b/planemo/commands/cmd_shed_upload.py index c76c31616..fe672f05e 100644 --- a/planemo/commands/cmd_shed_upload.py +++ b/planemo/commands/cmd_shed_upload.py @@ -9,6 +9,8 @@ from planemo.io import info from planemo.io import shell +import fnmatch +import os tar_path = click.Path( exists=True, @@ -56,8 +58,37 @@ type=tar_path, default=None, ) +@click.option( + '-r', '--recursive', + is_flag=True, + help="Recursively search for repositories to publish to a tool shed", +) @pass_context def cli(ctx, path, **kwds): + """Handle possible recursion through paths for uploading files to a toolshed + """ + if kwds['recursive']: + if kwds['name'] is not None: + error("--name is incompatible with --recursive") + return -1 + if kwds['tar'] is not None: + error("--tar is incompatible with --recursive") + return -1 + + ret_codes = [] + for base_path, dirnames, filenames in os.walk(path): + for filename in fnmatch.filter(filenames, '.shed.yml'): + ret_codes.append( + __handle_upload(ctx, base_path, **kwds) + ) + # "Good" returns are Nones, everything else is a -1 and should be + # passed upwards. + return None if all(x is None for x in ret_codes) else -1 + else: + return __handle_upload(ctx, path, **kwds) + + +def __handle_upload(ctx, path, **kwds): """Upload a tool directory as a tarball to a tool shed. """ tar_path = kwds.get("tar", None) @@ -71,10 +102,22 @@ def cli(ctx, path, **kwds): message = kwds.get("message", None) if message: update_kwds["commit_message"] = message - repo_id = shed.find_repository_id(ctx, tsi, path, **kwds) + try: + repo_id = shed.find_repository_id(ctx, tsi, path, **kwds) + except Exception as e: + error("Could not update %s" % path) + try: + error(e.read()) + except: + # I've seen a case where the error couldn't be read, so now + # wrapped in try/except + pass + return -1 + try: tsi.repositories.update_repository(repo_id, tar_path, **update_kwds) except Exception as e: + error("Could not update %s" % path) error(e.read()) return -1 - info("Repository updated successfully.") + info("Repository %s updated successfully." % path)