def get_preference_anyuser(preference_key, bundle_id): # Get the specified preference key from the specified preference # bundle ID for any user (current host) using CoreFoundation preference_value = CFPreferencesCopyValue(preference_key, bundle_id, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) return preference_value
def fact(): '''Returns the status of automatic updates to MAS apps''' status = "disabled" pref = CFPreferencesCopyValue( 'AutoUpdate', '/Library/Preferences/com.apple.commerce.plist', kCFPreferencesAnyUser, kCFPreferencesAnyHost) if pref: status = "enabled" return {factoid: status}
def fact(): """Returns the status of automatic updates to MAS apps""" status = "disabled" pref = CFPreferencesCopyValue( "AutoUpdateRestartRequired", "/Library/Preferences/com.apple.commerce.plist", kCFPreferencesAnyUser, kCFPreferencesAnyHost, ) if pref: status = "enabled" return {factoid: status}
def reset_original_catalogurl(): """Resets SoftwareUpdate's CatalogURL to the original value""" software_update_key_list = CFPreferencesCopyKeyList( APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) or [] if ORIGINAL_CATALOG_URL_KEY not in software_update_key_list: # do nothing return original_catalog_url = CFPreferencesCopyValue( ORIGINAL_CATALOG_URL_KEY, APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) if not original_catalog_url: original_catalog_url = None # reset CatalogURL to the one we stored os_version_tuple = osutils.getOsVersion(as_tuple=True) if os_version_tuple < (10, 11): CFPreferencesSetValue('CatalogURL', original_catalog_url, APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) else: if original_catalog_url: # use softwareupdate --set-catalog cmd = [ '/usr/sbin/softwareupdate', '--set-catalog', original_catalog_url ] else: # use softwareupdate --clear-catalog cmd = ['/usr/sbin/softwareupdate', '--clear-catalog'] proc = subprocess.Popen(cmd, bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (output, err) = proc.communicate() if output: display.display_detail(output) if err: display.display_error(err) # remove ORIGINAL_CATALOG_URL_KEY CFPreferencesSetValue(ORIGINAL_CATALOG_URL_KEY, None, APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) # sync if not CFPreferencesSynchronize(APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost): display.display_error( 'Error resetting com.apple.SoftwareUpdate CatalogURL.')
def read(appid, key): ''' Get the preference value for an Application ID and requested key. appid Bundle identifier such as com.apple.Finder key Preference key to read .. code-block:: bash salt '*' cfpreferences.read com.apple.Finder NSNavLastRootDirectory ''' value = CFPreferencesCopyValue(key, appid, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) return value
def read(appid, key, byHost=True): """ Get the preference value for an Application ID and requested key. appid Bundle identifier such as com.apple.Finder key Preference key to read .. code-block:: bash salt '*' cfpreferences.read com.apple.Finder NSNavLastRootDirectory """ host = kCFPreferencesCurrentHost if byHost else kCFPreferencesAnyHost value = CFPreferencesCopyValue(key, appid, kCFPreferencesAnyUser, host) valueType = CFGetTypeID(value) from pprint import pprint pprint(valueType) return value
def get_catalogurl(): """Returns Software Update's CatalogURL""" return CFPreferencesCopyValue('CatalogURL', APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost)
def get_config_level(bundle_id, pref_name, value): """Returns a string indicating where the given preference is defined""" if value is None: return "not set" if CFPreferencesAppValueIsForced(pref_name, bundle_id): return "MANAGED" home_dir = os.path.expanduser("~") # define all the places we need to search, in priority order levels = [ { "file": os.path.join(home_dir, "Library/Preferences/ByHost", bundle_id + ".xxxx.plist"), "domain": bundle_id, "user": kCFPreferencesCurrentUser, "host": kCFPreferencesCurrentHost, }, { "file": os.path.join(home_dir, "Library/Preferences/", bundle_id + ".plist"), "domain": bundle_id, "user": kCFPreferencesCurrentUser, "host": kCFPreferencesAnyHost, }, { "file": os.path.join(home_dir, "Library/Preferences/ByHost", ".GlobalPreferences.xxxx.plist"), "domain": ".GlobalPreferences", "user": kCFPreferencesCurrentUser, "host": kCFPreferencesCurrentHost, }, { "file": os.path.join(home_dir, "Library/Preferences", ".GlobalPreferences.plist"), "domain": ".GlobalPreferences", "user": kCFPreferencesCurrentUser, "host": kCFPreferencesAnyHost, }, { "file": os.path.join("/Library/Preferences", bundle_id + ".plist"), "domain": bundle_id, "user": kCFPreferencesAnyUser, "host": kCFPreferencesCurrentHost, }, { "file": "/Library/Preferences/.GlobalPreferences.plist", "domain": ".GlobalPreferences", "user": kCFPreferencesAnyUser, "host": kCFPreferencesCurrentHost, }, ] for level in levels: if value == CFPreferencesCopyValue(pref_name, level["domain"], level["user"], level["host"]): return level["file"] if value == DEFAULT_PREFS.get(pref_name): return "default" return "unknown"