HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/opkg.cpython-310.pyc
o

�N�gQ��@shdZddlZddlZddlZddlZddlZddlZddlZddlZ	ddl
Z	ddlZ	ddlZ	ddl
Z	ddlZ	ddlZ	ddlZ	ddlmZmZmZddlmZdZdZddd	d
ddd
�Ze�e�ZdZdZdd�Zdd�Zdd�Z dd�Z!dd�Z"dd�Z#dd�Z$e$Z%dd�Z&drd!d"�Z'd#d$�Z(d%d&�Z)d'd(�Z*d)d*�Z+d+d,�Z,	 dsd-d.�Z-d/d0�Z.dtd1d2�Z/dtd3d4�Z0dud6d7�Z1dvd8d9�Z2dvd:d;�Z3d<d=�Z4d>d?�Z5d@dA�Z6drdBdC�Z7dudDdE�Z8dFdG�Z9dHdI�Z:dJdK�Z;dLdM�Z<	 drdNdO�Z=dPdQ�Z>dRdS�Z?dTdU�Z@dVdW�ZAdXdY�ZBdZd[�ZCd\d]�ZDd^d_�ZEd`da�ZFdbdc�ZGddde�ZHdfdg�ZIdhdi�ZJdjdk�ZKdldm�ZLdndo�ZMdpdq�ZNdS)wa�
Support for Opkg

.. important::
    If you feel that Salt should be using this module to manage packages on a
    minion, and it is using a different module (or gives an error similar to
    *'pkg.install' is not available*), see :ref:`here
    <module-provider-override>`.

.. versionadded:: 2016.3.0

.. note::

    For version comparison support on opkg < 0.3.4, the ``opkg-utils`` package
    must be installed.

�N)�CommandExecutionError�MinionError�SaltInvocationError)�Versionz3^#?\s*(src|src/gz)\s+([^\s<>]+|"[^<>]+")\s+[^\s<>]+z	/etc/opkg�arch�urlZinstall_date_time_tZpackager�name�group)ZArchitectureZHomepagezInstalled-TimeZ
MaintainerZPackageZSection�pkgz /var/lib/salt/restartcheck_statecCsd�dt�d�vrd�Sd�S)Nz$/usr/lib/{}/nisysapi/conf.d/experts/ZarmZcpuarchzarm-linux-gnueabizx86_64-linux-gnu)�format�
__grains__�get�rr�E/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/opkg.py�_get_nisysapi_conf_d_path9s���rcCs�tdd�}tdd�|t��tdd�|t��d}tj�|�r6tdd�|t��tdd�|t��t�}tj�|�r{tjj	�
t�d	�d
��}|�tt
t�|����Wd�n1sawYt�|�D]}tt�||�t�t�d�qkdSdS)
a�
    NILRT systems determine whether to reboot after various package operations
    including but not limited to kernel module installs/removals by checking
    specific file md5sums & timestamps. These files are touched/modified by
    the post-install/post-remove functions of their respective packages.

    The opkg module uses this function to store/update those file timestamps
    and checksums to be used later by the restartcheck module.

    �cmd.run_stdoutzuname -r�	cmd.shellz@stat -c %Y /lib/modules/{}/modules.dep >{}/modules.dep.timestampz9md5sum /lib/modules/{}/modules.dep >{}/modules.dep.md5sumz%/usr/local/natinst/share/nisysapi.iniz(stat -c %Y {} >{}/nisysapi.ini.timestampz!md5sum {} >{}/nisysapi.ini.md5sumz/sysapi.conf.d.count�wN��filenameZfingerprint_dir)�__salt__r�NILRT_RESTARTCHECK_STATE_PATH�os�path�existsr�salt�utils�files�fopen�write�str�len�listdir�_fingerprint_file�pathlib�Path)�unameZ
nisysapi_pathZnisysapi_conf_d_pathZfcountZfexpertrrr�_update_nilrt_restart_stateAsL��������
��
��r'cCs>tdd�|||j��tdd|�d|�d|j�d��dS)a
    Compute stat & md5sum hash of provided ``filename``. Store
    the hash and timestamp in ``fingerprint_dir``.

    filename
        ``Path`` to the file to stat & hash.

    fingerprint_dir
        ``Path`` of the directory to store the stat and hash output files.
    rzstat -c %Y {} > {}/{}.timestampzmd5sum z > �/z.md5sumN)rrrrrrrr#ys���r#cCs2tddd�}t|t�rd|vr|�|d�|S)zM
    Return restartcheck result and append errors (if any) to ``errors``
    zrestartcheck.restartcheckF)�verbose�comment)r�
isinstance�dict�append)�errors�	rs_resultrrr�_get_restartcheck_result�sr0cCs^d|vrdS|D]$}d|vrt�td�qtj�d|�}tj�|�r,td|dg�qdS)zu
    Check restartcheck output to see if system/service restarts were requested
    and take appropriate action.
    z(No packages seem to need to be restartedNzSystem restart requiredz$system.set_reboot_required_witnessedz/etc/init.d�cmd.runZrestart)r'rrr�joinr)r/ZrstrZservicerrr�_process_restartcheck_result�s��r3c
Cs�t�d�dkrBzt�t�Wn)ty7}z|jtjkr-dd�t|j|j	�fWYd}~SWYd}~nd}~wwt�
t�s@t�tStj
�t�rJtSdS)z8
    Confirm this module is on a nilrt based system
    Z	os_familyZ	NILinuxRTFzError creating {} (-{}): {}N)Fz4Module opkg only works on OpenEmbedded based systems)rr
r�makedirsr�OSError�errnoZEEXISTr�strerrorr"r'�__virtualname__r�isdir�OPKG_CONFDIR)�excrrr�__virtual__�s&
�����
	r<c
	Os�tjj�|�dd��}|sdSi}|D]}d||<q|rt�ddg}td|ddd	�}tjj�|d
�D]}z|�d�\}}}	||vrG|	||<Wq4t	yQYq4wt
|�dkr^||d
S|S)a
    Return the latest version of the named package available for upgrade or
    installation. If more than one package name is specified, a dict of
    name/version pairs is returned.

    If the latest version of a given package is already installed, an empty
    string will be returned for that package.

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.latest_version <package name>
        salt '*' pkg.latest_version <package name>
        salt '*' pkg.latest_version <package1> <package2> <package3> ...
    �refreshT��opkg�list-upgradabler�traceF��output_loglevel�python_shell�
� - �r)rr�data�is_true�pop�
refresh_dbr�	itertools�split�
ValueErrorr!)
�names�kwargsr=�retr�cmd�out�line�_oldversion�
newversionrrr�latest_version�s,
��rWcOstd|i|��S)aY
    Returns a string representing the package version or an empty string if not
    installed. If more than one package name is specified, a dict of
    name/version pairs is returned.

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.version <package name>
        salt '*' pkg.version <package1> <package2> <package3> ...
    zpkg_resource.version)r)rOrPrrr�version�s
rXFc
Kstjj�t�i}g}ddg}td|ddddd�}|d}d	}tjj�|d
�D]H}d|vr?|����dd
d�}	d||	<n/d|vrT|����dd
d�}	d||	<nd|vrn|����d�d�d}	d||	<|�	|	�|}q(|r|rt
d�d�|����|ddkr�|s�t
|��|S)a�
    Updates the opkg database to latest packages based upon repositories

    Returns a dict, with the keys being package databases and the values being
    the result of the update attempt. Values can be one of the following:

    - ``True``: Database updated successfully
    - ``False``: Problem updating database

    failhard
        If False, return results of failed lines as ``False`` for the package
        database that encountered the error.
        If True, raise an error with a list of the package databases that
        encountered errors.

        .. versionadded:: 2018.3.0

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.refresh_db
    r?�update�cmd.run_allrAFT)rCrD�ignore_retcode�redirect_stderr�stdoutr>rEZ	InflatingrGN���zUpdated sourcezFailed to download��,rzError getting repos: {}z, �retcode)
rrr
Z
clear_rtag�__opts__rrLrM�stripr-rrr2)
ZfailhardrPrQZerror_reposrR�callrS�	prev_linerT�keyrrrrKs@�


�rKcKst|�d�p
t�d��S)zI
    Returns whether a test mode (noaction) operation was requested.
    �test)�boolr
rb�rPrrr�_is_testmodeHsrjcKs tdi|��r|�d�dSdS)zS
    Adds the --noaction flag to the command if it's running in the test mode.
    z
--noactionNr)rjr-)rRrPrrr�_append_noaction_if_testmodeOs�rkcCs�g}|rt�|�}|�|�|�|�|r)t�|�}|�d�|�|�|�|�|r?t�|�}|�d�|�|�|�|�|S)z�
    Builds a list of install commands to be executed in sequence in order to process
    each of the to_install, to_downgrade, and to_reinstall lists.
    �--force-downgrade�--force-reinstall)�copy�deepcopy�extendr-)�
cmd_prefix�
to_install�to_downgrade�to_reinstall�cmdsrRrrr�_build_install_command_listWs 










rvcCsfi}t�d�}t�d�}tjj�|d�D]}|�|�}|dur$|�|�}|r0|�d�||�d�<q|S)a7
    Parses the output of "opkg install" to determine what packages would have been
    installed by an operation run with the --noaction flag.

    We are looking for lines like:
        Installing <package> (<version>) on <target>
    or
        Upgrading <package> from <oldVersion> to <version> on root
    zGInstalling\s(?P<package>.*?)\s\((?P<version>.*?)\)\son\s(?P<target>.*?)zaUpgrading\s(?P<package>.*?)\sfrom\s(?P<oldVersion>.*?)\sto\s(?P<version>.*?)\son\s(?P<target>.*?)rENrX�package��re�compilerrrLrM�matchr	)�output�
reported_pkgsZinstall_patternZupgrade_patternrTr{rrr�,_parse_reported_packages_from_install_outputos
��

�r~cCshtd|ddd�}|ddkr%|dr|�|d�d	S|�|d�d	S|r2|�t|d��d	Sd	S)
a
    Executes a command for the install operation.
    If the command fails, its error output will be appended to the errors list.
    If the command succeeds and parse_output is true, updated packages will be appended
    to the parsed_packages dictionary.
    rZrAFrBrar�stderrr]N)rr-rYr~)rRZparse_outputr.Zparsed_packagesrSrrr�_execute_install_command�s
��r�c
Ks:tjj�|�}ztd|||fi|��\}}Wnty'}	zt|	��d}	~	wwt�}
ddg}g}g}
g}t|fi|��|sAiS|dkr]|rL|�	d�|�
dd�sW|�	d	�|�|�n�|d
kr�|�
dd�sl|�	d
�|��D]x\}}|r�|dur�|�
d�r�t
|�dkr�|d}n|}|dur�|r�||
vr�|
�	|�qp|�	|�qp|�d|��}|
�
|d�}|r�|r�tjjj|d|td�r�|
�	|�qp|r�tjjj|d|td�r�|�	|�qp|�
dd�s�|�	|�qp|�	|�qpt||||
�}|s�iS|r�t�g}td!i|��}i}|D]
}t||||��qt�dd�t�}|�r't�|�}|�|�tjj�|
|�}|dk�re|�re|D]+}dd|g}td|ddd�}|ddk�rct|dg�}|�rc|
�	tt|����q9|
D]}||v�ss||
v�r�|�||
�
|d�|�
|d�d�i��qgt|�}|�r�td||d�d ��t|�|S)"a	
    Install the passed package, add refresh=True to update the opkg database.

    name
        The name of the package to be installed. Note that this parameter is
        ignored if either "pkgs" or "sources" is passed. Additionally, please
        note that this option can only be used to install packages from a
        software repository. To install a package file manually, use the
        "sources" option.

        CLI Example:

        .. code-block:: bash

            salt '*' pkg.install <package name>

    refresh
        Whether or not to refresh the package database before installing.

    version
        Install a specific version of the package, e.g. 1.2.3~0ubuntu0. Ignored
        if "pkgs" or "sources" is passed.

        .. versionadded:: 2017.7.0

    reinstall : False
        Specifying reinstall=True will use ``opkg install --force-reinstall``
        rather than simply ``opkg install`` for requested packages that are
        already installed.

        If a version is specified with the requested package, then ``opkg
        install --force-reinstall`` will only be used if the installed version
        matches the requested version.

        .. versionadded:: 2017.7.0


    Multiple Package Installation Options:

    pkgs
        A list of packages to install from a software repository. Must be
        passed as a python list.

        CLI Example:

        .. code-block:: bash

            salt '*' pkg.install pkgs='["foo", "bar"]'
            salt '*' pkg.install pkgs='["foo", {"bar": "1.2.3-0ubuntu0"}]'

    sources
        A list of IPK packages to install. Must be passed as a list of dicts,
        with the keys being package names, and the values being the source URI
        or local path to the package.  Dependencies are automatically resolved
        and marked as auto-installed.

        CLI Example:

        .. code-block:: bash

            salt '*' pkg.install sources='[{"foo": "salt://foo.deb"},{"bar": "salt://bar.deb"}]'

    install_recommends
        Whether to install the packages marked as recommended. Default is True.

    only_upgrade
        Only upgrade the packages (disallow downgrades), if they are already
        installed. Default is False.

        .. versionadded:: 2017.7.0

    Returns a dict containing the new package names and versions::

        {'<package>': {'old': '<old-version>',
                       'new': '<new-version>'}}
    �pkg_resource.parse_targetsNr?�install�filermZonly_upgradeFrl�
repositoryZinstall_recommendsTz--no-install-recommendsrXrG�=r>z==)Zver1�operZver2Zcmp_funcz>=�
pkg.list_pkgs�inforZrArBrarr]��old�newz)Problem encountered installing package(s)�r.�changes�r�r) rrrHrIrrr�	list_pkgsrkr-r
rp�itemsr!ZversionsZcompare�version_cmprvrKrjr��__context__rJrnrorY�
compare_dicts�_process_info_installed_output�next�iterr0r3)rr=�pkgs�sourcesZ	reinstallrPZ	refreshdb�
pkg_paramsZpkg_typer;r�rqrrrtrs�pkgnameZ
pkgversionZversion_numZpkgstrZcverrur.Zis_testmodeZ
test_packagesrRr�rQZpkgfilerSZpkginfo_dictr/rrrr��s�O����


"
������


�����r�cCsDi}t�d�}tjj�|d�D]}|�|�}|rd||�d�<q|S)z�
    Parses the output of "opkg remove" to determine what packages would have been
    removed by an operation run with the --noaction flag.

    We are looking for lines like
        Removing <package> (<version>) from <Target>...
    zJRemoving\s(?P<package>.*?)\s\((?P<version>.*?)\)\sfrom\s(?P<target>.*?)...rEr>rwrx)r|r}Zremove_patternrTr{rrr�+_parse_reported_packages_from_remove_outputgs�
�r�c
sdztd||�d}Wnty}zt|��d}~wwt���fdd�|D�}|s,iSddg}t|fi|��|�dd	�rC|�d
�|�dd	�rN|�d�|�|�td
|dd	d�}|ddkrr|drl|dg}n|dg}ng}t�	dd�t�}	t
di|��r�t|d���fdd�|	��D�}	t
jj��|	�}
t|�}|r�td||
d�d��t|�|
S)al
    Remove packages using ``opkg remove``.

    name
        The name of the package to be deleted.


    Multiple Package Options:

    pkgs
        A list of packages to delete. Must be passed as a python list. The
        ``name`` parameter will be ignored if this option is passed.

    remove_dependencies
        Remove package and all dependencies

        .. versionadded:: 2019.2.0

    auto_remove_deps
        Remove packages that were installed automatically to satisfy dependencies

        .. versionadded:: 2019.2.0

    Returns a dict containing the changes.

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.remove <package name>
        salt '*' pkg.remove <package1>,<package2>,<package3>
        salt '*' pkg.remove pkgs='["foo", "bar"]'
        salt '*' pkg.remove pkgs='["foo", "bar"]' remove_dependencies=True auto_remove_deps=True
    r�rNcsg|]}|�vr|�qSrr��.0�x)r�rr�
<listcomp>�szremove.<locals>.<listcomp>r?�removeZremove_dependenciesFz%--force-removal-of-dependent-packagesZauto_remove_depsz--autoremoverZrArBrarr]r�csi|]\}}|�vr||�qSrr)r��k�v)�reportedPkgsrr�
<dictcomp>�szremove.<locals>.<dictcomp>z'Problem encountered removing package(s)r�r�r)rrrr�rkr
r-rpr�rJrjr�r�rrrHr�r0r3)rr�rPr�r;�targetsrRrSr.r�rQr/r)r�r�rr�{sJ#��


�r�cKst||d�S)ag
    Package purges are not supported by opkg, this function is identical to
    :mod:`pkg.remove <salt.modules.opkg.remove>`.

    name
        The name of the package to be deleted.


    Multiple Package Options:

    pkgs
        A list of packages to delete. Must be passed as a python list. The
        ``name`` parameter will be ignored if this option is passed.


    Returns a dict containing the changes.

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.purge <package name>
        salt '*' pkg.purge <package1>,<package2>,<package3>
        salt '*' pkg.purge pkgs='["foo", "bar"]'
    )rr�)r�)rr�rPrrr�purge�sr�Tc	Ks�iddd�}g}tjj�|�rt�t�}ddg}td|ddd	�}t�d
d�t�}tjj�	||�}|dd
kr>|�
|�t|�}|rMtd||d�d��t
|�|S)a.
    Upgrades all packages via ``opkg upgrade``

    Returns a dictionary containing the changes:

    .. code-block:: python

        {'<package>':  {'old': '<old-version>',
                        'new': '<new-version>'}}

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.upgrade
    Tr>)r��resultr*r?�upgraderZrAFrBr�Nrarz&Problem encountered upgrading packagesr�r�)rrrHrIrKr�rr�rJr�r-r0rr3)	r=rPrQr.r�rRr�r�r/rrrr��s.�
�r�c
KsV|s
|s
|s
td��|r|rtd��g}|r|�|�n|r-|D]}|�tt|���q n|�|�i}|D]r}t|t�rCtt|��}|iddd�||<t|�}|s]d|�d�||d<q6|d	kr�d
tvr|td
r|||j	dd�d|�d
�||d<q6t
|d	�}	||j	|	|dd�d|�d�||d<q6||j	dd�d�|�||d<q6|S)a�
    Set package in 'hold' state, meaning it will not be upgraded.

    name
        The name of the package, e.g., 'tmux'

        CLI Example:

        .. code-block:: bash

            salt '*' pkg.hold <package name>

    pkgs
        A list of packages to hold. Must be passed as a python list.

        CLI Example:

        .. code-block:: bash

            salt '*' pkg.hold pkgs='["foo", "bar"]'
    �0One of name, pkgs, or sources must be specified.�-Only one of pkgs or sources can be specified.Fr>�rr�r�r*�Package z not currently held.r*�holdrgN�r�z is set to be held.T�r�r�z is now being held.z%Package {} is already set to be held.�rrpr-r�r�r+r,�
_get_staterbrY�
_set_stater�
rr�r�rPr��sourcerQ�target�stater�rrrr�s@�


�r�c
KsP|s
|s
|s
td��|r|rtd��g}|r|�|�n|r-|D]}|�tt|���q n|�|�i}|D]o}t|t�rCtt|��}|iddd�||<t|�}|s]d|�d�||d<q6|d	kr�d
tvrztd
rz||j	dd�d|�d
�|d<q6t
|d�}	||j	|	|dd�d�|�||d<q6||j	dd�d�|�||d<q6|S)a�
    Set package current in 'hold' state to install state,
    meaning it will be upgraded.

    name
        The name of the package, e.g., 'tmux'

        CLI Example:

        .. code-block:: bash

            salt '*' pkg.unhold <package name>

    pkgs
        A list of packages to hold. Must be passed as a python list.

        CLI Example:

        .. code-block:: bash

            salt '*' pkg.unhold pkgs='["foo", "bar"]'
    r�r�Fr>r�r�z does not have a state.r*r�rgNr�z is set not to be held.�okTr�z#Package {} is no longer being held.z)Package {} is already set not to be held.r�r�rrr�unhold[sD�


��r�cCsZddg}|�|�td|dd�}d}tjj�|d�D]}|�d�r*|��\}}}}q|S)	zP
    View package state from the opkg database

    Return the state of pkg
    r?�statusr1F�rDr>rEZStatus)r-rrrrLrM�
startswith)r
rRrSZ
state_flagrTZ_statusZ_state_wantZ
_state_statusrrrr��s

�r�cCsdi}d}||vrtd|����t|�}ddg}|�|�|�|�td|dd�}||d�||<|S)	a�
    Change package state on the opkg database

    The state can be any of:

     - hold
     - noprune
     - user
     - ok
     - installed
     - unpacked

    This command is commonly used to mark a specific package to be held from
    being upgraded, that is, to be kept at a certain version.

    Returns a dict containing the package name, and the new and old
    versions.
    )r�Znoprune�userr�Z	installedZunpackedzInvalid state: r?�flagr1Fr�r�)rr�r-r)r
r�rQZvalid_statesZoldstaterRZ_outrrrr��s

r�cCs*|rtdSt�td�}td|�|S)z'
    Use pkg list from __context__
    r��pkg_resource.stringify)r�rnror)�versions_as_listrQrrr�_list_pkgs_from_context�s
r�cs�tjj�|�}t�fdd�dD��riSdtvrt|�Sddg}i}td|dd	d
�}tjj�	|d�D]}|r=|dd
kr>q3|�	dd�dd�\}}td|||�q3td|�t
�|�td<|shtd|�|S)z�
    List the packages currently installed in a dict::

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

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.list_pkgs
        salt '*' pkg.list_pkgs versions_as_list=True
    cs g|]}tjj���|���qSr)rrrHrIr
r�rirrr��s zlist_pkgs.<locals>.<listcomp>)ZremovedZ
purge_desiredr�r?zlist-installedr1rAFrBrEr� rF�Nzpkg_resource.add_pkgzpkg_resource.sort_pkglistr�)rrrHrI�anyr�r�rrLrMrnro)r�rPrRrQrSrT�pkg_nameZpkg_versionrrirr��s(
�r�cKs�i}tjj�|�rt�ddg}td|ddd�}|ddkr9d	}d
|vr+||d
7}d|vr5||d7}t|��|d}|��D]}|�d�\}}	}
|
||<qA|S)
z~
    List all available package upgrades.

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.list_upgrades
    r?r@rZrAFrBrarr>rr]rF)	rrrHrIrKrr�
splitlinesrM)r=rPrQrRrdr*rSrTrrUrVrrr�
list_upgradess"

r�cCs t�|d�}|dur|��S|S)z�
    Helper function for _process_info_installed_output()

    Converts an opkg attribute name to a standard attribute
    name which is used across 'pkg' modules.
    N)�ATTR_MAPr
�lower)�attrZret_attrrrr�_convert_to_standard_attr(sr�c	Cs�i}d}i}d}tjj�|d�D]a}|r;|ddkr;|dus"||vr:|��}||r2||d7<|||7<q|��}|sN|rG|||<d}i}d}q|�dd�\}}|��}t|�}|dkre|}q|dusm||vrq|||<q|rx|||<|S)zz
    Helper function for info_installed()

    Processes stdout output from a single invocation of
    'opkg status'.
    NrErr��:rGr)rrrLrMrc�lstripr�)	rS�filter_attrsrQrZattrsr�rTrf�valuerrrr�6s>�r�c	Os4|�dd�}|dur
d}nt|t�rt|�d��}nt|�}i}|ra|D]:}dd|g}td|ddd	�}|d
dkrTd}|d
rJ||d
7}t|��||d7}t|��|�t|d|��q$|Sddg}td|ddd	�}|d
dkr�d}|d
r�||d
7}t|��||d7}t|��|�t|d|��|S)a%
    Return the information of the named package(s), installed on the system.

    .. versionadded:: 2017.7.0

    :param names:
        Names of the packages to get information about. If none are specified,
        will return information for all installed packages.

    :param attr:
        Comma-separated package attributes. If no 'attr' is specified, all available attributes returned.

        Valid attributes are:
            arch, conffiles, conflicts, depends, description, filename, group,
            install_date_time_t, md5sum, packager, provides, recommends,
            replaces, size, source, suggests, url, version

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.info_installed
        salt '*' pkg.info_installed attr=version,packager
        salt '*' pkg.info_installed <package1>
        salt '*' pkg.info_installed <package1> <package2> <package3> ...
        salt '*' pkg.info_installed <package1> attr=version,packager
        salt '*' pkg.info_installed <package1> <package2> <package3> ... attr=version,packager
    r�Nr`r?r�rZrAFrBrarr>rr])	rJr+r �setrMrrrYr�)	rOrPr�r�rQrrRrdr*rrr�info_installedbsB

