Skip to content

Commit

Permalink
Add support for --userinstalled for repoquery command (RhBug:1278124)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrohel authored and DNF Bot committed May 3, 2017
1 parent ef436ec commit bb5ed9a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
9 changes: 6 additions & 3 deletions dnf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,14 @@ def _sltr_matches_installed(self, sltr):
inst = inst.filter(pkg=sltr.matches())
return list(inst)

def _is_userinstalled(self, pkg):
"""Returns true if the package is installed by user."""
return self._yumdb.get_package(pkg).get('reason') == 'user' and \
self._yumdb.get_package(pkg).get('from_repo') != 'anakonda'

def iter_userinstalled(self):
"""Get iterator over the packages installed by the user."""
return (pkg for pkg in self.sack.query().installed()
if self._yumdb.get_package(pkg).get('reason') == 'user' and
self._yumdb.get_package(pkg).get('from_repo') != 'anakonda')
return (pkg for pkg in self.sack.query().installed() if self._is_userinstalled(pkg))

def _run_hawkey_goal(self, goal, allow_erasing):
ret = goal.run(
Expand Down
55 changes: 30 additions & 25 deletions dnf/cli/commands/repoquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ def set_argparser(parser):
'extras': _('Display only packages that are not present in any of available repositories.'),
'upgrades': _('Display only packages that provide an upgrade for some already installed package.'),
'unneeded': _('Display only packages that can be removed by "dnf autoremove" command.'),
'userinstalled': _('Display only packages that were installed by user.')
}
list_group = parser.add_mutually_exclusive_group()
for list_arg in ('installed', 'extras', 'upgrades', 'unneeded'):
for list_arg in ('installed', 'extras', 'upgrades', 'unneeded', 'userinstalled'):
switch = '--%s' % list_arg
list_group.add_argument(switch, dest='list', action='store_const',
const=list_arg, help=help_list[list_arg])
Expand All @@ -241,7 +242,8 @@ def configure(self):
if self.opts.srpm:
self.base.repos.enable_source_repos()

if (self.opts.pkgfilter != "installonly" and self.opts.list != "installed") or self.opts.available:
if (self.opts.list not in ["installed", "userinstalled"] and
self.opts.pkgfilter != "installonly") or self.opts.available:
demands.available_repos = True

demands.sack_activation = True
Expand Down Expand Up @@ -322,7 +324,7 @@ def run(self):
"--available", "--" + self.opts.list)))
elif self.opts.list == "unneeded":
q = q._unneeded(self.base.sack, self.base._yumdb)
elif self.opts.list:
elif self.opts.list and self.opts.list != 'userinstalled':
q = getattr(q, self.opts.list)()

if self.opts.pkgfilter == "duplicated":
Expand Down Expand Up @@ -405,9 +407,10 @@ def run(self):
pkgs = set()
if self.opts.packageatr:
for pkg in q.run():
rels = getattr(pkg, OPTS_MAPPING[self.opts.packageatr])
for rel in rels:
pkgs.add(str(rel))
if self.opts.list != 'userinstalled' or self.base._is_userinstalled(pkg):
rels = getattr(pkg, OPTS_MAPPING[self.opts.packageatr])
for rel in rels:
pkgs.add(str(rel))
elif self.opts.location:
for pkg in q.run():
location = pkg.remote_location()
Expand All @@ -416,29 +419,31 @@ def run(self):
elif self.opts.deplist:
pkgs = []
for pkg in sorted(set(q.run())):
deplist_output = []
deplist_output.append('package: ' + str(pkg))
for req in sorted([str(req) for req in pkg.requires]):
deplist_output.append(' dependency: ' + req)
subject = dnf.subject.Subject(req)
query = subject.get_best_query(self.base.sack)
query = self.filter_repo_arch(
self.opts, query.available())
if not self.opts.verbose:
query = query.latest()
for provider in query.run():
deplist_output.append(' provider: ' + str(provider))
pkgs.append('\n'.join(deplist_output))
if self.opts.list != 'userinstalled' or self.base._is_userinstalled(pkg):
deplist_output = []
deplist_output.append('package: ' + str(pkg))
for req in sorted([str(req) for req in pkg.requires]):
deplist_output.append(' dependency: ' + req)
subject = dnf.subject.Subject(req)
query = subject.get_best_query(self.base.sack)
query = self.filter_repo_arch(
self.opts, query.available())
if not self.opts.verbose:
query = query.latest()
for provider in query.run():
deplist_output.append(' provider: ' + str(provider))
pkgs.append('\n'.join(deplist_output))
print('\n\n'.join(pkgs))
return
else:
for pkg in q.run():
try:
pkgs.add(self.build_format_fn(self.opts, pkg))
except AttributeError as e:
# catch that the user has specified attributes
# there don't exist on the dnf Package object.
raise dnf.exceptions.Error(str(e))
if self.opts.list != 'userinstalled' or self.base._is_userinstalled(pkg):
try:
pkgs.add(self.build_format_fn(self.opts, pkg))
except AttributeError as e:
# catch that the user has specified attributes
# there don't exist on the dnf Package object.
raise dnf.exceptions.Error(str(e))
if self.opts.resolve:
# find the providing packages and show them
query = self.filter_repo_arch(
Expand Down
5 changes: 5 additions & 0 deletions doc/command_ref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,11 @@ resulting packages matching the specification. All packages are considered if no
``--upgrades``
Limit the resulting set to packages that provide an upgrade for some already installed package.

``--userinstalled``
Limit the resulting set to packages instaled by user. The :ref:`exclude <exclude-label>` option
in configuration file (.conf) might influence the result, but if the command line option \-\
:ref:`-disableexcludes <disableexcludes-label>` is used, it ensures that all installed packages will be listed.

``--whatconflicts <capability>``
Limit the resulting set only to packages that conflict ``<capability>``.

Expand Down
6 changes: 5 additions & 1 deletion doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
DNF Release Notes
###################

.. contents::
API additions in :

DNF command additions in :

* ``dnf [options] repoquery --userinstalled`` limit the resulting set only to packages installed by user.

===================
2.4.0 Release Notes
Expand Down

0 comments on commit bb5ed9a

Please sign in to comment.