def list_pkgs(versions_as_list=False,
              removed=False,
              purge_desired=False,
              **kwargs):  # pylint: disable=W0613
    '''
    List the packages currently installed in a dict::

        {'<package_name>': '<version>'}

    removed
        If ``True``, then only packages which have been removed (but not
        purged) will be returned.

    purge_desired
        If ``True``, then only packages which have been marked to be purged,
        but can't be purged due to their status as dependencies for other
        installed packages, will be returned. Note that these packages will
        appear in installed

        .. versionchanged:: 2014.1.1

            Packages in this state now correctly show up in the output of this
            function.


    CLI Example:

    .. code-block:: bash

        salt '*' pkg.list_pkgs
        salt '*' pkg.list_pkgs versions_as_list=True
    '''
    versions_as_list = salt.utils.data.is_true(versions_as_list)
    removed = salt.utils.data.is_true(removed)
    purge_desired = salt.utils.data.is_true(purge_desired)

    if 'pkg.list_pkgs' in __context__:
        if removed:
            ret = copy.deepcopy(__context__['pkg.list_pkgs']['removed'])
        else:
            ret = copy.deepcopy(__context__['pkg.list_pkgs']['purge_desired'])
            if not purge_desired:
                ret.update(__context__['pkg.list_pkgs']['installed'])
        if not versions_as_list:
            __salt__['pkg_resource.stringify'](ret)
        return ret

    ret = {'installed': {}, 'removed': {}, 'purge_desired': {}}
    cmd = ['dpkg-query', '--showformat',
           '${Status} ${Package} ${Version} ${Architecture}\n', '-W']

    out = __salt__['cmd.run_stdout'](
            cmd,
            output_loglevel='trace',
            python_shell=False)
    # Typical lines of output:
    # install ok installed zsh 4.3.17-1ubuntu1 amd64
    # deinstall ok config-files mc 3:4.8.1-2ubuntu1 amd64
    for line in out.splitlines():
        cols = line.split()
        try:
            linetype, status, name, version_num, arch = \
                [cols[x] for x in (0, 2, 3, 4, 5)]
        except (ValueError, IndexError):
            continue
        if __grains__.get('cpuarch', '') == 'x86_64':
            osarch = __grains__.get('osarch', '')
            if arch != 'all' and osarch == 'amd64' and osarch != arch:
                name += ':{0}'.format(arch)
        if cols:
            if ('install' in linetype or 'hold' in linetype) and \
                    'installed' in status:
                __salt__['pkg_resource.add_pkg'](ret['installed'],
                                                 name,
                                                 version_num)
            elif 'deinstall' in linetype:
                __salt__['pkg_resource.add_pkg'](ret['removed'],
                                                 name,
                                                 version_num)
            elif 'purge' in linetype and status == 'installed':
                __salt__['pkg_resource.add_pkg'](ret['purge_desired'],
                                                 name,
                                                 version_num)

    for pkglist_type in ('installed', 'removed', 'purge_desired'):
        __salt__['pkg_resource.sort_pkglist'](ret[pkglist_type])

    __context__['pkg.list_pkgs'] = copy.deepcopy(ret)

    if removed:
        ret = ret['removed']
    else:
        ret = copy.deepcopy(__context__['pkg.list_pkgs']['purge_desired'])
        if not purge_desired:
            ret.update(__context__['pkg.list_pkgs']['installed'])
    if not versions_as_list:
        __salt__['pkg_resource.stringify'](ret)
    return ret
def list_pkgs ( versions_as_list = False , removed = False , purge_desired = False , ** kwargs ) : versions_as_list = salt . utils . data . is_true ( versions_as_list ) removed = salt . utils . data . is_true ( removed ) purge_desired = salt . utils . data . is_true ( purge_desired ) if 'pkg.list_pkgs' in __context__ : if removed : ret = copy . deepcopy ( __context__ [ 'pkg.list_pkgs' ] [ 'removed' ] ) else : ret = copy . deepcopy ( __context__ [ 'pkg.list_pkgs' ] [ 'purge_desired' ] ) if not purge_desired : ret . update ( __context__ [ 'pkg.list_pkgs' ] [ 'installed' ] ) if not versions_as_list : __salt__ [ 'pkg_resource.stringify' ] ( ret ) return ret ret = { 'installed' : { } , 'removed' : { } , 'purge_desired' : { } } cmd = [ 'dpkg-query' , '--showformat' , '${Status} ${Package} ${Version} ${Architecture}\n' , '-W' ] out = __salt__ [ 'cmd.run_stdout' ] ( cmd , output_loglevel = 'trace' , python_shell = False ) for line in out . splitlines ( ) : cols = line . split ( ) try : linetype , status , name , version_num , arch = [ cols [ x ] for x in ( 0 , 2 , 3 , 4 , 5 ) ] except ( ValueError , IndexError ) : continue if __grains__ . get ( 'cpuarch' , '' ) == 'x86_64' : osarch = __grains__ . get ( 'osarch' , '' ) if arch != 'all' and osarch == 'amd64' and osarch != arch : name += ':{0}' . format ( arch ) if cols : if ( 'install' in linetype or 'hold' in linetype ) and 'installed' in status : __salt__ [ 'pkg_resource.add_pkg' ] ( ret [ 'installed' ] , name , version_num ) elif 'deinstall' in linetype : __salt__ [ 'pkg_resource.add_pkg' ] ( ret [ 'removed' ] , name , version_num ) elif 'purge' in linetype and status == 'installed' : __salt__ [ 'pkg_resource.add_pkg' ] ( ret [ 'purge_desired' ] , name , version_num ) for pkglist_type in ( 'installed' , 'removed' , 'purge_desired' ) : __salt__ [ 'pkg_resource.sort_pkglist' ] ( ret [ pkglist_type ] ) __context__ [ 'pkg.list_pkgs' ] = copy . deepcopy ( ret ) if removed : ret = ret [ 'removed' ] else : ret = copy . deepcopy ( __context__ [ 'pkg.list_pkgs' ] [ 'purge_desired' ] ) if not purge_desired : ret . update ( __context__ [ 'pkg.list_pkgs' ] [ 'installed' ] ) if not versions_as_list : __salt__ [ 'pkg_resource.stringify' ] ( ret ) return ret
List the packages currently installed in a dict::

        {'<package_name>': '<version>'}

    removed
        If ``True``, then only packages which have been removed (but not
        purged) will be returned.

    purge_desired
        If ``True``, then only packages which have been marked to be purged,
        but can't be purged due to their status as dependencies for other
        installed packages, will be returned. Note that these packages will
        appear in installed

        .. versionchanged:: 2014.1.1

            Packages in this state now correctly show up in the output of this
            function.


    CLI Example:

    .. code-block:: bash

        salt '*' pkg.list_pkgs
        salt '*' pkg.list_pkgs versions_as_list=True