Skip to content

Instantly share code, notes, and snippets.

@benedicteb
Created March 22, 2016 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benedicteb/34f948f8c9834c04fd65 to your computer and use it in GitHub Desktop.
Save benedicteb/34f948f8c9834c04fd65 to your computer and use it in GitHub Desktop.
This is a script for fixing categories in a munki repo. Call it with the path to your repo.
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
This is a script for fixing categories in a munki repo. Call it with the path to
your repo.
./munki_fix_categories.py fix --munki-repo path/to/munki/repo
It will set the same category on items with the same name. If there are items
not categorized after the fix and the interactive flag is not disabled the
script will go into an input loop where it will ask the user for a category to
all those items missing a category.
You can also use this script to set the category for a single item.
./munki_fix_categories.py setcat --munki-repo path/to/munki/repo --pkginfo path/to/pkginfo --category "Category to set"
(Remember to run makecatalogs after using this)
@author Benedicte Emilie Brækken
"""
import os
import os.path as op
import sys
import argparse
from plistlib import readPlist, writePlist
def set_category(path, category):
plist = readPlist(path)
plist['category'] = category
writePlist(plist, path)
def fix(munki_repo):
# Get a dictionary of all categories
categories = {}
for root, dirs, files in os.walk(op.join(munki_repo, "pkgsinfo")):
for pkginfo in files:
path = op.join(root, pkginfo)
try:
plist = readPlist(path)
except Exception as e:
print 'ERROR: %s\n %s' % (str(e), path)
if 'category' in plist.keys() and plist['category'] != '':
if plist['category'] in categories.keys():
categories[plist['category']].append(plist['name'])
else:
categories[plist['category']] = [plist['name']]
# Walk again to fix categories
missing = []
for root, dirs, files in os.walk(op.join(munki_repo, "pkgsinfo")):
for pkginfo in files:
path = op.join(root, pkginfo)
try:
plist = readPlist(path)
except Exception as e:
print 'ERROR: %s\n %s' % (str(e), path)
if not 'category' in plist.keys() or plist['category'] == '':
for key,items in categories.iteritems():
if plist['name'] in items:
set_category(path, key)
break
if not 'category' in plist.keys() or plist['category'] == '':
# These will be items that didn't get categorized
missing.append(path)
return missing
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('operation', type=str, choices=['fix', 'setcat'],
help='Operation to run')
parser.add_argument('--munki-repo', type=str, required=True,
help='Path to munki repo root')
parser.add_argument('--pkginfo', type=str,
help='Path to a pkginfo')
parser.add_argument('-c', '--category', type=lambda s: unicode(s, 'utf8'),
help='Category to set')
parser.add_argument('-i', '--interactive', action='store_true',
default=True, help='Do the interactive cleanup.')
args = parser.parse_args()
if args.operation == 'fix':
missing = fix(args.munki_repo)
# Do interactive cleanup
if args.interactive:
while len(missing) > 0:
for path in missing:
print '%d missing\n %s' % (len(missing), path)
new_category = unicode(raw_input(' Category: '), 'utf8')
set_category(path, new_category)
break
missing = fix(args.munki_repo)
elif args.operation == 'setcat':
if not args.pkginfo or not args.category:
print 'No pkginfo or category given.'
sys.exit(1)
set_category(op.join(args.munki_repo,args.pkginfo), args.category)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment