Blame doc/api_queries.rst

Packit Service 21c75c
..
Packit Service 21c75c
  Copyright (C) 2014-2018 Red Hat, Inc.
Packit Service 21c75c
Packit Service 21c75c
  This copyrighted material is made available to anyone wishing to use,
Packit Service 21c75c
  modify, copy, or redistribute it subject to the terms and conditions of
Packit Service 21c75c
  the GNU General Public License v.2, or (at your option) any later version.
Packit Service 21c75c
  This program is distributed in the hope that it will be useful, but WITHOUT
Packit Service 21c75c
  ANY WARRANTY expressed or implied, including the implied warranties of
Packit Service 21c75c
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit Service 21c75c
  Public License for more details.  You should have received a copy of the
Packit Service 21c75c
  GNU General Public License along with this program; if not, write to the
Packit Service 21c75c
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 21c75c
  02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit Service 21c75c
  source code or documentation are not subject to the GNU General Public
Packit Service 21c75c
  License and may only be used or replicated with the express permission of
Packit Service 21c75c
  Red Hat, Inc.
Packit Service 21c75c
Packit Service 21c75c
======================
Packit Service 21c75c
 Queries and Subjects
Packit Service 21c75c
======================
Packit Service 21c75c
Packit Service 21c75c
.. module:: dnf.query
Packit Service 21c75c
Packit Service 21c75c
.. class:: Query
Packit Service 21c75c
Packit Service 21c75c
  Facilitates lookup of packages in a :class:`~dnf.sack.Sack` based on given criteria. Query actually does not consult the information in the :class:`~!dnf.sack.Sack` until it is evaluated. The evaluation happens either explicitly using :meth:`~dnf.query.Query.run` or by iterating the query, for example::
Packit Service 21c75c
Packit Service 21c75c
    #!/usr/bin/python3
Packit Service 21c75c
    import dnf
Packit Service 21c75c
Packit Service 21c75c
    base = dnf.Base()
Packit Service 21c75c
    base.fill_sack()
Packit Service 21c75c
Packit Service 21c75c
    q = base.sack.query()
Packit Service 21c75c
    i = q.installed()
Packit Service 21c75c
    i = i.filter(name='dnf')
Packit Service 21c75c
Packit Service 21c75c
    packages = list(i)  # i only gets evaluated here
Packit Service 21c75c
    print("Installed dnf package:")
Packit Service 21c75c
    for pkg in packages:
Packit Service 21c75c
        print(pkg, pkg.reponame)
Packit Service 21c75c
Packit Service 21c75c
  or::
Packit Service 21c75c
Packit Service 21c75c
    #!/usr/bin/python3
Packit Service 21c75c
    import dnf
Packit Service 21c75c
Packit Service 21c75c
    base = dnf.Base()
Packit Service 21c75c
    base.read_all_repos()
Packit Service 21c75c
    base.fill_sack()
Packit Service 21c75c
Packit Service 21c75c
    q = base.sack.query()
Packit Service 21c75c
    a = q.available()
Packit Service 21c75c
    a = a.filter(name='dnf')
Packit Service 21c75c
Packit Service 21c75c
    print("Available dnf packages:")
Packit Service 21c75c
    for pkg in a:  # a only gets evaluated here
Packit Service 21c75c
        print('{} in repo {}'.format(pkg, pkg.reponame))
Packit Service 21c75c
Packit Service 21c75c
Packit Service 21c75c
  Notice that none of the filtering methods mutates the state of the :class:`~dnf.query.Query` but produces a new object instead.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: available()
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query limiting the original query to the packages available from the repositories.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: difference(other)
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query that contains only those results of original query that are not in the results of the ``other`` query.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: downgrades()
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query that limits the result only to packages that can be downgrade candidates to other packages in the current set. Downgrade candidate has the same name, lower EVR and the architecture of the original and the downgrade candidate are suitable for a downgrade. Specifically, the filtering does not take any steps to establish that the downgrade candidate can actually be installed.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: duplicated()
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query that limits the result only to installed packages of same name and different version. Optional argument exclude accepts a list of package names that will be excluded from result.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: extras()
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query that limits the result to installed packages that are not present in any repo
Packit Service 21c75c
Packit Service 21c75c
  .. method:: filter(\*\*kwargs)
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query limiting the original query to the key/value pairs from `kwargs`. Multiple `kwargs` can be passed, the filter then works by applying all of them together (logical AND). Values inside of list or query are cumulative (logical OR).
Packit Service 21c75c
Packit Service 21c75c
    Allowed keys are:
