Blame doc/api_base.rst

Packit 6f3914
..
Packit 6f3914
  Copyright (C) 2014-2018 Red Hat, Inc.
Packit 6f3914
Packit 6f3914
  This copyrighted material is made available to anyone wishing to use,
Packit 6f3914
  modify, copy, or redistribute it subject to the terms and conditions of
Packit 6f3914
  the GNU General Public License v.2, or (at your option) any later version.
Packit 6f3914
  This program is distributed in the hope that it will be useful, but WITHOUT
Packit 6f3914
  ANY WARRANTY expressed or implied, including the implied warranties of
Packit 6f3914
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 6f3914
  Public License for more details.  You should have received a copy of the
Packit 6f3914
  GNU General Public License along with this program; if not, write to the
Packit 6f3914
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6f3914
  02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 6f3914
  source code or documentation are not subject to the GNU General Public
Packit 6f3914
  License and may only be used or replicated with the express permission of
Packit 6f3914
  Red Hat, Inc.
Packit 6f3914
Packit 6f3914
===================================
Packit 6f3914
 ``Base``---The centerpiece of DNF
Packit 6f3914
===================================
Packit 6f3914
Packit 6f3914
.. class:: dnf.Base
Packit 6f3914
Packit 6f3914
  Instances of :class:`dnf.Base` are the central point of functionality supplied by DNF. An application will typically create a single instance of this class which it will keep for the runtime needed to accomplish its packaging tasks. Plugins are managed by DNF and get a reference to :class:`dnf.Base` object when they run.
Packit 6f3914
Packit 6f3914
  :class:`.Base` instances are stateful objects holding references to various data sources and data sinks. To properly finalize and close off any handles the object may hold, client code should either call :meth:`.Base.close` when it has finished operations with the instance, or use the instance as a context manager. After the object has left the context, or its :meth:`.Base.close` has been called explicitly, it must not be used. :meth:`.Base.close` will delete all downloaded packages upon successful transaction.
Packit 6f3914
Packit 6f3914
  .. attribute:: comps
Packit 6f3914
Packit 6f3914
    Is ``None`` by default. Explicit load via :meth:`read_comps`  initializes this attribute to a :class:`dnf.comps.Comps` instance.
Packit 6f3914
Packit 6f3914
  .. attribute:: conf
Packit 6f3914
Packit 6f3914
    An instance of :class:`dnf.conf.Conf`, concentrates all the different configuration options. :meth:`__init__` initializes this to usable defaults.
Packit 6f3914
Packit 6f3914
  .. attribute:: repos
Packit 6f3914
Packit 6f3914
    A :class:`dnf.repodict.RepoDict` instance, this member object contains all the repositories available.
Packit 6f3914
Packit 6f3914
  .. attribute:: sack
Packit 6f3914
Packit 6f3914
    The :class:`Sack<dnf.sack.Sack>` that this :class:`Base<dnf.Base>` object is using. It needs to be explicitly initialized by :meth:`fill_sack`.
Packit 6f3914
Packit 6f3914
  .. attribute:: transaction
Packit 6f3914
Packit 6f3914
    A resolved transaction object, a :class:`dnf.transaction.Transaction` instance, or ``None`` if no transaction has been prepared yet.
Packit 6f3914
Packit 6f3914
  .. method:: __init__()
Packit 6f3914
Packit 6f3914
    Init an instance with a reasonable default configuration. The constructor takes no arguments.
Packit 6f3914
Packit 6f3914
  .. method:: add_remote_rpms(path_list, strict=True, progress=None)
Packit 6f3914
Packit 6f3914
    Add RPM files at list `path_list` to the :attr:`sack` and return the list of respective
Packit 6f3914
    :class:`dnf.package.Package` instances. Does the download to a temporary files for each path if
Packit 6f3914
    `path` is a remote URL. Raises :exc:`IOError` if there are problems obtaining during reading
Packit 6f3914
    files and `strict=True`. `progress`, if given, should be a :class:`.DownloadProgress` and can be
Packit 6f3914
    used by the caller to monitor the progress of the download.
Packit 6f3914
Packit 6f3914
  .. method:: close()
Packit 6f3914
Packit 6f3914
    Close all external handles the object holds. This is called automatically via context manager mechanism if the instance is handled using the ``with`` statement.
Packit 6f3914
Packit 6f3914
  .. method:: init_plugins([disabled_glob=None, cli=None])
Packit 6f3914
Packit 6f3914
     Initialize plugins. If you want to disable some plugins pass the list of their name patterns to
Packit 6f3914
     `disabled_glob`. When run from interactive script then also pass your :class:`dnf.cli.Cli` instance.
