def get_all_prefs(domain=BUNDLE_ID): """Return a dict (or an empty dict) with the contents of all preferences in the domain.""" prefs = {} # get keys stored via 'defaults write [domain]' user_keylist = CFPreferencesCopyKeyList(BUNDLE_ID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost) # get keys stored via 'defaults write /Library/Preferences/[domain]' system_keylist = CFPreferencesCopyKeyList(BUNDLE_ID, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) # CFPreferencesCopyAppValue() in get_pref() will handle returning the appropriate # value using the search order, so merging prefs in order here isn't be necessary for keylist in [system_keylist, user_keylist]: if keylist: for key in keylist: prefs[key] = get_pref(key, domain) return prefs
def keys(appid, byHost=True): """ Read a list of keys available appid Bundle identifier such as com.apple.Finder """ host = kCFPreferencesCurrentHost if byHost else kCFPreferencesAnyHost keyList = CFPreferencesCopyKeyList(appid, kCFPreferencesAnyUser, host) if keyList is None: return None else: return list(keyList)
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 set_custom_catalogurl(catalog_url): """Sets Software Update's CatalogURL to custom value, storing the original""" software_update_key_list = CFPreferencesCopyKeyList( APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) or [] if ORIGINAL_CATALOG_URL_KEY not in software_update_key_list: # store the original CatalogURL original_catalog_url = get_catalogurl() if not original_catalog_url: # can't store None as a CFPreference original_catalog_url = "" CFPreferencesSetValue( ORIGINAL_CATALOG_URL_KEY, original_catalog_url, APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) # now set our custom CatalogURL os_version_tuple = osutils.getOsVersion(as_tuple=True) if os_version_tuple < (10, 11): CFPreferencesSetValue( 'CatalogURL', catalog_url, APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost) # finally, sync things up if not CFPreferencesSynchronize( APPLE_SOFTWARE_UPDATE_PREFS_DOMAIN, kCFPreferencesAnyUser, kCFPreferencesCurrentHost): display.display_error( 'Error setting com.apple.SoftwareUpdate CatalogURL.') else: # use softwareupdate --set-catalog proc = subprocess.Popen( ['/usr/sbin/softwareupdate', '--set-catalog', catalog_url], bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (output, err) = proc.communicate() if output: display.display_detail(output) if err: display.display_error(err)