Packit Service 21c75c
Packit Service 21c75c
    ===============   ============== ======================================================
Packit Service 21c75c
    key               value type     value meaning
Packit Service 21c75c
    ===============   ============== ======================================================
Packit Service 21c75c
    arch              string         match against packages' architecture
Packit Service 21c75c
    downgrades        boolean        see :meth:`downgrades`. Defaults to ``False``.
Packit Service 21c75c
    empty             boolean        ``True`` limits to empty result set.
Packit Service 21c75c
                                     Defaults to ``False``.
Packit Service 21c75c
    epoch             integer        match against packages' epoch.
Packit Service 21c75c
    file              string         match against packages' files
Packit Service 21c75c
    latest            integer        limit to all packages of number of versions
Packit Service 21c75c
    latest_per_arch   integer        see :meth:`latest`.
Packit Service 21c75c
    name              string         match against packages' names
Packit Service 21c75c
    release           string         match against packages' releases
Packit Service 21c75c
    reponame          string         match against packages repositories' names
Packit Service 21c75c
    version           string         match against packages' versions
Packit Service 21c75c
    pkg               Query          match against packages in query
Packit Service 21c75c
    pkg*              list           match against hawkey.Packages in list
Packit Service 21c75c
    provides          string         match against packages' provides
Packit Service 21c75c
    provides*         Hawkey.Reldep  match against packages' provides
Packit Service 21c75c
    <DEP>             string         match against packages' <DEP>
Packit Service 21c75c
    <DEP>*            Hawkey.Reldep  match a reldep against packages' <DEP>
Packit Service 21c75c
    <DEP>*            Query          match the result of a query against packages' <DEP>
Packit Service 21c75c
    <DEP>*            list(Package)  match the list of hawkey.Packages against packages' <DEP>
Packit Service 21c75c
    sourcerpm         string         match against packages' source rpm
Packit Service 21c75c
    upgrades          boolean        see :meth:`upgrades`. Defaults to ``False``.
Packit Service 21c75c
    ===============   ============== ======================================================
Packit Service 21c75c
Packit Service 21c75c
    ``<DEP>`` can be any of: requires, conflicts, obsoletes, enhances, recomments, suggests, supplements
Packit Service 21c75c
Packit Service 21c75c
    \* The key can also accept a list of values with specified type.
Packit Service 21c75c
Packit Service 21c75c
    The key name can be supplemented with a relation-specifying suffix, separated by ``__``:
Packit Service 21c75c
Packit Service 21c75c
    ==========   =========== ==========================================================
Packit Service 21c75c
    key suffix   value type  semantics
Packit Service 21c75c
    ==========   =========== ==========================================================
Packit Service 21c75c
    eq           any         exact match; This is the default if no suffix is specified.
Packit Service 21c75c
    glob         string      shell-style wildcard match
Packit Service 21c75c
    gt           integer     the actual value is greater than specified
Packit Service 21c75c
    gte          integer     the actual value is greater than or equal to specified
Packit Service 21c75c
    lt           integer     the actual value is less than specified
Packit Service 21c75c
    lte          integer     the actual value is less than or equal to specified
Packit Service 21c75c
    neq          any         does not equal
Packit Service 21c75c
    substr       string      the specified value is contained in the actual value
Packit Service 21c75c
    ==========   =========== ==========================================================
Packit Service 21c75c
Packit Service 21c75c
    For example, the following creates a query that matches all packages containing the string "club" in its name::
Packit Service 21c75c
Packit Service 21c75c
      q = base.sack.query().filter(name__substr="club")
Packit Service 21c75c
Packit Service 21c75c
    Note that using packages or queries for dependency filtering performs a more advanced resolution than using a string or a reldep. When a package list or a query is used, rich dependencies are resolved in a more precise way than what is possible when a string or a reldep is used.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: filterm(\*\*kwargs)
Packit Service 21c75c
Packit Service 21c75c
    Similar to :meth:`dnf.query.Query.filter` but it modifies the query in place.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: installed()
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query that limits the result to the installed packages only.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: intersection(other)
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query where the result contains only packages that are found in both original and ``other`` queries.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: latest(limit=1)
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query that limits the result to ``limit`` highest version of packages per package
Packit Service 21c75c
    name and per architecture. In case the limit is negative number, it excludes the number of
Packit Service 21c75c
    latest versions according to limit.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: run()