Packit 6f3914
Packit 6f3914
  .. method:: pre_configure_plugins()
Packit 6f3914
Packit 6f3914
     Configure plugins by running their pre_configure() method. It makes possible to change
Packit 6f3914
     variables before repo files and rpmDB are loaded. It also makes possible to create internal
Packit 6f3914
     repositories that will be affected by ``--disablerepo`` and ``--enablerepo``.
Packit 6f3914
Packit 6f3914
  .. method:: configure_plugins()
Packit 6f3914
Packit 6f3914
     Configure plugins by running their configure() method.
Packit 6f3914
Packit 6f3914
  .. method:: fill_sack([load_system_repo=True, load_available_repos=True])
Packit 6f3914
Packit 6f3914
    Setup the package sack. If `load_system_repo` is ``True``, load information about packages in the local RPMDB into the sack. Else no package is considered installed during dependency solving. If `load_available_repos` is ``True``, load information about packages from the available repositories into the sack.
Packit 6f3914
Packit 6f3914
    This operation will call :meth:`load() <dnf.repo.Repo.load>` for repos as necessary and can take a long time. Adding repositories or changing repositories' configuration does not affect the information within the sack until :meth:`fill_sack` has been called.
Packit 6f3914
Packit 6f3914
    Before this method is invoked, the client application should setup any explicit configuration relevant to the operation. This will often be at least :attr:`conf.cachedir <.Conf.cachedir>` and the substitutions used in repository URLs. See :attr:`.Conf.substitutions`.
Packit 6f3914
Packit 6f3914
    Throws `IOError` exception in case cached metadata could not be opened.
Packit 6f3914
Packit 6f3914
    Example::
Packit 6f3914
Packit 6f3914
        #!/usr/bin/python3
Packit 6f3914
        import dnf
Packit 6f3914
Packit 6f3914
        base = dnf.Base()
Packit 6f3914
        conf = base.conf
Packit 6f3914
        conf.cachedir = '/tmp/my_cache_dir'
Packit 6f3914
        conf.substitutions['releasever'] = '30'
Packit 6f3914
        conf.substitutions['basearch'] = 'x86_64'
Packit 6f3914
Packit 6f3914
        base.repos.add_new_repo('my-repo', conf,
Packit 6f3914
            baseurl=["http://download.fedoraproject.org/pub/fedora/linux/releases/$releasever/Everything/$basearch/os/"])
Packit 6f3914
        base.fill_sack()
Packit 6f3914
Packit 6f3914
        print("Enabled repositories:")
Packit 6f3914
        for repo in base.repos.iter_enabled():
Packit 6f3914
            print("id: {}".format(repo.id))
Packit 6f3914
            print("baseurl: {}".format(repo.baseurl))
Packit 6f3914
Packit 6f3914
Packit 6f3914
  .. method:: do_transaction([display])
Packit 6f3914
Packit 6f3914
    Perform the resolved transaction. Use the optional `display` object(s) to report the progress. `display` can be either an instance of a subclass of :class:`dnf.callback.TransactionProgress` or a sequence of such instances. Raise :exc:`dnf.exceptions.Error` or dnf.exceptions.TransactionCheckError.
Packit 6f3914
Packit 6f3914
  .. method:: download_packages(pkglist, progress=None, callback_total=None)
Packit 6f3914
Packit 6f3914
    Download packages in `pkglist` from remote repositories. Packages from local repositories or from the command line are not downloaded. `progress`, if given, should be a :class:`.DownloadProgress` and can be used by the caller to monitor the progress of the download. `callback_total` is a function accepting two parameters: total size of the downloaded content in bytes and time when the download process started, in seconds since the epoch. Raises :exc:`.DownloadError` if some packages failed to download.
Packit 6f3914
Packit 6f3914
  .. method:: group_install(group_id, pkg_types, exclude=None, strict=True)
Packit 6f3914
Packit 6f3914
    Mark group with corresponding `group_id` installed and mark the packages in the group for installation. Return the number of packages that the operation has marked for installation. `pkg_types` is a sequence of strings determining the kinds of packages to be installed, where the respective groups can be selected by including ``"mandatory"``, ``"default"`` or ``"optional"`` in it. If `exclude` is given, it has to be an iterable of package name glob patterns: :meth:`.group_install` will then not mark the respective packages for installation whenever possible. Parameter `strict` is a boolean indicating whether group packages that exist but are non-installable due to e.g. dependency issues should be skipped (False) or cause transaction to fail to resolve (True).
Packit 6f3914
Packit 6f3914
  .. method:: group_remove(group_id)
