From ce199f9b9d2cd56c25490baf7dfc17fd5ddfd278 Mon Sep 17 00:00:00 2001 From: Justin L R Graham Date: Thu, 23 Oct 2014 23:40:32 -0500 Subject: [PATCH] Move common icon installation code to a function. --- code/client/munkiimport | 65 +++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/code/client/munkiimport b/code/client/munkiimport index 0f7af753..d54b31b0 100755 --- a/code/client/munkiimport +++ b/code/client/munkiimport @@ -231,14 +231,6 @@ def iconExistsInRepo(pkginfo): def generate_png_from_copy_from_dmg_item(dmg_path, pkginfo): '''Generates a product icon from a copy_from_dmg item and uploads to the repo''' - destination_path = os.path.join(REPO_PATH, 'icons') - if not os.path.exists(destination_path): - try: - os.makedirs(destination_path) - except OSError, errmsg: - print >> sys.stderr, ('Could not create %s: %s' % - (destination_path, errmsg)) - mountpoints = munkicommon.mountdmg(dmg_path) if mountpoints: mountpoint = mountpoints[0] @@ -248,14 +240,7 @@ def generate_png_from_copy_from_dmg_item(dmg_path, pkginfo): app_path = os.path.join(mountpoint, apps[0]['source_item']) icon_path = iconutils.findIconForApp(app_path) if icon_path: - png_path = os.path.join( - destination_path, pkginfo['name'] + u'.png') - result = iconutils.convertIconToPNG(icon_path, png_path) - if result: - print 'Created icon: %s' % png_path - else: - print >> sys.stderr, ( - u'\tError converting %s to png.' % icon_path) + convert_and_install_icon(pkginfo, icon_path) else: print 'No application icons found.' else: @@ -266,14 +251,6 @@ def generate_png_from_copy_from_dmg_item(dmg_path, pkginfo): def generate_pngs_from_installer_pkg(item_path, pkginfo): '''Generates a product icon (or candidate icons) from an installer pkg and uploads to the repo''' - destination_path = os.path.join(REPO_PATH, 'icons') - if not os.path.exists(destination_path): - try: - os.makedirs(destination_path) - except OSError, errmsg: - print >> sys.stderr, ('Could not create %s: %s' % - (destination_path, errmsg)) - icon_paths = [] mountpoint = None pkg_path = None @@ -302,29 +279,39 @@ def generate_pngs_from_installer_pkg(item_path, pkginfo): munkicommon.unmountdmg(mountpoint) if len(icon_paths) == 1: - png_path = os.path.join( - destination_path, pkginfo['name'] + u'.png') - result = iconutils.convertIconToPNG(icon_paths[0], png_path) - if result: - print 'Created icon: %s' % png_path - else: - print >> sys.stderr, u'Error converting %s to png.' % icon_paths[0] + convert_and_install_icon(pkginfo, icon_paths[0]) elif len(icon_paths) > 1: index = 1 for icon_path in icon_paths: - png_path = os.path.join( - destination_path, - pkginfo['name'] + '_' + str(index) + u'.png') - result = iconutils.convertIconToPNG(icon_path, png_path) - if result: - print 'Created icon: %s' % png_path - else: - print >> sys.stderr, u'Error converting %s to png.' % icon_path + convert_and_install_icon(pkginfo, icon_path, index=index) index += 1 else: print 'No application icons found.' +def convert_and_install_icon(pkginfo, icon_path, index=None): + destination_path = os.path.join(REPO_PATH, 'icons') + if not os.path.exists(destination_path): + try: + os.makedirs(destination_path) + except OSError, errmsg: + print >> sys.stderr, ('Could not create %s: %s' % + (destination_path, errmsg)) + + if index is not None: + destination_name = pkginfo['name'] + '_' + str(index) + else: + destination_name = pkginfo['name'] + + png_path = os.path.join( + destination_path, destination_name + u'.png') + result = iconutils.convertIconToPNG(icon_path, png_path) + if result: + print 'Created icon: %s' % png_path + else: + print >> sys.stderr, u'Error converting %s to png.' % icon_path + + def copyIconToRepo(iconpath): """Saves a product icon to the repo""" destination_path = os.path.join(REPO_PATH, 'icons')