����r�cKst|�dkS)z�
    Check whether or not an upgrade is available for a given package

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.upgrade_available <package name>
    r>)rW)rrPrrr�upgrade_available�s
r�cs��fdd�}||�}||�}tdddgddd�}|�d	�d
��}t|�td�kr/ddg}ntjj�d
�r:d
g}nt�	d�dSdD]1\}}	|dd�}
|
�
t�|��|
�
|�|
�
t�|��td|
dddd�}|dkrt|	SqCdS)a�
    Do a cmp-style comparison on two packages. Return -1 if pkg1 < pkg2, 0 if
    pkg1 == pkg2, and 1 if pkg1 > pkg2. Return None if there was a problem
    making the comparison.

    ignore_epoch : False
        Set to ``True`` to ignore the epoch when comparing versions

        .. versionadded:: 2016.3.4

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.version_cmp '0.2.4-0' '0.2.4.1-0'
    cs �rt|��dd�dSt|�S)Nr�rGr^)r rM)r���ignore_epochrr�	normalize�s zversion_cmp.<locals>.normalizerr?z	--versionrAFrBr�r�z0.3.4zcompare-versionszopkg-compare-versionsz�Unable to find a compare-versions utility installed. Either upgrade opkg to version > 0.3.4 (preferred) or install the older opkg-compare-versions script.N))z<<r^)r�r)z>>rGzcmd.retcodeT)rCr[rDr)
