Skip to content

Instantly share code, notes, and snippets.

@progandy
Last active July 27, 2017 18:33
Show Gist options
  • Save progandy/0a672083a373d69e819a508578c327b5 to your computer and use it in GitHub Desktop.
Save progandy/0a672083a373d69e819a508578c327b5 to your computer and use it in GitHub Desktop.
pacman hook to list new optional dependencies after an upgrade
[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = *
[Action]
Description = Preparing to detect new optional dependencies...
Depends = pyalpm
When = PreTransaction
Exec = /etc/pacman.d/display_optdepends.py pre
[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = *
[Action]
Description = Detecting new optional dependencies...
Depends = pyalpm
When = PostTransaction
Exec = /etc/pacman.d/display_optdepends.py post
#!/usr/bin/env python3
#
# pacman hook to list new optional dependencies
# requires pyalpm
# Copyright (C) 2017 A. Bosch <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import sys
from pycman import config
import pyalpm
import json
import os.path
def get_optdeps(hpm):
odeps={}
for pkg in hpm.get_localdb().pkgcache:
for odep in pkg.optdepends:
name, sep, desc = odep.partition(':')
odeps.setdefault(name.strip(), {})[pkg.name] = desc.strip()
return odeps
def get_installed_str(hlist, name):
if pyalpm.find_satisfier(hlist, name):
return "[installed]"
return ""
def main():
hpm = config.init_with_config("/etc/pacman.conf")
cachefile= hpm.dbpath.rstrip('/') + '/local_optdeps.json'
if len(sys.argv) != 2:
return
if sys.argv[1] == "pre":
if not os.path.isfile(cachefile):
with open(cachefile, 'w') as f:
json.dump(get_optdeps(hpm), f)
elif sys.argv[1] == "post":
newdeps = get_optdeps(hpm)
olddeps = {}
with open(cachefile, 'r') as f:
olddeps = json.load(f)
with open(cachefile, 'w') as f:
json.dump(newdeps, f)
hinst = hpm.get_localdb().pkgcache
for odep in newdeps:
first = True
for request in newdeps[odep]:
if odep not in olddeps or request not in olddeps[odep]:
if first:
print(odep, get_installed_str(hinst, odep), "suggested by:")
first = False
print(" ", request, ":", newdeps[odep][request])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment