Created
August 14, 2014 03:26
-
-
Save meoow/6e276aec5f8e511c137b to your computer and use it in GitHub Desktop.
Add disabled into plist file of iOS apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import plistlib as plib | |
import subprocess as subp | |
from contextlib import closing | |
import os, tempfile, shutil, re | |
from NameAppend import nameAppend | |
def parsePlistToString(plistFile=None): | |
'''parsePlistToString(plistFile) | |
return plistDataString, isBinary''' | |
# fixBugSearchRegex = re.compile(r'(?<=<array>)(.*?)(<key>.+?</key>)(.*?)(?=</array>)',flags=re.DOTALL) | |
# fixBugSubRegex = re.compile(r'(?<=<array>)(.*?)<key>(.+?)</key>(.*?)(?=</array>)',flags=re.DOTALL) | |
with closing(open(plistFile, 'rb')) as pfFH: | |
plistString = pfFH.read() | |
if plistString.find('bplist') == 0: | |
isBinary = True | |
else: | |
isBinary = False | |
if isBinary is True: | |
#tempPlistFile = tempfile.mktemp() | |
#subp.call('plutil -i {0} -o {1}'.format(plistFile, tempPlistFile), | |
# shell=True) | |
out = subp.check_output('plutil.pl {0}'.format(plistFile), shell=True) | |
try: | |
plistTextFile = out.split('\n')[5].split(': ')[1] | |
if not os.path.exists(plistTextFile): | |
raise NameError, '{0}: no such file'.format(plistTextFile) | |
#with closing(open(tempPlistFile, 'rb')) as pfFH: | |
with closing(open(plistTextFile, 'rb')) as pfFH: | |
plistString = pfFH.read() | |
finally: | |
#os.unlink(tempPlistFile) | |
os.unlink(plistTextFile) | |
#while fixBugSearchRegex.search(plistString): | |
#plistString=fixBugSubRegex.sub(r'\1<string>\2</string>\3',plistString) | |
plistData = plib.readPlistFromString(plistString) | |
return plistData, isBinary | |
def main(plistFile=None, backup=True): | |
plistData, isBinary = parsePlistToString(plistFile) | |
plistData['Disabled']=True | |
#tempOutFile = tempfile.NamedTemporaryFile() | |
tempOutFile = tempfile.NamedTemporaryFile(suffix='.plist') | |
try: | |
plib.writePlist(plistData, tempOutFile) | |
tempOutFile.seek(0) | |
if backup is True: | |
#shutil.move(plistFile, plistFile+'.bak') | |
os.system('mv {0} {1}'.format(plistFile, plistFile+'.bak')) | |
if isBinary is True: | |
#subp.call('plutil -i {0} -o {1}'.format(tempOutFile.name, plistFile), shell=True) | |
subp.call('plutil.pl {0}'.format(tempOutFile.name), shell=True, stdout=open(os.devnull, 'w')) | |
#shutil.move(nameAppend(tempOutFile.name, '.binary'), plistFile) | |
os.system('mv {0} {1}'.format(nameAppend(tempOutFile.name, '.binary'), plistFile)) | |
else: | |
#shutil.copy(tempOutFile.name, plistFile) | |
os.system('cp {0} {1}'.format(tempOutFile.name, plistFile)) | |
finally: | |
tempOutFile.close() | |
if __name__ == '__main__': | |
from argparse import ArgumentParser | |
import sys | |
parser = ArgumentParser() | |
parser.add_argument('PLISTFILE', nargs='+', help='plist file', type=str) | |
parser.add_argument('-b', '--backup', action='store_true', default=False, help='backup original plist file', dest='backup') | |
args = parser.parse_args() | |
for plistfile in args.PLISTFILE: | |
main(plistFile=plistfile, backup=args.backup) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment