#!/usr/bin/env python import sys import zipfile import shutil import time import re if (len(sys.argv) != 2): print "Usage: %s " % (sys.argv[0]); print "\nThis script will add the Emacs keybindings to 's toolkit.jar" print "file. It assumes that the appliation is Firefox, Thunderbird, " print "or one of the other Mozilla-based applications that has a " print "/Applications/.app/Contents/MacOS/chrome/toolkit.jar file." sys.exit(1) # text to add bindings = """ """ # path to the app apppath = "/Applications/%s.app/Contents/MacOS/chrome" % (sys.argv[1]) # create the string for toolkit.jar zipfilename = apppath + "/toolkit.jar" # make sure this is the correct type of file if not zipfile.is_zipfile(zipfilename): # something's wrong with the zip file. Maybe it's working with an app that uses # embed.jar instead, such as camino. let's try that now. zipfilename = apppath + "/embed.jar" if not zipfile.is_zipfile(zipfilename): print "%s/toolkit.jar or %s/embed.jar is not the expcected format." % (apppath, apppath) sys.exit(1) # create backup file string backup = "%s-backup-%s" % (zipfilename, time.strftime("%Y%m%d-%H%M%S")) # make a backup with timestamp shutil.copy2(zipfilename, backup) # open the input file (the just-created backupfile) readzip = zipfile.ZipFile(backup, mode='r') # make sure the backup is not corrupt if readzip.testzip(): print "%s is corrupt." % (zipfilename) sys.exit(1) # open the write file writezip = zipfile.ZipFile(zipfilename, mode='w') # process the toolkit.jar file # the idea here is to read in the name of file in the jar(zip), see if it's the # file we're looking for and, if not, directly output it to the new jar file. If # it is, then we'll process it, inserting the strings we want. we'll edit its # zipinfo file, adding the new timestamp, then output it to the new jar file too. for filename in readzip.namelist(): info = readzip.getinfo(filename) filestr = readzip.read(filename) if filename == "content/global/platformHTMLBindings.xml": # prepare the regex regex = re.compile(r'\s+\s') # split the file in the appropriate places chunks = regex.split(filestr) # reassemble the file filestr = """%s %s %s %s %s %s %s""" % (chunks[0], chunks[1], bindings, chunks[2], chunks[3], bindings, chunks[4], chunks[5], bindings, chunks[6]) # prepare the ZipInfo object info.date_time = time.localtime(time.time())[ :6] # add the file to the zip writezip.writestr(info, filestr) # close the zip file writezip.close() # make sure the new file is not corrupt writezip = zipfile.ZipFile(zipfilename, mode='r') if writezip.testzip(): print "%s.new is corrupt." % (zipfilename) else: print "%s.new is OK." % (zipfilename)