Created
June 12, 2012 23:21
-
-
Save jbowes/2920771 to your computer and use it in GitHub Desktop.
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/python | |
# | |
# Fixes your manifest. Oops! | |
import os | |
import glob | |
import shutil | |
import simplejson as json | |
import sys | |
import tempfile | |
from zipfile import ZipFile | |
def fix_entitlement(entitlement): | |
print("Fixing entitlement: " + os.path.basename(entitlement)) | |
entitlement_file = open(entitlement) | |
entitlement_json = json.load(entitlement_file) | |
entitlement_json['consumer'] = None | |
entitlement_file = open(entitlement, 'w') | |
json.dump(entitlement_json, entitlement_file) | |
def main(args): | |
manifest = args[1] | |
print("creating work directory") | |
workdir = tempfile.mkdtemp(prefix="work-dir", dir=".") | |
print("extracting manifest") | |
zip_file = ZipFile(manifest, "r") | |
zip_file.extractall(workdir) | |
zip_file = ZipFile(os.path.join(workdir, "consumer_export.zip"), "r") | |
zip_file.extractall(workdir) | |
print("reading entitlements") | |
entitlements = glob.glob(os.path.join(workdir, "export", "entitlements", | |
"*.json")) | |
for entitlement in entitlements: | |
fix_entitlement(entitlement) | |
print("rebuilding manifest") | |
zip_file = ZipFile(os.path.join(workdir, "consumer_export.zip"), "w") | |
for subdir, dirs, files in os.walk(os.path.join(workdir, "export")): | |
for afile in files: | |
rel_path = os.path.join(subdir, afile) | |
zip_path = os.path.join(*(os.path.split(subdir)[1:] + (afile,))) | |
if not zip_path.startswith("export"): | |
zip_path = os.path.join("export", zip_path) | |
zip_file.write(rel_path, zip_path) | |
zip_file = ZipFile("manifest-fixed.zip", "w") | |
zip_file.write(os.path.join(workdir, "consumer_export.zip"), | |
"consumer_export.zip") | |
zip_file.write(os.path.join(workdir, "signature"), | |
"signature") | |
print("cleaning up") | |
shutil.rmtree(workdir) | |
print("all done. your new manifest is in manifest-fixed.zip") | |
if __name__ == "__main__": | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment