beblog

Find orphaned munki pkgs with Python

~ January 02 2017

If you use munki to serve software to your Macs, and autopkg to automatically fetch new updates, you might end up with a bit of an oversized munki-repo after a while.

This could be due to some automation gone wrong downloading duplicate pkgs without adding pkginfo-files. Or it could be a colleague who hasn’t had their coffee yet munkiimporting something without committing the pkginfo-file or it could be a lot of things!

For these cases you could use a simple Python-script for traversing the pkgs-directory, and checking every pkginfo-file for whether or not that pkg is used anywhere. Such a script could look something like this.

This can easily be used together with other unix-tools like grep and rm to filter and delete the pkgs you want.

$ ./find_orphaned_pkgs.py | grep -ie spotify
./pkgs/free/Spotify-1.0.7.157.g2a6526f9.dmg
./pkgs/free/Spotify-0.9.14.11.g7e298e37.dmg
./pkgs/free/Spotify-0.9.14.13.gba5645ad.dmg
./pkgs/free/Spotify-0.9.10.21.g22fbdb39.dmg
./pkgs/free/Spotify-1.0.4.89.g22b04e33.dmg
./pkgs/free/Spotify-1.0.47.13.gd8e05b1f.dmg
./pkgs/free/Spotify-1.0.43.123.g80176796__1.dmg
./pkgs/free/Spotify-1.0.9.133.gcedaee38.dmg
./pkgs/free/Spotify-1.0.27.75.gdc223232__2.dmg
./pkgs/free/Spotify-0.9.11.27.g2b1a638c.dmg
./pkgs/free/Spotify-1.0.43.123.g80176796.dmg
./pkgs/free/Spotify-1.0.20.94.g8f8543b3__1.dmg
./pkgs/free/Spotify-1.0.27.75.gdc223232__1.dmg

$ ./find_orphaned_pkgs.py | grep -ie spotify | xargs rm

$ ./find_orphaned_pkgs.py | grep -ie spotify