Packit 6f3914
Packit 6f3914
    Mark group with corresponding `group_id` not installed. All the packages marked as belonging to this group will be marked for removal. Return the number of packages marked for removal in this call.
Packit 6f3914
Packit 6f3914
  .. method:: group_upgrade(group_id)
Packit 6f3914
Packit 6f3914
    Upgrade group with corresponding `group_id`. If there has been packages added to the group's comps information since installing on the system, they will be marked for installation. Similarly, removed packages get marked for removal. The remaining packages in the group are marked for an upgrade. The operation respects the package types from the original installation of the group.
Packit 6f3914
Packit 6f3914
  .. method:: environment_install(env_id, types, exclude=None, strict=True, exclude_groups=None)
Packit 6f3914
Packit 6f3914
    Similar to :meth:`.group_install` but operates on environmental groups. `exclude_groups` is an iterable of group IDs that will not be marked as installed.
Packit 6f3914
Packit 6f3914
  .. method:: environment_remove(env_id)
Packit 6f3914
Packit 6f3914
    Similar to :meth:`.group_remove` but operates on environmental groups.
Packit 6f3914
Packit 6f3914
  .. method:: environment_upgrade(env_id)
Packit 6f3914
Packit 6f3914
    Similar to :meth:`.group_upgrade` but operates on environmental groups.
Packit 6f3914
Packit 6f3914
  .. method:: read_all_repos()
Packit 6f3914
Packit 6f3914
    Read repository configuration from the main configuration file specified by :attr:`dnf.conf.Conf.config_file_path` and any ``.repo`` files under :attr:`dnf.conf.Conf.reposdir`. All the repositories found this way are added to :attr:`~.Base.repos`.
Packit 6f3914
Packit 6f3914
  .. method:: read_comps(arch_filter=False)
Packit 6f3914
Packit 6f3914
    Read comps data from all the enabled repositories and initialize the :attr:`comps` object. If `arch_filter` is set to ``True``, the result is limited to system basearch.
Packit 6f3914
Packit 6f3914
  .. method:: reset(\*\*kwargs)
Packit 6f3914
Packit 6f3914
    Reset the state of different :class:`.Base` attributes. Selecting attributes to reset is controlled by passing the method keyword arguments set to ``True``. When called with no arguments the method has no effect.
Packit 6f3914
Packit 6f3914
    =============== =================================================
Packit 6f3914
    argument passed effect
Packit 6f3914
    =============== =================================================
Packit 6f3914
    `goal=True`     drop all the current :ref:`packaging requests <package_marking-label>`
Packit 6f3914
    `repos=True`    drop the current repositories (see :attr:`.repos`). This won't
Packit 6f3914
                    affect the package data already loaded into the :attr:`.sack`.
Packit 6f3914
    `sack=True`     drop the current sack (see :attr:`.sack`)
Packit 6f3914
    =============== =================================================
Packit 6f3914
Packit 6f3914
  .. method:: resolve(allow_erasing=False)
Packit 6f3914
Packit 6f3914
    Resolve the marked requirements and store the resulting :class:`dnf.transaction.Transaction` into :attr:`transaction`. Raise :exc:`dnf.exceptions.DepsolveError` on a depsolving error. Return ``True`` if the resolved transaction is non-empty.
Packit 6f3914
Packit 6f3914
    Enabling `allow_erasing` lets the solver remove other packages while looking to fulfill the current packaging requests. For instance, this is used to allow the solver to remove dependants of a package being removed.
Packit 6f3914
Packit 6f3914
    The exact operation of the solver further depends on the :attr:`dnf.conf.Conf.best` setting.
Packit 6f3914
Packit 6f3914
  .. method:: update_cache(timer=False)
Packit 6f3914
Packit 6f3914
    Downloads and caches in binary format metadata for all known repos. Tries to avoid downloading
Packit 6f3914
    whenever possible (e.g. when the local metadata hasn’t expired yet or when the metadata
Packit 6f3914
    timestamp hasn’t changed).
Packit 6f3914
Packit 6f3914
    If 'timer' equals 'True', DNF becomes more resource-aware, meaning DNF will not do anything if
Packit 6f3914
    running on battery power and will terminate immediately if it’s too soon after the last
Packit 6f3914
    successful update_cache operation.
Packit 6f3914
Packit 6f3914
    When the method is used after :meth:`fill_sack`, information about packages will not be updated.
Packit 6f3914
Packit 6f3914
  .. _package_marking-label:
Packit 6f3914
Packit 6f3914
  The :class:`.Base` class provides a number of methods to make packaging requests that can later be resolved and turned into a transaction. The `pkg_spec` argument some of them take must be a package specification recognized by :class:`dnf.subject.Subject`. If these methods fail to find suitable packages for the operation they raise a :exc:`~dnf.exceptions.MarkingError`. Note that successful completion of these methods does not necessarily imply that the desired transaction can be carried out (e.g. for dependency reasons).