Packit Service 21c75c
Packit Service 21c75c
    Evaluate the query. Returns a list of matching :class:`dnf.package.Package` instances.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: union(other)
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query where the results of the ``other`` query are added to the results of the original query.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: upgrades()
Packit Service 21c75c
Packit Service 21c75c
    Returns a new query that limits the result only to packages that can be upgrade candidates to at least one package in the current set. Upgrade candidate has the same name, higher EVR and the architectures of the original and the upgrade candidate package are suitable for an upgrade. Specifically, the filtering does not take any steps to establish that the upgrade candidate can actually be installed.
Packit Service 21c75c
Packit Service 21c75c
.. module:: dnf.subject
Packit Service 21c75c
Packit Service 21c75c
.. class:: Subject
Packit Service 21c75c
Packit Service 21c75c
  As :ref:`explained on the DNF man page <specifying_packages-label>`, users of the CLI are able to select packages for an operation in different formats, leaving seemingly arbitrary parts out of the spec and even using globbing characters. This class implements a common approach to parsing such input and produce a :class:`~dnf.query.Query` listing all packages matching the input or a :class:`~dnf.selector.Selector` selecting a single package that best matches the input given a transaction operation.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: __init__(pkg_spec, ignore_case=False)
Packit Service 21c75c
Packit Service 21c75c
    Initialize the :class:`Subject` with `pkg_spec` input string with following :ref:`semantic <specifying_packages-label>`. If `ignore_case` is ``True`` ignore the case of characters in `pkg_spec`.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: get_best_query(sack, with_nevra=True, with_provides=True, with_filenames=True, forms=None)
Packit Service 21c75c
Packit Service 21c75c
    Returns a :class:`~Query` yielding packages matching the given input. The result of the returned
Packit Service 21c75c
    query can be an empty set if no package matches. `sack` is the :class:`~dnf.sack.Sack` that the
Packit Service 21c75c
    returned query will search. `with_nevra` enable search by nevra, `with_provides` indicates
Packit Service 21c75c
    whether besides package names also packages' provides are searched for a match, and
Packit Service 21c75c
    `with_filenames` indicates whether besides package provides also packages' file provides are
Packit Service 21c75c
    searched for a match. `forms` is a list of pattern forms from `hawkey`_. Leaving the parameter
Packit Service 21c75c
    to ``None`` results in using a reasonable default list of forms.
Packit Service 21c75c
Packit Service 21c75c
  .. method:: get_best_selector(sack, forms=None, obsoletes=True, reponame=None)
Packit Service 21c75c
Packit Service 21c75c
    Returns a :class:`~dnf.selector.Selector` that will select a single best-matching package when
Packit Service 21c75c
    used in a transaction operation. `sack` and `forms` have the same meaning as in
Packit Service 21c75c
    :meth:`get_best_query`. If ``obsoletes``, selector will also contain packages that obsoletes
Packit Service 21c75c
    requested packages (default is True). If ``reponame``, the selection of available packages is
Packit Service 21c75c
    limited to packages from that repo (default is None).
Packit Service 21c75c
Packit Service 21c75c
  .. method:: get_nevra_possibilities(self, forms=None)
Packit Service 21c75c
Packit Service 21c75c
    Returns generator for every possible nevra. Each possible nevra is represented by NEVRA class
Packit Service 21c75c
    (libdnf) that has attributes name, epoch, version, release, arch. `forms` have the same
Packit Service 21c75c
    meaning as in :meth:`get_best_query`.
Packit Service 21c75c
Packit Service 21c75c
    Example how to use it when it is known that string could be full NEVRA or NEVR::
Packit Service 21c75c
Packit Service 21c75c
        #!/usr/bin/python3
Packit Service 21c75c
        import dnf
Packit Service 21c75c
        import hawkey
Packit Service 21c75c
Packit Service 21c75c
        nevra_string = "dnf-0:4.2.2-2.fc30.noarch"
Packit Service 21c75c
        subject = dnf.subject.Subject(nevra_string)
Packit Service 21c75c
        possible_nevra = subject.get_nevra_possibilities(
Packit Service 21c75c
            forms=[hawkey.FORM_NEVRA, hawkey.FORM_NEVR])
Packit Service 21c75c
Packit Service 21c75c
        for i,nevra in enumerate(possible_nevra):
Packit Service 21c75c
            print("Possibility {} for \"{}\":".format(i+1, nevra_string))
Packit Service 21c75c
            print("name: {}".format(nevra.name))
Packit Service 21c75c
            print("epoch: {}".format(nevra.epoch))
Packit Service 21c75c
            print("version: {}".format(nevra.version))
Packit Service 21c75c
            print("release: {}".format(nevra.release))
Packit Service 21c75c
            print("architecture: {}".format(nevra.arch))
Packit Service 21c75c
            print()