rrMrcrrrr�which�log�warningr-�shlex�quote)Zpkg1Zpkg2r�rPr�r|Zopkg_versionZcmd_comparer�rQrRrarr�rr��s6
�
�
��r�cCsV|sdS|�d�}t|�dkrdS|ddkr!|ddk|d<dS|d||d<dS)z 
    Set the option to repo
    Nr�r�r�trustedrGZyes)rMr!)�repo�option�optrrr�_set_repo_option�s
r�cCsDd}d�ttj|��}|D]}t�||�}|D]}t||�qq
dS)z&
    Set the options to the repo.
    )�[�]�|N)r2�mapry�escaperMr�)r��optionsZ
delimiters�patternr�Zsplittedr�rrr�_set_repo_options�s��r�cCs�i}|�d�rd|d<|dd�}nd|d<tjj�|���}|ddv|d	<|d|d
<|d|d<tj�t	|�|d
<t
|�dkrKt||dd��|S)z
    Create repo
    �#F�enabledrGNTr�src�
compressedrr��urir��)r�rr�args�shlex_splitrcrrr2r:r!r�)rTrr��colsrrr�_create_repos
r�cCsL|D]!}tjj�|�}|�|�sqt||�}|d|vr#|g||d<qdS)z,
    Read repos from configuration file
    r�N)rr�stringutils�
