コード例 #1
0
def action_query_result_db(storage, checks = {}, **kwargs):
    '''
    Get results from the database.

    Parameters
    ----------
    storage : ResultsStorageInterface
        The store to use.
    checks : dict, optional (default is {})
        Dictionary describing the checks to perform on some values.
        Will be passed to :py:method:`.MongoUtil.build_checks_filter` (as keyword arguments)
        checks_non_empty_list : iterable<str>, optional (default is ())
            Check the keys against a non empty list.
        checks_empty_list : iterable<str>, optional (default is ())
            Check the keys against an empty list.
        checks_true : iterable<str>, optional (default is ())
            Check if the values of the given keys are true.
        checks_false : iterable<str>, optional (default is ())
            Check if the values of the given keys are false.
        checks_not_null : iterable<str>, optional (default is ())
            Check if the values of the given keys are null (python None).
        checks_null : iterable<str>, optional (default is ())
            Check if the values of the given keys are not null (python None).
        conjunction : str, optional (default is 'or')
            Choose between 'or' and 'and'.
            Specifies how to to link the checks together.

    Other Parameters
    ----------------
    include_fields : list<str>, optional (default is [])
        List of fields to include in the result.
        Mutually exclusive with `exclude_fields`.
    exclude_fields : list<str>, optional (default is [])
        List of fields to exclude from the result.
        Mutually exclusive with `include_fields`.

    where : dict, optional (default is {})
        A filter.
    remove_id_field : bool, optional (default is True)
        Will remove the `_id` field by default.

    distinct_key : str, optional (default is None)
        If given, list the distinct values for the `distinct_key.
    list_ran_scripts: bool, optional (default is False)
        List all scripts that have been run on the given selection.
        Normally you want to supply the `package_name`.
        Overrides `distinct_key`.

    sort : bool, optional (default is True)
        If true sort by analysis date.
    latest : bool, optional (default is False)
        Get the result of the latest script run.
        Will only return one result.
    n : int, optional (default is None)
        Number of results to return.
        None means no limit.

    non_document : bool, optional (default is False)
        Get custom data from mongodb's gridfs.
    non_document_raw : bool, optional (default is False)
        Get the raw data from the database. Otherwise meta infos will be returned.
        Only interesting if `non_document`.

    package_name : str, optional (default is None)
    apk_hash : str, optional (default is None)
    version_name : str, optional (default is None)
    tag : str, optional (default is None)

    script_hash : str, optional (default is None)
    script_name : str, optional (default is None)
    script_version : str, optional (default is None)

    Notes
    -----
    If any of the other parameters is None it won't be used for filtering.

    Returns
    -------
    gridfs.grid_file.GridOutCursor
        If non_document and non_document_raw.
    pymongo.cursor.Cursor
        Otherwise

    Raises
    ------
    DatabaseLoadException

    Examples
    --------
    >>> import androlyzelab
    ... from androlyze.storage.resultdb.ResultDatabaseStorage import ResultDatabaseStorage
    ... from androlyze.model.script.ScriptUtil import dict2json
    ... storage = ResultDatabaseStorage('127.0.0.1', 27017)
    ... res = androlyze.action_query_result_db(storage, n = 2, script_name = "ChainedApkInfos", include_fields = ["apkinfo.components.activities"])
    ... for r in res:
    ...     # get dict
    ...     # print r
    ...     # get json
    ...     print dict2json(r)
    {
    "apkinfo": {
        "components": {
            "activities": {
                "all": [
                    "cn.wps.impress.test.selfvalidate.lmj.TestServiceActivity",
    ...
    '''
    # build check filter dict if some checks are given which shall be done on some attributes
    if checks:
        checks = MongoUtil.build_checks_filter(**checks)

    # update with checks dict or {}
    if 'where' in kwargs and kwargs['where'] is not None:
        kwargs['where'].update(checks)
    else:
        kwargs['where'] = checks

    non_document = kwargs.get("non_document", False)
    if kwargs.get("list_ran_scripts", False):
        kwargs['distinct_key'] = MongoUtil.get_attr_str(StaticResultKeys.RESOBJ_SCRIPT_META, StaticResultKeys.RESOBJ_SCRIPT_META_NAME, non_document)

    return storage.get_results(**kwargs)