Packit 6f3914
Packit 6f3914
  .. method:: downgrade(pkg_spec)
Packit 6f3914
Packit 6f3914
    Mark packages matching `pkg_spec` for downgrade.
Packit 6f3914
Packit 6f3914
  .. method:: install(pkg_spec, reponame=None, strict=True, forms=None)
Packit 6f3914
Packit 6f3914
    Mark packages matching `pkg_spec` for installation. 
Packit 6f3914
    `reponame` can be a name of a repository or a list of repository names. If given, the selection of available packages is limited to packages from these repositories. If strict is set to False, the installation ignores packages with dependency solving problems. Parameter `forms` has the same meaning as in :meth:`dnf.subject.Subject.get_best_query`.
Packit 6f3914
Packit 6f3914
  .. method:: package_downgrade(pkg, strict=False)
Packit 6f3914
Packit 6f3914
    If `pkg` is a :class:`dnf.package.Package` in an available repository, mark the matching installed package for downgrade to `pkg`. If strict=False it ignores problems with dep-solving.
Packit 6f3914
Packit 6f3914
  .. method:: package_install(pkg, strict=True)
Packit 6f3914
Packit 6f3914
    Mark `pkg` (a :class:`dnf.package.Package` instance) for installation. Ignores package that is already installed. `strict` has the same meaning as in :meth:`install`.
Packit 6f3914
Packit 6f3914
  .. method:: package_upgrade(pkg)
Packit 6f3914
Packit 6f3914
    If `pkg` is a :class:`dnf.package.Package` in an available repository, mark the matching installed package for upgrade to `pkg`.
Packit 6f3914
Packit 6f3914
  .. method:: autoremove()
Packit 6f3914
Packit 6f3914
    Removes all 'leaf' packages from the system that were originally installed as dependencies of user-installed packages but which are no longer required by any such package.
Packit 6f3914
Packit 6f3914
  .. method:: remove(pkg_spec, reponame=None, forms=None)
Packit 6f3914
Packit 6f3914
    Mark packages matching `pkg_spec` for removal. `reponame` and `forms` have the same meaning as in :meth:`install`.
Packit 6f3914
Packit 6f3914
  .. method:: upgrade(pkg_spec, reponame=None)
Packit 6f3914
Packit 6f3914
    Mark packages matching `pkg_spec` for upgrade. `reponame` has the same meaning as in :meth:`install`.
Packit 6f3914
Packit 6f3914
  .. method:: upgrade_all(reponame=None)
Packit 6f3914
Packit 6f3914
    Mark all installed packages for an upgrade. `reponame` has the same meaning as in :meth:`install`.
Packit 6f3914
Packit 6f3914
  .. method:: urlopen(url, repo=None, mode='w+b', \*\*kwargs):
Packit 6f3914
Packit 6f3914
    Open the specified absolute `url` and return a file object which respects proxy setting even for non-repo downloads
Packit 6f3914
Packit 6f3914
  .. method:: install_specs(install, exclude=None, reponame=None, strict=True, forms=None)
Packit 6f3914
Packit 6f3914
    Provides unified way to mark packages, groups or modules for installation. The `install` and `exclude` arguments have to be iterables containing specifications of packages (e.g. 'dnf') or groups/modules (e.g. '\@core'). Specifications from the `exclude` list will not be marked for installation. The `reponame`, `strict` and `forms` parameters have the same meaning as in :meth:`install`. In case of errors the method raises :exc:`dnf.exceptions.MarkingErrors`.
Packit 6f3914
Packit 6f3914
    Example to install two groups and a package::
Packit 6f3914
Packit 6f3914
        #!/usr/bin/python3
Packit 6f3914
        import dnf
Packit 6f3914
        import dnf.cli.progress
Packit 6f3914
Packit 6f3914
        base = dnf.Base()
Packit 6f3914
        base.read_all_repos()
Packit 6f3914
        base.fill_sack()
Packit 6f3914
Packit 6f3914
        base.install_specs(['acpi', '@Web Server', '@core'])
Packit 6f3914
        print("Resolving transaction...",)
Packit 6f3914
        base.resolve()
Packit 6f3914
        print("Downloading packages...")
Packit 6f3914
        progress = dnf.cli.progress.MultiFileProgressMeter()
Packit 6f3914
        base.download_packages(base.transaction.install_set, progress)
Packit 6f3914
        print("Installing...")
Packit 6f3914
        base.do_transaction()