From 8deb944a7c783325eaebd8005eeabcfacb54bb6e Mon Sep 17 00:00:00 2001 From: Greg Neagle Date: Mon, 9 Jan 2017 10:14:16 -0800 Subject: [PATCH] decrease repeated defintions of the same function --- code/client/makepkginfo | 2 + code/client/munkilib/adobeutils/adobeinfo.py | 22 +--------- code/client/munkilib/pkgutils.py | 46 ++++++++++++-------- 3 files changed, 32 insertions(+), 38 deletions(-) mode change 100644 => 100755 code/client/munkilib/pkgutils.py diff --git a/code/client/makepkginfo b/code/client/makepkginfo index 48ce5264..de1aca5d 100755 --- a/code/client/makepkginfo +++ b/code/client/makepkginfo @@ -263,6 +263,7 @@ def getCatalogInfoFromDmg(dmgpath, options): return cataloginfo +# TO-DO: delete this and use pkgutils.getBundleInfo() other places here def getBundleInfo(path): """ Returns Info.plist data if available @@ -282,6 +283,7 @@ def getBundleInfo(path): return None +# TO-DO: this (or a similar) function is defined several places. De-dupe. def readfile(path): '''Reads file at path. Returns a string.''' try: diff --git a/code/client/munkilib/adobeutils/adobeinfo.py b/code/client/munkilib/adobeutils/adobeinfo.py index 8e6a2742..83bc3b01 100755 --- a/code/client/munkilib/adobeutils/adobeinfo.py +++ b/code/client/munkilib/adobeutils/adobeinfo.py @@ -31,7 +31,6 @@ from xml.dom import minidom from .. import osutils from .. import pkgutils -from .. import FoundationPlist # we use lots of camelCase-style names. Deal with it. # pylint: disable=C0103 @@ -395,25 +394,6 @@ def getHDInstallerInfo(hd_payload_root, sap_code): return hd_app_info -def getBundleInfo(path): - """ - Returns Info.plist data if available - for bundle at path - """ - infopath = os.path.join(path, "Contents", "Info.plist") - if not os.path.exists(infopath): - infopath = os.path.join(path, "Resources", "Info.plist") - - if os.path.exists(infopath): - try: - plist = FoundationPlist.readPlist(infopath) - return plist - except FoundationPlist.NSPropertyListSerializationException: - pass - - return None - - def getCS5mediaSignature(dirpath): '''Returns the CS5 mediaSignature for an AAMEE CS5 install. dirpath is typically the root of a mounted dmg''' @@ -736,7 +716,7 @@ def getAdobeCatalogInfo(mountpoint, pkgname=""): cataloginfo = {} cataloginfo['installer_type'] = "AdobeAcrobatUpdater" cataloginfo['uninstallable'] = False - plist = getBundleInfo(acrobatpatcherapp) + plist = pkgutils.getBundleInfo(acrobatpatcherapp) cataloginfo['version'] = pkgutils.getVersionString(plist) cataloginfo['name'] = "AcrobatPro9Update" cataloginfo['display_name'] = "Adobe Acrobat Pro Update" diff --git a/code/client/munkilib/pkgutils.py b/code/client/munkilib/pkgutils.py old mode 100644 new mode 100755 index f32b7dc5..bdb2a71b --- a/code/client/munkilib/pkgutils.py +++ b/code/client/munkilib/pkgutils.py @@ -173,11 +173,29 @@ def getVersionString(plist, key=None): return '' +def getBundleInfo(path): + """ + Returns Info.plist data if available + for bundle at path + """ + infopath = os.path.join(path, "Contents", "Info.plist") + if not os.path.exists(infopath): + infopath = os.path.join(path, "Resources", "Info.plist") + + if os.path.exists(infopath): + try: + plist = FoundationPlist.readPlist(infopath) + return plist + except FoundationPlist.NSPropertyListSerializationException: + pass + + return None + + def getAppBundleExecutable(bundlepath): """Returns path to the actual executable in an app bundle or None""" - infoPlist = os.path.join(bundlepath, 'Contents', 'Info.plist') - if os.path.exists(infoPlist): - plist = FoundationPlist.readPlist(infoPlist) + plist = getBundleInfo(bundlepath) + if plist: if 'CFBundleExecutable' in plist: executable = plist['CFBundleExecutable'] elif 'CFBundleName' in plist: @@ -198,11 +216,8 @@ def getBundleVersion(bundlepath, key=None): Specify key to use a specific key in the Info.plist for the version string. """ - infoPlist = os.path.join(bundlepath, 'Contents', 'Info.plist') - if not os.path.exists(infoPlist): - infoPlist = os.path.join(bundlepath, 'Resources', 'Info.plist') - if os.path.exists(infoPlist): - plist = FoundationPlist.readPlist(infoPlist) + plist = getBundleInfo(bundlepath) + if plist: versionstring = getVersionString(plist, key) if versionstring: return versionstring @@ -412,11 +427,10 @@ def getBomList(pkgpath): def getOnePackageInfo(pkgpath): """Gets receipt info for a single bundle-style package""" pkginfo = {} - plistpath = os.path.join(pkgpath, 'Contents', 'Info.plist') - if os.path.exists(plistpath): + plist = getBundleInfo(pkgpath) + if plist: pkginfo['filename'] = os.path.basename(pkgpath) try: - plist = FoundationPlist.readPlist(plistpath) if 'CFBundleIdentifier' in plist: pkginfo['packageid'] = plist['CFBundleIdentifier'] elif 'Bundle identifier' in plist: @@ -491,9 +505,8 @@ def getBundlePackageInfo(pkgpath): # no .dist file found, look for packages in subdirs dirsToSearch = [] - plistpath = os.path.join(pkgpath, 'Contents', 'Info.plist') - if os.path.exists(plistpath): - plist = FoundationPlist.readPlist(plistpath) + plist = getBundleInfo(pkgpath) + if plist: if 'IFPkgFlagComponentDirectory' in plist: componentdir = plist['IFPkgFlagComponentDirectory'] dirsToSearch.append(componentdir) @@ -842,9 +855,8 @@ def isApplication(pathname): if os.path.isdir(pathname): # look for app bundle structure # use Info.plist to determine the name of the executable - infoplist = os.path.join(pathname, 'Contents', 'Info.plist') - if os.path.exists(infoplist): - plist = FoundationPlist.readPlist(infoplist) + plist = getBundleInfo(pathname) + if plist: if 'CFBundlePackageType' in plist: if plist['CFBundlePackageType'] != 'APPL': return False