|
| 1 | +# supplies the 'diff' command. |
| 2 | +# |
| 3 | +# Copyright (C) 2018 Red Hat, Inc. |
| 4 | +# |
| 5 | +# This copyrighted material is made available to anyone wishing to use, |
| 6 | +# modify, copy, or redistribute it subject to the terms and conditions of |
| 7 | +# the GNU General Public License v.2, or (at your option) any later version. |
| 8 | +# This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | +# ANY WARRANTY expressed or implied, including the implied warranties of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 11 | +# Public License for more details. You should have received a copy of the |
| 12 | +# GNU General Public License along with this program; if not, write to the |
| 13 | +# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 14 | +# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the |
| 15 | +# source code or documentation are not subject to the GNU General Public |
| 16 | +# License and may only be used or replicated with the express permission of |
| 17 | +# Red Hat, Inc. |
| 18 | + |
| 19 | +from __future__ import print_function |
| 20 | +import os |
| 21 | +import dnf |
| 22 | +import rpm |
| 23 | +import subprocess |
| 24 | + |
| 25 | +import logging |
| 26 | +from dnfpluginsextras import _, logger |
| 27 | + |
| 28 | +@dnf.plugin.register_command |
| 29 | +class DiffCommand(dnf.cli.Command): |
| 30 | + aliases = ("diff",) |
| 31 | + summary = "Do blah." |
| 32 | + usage = "ahoj" |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def set_argparser(parser): |
| 36 | + parser.add_argument('pkg') |
| 37 | + parser.add_argument('file', nargs='*') |
| 38 | + |
| 39 | + def configure(self): |
| 40 | + # per https://bugzilla.redhat.com/1358245 |
| 41 | + self.cli.redirect_logger(stdout=logging.WARNING, stderr=logging.INFO) |
| 42 | + self.base.conf.destdir = os.path.join( |
| 43 | + os.path.expanduser("~"), |
| 44 | + '.cache', |
| 45 | + 'dnf-diff', |
| 46 | + ) |
| 47 | + |
| 48 | + def _resolve_local_package(self, name): |
| 49 | + self.base.fill_sack() |
| 50 | + subj = dnf.subject.Subject(name) |
| 51 | + q = subj.get_best_query(self.base.sack) |
| 52 | + q = q.available() |
| 53 | + q = q.latest() |
| 54 | + if len(q.run()) == 0: |
| 55 | + msg = _("No package %s available.") % (name) |
| 56 | + raise dnf.exceptions.PackageNotFoundError(msg) |
| 57 | + |
| 58 | + return list(q) |
| 59 | + |
| 60 | + |
| 61 | + def _diff_package(self, package, package_name, files): |
| 62 | + for fname in files: |
| 63 | + if not fname in package.files: |
| 64 | + # Should not happen? |
| 65 | + logger.error(_("file '{0}' not found in '{1}'".format(fname, |
| 66 | + package_name))) |
| 67 | + continue |
| 68 | + |
| 69 | + rpm_file_name = '{name}-{version}-{release}.{arch}.rpm'.format( |
| 70 | + name=package.name, |
| 71 | + version=package.version, |
| 72 | + release=package.release, |
| 73 | + arch=package.arch |
| 74 | + ) |
| 75 | + |
| 76 | + subprocess.call(['/usr/libexec/dnf-diff-rpm-filename', |
| 77 | + rpm_file_name, |
| 78 | + fname]) |
| 79 | + |
| 80 | + def _list_of_changed_files(self, package): |
| 81 | + proc = subprocess.Popen( |
| 82 | + ['/usr/libexec/dnf-diff-changed-files', package], |
| 83 | + stdout=subprocess.PIPE, |
| 84 | + ) |
| 85 | + lines = [x.decode('ascii').rstrip() for x in proc.stdout.readlines()] |
| 86 | + return lines |
| 87 | + |
| 88 | + def run(self): |
| 89 | + to_download = self._resolve_local_package(self.opts.pkg) |
| 90 | + self.base.download_packages(to_download) |
| 91 | + |
| 92 | + for pkg in to_download: |
| 93 | + check_pkg = '{0}.{1}'.format(pkg.name, pkg.arch) |
| 94 | + if self.opts.file: |
| 95 | + self._diff_package(pkg, check_pkg, self.opts.file) |
| 96 | + else: |
| 97 | + self._diff_package(pkg, check_pkg, |
| 98 | + self._list_of_changed_files(check_pkg)) |
0 commit comments