Ejemplo n.º 1
0
def list_(ctx, number):
    """Get a list of submitted analyses.
    \f

    :param ctx: Click context holding group-level parameters
    :param number: The number of analysis jobs to display
    :return:
    """

    result = AnalysisListResponse(analyses=[], total=0)
    try:
        offset = 0
        while True:
            resp = ctx["client"].analysis_list(offset=offset)
            if not resp.analyses:
                break
            offset += len(resp.analyses)
            result.analyses.extend(resp.analyses)
            if len(result.analyses) >= number:
                break

        # trim result to desired result number
        LOGGER.debug(resp.total)
        result = AnalysisListResponse(analyses=result[:number],
                                      total=resp.total)
    except MythXAPIError:
        raise click.UsageError(
            ("This functionality is only available to registered users. "
             "Head over to https://mythx.io/ and register a free account to "
             "list your past analyses. Alternatively, you can look up the "
             "status of a specific job by calling 'mythx status <uuid>'."))
    click.echo(FORMAT_RESOLVER[ctx["fmt"]].format_analysis_list(result))
def analysis_list(ctx, number: int) -> None:
    """Get a list of submitted analyses.

    \f

    :param ctx: Click context holding group-level parameters
    :param number: The number of analysis jobs to display
    :return:
    """

    result = AnalysisListResponse(analyses=[], total=0)
    offset = 0
    while True:
        LOGGER.debug(f"Fetching analyses with offset {offset}")
        resp = ctx["client"].analysis_list(offset=offset)
        if not resp.analyses:
            LOGGER.debug("Received empty analysis list response")
            break
        offset += len(resp.analyses)
        result.analyses.extend(resp.analyses)
        if len(result.analyses) >= number:
            LOGGER.debug(f"Received {len(result.analyses)} analyses")
            break

    # trim result to desired result number
    LOGGER.debug(f"Got {len(result.analyses)} analyses, trimming to {number}")
    result = AnalysisListResponse(analyses=result[:number], total=resp.total)
    write_or_print(FORMAT_RESOLVER[ctx["fmt"]].format_analysis_list(result))
Ejemplo n.º 3
0
def analysis_list(ctx, number):
    """Get a list of submitted analyses.
    \f

    :param ctx: Click context holding group-level parameters
    :param number: The number of analysis jobs to display
    :return:
    """

    result = AnalysisListResponse(analyses=[], total=0)
    offset = 0
    while True:
        resp = ctx["client"].analysis_list(offset=offset)
        if not resp.analyses:
            break
        offset += len(resp.analyses)
        result.analyses.extend(resp.analyses)
        if len(result.analyses) >= number:
            break

    # trim result to desired result number
    LOGGER.debug(resp.total)
    result = AnalysisListResponse(analyses=result[:number], total=resp.total)
    write_or_print(FORMAT_RESOLVER[ctx["fmt"]].format_analysis_list(result))
Ejemplo n.º 4
0
    def format_analysis_list(resp: AnalysisListResponse) -> str:
        """Format an analysis list response as compressed JSON."""

        return resp.to_json()
def test_serde(response):
    resp = AnalysisListResponse(**response)
    assert resp.dict(by_alias=True) == response
Ejemplo n.º 6
0
ANALYSIS_LIST_RESPONSE_OBJECT = AnalysisListResponse(
    analyses=[
        Analysis(
            uuid=UUID_1,
            api_version=API_VERSION_1,
            maru_version=MARU_VERSION_1,
            mythril_version=MYTHRIL_VERSION_1,
            harvey_version=HARVEY_VERSION_1,
            queue_time=QUEUE_TIME_1,
            run_time=RUN_TIME_1,
            status=STATUS_1,
            submitted_at=SUBMITTED_AT_1,
            submitted_by=SUBMITTED_BY_1,
            client_tool_name=CLIENT_TOOL_NAME_1,
        ),
        Analysis(
            uuid=UUID_2,
            api_version=API_VERSION_2,
            maru_version=MARU_VERSION_2,
            mythril_version=MYTHRIL_VERSION_2,
            harvey_version=HARVEY_VERSION_2,
            queue_time=QUEUE_TIME_2,
            run_time=RUN_TIME_2,
            status=STATUS_2,
            submitted_at=SUBMITTED_AT_2,
            submitted_by=SUBMITTED_BY_2,
            client_tool_name=CLIENT_TOOL_NAME_2,
        ),
    ],
    total=2,
)
import json
from copy import deepcopy

import pytest

from mythx_models.response import AnalysisListResponse
from mythx_models.response.analysis import Analysis
from mythx_models.util import serialize_api_timestamp

from .common import get_test_case

JSON_DATA, DICT_DATA = get_test_case("testdata/analysis-list-response.json")
OBJ_DATA = AnalysisListResponse.from_json(JSON_DATA)


def assert_analysis_data(expected, analysis: Analysis):
    assert expected["apiVersion"] == analysis.api_version
    assert expected["maruVersion"] == analysis.maru_version
    assert expected["mythrilVersion"] == analysis.mythril_version
    assert expected["harveyVersion"] == analysis.harvey_version
    assert expected["queueTime"] == analysis.queue_time
    assert expected["runTime"] == analysis.run_time
    assert expected["status"] == analysis.status
    assert expected["submittedAt"] == serialize_api_timestamp(
        analysis.submitted_at)
    assert expected["submittedBy"] == analysis.submitted_by
    assert expected["uuid"] == analysis.uuid
    assert expected["groupName"] == analysis.group_name
    assert expected["groupId"] == analysis.group_id
    assert expected["analysisMode"] == analysis.analysis_mode