Esempio n. 1
0
def _upload_workflow(workflow: str, file: str) -> None:
    reana_specification = _load_reana_specification(file)

    filenames = []

    if "inputs" in reana_specification:
        filenames += [
            os.path.join(CURRENT_WORKING_DIRECTORY, f)
            for f in reana_specification["inputs"].get("files") or []
        ]
        filenames += [
            os.path.join(CURRENT_WORKING_DIRECTORY, d)
            for d in reana_specification["inputs"].get("directories") or []
        ]

    for filename in filenames:
        upload_to_server(workflow, filename, REANA_ACCESS_TOKEN)
Esempio n. 2
0
def upload_files(ctx, workflow, filenames, access_token):  # noqa: D301
    """Upload files and directories to workspace.

    The `upload` command allows to upload workflow input files and
    directories. The SOURCES argument can be repeated and specifies which files
    and directories are to be uploaded, see examples below. The default
    behaviour is to upload all input files and directories specified in the
    reana.yaml file.

    Examples: \n
    \t $ reana-client upload -w myanalysis.42 \n
    \t $ reana-client upload -w myanalysis.42 code/mycode.py
    """
    from reana_client.api.client import (get_workflow_specification,
                                         upload_to_server)

    logging.debug('command: {}'.format(ctx.command_path.replace(" ", ".")))
    for p in ctx.params:
        logging.debug('{param}: {value}'.format(param=p, value=ctx.params[p]))
    if not filenames:
        reana_spec = get_workflow_specification(workflow,
                                                access_token)['specification']
        if 'inputs' in reana_spec:
            filenames = []
            filenames += [
                os.path.join(os.getcwd(), f)
                for f in reana_spec['inputs'].get('files') or []
            ]
            filenames += [
                os.path.join(os.getcwd(), d)
                for d in reana_spec['inputs'].get('directories') or []
            ]

    if workflow:
        if filenames:
            for filename in filenames:
                try:
                    response = upload_to_server(workflow, filename,
                                                access_token)
                    for file_ in response:
                        if file_.startswith('symlink:'):
                            click.echo(
                                click.style('Symlink resolved to {}. Uploaded'
                                            ' hard copy.'.format(
                                                file_[len('symlink:'):]),
                                            fg='green'))
                        else:
                            click.echo(
                                click.style('File {} was successfully '
                                            'uploaded.'.format(file_),
                                            fg='green'))
                except FileNotFoundError as e:
                    logging.debug(traceback.format_exc())
                    logging.debug(str(e))
                    click.echo(click.style(
                        'File {0} could not be uploaded: {0} does not'
                        ' exist.'.format(filename),
                        fg='red'),
                               err=True)
                    if 'invoked_by_subcommand' in ctx.parent.__dict__:
                        sys.exit(1)
                except FileUploadError as e:
                    logging.debug(traceback.format_exc())
                    logging.debug(str(e))
                    click.echo(click.style(
                        'Something went wrong while uploading {0}.\n{1}'.
                        format(filename, str(e)),
                        fg='red'),
                               err=True)
                    if 'invoked_by_subcommand' in ctx.parent.__dict__:
                        sys.exit(1)
                except Exception as e:
                    logging.debug(traceback.format_exc())
                    logging.debug(str(e))
                    click.echo(click.style(
                        'Something went wrong while uploading {}'.format(
                            filename),
                        fg='red'),
                               err=True)
                    if 'invoked_by_subcommand' in ctx.parent.__dict__:
                        sys.exit(1)
