def main(argv): p = optparse.OptionParser() p.set_usage("""Usage: %prog [options] userdata.plist [password]""") p.add_option("-v", "--verbose", action="store_true", help="Verbose output.") options, argv = p.parse_args(argv) if len(argv) not in (2, 3): print >>sys.stderr, p.get_usage() return 1 # Read the userdata.plist. plist = argv[1] plist_data = NSData.dataWithContentsOfFile_(plist) if not plist_data: print >>sys.stderr, "Couldn't read %s" % plist return 1 user_plist, plist_format, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plist_data, NSPropertyListMutableContainers, None, None) if error: print >>sys.stderr, "Can't read %s: %s" % (plist, error) return 1 # Use password on commandline, or ask if one isn't provided. try: password = argv[2] except IndexError: while True: password = getpass.getpass() verify_password = getpass.getpass("Password again: ") if password == verify_password: break else: print "Passwords don't match!" # Hash password with all available methods. hashed_passwords = dict() for k, m in hash_methods.items(): hashed_pwd = m(password) pwd_data = NSData.alloc().initWithBytes_length_(hashed_pwd, len(hashed_pwd)) hashed_passwords[k] = pwd_data # Serialize hashed passwords to a binary plist. serialized_passwords = serialize_hash_dict(hashed_passwords) if not serialized_passwords: return 2 # Write back userdata.plist with ShadowHashData. user_plist["ShadowHashData"] = list() user_plist["ShadowHashData"].append(serialized_passwords) plist_data, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( user_plist, plist_format, None) plist_data.writeToFile_atomically_(argv[1], True) return 0
def _dataToPlist(data): """low-level function that parses a data object into a propertyList object""" darwin_vers = int(os.uname()[2].split(".")[0]) if darwin_vers > 10: ( plistObject, plistFormat, error, ) = NSPropertyListSerialization.propertyListWithData_options_format_error_( data, NSPropertyListMutableContainersAndLeaves, None, None ) else: # 10.5 doesn't support propertyListWithData:options:format:error: # 10.6's PyObjC wrapper for propertyListWithData:options:format:error: # is broken # so use the older NSPropertyListSerialization function ( plistObject, plistFormat, error, ) = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( data, NSPropertyListMutableContainersAndLeaves, None, None ) if plistObject is None: if error is None: error = "Plist data is invalid and could not be deserialized." raise NSPropertyListSerializationException(error) else: return plistObject
def ShadowData(user): ## Open User's Plist Data data = open('/var/db/dslocal/nodes/Default/users/%s.plist' % user, 'r') ## Read and buffer the user's Plist Data plistData = buffer(data.read()) data.close ## Convert the Plist Data into a Dictionary (userPlist, _, _) = (NSPropertyListSerialization.propertyListWithData_options_format_error_(plistData, NSPropertyListXMLFormat_v1_0, None, None)) ## Read and buffer the user's ShadowHashData userShadowHashData = buffer(userPlist['ShadowHashData'][0]) ## Convert the ShadowHashData Data into a Dictionary (userShadowHashPlist, _, _) = (NSPropertyListSerialization.propertyListWithData_options_format_error_(userShadowHashData, NSPropertyListXMLFormat_v1_0, None, None)) ## Remove unsecured hash types del userShadowHashPlist['CRAM-MD5'] del userShadowHashPlist['NT'] ## Convert the ShadowHashData back to data (userShadowHashData, _) = (NSPropertyListSerialization.dataWithPropertyList_format_options_error_(userShadowHashPlist, NSPropertyListBinaryFormat_v1_0, 0, None)) return str(userShadowHashData).encode('hex')
def writeHash(username, userHash): bashCommand(['dscacheutil', '-flushcache']) time.sleep(2) ## Open User's Plist Data data = open('/var/db/dslocal/nodes/Default/users/%s.plist' % username, 'r') ## Read and buffer the user's Plist Data plistData = buffer(data.read()) data.close ## Convert the Plist Data into a Dictionary (userPlist, _, _) = ( NSPropertyListSerialization.propertyListWithData_options_format_error_(plistData, NSPropertyListXMLFormat_v1_0, None, None)) ## Read and buffer the new ShadowHashData userShadowHashData = buffer(userHash.decode('hex')) ## Convert the ShadowHashData into a Dictionary (userShadowHashPlist, _, _) = ( NSPropertyListSerialization.propertyListWithData_options_format_error_(userShadowHashData, NSPropertyListXMLFormat_v1_0, None, None)) ## Remove unsecured hash types del userShadowHashPlist['CRAM-MD5'] del userShadowHashPlist['NT'] ## Convert the ShadowHashData back to data (userShadowHashData, _) = ( NSPropertyListSerialization.dataWithPropertyList_format_options_error_(userShadowHashPlist, NSPropertyListBinaryFormat_v1_0, 0, None)) ## Insert the new ShadowHashData into the User's Plist Dictionary userPlist['ShadowHashData'][0] = userShadowHashData ## Convert the UserPlist back to data (plistData, _) = ( NSPropertyListSerialization.dataWithPropertyList_format_options_error_(userPlist, NSPropertyListBinaryFormat_v1_0, 0, None)) ## Write user's updated plist to disk stream = io.open('/var/db/dslocal/nodes/Default/users/%s.plist' % username, 'bw') stream.write(plistData) stream.close bashCommand(['dscacheutil', '-flushcache']) time.sleep(2) print '[+] User ['+username+'] new hash injected'
def writeHash(username, userHash): bashCommand(['dscacheutil', '-flushcache']) time.sleep(2) ## Open User's Plist Data data = open('/var/db/dslocal/nodes/Default/users/%s.plist' % username, 'r') ## Read and buffer the user's Plist Data plistData = buffer(data.read()) data.close ## Convert the Plist Data into a Dictionary (userPlist, _, _) = ( NSPropertyListSerialization.propertyListWithData_options_format_error_( plistData, NSPropertyListXMLFormat_v1_0, None, None)) ## Read and buffer the new ShadowHashData userShadowHashData = buffer(userHash.decode('hex')) ## Convert the ShadowHashData into a Dictionary (userShadowHashPlist, _, _) = ( NSPropertyListSerialization.propertyListWithData_options_format_error_( userShadowHashData, NSPropertyListXMLFormat_v1_0, None, None)) ## Remove unsecured hash types del userShadowHashPlist['CRAM-MD5'] del userShadowHashPlist['NT'] ## Convert the ShadowHashData back to data (userShadowHashData, _) = ( NSPropertyListSerialization.dataWithPropertyList_format_options_error_( userShadowHashPlist, NSPropertyListBinaryFormat_v1_0, 0, None)) ## Insert the new ShadowHashData into the User's Plist Dictionary userPlist['ShadowHashData'][0] = userShadowHashData ## Convert the UserPlist back to data (plistData, _) = ( NSPropertyListSerialization.dataWithPropertyList_format_options_error_( userPlist, NSPropertyListBinaryFormat_v1_0, 0, None)) ## Write user's updated plist to disk stream = io.open('/var/db/dslocal/nodes/Default/users/%s.plist' % username, 'bw') stream.write(plistData) stream.close bashCommand(['dscacheutil', '-flushcache']) time.sleep(2)
def bin2str(token_bplist, account_bplist=None): # Convert the decrypted binary plist to an NSData object that can be read. bin_list = NSData.dataWithBytes_length_(token_bplist, len(token_bplist)) # Convert the binary NSData object into a dictionary object. token_plist = NSPropertyListSerialization.propertyListWithData_options_format_error_( bin_list, 0, None, None)[0] # Accounts DB cache if "$objects" in token_plist: # Because it is accounts db cache, we should also have been passed # account_bplist. bin_list = NSData.dataWithBytes_length_(account_bplist, len(account_bplist)) dsid_plist = NSPropertyListSerialization.propertyListWithData_options_format_error_( bin_list, 0, None, None)[0] for obj in dsid_plist["$objects"]: if "{}".format(obj).startswith("urn:ds:"): dsid = obj.replace("urn:ds:", "") token_dict = {"dsid": dsid} # Do some parsing to get the data out because it is not stored # in a format that is easy to process with stdlibs token_l = [ x.strip().replace(",", "") for x in "{}".format(token_plist["$objects"]).splitlines() ] pos_start = token_l.index("mmeBTMMInfiniteToken") pos_end = (token_l.index("cloudKitToken") - pos_start + 1) * 2 token_short = token_l[pos_start:pos_start + pos_end] zipped = zip(token_short[:len(token_short) / 2], token_short[len(token_short) / 2:]) for token_type, token_value in zipped: # Attempt to get generation time # this parsing is a little hacky, but it seems to be the best way # to handle all different kinds of iCloud tokens (new and old) gen_time = get_generation_time(token_value) token_dict[token_type] = (token_value, gen_time) return token_dict else: return token_plist
def _plistToData(plistObject): """low-level function that creates NSData from a plist object""" darwin_vers = int(os.uname()[2].split(".")[0]) if darwin_vers > 10: (data, error) = NSPropertyListSerialization.dataWithPropertyList_format_options_error_( plistObject, NSPropertyListXMLFormat_v1_0, 0, None ) else: # use the older NSPropertyListSerialization function on 10.6 and 10.5 (data, error) = NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( plistObject, NSPropertyListXMLFormat_v1_0, None ) if error: raise NSPropertyListSerializationException(error) return data
def plist(obj): from Foundation import NSPropertyListSerialization, NSPropertyListBinaryFormat_v1_0 b = NSPropertyListSerialization.dataWithPropertyList_format_options_error_( obj, NSPropertyListBinaryFormat_v1_0, 0, None ) return str(b.bytes())
def _readPlist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). If the file doesn't exist, this returns None """ if not os.path.isfile(filepath): log.debug( 'Tried to read non-existent property list at path: {0}'.format( filepath)) return None plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: error = error.encode('ascii', 'ignore') errmsg = "%s in file %s" % (error, filepath) import traceback log.debug(errmsg) log.debug(traceback.format_exc()) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def read_file(self, path): """Replace internal XML dict with data from plist at path. Args: path: String path to a plist file. Raises: PlistParseError: Error in reading plist file. """ # pylint: disable=unused-variable ( info, pformat, error, ) = NSPropertyListSerialization.propertyListWithData_options_format_error_( NSData.dataWithContentsOfFile_(os.path.expanduser(path)), NSPropertyListMutableContainersAndLeaves, None, None, ) # pylint: enable=unused-variable if info is None: if error is None: error = "Invalid plist file." raise PlistParseError("Can't read %s: %s" % (path, error)) return info
def _read_plist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). If the file doesn't exist, this returns None """ if not os.path.isfile(filepath): log.debug( 'Tried to read non-existent property list at path: {0}'.format( filepath)) return None plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: error = error.encode('ascii', 'ignore') raise salt.exceptions.SaltInvocationError( 'Error decoding Property List : {}'.format(error)) else: return dataObject
def _readPlist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). If the file doesn't exist, this returns None """ if not os.path.isfile(filepath): log.debug('Tried to read non-existent property list at path: {0}'.format(filepath)) return None plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: error = error.encode('ascii', 'ignore') errmsg = "%s in file %s" % (error, filepath) import traceback log.debug(errmsg) log.debug(traceback.format_exc()) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def _plistToData(plistObject): '''low-level function that creates NSData from a plist object''' darwin_vers = int(os.uname()[2].split('.')[0]) if darwin_vers > 10: (data, error) = ( NSPropertyListSerialization.dataWithPropertyList_format_options_error_( plistObject, NSPropertyListXMLFormat_v1_0, 0, None)) else: # use the older NSPropertyListSerialization function on 10.6 and 10.5 (data, error) = ( NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( plistObject, NSPropertyListXMLFormat_v1_0, None)) if data is None: if error is None: error = "Property list invalid for format." raise NSPropertyListSerializationException(error) return data
def writePlistToString(rootObject): '''Return 'rootObject' as a plist-formatted string.''' (plistData, error) = ( NSPropertyListSerialization.dataWithPropertyList_format_options_error_( rootObject, NSPropertyListXMLFormat_v1_0, 0, None)) if error: raise NSPropertyListSerializationException(error) else: return str(plistData)
def writePlistToString(rootObject): '''Return 'rootObject' as a plist-formatted string.''' plistData, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( rootObject, NSPropertyListXMLFormat_v1_0, None) if error: raise NSPropertyListSerializationException(error) else: return str(plistData)
def readPlistFromString(data): '''Read a plist data from a string. Return the root object.''' plistData = buffer(data) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: raise NSPropertyListSerializationException(error) else: return dataObject
def writePlistToString(rootObject): """Return 'rootObject' as a plist-formatted string.""" plistData, error = NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( rootObject, NSPropertyListXMLFormat_v1_0, None ) if error: error = error.encode("ascii", "ignore") raise NSPropertyListSerializationException(error) else: return str(plistData)
def serialize_hash_dict(hash_dict): plist_data, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( hash_dict, NSPropertyListBinaryFormat_v1_0, None) if error: # FIXME: Raise an exception instead. print >>sys.stderr, error return None return plist_data
def serialize_hash_dict(hash_dict): plist_data, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( hash_dict, NSPropertyListBinaryFormat_v1_0, None) if error: # FIXME: Raise an exception instead. print >> sys.stderr, error return None return plist_data
def decode_bdisk(key): """Return the relative path of file referenced by key """ decodedBytes = base64.b64decode(key) nsData = NSData.dataWithBytes_length_(decodedBytes, len(decodedBytes)) plist, fmt, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( nsData, 0, None, None) if plist is None: return "Missed" else: return plist['relativePath']
def readPlist(path): plistData = NSData.dataWithContentsOfFile_(path) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: errmsg = "%s in file %s" % (repr(error), repr(path)) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def readPlistFromString(data): '''Read a plist data from a string. Return the root object.''' plistData = buffer(data) (dataObject, plistFormat, error) = ( NSPropertyListSerialization.propertyListWithData_options_format_error_( plistData, NSPropertyListMutableContainersAndLeaves, None, None)) if error: raise NSPropertyListSerializationException(error) else: return dataObject
def readPlistFromString(data): """Read a plist data from a string. Return the root object.""" plistData = buffer(data) dataObject, plistFormat, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None ) if error: error = error.encode("ascii", "ignore") raise NSPropertyListSerializationException(error) else: return dataObject
def _generate_plist_string(rootObject, format=NSPropertyListXMLFormat_v1_0): '''Return 'rootObject' as a plist-formatted string.''' plistData, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( rootObject, format, None) if error: error = error.encode('ascii', 'ignore') raise salt.exceptions.SaltInvocationError( 'Error encoding Property List: {}'.format(error)) else: return str(plistData)
def _writePlistToString(rootObject): '''Return 'rootObject' as a plist-formatted string.''' plistData, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( rootObject, NSPropertyListXMLFormat_v1_0, None) if error: error = error.encode('ascii', 'ignore') log.debug('Error encoding to plist') log.debug(error) raise NSPropertyListSerializationException(error) else: return str(plistData)
def readPlist(filepath): '''Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary).''' plistData = NSData.dataWithContentsOfFile_(filepath) (dataObject, plistFormat, error) = ( NSPropertyListSerialization.propertyListWithData_options_format_error_( plistData, NSPropertyListMutableContainersAndLeaves, None, None)) if error: errmsg = u"%s in file %s" % (error, filepath) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def write_recipe(self, path): """Write a recipe to path.""" path = os.path.expanduser(path) plist_data, error = NSPropertyListSerialization.dataWithPropertyList_format_options_error_( self._xml, NSPropertyListXMLFormat_v1_0, 0, None) if error: raise Exception(error) else: if plist_data.writeToFile_atomically_(path, True): return else: raise Exception("Failed writing data to %s" % path)
def _generate_plist_string(rootObject, format=NSPropertyListXMLFormat_v1_0): '''Return 'rootObject' as a plist-formatted string.''' plistData, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( rootObject, format, None) if error: error = error.encode('ascii', 'ignore') raise salt.exceptions.SaltInvocationError( 'Error encoding Property List: {}'.format(error) ) else: return str(plistData)
def writePlist(dataObject, filepath): '''Write 'rootObject' as a plist to filepath.''' (plistData, error) = ( NSPropertyListSerialization.dataWithPropertyList_format_options_error_( dataObject, NSPropertyListXMLFormat_v1_0, 0, None)) if error: raise NSPropertyListSerializationException(error) else: if plistData.writeToFile_atomically_(filepath, True): return else: raise NSPropertyListWriteException( u"Failed to write plist data to %s" % filepath)
def export_command(args): """Export EAPClientProfile Command.""" if args.uuid: p = profile_with_uuid(args.uuid) plist_data = p.__plist__() d, err = NSPropertyListSerialization.dataWithPropertyList_format_options_error_( plist_data, NSPropertyListXMLFormat_v1_0, 0, None, ) print(d)
def readPlist(self, filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). """ plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: errmsg = "%s in file %s" % (error, filepath) raise ProcessorError(errmsg) else: return dataObject
def writePlist(dataObject, filepath): """ Write 'rootObject' as a plist to filepath. """ plistData, error = NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( dataObject, NSPropertyListXMLFormat_v1_0, None ) if error: raise NSPropertyListSerializationException(error) else: if plistData.writeToFile_atomically_(filepath, True): return else: raise Exception("Failed to write plist data to %s" % filepath)
def readPlist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). """ plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, plistFormat, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None) if error: errmsg = "%s in file %s" % (error, filepath) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def _dataToPlist(data): '''low-level function that parses a data object into a propertyList object''' darwin_vers = int(os.uname()[2].split('.')[0]) if darwin_vers > 10: (plistObject, plistFormat, error) = ( NSPropertyListSerialization.propertyListWithData_options_format_error_( data, NSPropertyListMutableContainersAndLeaves, None, None)) else: # 10.5 doesn't support propertyListWithData:options:format:error: # 10.6's PyObjC wrapper for propertyListWithData:options:format:error: # is broken # so use the older NSPropertyListSerialization function (plistObject, plistFormat, error) = ( NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( data, NSPropertyListMutableContainersAndLeaves, None, None ) ) if plistObject is None: if error is None: error = "Plist data is invalid and could not be deserialized." raise NSPropertyListSerializationException(error) else: return plistObject
def writePlistToString(rootObject): """Return 'rootObject' as a plist-formatted string.""" ( plistData, error, ) = NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( rootObject, NSPropertyListXMLFormat_v1_0, None) if plistData is None: if error: error = error.encode("ascii", "ignore") else: error = "Unknown error" raise NSPropertyListSerializationException(error) else: return str(plistData)
def read_bundle_info(self, path): """Read Contents/Info.plist inside a bundle.""" plistpath = os.path.join(path, "Contents", "Info.plist") info, format, error = \ NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( NSData.dataWithContentsOfFile_(plistpath), NSPropertyListMutableContainers, None, None ) if error: raise ProcessorError("Can't read %s: %s" % (plistpath, error)) return info
def write_bundle_info(self, info, path): """Write Contents/Info.plist inside a bundle.""" plistpath = os.path.join(path, "Contents", "Info.plist") plist_data, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( info, NSPropertyListXMLFormat_v1_0, None ) if error: raise ProcessorError("Can't serialize %s: %s" % (plistpath, error)) if not plist_data.writeToFile_atomically_(plistpath, True): raise ProcessorError("Can't write %s" % (plistpath))
def read_recipe(self, path): """Read a recipe into a dict.""" path = os.path.expanduser(path) if not (os.path.isfile(path)): raise Exception("File does not exist: %s" % path) info, pformat, error = \ NSPropertyListSerialization.propertyListWithData_options_format_error_( NSData.dataWithContentsOfFile_(path), NSPropertyListMutableContainers, None, None ) if error: raise Exception("Can't read %s: %s" % (path, error)) self._xml = info
def writePlist(dataObject, filepath): ''' Write 'rootObject' as a plist to filepath. ''' plistData, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( dataObject, NSPropertyListXMLFormat_v1_0, None) if error: error = error.encode('ascii', 'ignore') raise NSPropertyListSerializationException(error) else: if plistData.writeToFile_atomically_(filepath, True): return else: raise NSPropertyListWriteException( "Failed to write plist data to %s" % filepath)
def write_recipe(self, path): """Write a recipe to path.""" path = os.path.expanduser(path) plist_data, error = NSPropertyListSerialization.dataWithPropertyList_format_options_error_( self._xml, NSPropertyListXMLFormat_v1_0, 0, None ) if error: raise Exception(error) else: if plist_data.writeToFile_atomically_(path, True): return else: raise Exception("Failed writing data to %s" % path)
def _write_plist(dataObject, filepath, format=NSPropertyListXMLFormat_v1_0): ''' Write 'rootObject' as a plist to filepath. ''' plistData, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( dataObject, format, None) if error: error = error.encode('ascii', 'ignore') raise salt.exceptions.SaltInvocationError( 'Error encoding Property List: {}'.format(error)) else: if plistData.writeToFile_atomically_(filepath, True): return else: raise salt.exceptions.SaltInvocationError( 'Failed to write Property List at path: {}'.format(filepath))
def read_bundle_info(self, path): """Read Contents/Info.plist inside a bundle.""" # pylint: disable=no-self-use plistpath = os.path.join(path, "Contents", "Info.plist") # pylint: disable=line-too-long info, _, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( NSData.dataWithContentsOfFile_(plistpath), NSPropertyListMutableContainers, None, None, ) # pylint: enable=line-too-long if error: raise ProcessorError("Can't read %s: %s" % (plistpath, error)) return info
def read(plist_path: str) -> Dict: """Read a plist file and return its contents as a dictionary.""" exc.raise_if_falsy(plist_path=plist_path) data, error = NSData.dataWithContentsOfFile_options_error_( plist_path, 0, objc.nil) if not data: msg = 'Failed to load plist file at path: {}'.format(plist_path) _raise_ioerror_from_nserror(error, msg) contents, dummy, error = NSPropertyListSerialization.propertyListWithData_options_format_error_( data, NSPropertyListMutableContainersAndLeaves, objc.nil, objc.nil) if not contents: msg = 'Failed to deserialize plist at path: {}'.format(plist_path) _raise_ioerror_from_nserror(error, msg) return contents
def read_plist(filepath): ''' Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). ''' plist_data = NSData.dataWithContentsOfFile_(filepath) data_object, dummy_plist_format, error = ( NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plist_data, NSPropertyListMutableContainers, None, None)) if data_object is None: if error: error = error.encode('ascii', 'ignore') else: error = "Unknown error" errmsg = "%s in file %s" % (error, filepath) raise NSPropertyListSerializationException(errmsg) # pylint: disable=E0602 else: return data_object
def readPlist(filepath): """ Read a .plist file from filepath. Return the unpacked root object (which is usually a dictionary). """ plistData = NSData.dataWithContentsOfFile_(filepath) dataObject, dummy_plistFormat, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_( plistData, NSPropertyListMutableContainers, None, None ) if dataObject is None: if error: error = error.encode("ascii", "ignore") else: error = "Unknown error" errmsg = "%s in file %s" % (error, filepath) raise NSPropertyListSerializationException(errmsg) else: return dataObject
def _write_plist(dataObject, filepath, format=NSPropertyListXMLFormat_v1_0): ''' Write 'rootObject' as a plist to filepath. ''' plistData, error = \ NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_( dataObject, format, None) if error: error = error.encode('ascii', 'ignore') raise salt.exceptions.SaltInvocationError( 'Error encoding Property List: {}'.format(error) ) else: if plistData.writeToFile_atomically_(filepath, True): return else: raise salt.exceptions.SaltInvocationError( 'Failed to write Property List at path: {}'.format(filepath) )