OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | |
2 # | |
3 # Redistribution and use in source and binary forms, with or without | |
4 # modification, are permitted provided that the following conditions are | |
5 # met: | |
6 # | |
7 # * Redistributions of source code must retain the above copyright | |
8 # notice, this list of conditions and the following disclaimer. | |
9 # * Redistributions in binary form must reproduce the above | |
10 # copyright notice, this list of conditions and the following disclaimer | |
11 # in the documentation and/or other materials provided with the | |
12 # distribution. | |
13 # * Neither the name of Google Inc. nor the names of its | |
14 # contributors may be used to endorse or promote products derived from | |
15 # this software without specific prior written permission. | |
16 # | |
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 import os.path | |
30 import shutil | |
31 | |
32 from in_file import InFile | |
33 | |
34 | |
35 class Writer(object): | |
36 # Subclasses should override. | |
37 class_name = None | |
eseidel
2013/04/30 21:07:29
Why is this a class-level property?
abarth-chromium
2013/04/30 21:09:41
We might end up making it an instance-level proper
| |
38 defaults = None | |
eseidel
2013/04/30 21:07:29
This makes more sense as a class-level property.
| |
39 | |
40 def __init__(self, in_file_path): | |
41 self.in_file = InFile.load_from_path(in_file_path, self.defaults) | |
42 | |
43 # Subclasses should override. | |
44 def generate_header(self): | |
45 raise NotImplementedError | |
46 | |
47 # Subclasses should override. | |
48 def generate_implementation(self): | |
49 raise NotImplementedError | |
50 | |
51 def _forcibly_create_text_file_at_path_with_contents(self, file_path, conten ts): | |
52 # FIXME: This method can be made less force-full anytime after 6/1/2013. | |
53 # A gyp error was briefly checked into the tree, causing | |
54 # a directory to have been generated in place of one of | |
55 # our output files. Clean up after that error so that | |
56 # all users don't need to clobber their output directories. | |
57 shutil.rmtree(file_path, ignore_errors=True) | |
58 # The build system should ensure our output directory exists, but just i n case. | |
59 directory = os.path.dirname(file_path) | |
60 if not os.path.exists(directory): | |
61 os.makedirs(directory) | |
62 | |
63 with open(file_path, "w") as file_to_write: | |
64 file_to_write.write(contents) | |
65 | |
66 def write_header(self, output_dir): | |
67 header_path = os.path.join(output_dir, self.class_name + ".h") | |
68 self._forcibly_create_text_file_at_path_with_contents(header_path, self. generate_header()) | |
69 | |
70 def write_implmentation(self, output_dir): | |
71 implmentation_path = os.path.join(output_dir, self.class_name + ".cpp") | |
72 self._forcibly_create_text_file_at_path_with_contents(implmentation_path , self.generate_implementation()) | |
73 | |
74 | |
75 class Maker(object): | |
76 def __init__(self, writer_class): | |
77 self._writer_class = writer_class | |
78 | |
79 def main(self, argv): | |
80 script_name = os.path.basename(argv[0]) | |
81 args = argv[1:] | |
82 if len(args) < 1: | |
83 print "USAGE: %i INPUT_FILE [OUTPUT_DIRECTORY]" % script_name | |
84 exit(1) | |
85 output_dir = args[1] if len(args) > 1 else os.getcwd() | |
86 | |
87 writer = self._writer_class(args[0]) | |
88 writer.write_header(output_dir) | |
89 writer.write_implmentation(output_dir) | |
OLD | NEW |