1# sack.py
2# The dnf.Sack class, derived from hawkey.Sack
3#
4# Copyright (C) 2012-2016 Red Hat, Inc.
5#
6# This copyrighted material is made available to anyone wishing to use,
7# modify, copy, or redistribute it subject to the terms and conditions of
8# the GNU General Public License v.2, or (at your option) any later version.
9# This program is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY expressed or implied, including the implied warranties of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
12# Public License for more details.  You should have received a copy of the
13# GNU General Public License along with this program; if not, write to the
14# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
16# source code or documentation are not subject to the GNU General Public
17# License and may only be used or replicated with the express permission of
18# Red Hat, Inc.
19#
20
21from __future__ import absolute_import
22from __future__ import unicode_literals
23import dnf.util
24import dnf.package
25import dnf.query
26import logging
27import hawkey
28import os
29from dnf.pycomp import basestring
30from dnf.i18n import _
31
32logger = logging.getLogger("dnf")
33
34class Sack(hawkey.Sack):
35    # :api
36
37    def __init__(self, *args, **kwargs):
38        super(Sack, self).__init__(*args, **kwargs)
39
40    def _configure(self, installonly=None, installonly_limit=0, allow_vendor_change=None):
41        if installonly:
42            self.installonly = installonly
43        self.installonly_limit = installonly_limit
44        if allow_vendor_change is not None:
45            self.allow_vendor_change = allow_vendor_change
46            if allow_vendor_change is False:
47                logger.warning(_("allow_vendor_change is disabled. This option is currently not supported for downgrade and distro-sync commands"))
48
49    def query(self, flags=0):
50        # :api
51        """Factory function returning a DNF Query."""
52        return dnf.query.Query(self, flags)
53
54
55def _build_sack(base):
56    cachedir = base.conf.cachedir
57    # create the dir ourselves so we have the permissions under control:
58    dnf.util.ensure_dir(cachedir)
59    return Sack(pkgcls=dnf.package.Package, pkginitval=base,
60                arch=base.conf.substitutions["arch"],
61                cachedir=cachedir, rootdir=base.conf.installroot,
62                logfile=os.path.join(base.conf.logdir, dnf.const.LOG_HAWKEY),
63                logdebug=base.conf.logfilelevel > 9)
64
65
66def _rpmdb_sack(base):
67    # used by subscription-manager (src/dnf-plugins/product-id.py)
68    sack = _build_sack(base)
69    try:
70        # It can fail if rpmDB is not present
71        sack.load_system_repo(build_cache=False)
72    except IOError:
73        pass
74    return sack
75
76
77def rpmdb_sack(base):
78    # :api
79    """
80    Returns a new instance of sack containing only installed packages (@System repo)
81    Useful to get list of the installed RPMs after transaction.
82    """
83    return _rpmdb_sack(base)
84