Example #1
0
def move_files(ctx, source, target, workflow, access_token):  # noqa: D301
    """Move files within workspace.

    The `mv` command allow to move the files within workspace.

    Examples:\n
    \t $ reana-client mv data/input.txt input/input.txt
    """
    from reana_client.api.client import get_workflow_status, list_files, mv_files

    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 workflow:
        try:
            current_status = get_workflow_status(workflow,
                                                 access_token).get("status")
            if current_status == "running":
                click.echo(
                    click.style(
                        "File(s) could not be moved for running "
                        "workflow",
                        fg="red"),
                    err=True,
                )
                sys.exit(1)
            files = list_files(workflow, access_token)
            current_files = [file["name"] for file in files]
            if not any(source in item for item in current_files):
                click.echo(
                    click.style(
                        "Source file(s) {} does not exist in "
                        "workspace {}".format(source, current_files),
                        fg="red",
                    ),
                    err=True,
                )
                sys.exit(1)
            response = mv_files(source, target, workflow, access_token)
            click.echo(
                click.style(
                    "{} was successfully moved to {}.".format(source, target),
                    fg="green",
                ))
        except Exception as e:
            logging.debug(traceback.format_exc())
            logging.debug(str(e))
            click.echo(click.style("Something went wrong. {}".format(e),
                                   fg="red"),
                       err=True)
Example #2
0
def move_files(ctx, source, target, workflow, access_token):  # noqa: D301
    r"""Move files within workspace.

    Examples:\n
    \t $ reana-client mv data/input.txt input/input.txt

    """
    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 workflow:
        try:
            current_status = get_workflow_status(workflow,
                                                 access_token).get('status')
            if current_status == 'running':
                click.echo(click.style(
                    'File(s) could not be moved for running '
                    'workflow',
                    fg='red'),
                           err=True)
                sys.exit(1)
            files = list_files(workflow, access_token)
            current_files = [file['name'] for file in files]
            if not any(source in item for item in current_files):
                click.echo(click.style('Source file(s) {} does not exist in '
                                       'workspace {}'.format(
                                           source, current_files),
                                       fg='red'),
                           err=True)
                sys.exit(1)
            response = mv_files(source, target, workflow, access_token)
            click.echo(
                click.style('{} was successfully moved to {}.'.format(
                    source, target),
                            fg='green'))
        except Exception as e:
            logging.debug(traceback.format_exc())
            logging.debug(str(e))
            click.echo(click.style('Something went wrong. {}'.format(e),
                                   fg='red'),
                       err=True)
def list_reana_workflow_files(workflow_id, workflow=None):
    """Show the files of a workflow."""
    rec_uuid = resolve_uuid(workflow_id)
    token = get_reana_token(rec_uuid)

    try:
        resp = list_files(workflow_id, token)
        _files = {
            'rec_uuid': rec_uuid,
            'workflow_id': workflow_id,
            'files': resp
        }
        return jsonify(_files)
    except Exception:
        return jsonify({
            'message':
            'File list from workflow {} could not be '
            'retrieved. Aborting listing.'.format(workflow_id)
        }), 400
Example #4
0
def get_files(ctx, workflow, _filter, output_format, access_token):
    """List workflow workspace files."""
    logging.debug('command: {}'.format(ctx.command_path.replace(" ", ".")))
    for p in ctx.params:
        logging.debug('{param}: {value}'.format(param=p, value=ctx.params[p]))

    try:
        _url = current_rs_api_client.swagger_spec.api_url
    except MissingAPIClientConfiguration as e:
        click.secho('REANA client is not connected to any REANA cluster.',
                    fg='red',
                    err=True)
        sys.exit(1)

    if not access_token:
        click.echo(click.style(ERROR_MESSAGES['missing_access_token'],
                               fg='red'),
                   err=True)
        sys.exit(1)
    if _filter:
        parsed_filters = parse_parameters(_filter)
    if workflow:
        logging.info('Workflow "{}" selected'.format(workflow))
        try:
            response = list_files(workflow, access_token)
            headers = ['name', 'size', 'last-modified']
            data = []
            for file_ in response:
                data.append(
                    list(
                        map(str, [
                            file_['name'], file_['size'],
                            file_['last-modified']
                        ])))
            tablib_data = tablib.Dataset()
            tablib_data.headers = headers
            for row in data:
                tablib_data.append(row)
            if _filter:
                tablib_data, filtered_headers = \
                    filter_data(parsed_filters, headers, tablib_data)
                if output_format:
                    click.echo(json.dumps(tablib_data))
                else:
                    tablib_data = [list(item.values()) for item in tablib_data]
                    click_table_printer(filtered_headers, filtered_headers,
                                        tablib_data)
            else:
                if output_format:
                    click.echo(tablib_data.export(output_format))
                else:
                    click_table_printer(headers, _filter, data)

        except Exception as e:
            logging.debug(traceback.format_exc())
            logging.debug(str(e))

            click.echo(click.style(
                'Something went wrong while retrieving file list'
                ' for workflow {0}:\n{1}'.format(workflow, str(e)),
                fg='red'),
                       err=True)
