| OLD | NEW |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 import os | 29 import os |
| 30 import os.path | 30 import os.path |
| 31 import shlex | 31 import shlex |
| 32 import shutil | 32 import shutil |
| 33 import optparse | 33 import optparse |
| 34 | 34 |
| 35 from in_file import InFile | 35 from in_file import InFile |
| 36 | 36 |
| 37 | 37 |
| 38 class Writer(object): | 38 class GenericWriter(object): |
| 39 # Subclasses should override. | |
| 40 class_name = None | |
| 41 defaults = None | |
| 42 valid_values = None | |
| 43 default_parameters = None | |
| 44 | |
| 45 def __init__(self, in_files): | 39 def __init__(self, in_files): |
| 46 if isinstance(in_files, basestring): | |
| 47 in_files = [in_files] | |
| 48 if in_files: | |
| 49 self.in_file = InFile.load_from_files(in_files, self.defaults, self.
valid_values, self.default_parameters) | |
| 50 else: | |
| 51 self.in_file = None | |
| 52 self._outputs = {} # file_name -> generator | 40 self._outputs = {} # file_name -> generator |
| 53 | 41 |
| 54 def _write_file_if_changed(self, output_dir, contents, file_name): | 42 def _write_file_if_changed(self, output_dir, contents, file_name): |
| 55 path = os.path.join(output_dir, file_name) | 43 path = os.path.join(output_dir, file_name) |
| 56 | 44 |
| 57 # The build system should ensure our output directory exists, but just i
n case. | 45 # The build system should ensure our output directory exists, but just i
n case. |
| 58 directory = os.path.dirname(path) | 46 directory = os.path.dirname(path) |
| 59 if not os.path.exists(directory): | 47 if not os.path.exists(directory): |
| 60 os.makedirs(directory) | 48 os.makedirs(directory) |
| 61 | 49 |
| 62 # Only write the file if the contents have changed. This allows ninja to | 50 # Only write the file if the contents have changed. This allows ninja to |
| 63 # skip rebuilding targets which depend on the output. | 51 # skip rebuilding targets which depend on the output. |
| 64 with open(path, "a+") as output_file: | 52 with open(path, "a+") as output_file: |
| 65 output_file.seek(0) | 53 output_file.seek(0) |
| 66 if output_file.read() != contents: | 54 if output_file.read() != contents: |
| 67 output_file.truncate(0) | 55 output_file.truncate(0) |
| 68 output_file.write(contents) | 56 output_file.write(contents) |
| 69 | 57 |
| 70 def write_files(self, output_dir): | 58 def write_files(self, output_dir): |
| 71 for file_name, generator in self._outputs.items(): | 59 for file_name, generator in self._outputs.items(): |
| 72 self._write_file_if_changed(output_dir, generator(), file_name) | 60 self._write_file_if_changed(output_dir, generator(), file_name) |
| 73 | 61 |
| 74 def set_gperf_path(self, gperf_path): | 62 def set_gperf_path(self, gperf_path): |
| 75 self.gperf_path = gperf_path | 63 self.gperf_path = gperf_path |
| 76 | 64 |
| 77 | 65 |
| 66 class Writer(GenericWriter): |
| 67 # Subclasses should override. |
| 68 class_name = None |
| 69 defaults = None |
| 70 valid_values = None |
| 71 default_parameters = None |
| 72 |
| 73 def __init__(self, in_files): |
| 74 super(Writer, self).__init__(in_files) |
| 75 |
| 76 if isinstance(in_files, basestring): |
| 77 in_files = [in_files] |
| 78 if in_files: |
| 79 self.in_file = InFile.load_from_files(in_files, self.defaults, self.
valid_values, self.default_parameters) |
| 80 else: |
| 81 self.in_file = None |
| 82 |
| 83 |
| 78 class Maker(object): | 84 class Maker(object): |
| 79 def __init__(self, writer_class): | 85 def __init__(self, writer_class): |
| 80 self._writer_class = writer_class | 86 self._writer_class = writer_class |
| 81 | 87 |
| 82 def main(self, argv): | 88 def main(self, argv): |
| 83 script_name = os.path.basename(argv[0]) | 89 script_name = os.path.basename(argv[0]) |
| 84 args = argv[1:] | 90 args = argv[1:] |
| 85 if len(args) < 1: | 91 if len(args) < 1: |
| 86 print "USAGE: %s INPUT_FILES" % script_name | 92 print "USAGE: %s INPUT_FILES" % script_name |
| 87 exit(1) | 93 exit(1) |
| 88 | 94 |
| 89 parser = optparse.OptionParser() | 95 parser = optparse.OptionParser() |
| 90 | 96 |
| 91 parser.add_option("--gperf", default="gperf") | 97 parser.add_option("--gperf", default="gperf") |
| 92 parser.add_option("--developer_dir", | 98 parser.add_option("--developer_dir", |
| 93 help='Path to Xcode.') | 99 help='Path to Xcode.') |
| 94 parser.add_option("--output_dir", default=os.getcwd()) | 100 parser.add_option("--output_dir", default=os.getcwd()) |
| 95 options, args = parser.parse_args() | 101 options, args = parser.parse_args() |
| 96 | 102 |
| 97 if options.developer_dir: | 103 if options.developer_dir: |
| 98 os.environ['DEVELOPER_DIR'] = options.developer_dir | 104 os.environ['DEVELOPER_DIR'] = options.developer_dir |
| 99 | 105 |
| 100 writer = self._writer_class(args) | 106 writer = self._writer_class(args) |
| 101 writer.set_gperf_path(options.gperf) | 107 writer.set_gperf_path(options.gperf) |
| 102 writer.write_files(options.output_dir) | 108 writer.write_files(options.output_dir) |
| OLD | NEW |