Exemplo n.º 1
0
def download_files(ctx, workflow, filenames, output_directory,
                   access_token):  # noqa: D301
    """Download workspace files.

    The `download` command allows to download workspace files. By default, the
    files specified in the workflow specification as outputs are downloaded.
    You can also specify the individual files you would like to download, see
    examples below. Note that downloading directories is not yet supported.

    Examples: \n
    \t $ reana-client download # download all output files \n
    \t $ reana-client download mydata.tmp outputs/myplot.png
    """
    from reana_client.api.client import (download_file,
                                         get_workflow_specification)

    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 'outputs' in reana_spec:
            filenames = reana_spec['outputs'].get('files') or []

    if workflow:
        for file_name in filenames:
            try:
                binary_file = download_file(workflow, file_name, access_token)

                logging.info(
                    '{0} binary file downloaded ... writing to {1}'.format(
                        file_name, output_directory))

                outputs_file_path = os.path.join(output_directory, file_name)
                if not os.path.exists(os.path.dirname(outputs_file_path)):
                    os.makedirs(os.path.dirname(outputs_file_path))

                with open(outputs_file_path, 'wb') as f:
                    f.write(binary_file)
                click.secho('File {0} downloaded to {1}.'.format(
                    file_name, output_directory),
                            fg='green')
            except OSError as e:
                logging.debug(traceback.format_exc())
                logging.debug(str(e))
                click.echo(click.style(
                    'File {0} could not be written.'.format(file_name),
                    fg='red'),
                           err=True)
            except Exception as e:
                logging.debug(traceback.format_exc())
                logging.debug(str(e))
                click.echo(click.style(
                    'File {0} could not be downloaded: {1}'.format(
                        file_name, e),
                    fg='red'),
                           err=True)
Exemplo 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)
Exemplo n.º 3
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)
Exemplo n.º 4
0
def download_files(ctx, workflow, filenames, output_directory,
                   access_token):  # noqa: D301
    """Download workspace files.

    The ``download`` command allows to download workspace files and directories.
    By default, the files specified in the workflow specification as outputs
    are downloaded. You can also specify the individual files you would like
    to download, see examples below.

    Examples: \n
    \t $ reana-client download # download all output files \n
    \t $ reana-client download mydata.tmp outputs/myplot.png
    """
    from reana_client.api.client import download_file, get_workflow_specification

    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 "outputs" in reana_spec:
            filenames = []
            filenames += reana_spec["outputs"].get("files", [])
            filenames += reana_spec["outputs"].get("directories", [])

    if workflow:
        for file_name in filenames:
            try:
                binary_file, file_name = download_file(workflow, file_name,
                                                       access_token)

                logging.info(
                    "{0} binary file downloaded ... writing to {1}".format(
                        file_name, output_directory))

                outputs_file_path = os.path.join(output_directory, file_name)
                if not os.path.exists(os.path.dirname(outputs_file_path)):
                    os.makedirs(os.path.dirname(outputs_file_path))

                with open(outputs_file_path, "wb") as f:
                    f.write(binary_file)
                display_message(
                    "File {0} downloaded to {1}.".format(
                        file_name, output_directory),
                    msg_type="success",
                )
            except OSError as e:
                logging.debug(traceback.format_exc())
                logging.debug(str(e))
                display_message(
                    "File {0} could not be written.".format(file_name),
                    msg_type="error",
                )
            except Exception as e:
                logging.debug(traceback.format_exc())
                logging.debug(str(e))
                display_message(
                    "File {0} could not be downloaded: {1}".format(
                        file_name, e),
                    msg_type="error",
                )