Esempio n. 3
0
def upload_files(ctx, workflow, filenames, access_token):
    """Upload files and directories to workflow workspace."""
    logging.debug('command: {}'.format(ctx.command_path.replace(" ", ".")))
    for p in ctx.params:
        logging.debug('{param}: {value}'.format(param=p, value=ctx.params[p]))

    if not access_token:
        click.echo(click.style(ERROR_MESSAGES['missing_access_token'],
                               fg='red'),
                   err=True)
        sys.exit(1)

    if not filenames:
        reana_spec = load_reana_spec(
            os.path.join(get_workflow_root(), 'reana.yaml'), False)
        if 'inputs' in reana_spec:
            filenames = []
            filenames += [
                os.path.join(get_workflow_root(), f)
                for f in reana_spec['inputs'].get('files') or []
            ]
            filenames += [
                os.path.join(get_workflow_root(), d)
                for d in reana_spec['inputs'].get('directories') or []
            ]

    if workflow:
        for filename in filenames:
            try:
                response = upload_to_server(workflow, filename, access_token)
                for file_ in response:
                    if file_.startswith('symlink:'):
                        click.echo(
                            click.style('Symlink resolved to {}. Uploaded'
                                        ' hard copy.'.format(
                                            file_[len('symlink:'):]),
                                        fg='green'))
                    else:
                        click.echo(
                            click.style(
                                'File {} was successfully uploaded.'.format(
                                    file_),
                                fg='green'))
            except FileNotFoundError as e:
                logging.debug(traceback.format_exc())
                logging.debug(str(e))
                click.echo(click.style(
                    'File {0} could not be uploaded: {0} does not exist.'.
                    format(filename),
                    fg='red'),
                           err=True)
                if 'invoked_by_subcommand' in ctx.parent.__dict__:
                    sys.exit(1)
            except FileUploadError as e:
                logging.debug(traceback.format_exc())
                logging.debug(str(e))
                click.echo(click.style(
                    'Something went wrong while uploading {0}.\n{1}'.format(
                        filename, str(e)),
                    fg='red'),
                           err=True)
                if 'invoked_by_subcommand' in ctx.parent.__dict__:
                    sys.exit(1)
            except Exception as e:
                logging.debug(traceback.format_exc())
                logging.debug(str(e))
                click.echo(click.style(
                    'Something went wrong while uploading {}'.format(filename),
                    fg='red'),
                           err=True)
                if 'invoked_by_subcommand' in ctx.parent.__dict__:
                    sys.exit(1)
Esempio n. 4
0
def upload_files(ctx, workflow, filenames, access_token):  # noqa: D301
    """Upload files and directories to workspace.

    The ``upload`` command allows to upload workflow input files and
    directories. The SOURCES argument can be repeated and specifies which files
    and directories are to be uploaded, see examples below. The default
    behaviour is to upload all input files and directories specified in the
    reana.yaml file.

    Examples: \n
    \t $ reana-client upload -w myanalysis.42 \n
    \t $ reana-client upload -w myanalysis.42 code/mycode.py
    """
    from reana_client.api.client import get_workflow_specification, upload_to_server

    logging.debug("command: {}".format(ctx.command_path.replace(" ", ".")))
    for p in ctx.params:
        logging.debug("{param}: {value}".format(param=p, value=ctx.params[p]))
    if not filenames:
        reana_spec = get_workflow_specification(workflow,
                                                access_token)["specification"]
        if "inputs" in reana_spec:
            filenames = []
            filenames += [
                os.path.join(os.getcwd(), f)
                for f in reana_spec["inputs"].get("files") or []
            ]
            filenames += [
                os.path.join(os.getcwd(), d)
                for d in reana_spec["inputs"].get("directories") or []
            ]

    if workflow:
        if filenames:
            for filename in filenames:
                try:
                    response = upload_to_server(workflow, filename,
                                                access_token)
                    for file_ in response:
                        if file_.startswith("symlink:"):
                            display_message(
                                "Symlink resolved to {}. "
                                "Uploaded hard copy.".format(
                                    file_[len("symlink:"):]),
                                msg_type="success",
                            )
                        else:
                            display_message(
                                "File {} was successfully uploaded.".format(
                                    file_),
                                msg_type="success",
                            )
                except FileNotFoundError as e:
                    logging.debug(traceback.format_exc())
                    logging.debug(str(e))
                    display_message(
                        "File {0} could not be uploaded: "
                        "{0} does not exist.".format(filename),
                        msg_type="error",
                    )
                    if "invoked_by_subcommand" in ctx.parent.__dict__:
                        sys.exit(1)
                except FileUploadError as e:
                    logging.debug(traceback.format_exc())
                    logging.debug(str(e))
                    display_message(
                        "Something went wrong while uploading {0}.\n"
                        "{1}".format(filename, str(e)),
                        msg_type="error",
                    )
                    if "invoked_by_subcommand" in ctx.parent.__dict__:
                        sys.exit(1)
                except Exception as e:
                    logging.debug(traceback.format_exc())
                    logging.debug(str(e))
                    display_message(
                        "Something went wrong while uploading {}: \n"
                        "{}".format(filename, str(e)),
                        msg_type="error",
                    )
                    if "invoked_by_subcommand" in ctx.parent.__dict__:
                        sys.exit(1)