| 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 16 matching lines...) Expand all Loading... |
| 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | 28 |
| 29 import os.path | 29 import os.path |
| 30 import shlex | 30 import shlex |
| 31 import shutil | 31 import shutil |
| 32 import optparse | 32 import optparse |
| 33 | 33 |
| 34 from in_file import InFile | 34 from in_file import InFile |
| 35 | 35 |
| 36 | 36 |
| 37 class Writer(object): | 37 class GenericWriter(object): |
| 38 # Subclasses should override. | |
| 39 class_name = None | |
| 40 defaults = None | |
| 41 valid_values = None | |
| 42 default_parameters = None | |
| 43 | |
| 44 def __init__(self, in_files): | 38 def __init__(self, in_files): |
| 45 if isinstance(in_files, basestring): | |
| 46 in_files = [in_files] | |
| 47 if in_files: | |
| 48 self.in_file = InFile.load_from_files(in_files, self.defaults, self.
valid_values, self.default_parameters) | |
| 49 else: | |
| 50 self.in_file = None | |
| 51 self._outputs = {} # file_name -> generator | 39 self._outputs = {} # file_name -> generator |
| 52 | 40 |
| 53 def _write_file_if_changed(self, output_dir, contents, file_name): | 41 def _write_file_if_changed(self, output_dir, contents, file_name): |
| 54 path = os.path.join(output_dir, file_name) | 42 path = os.path.join(output_dir, file_name) |
| 55 | 43 |
| 56 # The build system should ensure our output directory exists, but just i
n case. | 44 # The build system should ensure our output directory exists, but just i
n case. |
| 57 directory = os.path.dirname(path) | 45 directory = os.path.dirname(path) |
| 58 if not os.path.exists(directory): | 46 if not os.path.exists(directory): |
| 59 os.makedirs(directory) | 47 os.makedirs(directory) |
| 60 | 48 |
| 61 # Only write the file if the contents have changed. This allows ninja to | 49 # Only write the file if the contents have changed. This allows ninja to |
| 62 # skip rebuilding targets which depend on the output. | 50 # skip rebuilding targets which depend on the output. |
| 63 with open(path, "a+") as output_file: | 51 with open(path, "a+") as output_file: |
| 64 output_file.seek(0) | 52 output_file.seek(0) |
| 65 if output_file.read() != contents: | 53 if output_file.read() != contents: |
| 66 output_file.truncate(0) | 54 output_file.truncate(0) |
| 67 output_file.write(contents) | 55 output_file.write(contents) |
| 68 | 56 |
| 69 def write_files(self, output_dir): | 57 def write_files(self, output_dir): |
| 70 for file_name, generator in self._outputs.items(): | 58 for file_name, generator in self._outputs.items(): |
| 71 self._write_file_if_changed(output_dir, generator(), file_name) | 59 self._write_file_if_changed(output_dir, generator(), file_name) |
| 72 | 60 |
| 73 def set_gperf_path(self, gperf_path): | 61 def set_gperf_path(self, gperf_path): |
| 74 self.gperf_path = gperf_path | 62 self.gperf_path = gperf_path |
| 75 | 63 |
| 76 | 64 |
| 65 class Writer(GenericWriter): |
| 66 # Subclasses should override. |
| 67 class_name = None |
| 68 defaults = None |
| 69 valid_values = None |
| 70 default_parameters = None |
| 71 |
| 72 def __init__(self, in_files): |
| 73 super(Writer, self).__init__(in_files) |
| 74 |
| 75 if isinstance(in_files, basestring): |
| 76 in_files = [in_files] |
| 77 if in_files: |
| 78 self.in_file = InFile.load_from_files(in_files, self.defaults, self.
valid_values, self.default_parameters) |
| 79 else: |
| 80 self.in_file = None |
| 81 |
| 82 |
| 77 class Maker(object): | 83 class Maker(object): |
| 78 def __init__(self, writer_class): | 84 def __init__(self, writer_class): |
| 79 self._writer_class = writer_class | 85 self._writer_class = writer_class |
| 80 | 86 |
| 81 def main(self, argv): | 87 def main(self, argv): |
| 82 script_name = os.path.basename(argv[0]) | 88 script_name = os.path.basename(argv[0]) |
| 83 args = argv[1:] | 89 args = argv[1:] |
| 84 if len(args) < 1: | 90 if len(args) < 1: |
| 85 print "USAGE: %s INPUT_FILES" % script_name | 91 print "USAGE: %s INPUT_FILES" % script_name |
| 86 exit(1) | 92 exit(1) |
| 87 | 93 |
| 88 parser = optparse.OptionParser() | 94 parser = optparse.OptionParser() |
| 89 parser.add_option("--gperf", default="gperf") | 95 parser.add_option("--gperf", default="gperf") |
| 90 parser.add_option("--output_dir", default=os.getcwd()) | 96 parser.add_option("--output_dir", default=os.getcwd()) |
| 91 options, args = parser.parse_args() | 97 options, args = parser.parse_args() |
| 92 | 98 |
| 93 writer = self._writer_class(args) | 99 writer = self._writer_class(args) |
| 94 writer.set_gperf_path(options.gperf) | 100 writer.set_gperf_path(options.gperf) |
| 95 writer.write_files(options.output_dir) | 101 writer.write_files(options.output_dir) |
| OLD | NEW |