Skip to content

Instantly share code, notes, and snippets.

@rowanj
Last active December 18, 2015 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rowanj/5811447 to your computer and use it in GitHub Desktop.
Save rowanj/5811447 to your computer and use it in GitHub Desktop.
Script to install or update all .mobileprovision files under the current path
__author__ = 'rowanj@burninator.net'
import os
import fnmatch
import plistlib
import filecmp
import shutil
import sys
ext = '.mobileprovision'
install_dir = os.path.expanduser('~/Library/MobileDevice/Provisioning Profiles')
if not os.path.exists(install_dir):
os.makedirs(install_dir)
class MobileProvisionReadException(Exception):
pass
def findMatchingFiles(basedir, pattern):
matches = []
for root, dirnames, filenames in os.walk(basedir):
for filename in fnmatch.filter(filenames, pattern):
matches.append(os.path.join(root, filename))
return matches
def installProfile(filename):
with open(filename) as profile_file:
profile_data = profile_file.read()
start_tag = '<?xml version="1.0" encoding="UTF-8"?>'
stop_tag = '</plist>'
try:
start_index = profile_data.index(start_tag)
stop_index = profile_data.index(stop_tag, start_index + len(start_tag)) + len(stop_tag)
except ValueError:
raise MobileProvisionReadException('This is not a valid mobile provision file')
plist_data = profile_data[start_index:stop_index]
profile_dict = plistlib.readPlistFromString(plist_data)
name = profile_dict['Name']
UUID = profile_dict['UUID']
installed = False
install_path = os.path.join(install_dir, UUID + ext)
if os.path.exists(install_path):
if filecmp.cmp(install_path, filename):
print 'found installed profile %r : %r %r' % (filename, UUID, name)
installed = True
else:
print 'found differing profile %r : %r %r' % (filename, UUID, name)
else:
print 'found new profile %r : %r %r' % (filename, UUID, name)
if not installed:
shutil.copy(filename, install_path)
print 'installed %r %r' % (UUID, name)
def main():
profiles = findMatchingFiles(os.getcwd(), '*' + ext)
for profile in profiles:
installProfile(profile)
if __name__ == '__main__':
main()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment