Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | import re |
michael@0 | 6 | import os |
michael@0 | 7 | import sys |
michael@0 | 8 | import optparse |
michael@0 | 9 | |
michael@0 | 10 | def getFile(filename): |
michael@0 | 11 | fHandle = open(filename, 'r') |
michael@0 | 12 | data = fHandle.read() |
michael@0 | 13 | fHandle.close() |
michael@0 | 14 | return data |
michael@0 | 15 | |
michael@0 | 16 | def findIDs(data): |
michael@0 | 17 | start_function = False |
michael@0 | 18 | reID = re.compile('.*public static final class id {.*') |
michael@0 | 19 | reEnd = re.compile('.*}.*') |
michael@0 | 20 | idlist = [] |
michael@0 | 21 | |
michael@0 | 22 | for line in data.split('\n'): |
michael@0 | 23 | if reEnd.match(line): |
michael@0 | 24 | start_function = False |
michael@0 | 25 | |
michael@0 | 26 | if start_function: |
michael@0 | 27 | id_value = line.split(' ')[-1] |
michael@0 | 28 | idlist.append(id_value.split(';')[0].split('=')) |
michael@0 | 29 | |
michael@0 | 30 | if reID.match(line): |
michael@0 | 31 | start_function = True |
michael@0 | 32 | |
michael@0 | 33 | return idlist |
michael@0 | 34 | |
michael@0 | 35 | |
michael@0 | 36 | def printIDs(outputFile, idlist): |
michael@0 | 37 | fOutput = open(outputFile, 'w') |
michael@0 | 38 | for item in idlist: |
michael@0 | 39 | fOutput.write("%s=%s\n" % (item[0], item[1])) |
michael@0 | 40 | fOutput.close() |
michael@0 | 41 | |
michael@0 | 42 | def main(args=sys.argv[1:]): |
michael@0 | 43 | parser = optparse.OptionParser() |
michael@0 | 44 | parser.add_option('-o', '--output', dest='outputFile', default='', |
michael@0 | 45 | help="output file with the id=value pairs") |
michael@0 | 46 | parser.add_option('-i', '--input', dest='inputFile', default='', |
michael@0 | 47 | help="filename of the input R.java file") |
michael@0 | 48 | options, args = parser.parse_args(args) |
michael@0 | 49 | |
michael@0 | 50 | if options.inputFile == '': |
michael@0 | 51 | print "Error: please provide input file: -i <filename>" |
michael@0 | 52 | sys.exit(1) |
michael@0 | 53 | |
michael@0 | 54 | if options.outputFile == '': |
michael@0 | 55 | print "Error: please provide output file: -o <filename>" |
michael@0 | 56 | sys.exit(1) |
michael@0 | 57 | |
michael@0 | 58 | data = getFile(os.path.abspath(options.inputFile)); |
michael@0 | 59 | idlist = findIDs(data) |
michael@0 | 60 | printIDs(os.path.abspath(options.outputFile), idlist) |
michael@0 | 61 | |
michael@0 | 62 | if __name__ == "__main__": |
michael@0 | 63 | main() |