michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import re michael@0: import os michael@0: import sys michael@0: import optparse michael@0: michael@0: def getFile(filename): michael@0: fHandle = open(filename, 'r') michael@0: data = fHandle.read() michael@0: fHandle.close() michael@0: return data michael@0: michael@0: def findIDs(data): michael@0: start_function = False michael@0: reID = re.compile('.*public static final class id {.*') michael@0: reEnd = re.compile('.*}.*') michael@0: idlist = [] michael@0: michael@0: for line in data.split('\n'): michael@0: if reEnd.match(line): michael@0: start_function = False michael@0: michael@0: if start_function: michael@0: id_value = line.split(' ')[-1] michael@0: idlist.append(id_value.split(';')[0].split('=')) michael@0: michael@0: if reID.match(line): michael@0: start_function = True michael@0: michael@0: return idlist michael@0: michael@0: michael@0: def printIDs(outputFile, idlist): michael@0: fOutput = open(outputFile, 'w') michael@0: for item in idlist: michael@0: fOutput.write("%s=%s\n" % (item[0], item[1])) michael@0: fOutput.close() michael@0: michael@0: def main(args=sys.argv[1:]): michael@0: parser = optparse.OptionParser() michael@0: parser.add_option('-o', '--output', dest='outputFile', default='', michael@0: help="output file with the id=value pairs") michael@0: parser.add_option('-i', '--input', dest='inputFile', default='', michael@0: help="filename of the input R.java file") michael@0: options, args = parser.parse_args(args) michael@0: michael@0: if options.inputFile == '': michael@0: print "Error: please provide input file: -i " michael@0: sys.exit(1) michael@0: michael@0: if options.outputFile == '': michael@0: print "Error: please provide output file: -o " michael@0: sys.exit(1) michael@0: michael@0: data = getFile(os.path.abspath(options.inputFile)); michael@0: idlist = findIDs(data) michael@0: printIDs(os.path.abspath(options.outputFile), idlist) michael@0: michael@0: if __name__ == "__main__": michael@0: main()