dnf-qubes-hooks.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # coding=utf-8
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2015 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. from __future__ import absolute_import
  22. from distutils.version import LooseVersion
  23. import logging
  24. import dnf
  25. import dnf.const
  26. import subprocess
  27. PLUGIN_CONF = 'qubes-hooks'
  28. class QubesHooks(dnf.Plugin):
  29. name = 'qubes-hooks'
  30. def __init__(self, base, cli):
  31. super(QubesHooks, self).__init__(base, cli)
  32. self.base = base
  33. self.log = logging.getLogger('dnf')
  34. def resolved(self):
  35. # in case of no action to do, transaction() hook won't be called;
  36. # report updates availability here - especially when everything is up
  37. # to date - to clear updates-available flag
  38. if not self.base.transaction:
  39. query = self.base.sack.query()
  40. query = query.upgrades()
  41. updates = set(query.run())
  42. subprocess.call([
  43. '/usr/lib/qubes/qrexec-client-vm',
  44. 'dom0',
  45. 'qubes.NotifyUpdates',
  46. '/bin/echo',
  47. str(len(updates))
  48. ])
  49. def transaction(self):
  50. if LooseVersion(dnf.const.VERSION) < '2.0.0':
  51. config = self.read_config(self.base.conf, PLUGIN_CONF)
  52. else:
  53. config = self.read_config(self.base.conf)
  54. if config.getboolean('main', 'notify-updates'):
  55. if LooseVersion(dnf.const.VERSION) > '4.0.0':
  56. # self.base.transaction empty at this point in dnf4
  57. # https://bugzilla.redhat.com/1650446
  58. # until fixed, load the repositories again and again check for
  59. # updates
  60. base = dnf.Base()
  61. base.read_all_repos()
  62. base.fill_sack()
  63. query = base.sack.query()
  64. query = query.upgrades()
  65. updates = set(query.run())
  66. else:
  67. # Get all updates available _before_ this transaction
  68. query = self.base.sack.query()
  69. query = query.upgrades()
  70. updates = set(query.run())
  71. # Get packages installed in this transaction...
  72. just_installed = self.base.transaction
  73. # ...and filter them out of available updates
  74. for item in just_installed:
  75. for pkg in item.installs():
  76. updates.discard(pkg)
  77. subprocess.call([
  78. '/usr/lib/qubes/qrexec-client-vm',
  79. 'dom0',
  80. 'qubes.NotifyUpdates',
  81. '/bin/echo',
  82. str(len(updates))
  83. ])
  84. self.log.info("Notifying dom0 about installed applications")
  85. subprocess.call(['/etc/qubes-rpc/qubes.PostInstall'])