David Ghandehari | 8c5039b | 2016-08-17 19:39:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | """Converts XLB files into CSV files in a given output directory. |
| 17 | |
| 18 | Since the output of this script is intended to be use by GYP, all resulting |
| 19 | paths are using Unix-style forward slashes. |
| 20 | """ |
| 21 | |
| 22 | import argparse |
| 23 | import os |
| 24 | import posixpath |
| 25 | import sys |
| 26 | import xml.etree.ElementTree |
| 27 | |
| 28 | |
| 29 | class WrongNumberOfArgumentsException(Exception): |
| 30 | pass |
| 31 | |
| 32 | |
| 33 | def EscapePath(path): |
| 34 | """Returns a path with spaces escaped.""" |
| 35 | return path.replace(' ', '\\ ') |
| 36 | |
| 37 | |
| 38 | def ChangeSuffix(filename, new_suffix): |
| 39 | """Changes the suffix of |filename| to |new_suffix|. If no current suffix, |
| 40 | adds |new_suffix| to the end of |filename|.""" |
| 41 | (root, ext) = os.path.splitext(filename) |
| 42 | return root + '.' + new_suffix |
| 43 | |
| 44 | |
| 45 | def ConvertSingleFile(filename, output_filename): |
| 46 | """Converts a single input XLB file to a CSV file.""" |
| 47 | tree = xml.etree.ElementTree.parse(filename) |
| 48 | root = tree.getroot() |
| 49 | |
| 50 | # First child of the root is the list of messages. |
| 51 | messages = root[0] |
| 52 | |
| 53 | # Write each message to the output file on its own line. |
Andrew Top | 2ea2238 | 2016-12-08 09:47:36 -0800 | [diff] [blame] | 54 | with open(output_filename, 'wb') as output_file: |
David Ghandehari | 8c5039b | 2016-08-17 19:39:30 -0700 | [diff] [blame] | 55 | for msg in messages: |
| 56 | # Use ; as the separator. Which means it better not be in the name. |
| 57 | assert not (';' in msg.attrib['name']) |
| 58 | output_file.write(msg.attrib['name']) |
| 59 | output_file.write(';') |
| 60 | # Encode the text as UTF8 to accommodate special characters. |
| 61 | output_file.write(msg.text.encode('utf8')) |
| 62 | output_file.write('\n') |
| 63 | |
| 64 | |
| 65 | def GetOutputs(files_to_convert, output_basedir): |
| 66 | """Returns a list of filenames relative to the output directory, |
| 67 | based on a list of input files.""" |
| 68 | outputs = []; |
| 69 | for filename in files_to_convert: |
| 70 | dirname = posixpath.dirname(filename) |
| 71 | relative_filename = posixpath.relpath(filename, dirname) |
| 72 | relative_filename = ChangeSuffix(relative_filename, 'csv') |
| 73 | output_filename = posixpath.join(output_basedir, relative_filename) |
| 74 | outputs.append(output_filename) |
| 75 | |
| 76 | return outputs |
| 77 | |
| 78 | |
| 79 | def ConvertFiles(files_to_convert, output_basedir): |
| 80 | """Converts files and writes the result the given output directory.""" |
| 81 | for filename in files_to_convert: |
| 82 | dirname = posixpath.dirname(filename) |
| 83 | relative_filename = posixpath.relpath(filename, dirname) |
| 84 | output_filename = posixpath.join(output_basedir, relative_filename) |
| 85 | output_dir = posixpath.dirname(output_filename) |
| 86 | |
| 87 | if not os.path.exists(output_dir): |
| 88 | os.makedirs(output_dir) |
| 89 | |
| 90 | output_filename = ChangeSuffix(output_filename, 'csv') |
| 91 | print 'Converting ' + filename + ' to ' + output_filename |
| 92 | ConvertSingleFile(filename, output_filename) |
| 93 | |
| 94 | |
| 95 | def DoMain(argv): |
| 96 | """Called by GYP using pymod_do_main.""" |
| 97 | parser = argparse.ArgumentParser() |
| 98 | parser.add_argument('-o', dest='output_dir', help='output directory') |
| 99 | parser.add_argument('--inputs', action='store_true', dest='list_inputs', |
| 100 | help='prints a list of all input files') |
| 101 | parser.add_argument('--outputs', action='store_true', dest='list_outputs', |
| 102 | help='prints a list of all output files') |
| 103 | parser.add_argument('input_paths', metavar='path', nargs='+', |
| 104 | help='path to an input file or directory') |
| 105 | options = parser.parse_args(argv) |
| 106 | |
| 107 | files_to_convert = [EscapePath(x) for x in options.input_paths] |
| 108 | |
| 109 | if options.list_inputs: |
| 110 | return '\n'.join(files_to_convert) |
| 111 | |
| 112 | if not options.output_dir: |
| 113 | raise WrongNumberOfArgumentsException('-o required.') |
| 114 | |
| 115 | if options.list_outputs: |
| 116 | outputs = GetOutputs(files_to_convert, options.output_dir) |
| 117 | return '\n'.join(outputs) |
| 118 | |
| 119 | ConvertFiles(files_to_convert, options.output_dir) |
| 120 | return |
| 121 | |
| 122 | |
| 123 | def main(argv): |
| 124 | print 'Running... in main()' |
| 125 | try: |
| 126 | result = DoMain(argv[1:]) |
| 127 | except WrongNumberOfArgumentsException, e: |
| 128 | print >> sys.stderr, e |
| 129 | return 1 |
| 130 | if result: |
| 131 | print result |
| 132 | return 0 |
| 133 | |
| 134 | if __name__ == '__main__': |
| 135 | sys.exit(main(sys.argv)) |