to_unicode�searchr�)�	conf_file�reposr�regexrTr�rrr�_read_reposs

��r�c	Ksti}t�t�}t�t�D]+}|�d�sqtjj	�
tj�t|���}t
||||�Wd�n1s2wYq|S)z}
    Lists all repos on ``/etc/opkg/*.conf``

    CLI Example:

    .. code-block:: bash

       salt '*' pkg.list_repos
    �.confN)ryrz�REPO_REGEXPrr"r:�endswithrrrrrr2r�)rPr�r�rr�rrr�
list_repos+s


��r�cKs>t�}|r|��D]}|D]}|d|kr|Sq
q	iS)z�
    Display a repo from the ``/etc/opkg/*.conf``

    CLI Examples:

    .. code-block:: bash

        salt '*' pkg.get_repo repo
    r)r��values)r�rPr�r��subrrr�get_repo?s
��r�cCs�tjj�|��D}g}t�t�}|D]2}tjj�|�}|�	|�rC|�
d�r*|dd�}tjj�|�
��}||dkrC|�tjj�|��qWd�n1sNwYtjj�|d��}|�|�Wd�dS1slwYdS)z%
    Remove a repo from filepath
    r�rGNr)rrrrryrzr�r�r�r�r�r�r�rcr-�to_str�
writelines)r��filepath�fhandler|r�rTr�rrr�_del_repo_from_fileSs"


���"�rcCs(|dur
|d7}|S|dur|d7}|S)z.
    Set trusted option to repo if needed
    Tz [trusted=yes]Fz
 [trusted=no]r)�repostrr�rrr�_set_trusted_option_if_neededfs�rcCs�|�d�sdnd}||�d�rdnd7}d|vr!|d|d	7}n||d7}||�d
�7}t||�d��}|d7}tj�t|d
�}tjj�	|d��}|�
tjj�|��Wd�dS1sawYdS)z
    Add a new repo entry
    r��# r>r��src/gz zsrc r��"z" r�r�rEr��aN)
r
rrrr2r:rrrrrr�r�)r��
propertiesr�conffilerrrr�
_add_new_repoqs"�rcCs�tjj�|��3}g}|D]&}tjj�tjj�|����}||vr&|�	|�q|�	tjj�
|d��qWd�n1s=wYtjj�|d��}|�|�Wd�dS1s[wYdS)z7
    Replace a repo entry in filepath with repostr
    rENr)rrrrr�r�r�r�rcr-r�r)r�rrrr|rTr�rrr�_mod_repo_in_file�s���
"�r
c	Kstjj�|�dd��}t�}|r�t�}|D]}||d}|d|kr0d||d<t||d�q|r�d}|D]}||d}|d|vrO||dd7<q7|��D]+\}}	d}
|	dkrwt	j
�|�rwd	}
zt	�|�Wn	t
yvYnw||
�||�7}qT|r�t�|Sd
|�d�S)z�
    Delete a repo from ``/etc/opkg/*.conf``

    If the file does not contain any other repo configuration, the file itself
    will be deleted.

    CLI Examples:

    .. code-block:: bash

        salt '*' pkg.del_repo repo
    r=Trrr�r>rGz$Repo '{}' has been removed from {}.
z1File {1} containing repo '{0}' has been removed.
zRepo z% doesn't exist in the opkg repo lists)rrrHrIr
r�r,rr�rr�isfiler�r5rrK)r�rPr=r�Zdeleted_fromr�r�rQZ	repo_file�count�msgrrr�del_repo�s>
���rcKs�t�}d}d}d|vr|d}|D]�}||d}|d|kr�d}d}d|vr/|ds/|d7}d	|vr>||d	r:d
nd7}n
||d	rEdnd7}d
|vrP|d
n|}d|vr_|d|�d�7}n|d|��7}|d�d|vrq|dn|d�7}|�d�}	|	dur�t||	�nt||�d��}t|||d�q|r�|d|kr�td�||d���q|s�d|vr�td�|���d|di}
d	|vr�|d	nd|
d	<d|vr�|dnd|
d<|�d�|
d<t||
�d|vr�t�dSdS)a�
    Modify one or more values for a repo.  If the repo does not exist, it will
    be created, so long as uri is defined.

    The following options are available to modify a repo definition:

    repo
        alias by which opkg refers to the repo.
    uri
        the URI to the repo.
    compressed
        defines (True or False) if the index file is compressed
    enabled
        enable or disable (True or False) repository
        but do not remove if disabled.
    refresh
        enable or disable (True or False) auto-refresh of the repositories

    CLI Examples:

    .. code-block:: bash

        salt '*' pkg.mod_repo repo uri=http://new/uri
        salt '*' pkg.mod_repo repo enabled=False
    Fr>r�rrTr�rr�rr�zsrc/gz�aliasr�z "rz {}r�Nr�z'Repository '{}' already exists as '{}'.z:Repository '{}' not found and no URI passed to create one.r=)r�rr
rr
rrrK)r�rPr��foundr�r�r�rZ
repo_aliasr�r
rrr�mod_repo�s^"

�������

�rcOs:t|�}g}t|d���D]}|�|�q|d|d�S)aV
    List the files that belong to a package. Not specifying any packages will
    return a list of _every_ file on the system's package database (not
    generally recommended).

    CLI Examples:

    .. code-block:: bash

        salt '*' pkg.file_list httpd
        salt '*' pkg.file_list httpd postfix
        salt '*' pkg.file_list
    �packagesr.)r.r)�	file_dict�listr�rp)rrPr|rrwrrr�	file_lists
rc
Os�g}i}ddg}|stt����}|D]B}g}|dd�}|�|�td|ddd�}|d��D]}	|	�d	�r>|�|	�q1|	�d
�rN|�|	dd��nq1|rU|||<q||d�S)
aj
    List the files that belong to a package, grouped by package. Not
    specifying any packages will return a list of _every_ file on the system's
    package database (not generally recommended).

    CLI Examples:

    .. code-block:: bash

        salt '*' pkg.file_list httpd
        salt '*' pkg.file_list httpd postfix
        salt '*' pkg.file_list
    r?rNrZrAFrBr]r(z * r�)r.r)rr��keysr-rr�r�)
rrPr.rQZ	cmd_filesrwrrRrSrTrrrr+s*


�
rcOs�|sdSi}ddg}|D](}|dd�}|�|�td|ddd�}|r0|�d	�d
��||<qd||<qt|�dkrCtt|����S|S)aS
    Return the name of the package that owns the file. Multiple file paths can
    be passed. Like :mod:`pkg.version <salt.modules.opkg.version`, if a single
    path is passed, a string will be returned, and if multiple paths are passed,
    a dictionary of file/package name pairs will be returned.

    If the file is not owned by a package, or is not present on the minion,
    then an empty string will be returned for that path.

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.owner /usr/bin/apachectl
        salt '*' pkg.owner /usr/bin/apachectl /usr/bin/basename
    r>r?r�NrrAFrBrFrrG)r-rrMrcr!r�r�r�)�pathsrPrQZ
cmd_searchrrRr|rrr�ownerSs 
�
rcCs|S)z�
    Clean the version string removing extra data.
    There's nothing do to here for nipkg.py, therefore it will always
    return the given version.
    r)rXrrr�
version_cleanw�rcCsdS)z�
    Check if the installed package already has the given requirements.
    There's nothing do to here for nipkg.py, therefore it will always
    return True.
    Tr)r�Zpkgverrrr�check_extra_requirements�rr)F)NFNNF)NN)T)NNN)O�__doc__rnr6�loggingrr$ryr�Zsalt.utils.argsrZsalt.utils.dataZsalt.utils.filesZsalt.utils.itertoolsZsalt.utils.pathZsalt.utils.pkgZsalt.utils.stringutilsZsalt.utils.versionsZsalt.exceptionsrrrrr�r:r��	getLogger�__name__r�r8rrr'r#r0r3r<rWZavailable_versionrXrKrjrkrvr~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr
rrrrrrrrrrr�<module>s��
	8
/
D
�K

Q

4
>A"

+",G
�7-S($