Example #5
0
def get_files(ctx, workflow, _filter, output_format,
              access_token):  # noqa: D301
    """List workspace files.

    The `ls` command lists workspace files of a workflow specified by the
    environment variable REANA_WORKON or provided as a command-line flag
    `--workflow` or `-w`.

    Examples: \n
    \t $ reana-client ls --workflow myanalysis.42
    """
    import tablib
    from reana_client.api.client import current_rs_api_client, list_files

    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 _filter:
        parsed_filters = parse_parameters(_filter)
    if workflow:
        logging.info('Workflow "{}" selected'.format(workflow))
        try:
            response = list_files(workflow, access_token)
            headers = ['name', 'size', 'last-modified']
            data = []
            file_path = get_path_from_operation_id(
                current_rs_api_client.swagger_spec.spec_dict['paths'],
                'download_file')
            urls = []
            for file_ in response:
                if not file_['name'].startswith(FILES_BLACKLIST):
                    data.append(
                        list(
                            map(str, [
                                file_['name'], file_['size'],
                                file_['last-modified']
                            ])))
                    urls.append(ctx.obj.reana_server_url + file_path.format(
                        workflow_id_or_name=workflow, file_name=file_['name']))
            tablib_data = tablib.Dataset()
            tablib_data.headers = headers
            for row in data:
                tablib_data.append(row)
            if output_format == URL:
                click.echo('\n'.join(urls))
            elif _filter:
                tablib_data, filtered_headers = \
                    filter_data(parsed_filters, headers, tablib_data)
                if output_format == JSON:
                    click.echo(json.dumps(tablib_data))
                else:
                    tablib_data = [list(item.values()) for item in tablib_data]
                    click_table_printer(filtered_headers, filtered_headers,
                                        tablib_data)
            else:
                if output_format == JSON:
                    click.echo(tablib_data.export(output_format))
                else:
                    click_table_printer(headers, _filter, data)

        except Exception as e:
            logging.debug(traceback.format_exc())
            logging.debug(str(e))

            click.echo(click.style(
                'Something went wrong while retrieving file list'
                ' for workflow {0}:\n{1}'.format(workflow, str(e)),
                fg='red'),
                       err=True)
Example #6
0
def get_files(
    ctx,
    workflow,
    _format,
    filters,
    output_format,
    filename,
    access_token,
    page,
    size,
    human_readable_or_raw,
):  # noqa: D301
    """List workspace files.

    The ``ls`` command lists workspace files of a workflow specified by the
    environment variable REANA_WORKON or provided as a command-line flag
    ``--workflow`` or ``-w``. The SOURCE argument is optional and specifies a
    pattern matching files and directories.

    Examples: \n
    \t $ reana-client ls --workflow myanalysis.42 \n
    \t $ reana-client ls --workflow myanalysis.42 --human-readable \n
    \t $ reana-client ls --workflow myanalysis.42 'data/*root*' \n
    \t $ reana-client ls --workflow myanalysis.42 --filter name=hello
    """  # noqa: W605
    import tablib
    from reana_client.api.client import current_rs_api_client, list_files

    logging.debug("command: {}".format(ctx.command_path.replace(" ", ".")))
    for p in ctx.params:
        logging.debug("{param}: {value}".format(param=p, value=ctx.params[p]))

    search_filter = None
    headers = ["name", "size", "last-modified"]
    if filters:
        _, search_filter = parse_filter_parameters(filters, headers)
    if _format:
        parsed_format_filters = parse_format_parameters(_format)
    if workflow:
        logging.info('Workflow "{}" selected'.format(workflow))
        try:
            response = list_files(workflow, access_token, filename, page, size,
                                  search_filter)
            data = []
            file_path = get_path_from_operation_id(
                current_rs_api_client.swagger_spec.spec_dict["paths"],
                "download_file")
            urls = []
            for file_ in response:
                if not file_["name"].startswith(FILES_BLACKLIST):
                    data.append(
                        list(
                            map(
                                str,
                                [
                                    file_["name"],
                                    file_["size"][human_readable_or_raw],
                                    file_["last-modified"],
                                ],
                            )))
                    urls.append(ctx.obj.reana_server_url + file_path.format(
                        workflow_id_or_name=workflow, file_name=file_["name"]))
            tablib_data = tablib.Dataset()
            tablib_data.headers = headers
            for row in data:
                tablib_data.append(row)
            if output_format == URL:
                display_message("\n".join(urls))
            elif _format:
                tablib_data, filtered_headers = format_data(
                    parsed_format_filters, headers, tablib_data)
                if output_format == JSON:
                    display_message(json.dumps(tablib_data))
                else:
                    tablib_data = [list(item.values()) for item in tablib_data]
                    click_table_printer(filtered_headers, filtered_headers,
                                        tablib_data)
            else:
                if output_format == JSON:
                    display_message(tablib_data.export(output_format))
                else:
                    click_table_printer(headers, _format, data)

        except Exception as e:
            logging.debug(traceback.format_exc())
            logging.debug(str(e))

            display_message(
                "Something went wrong while retrieving file list"
                " for workflow {0}:\n{1}".format(workflow, str(e)),
                msg_type="error",
            )