OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
3 # for details. All rights reserved. Use of this source code is governed by a | |
4 # BSD-style license that can be found in the LICENSE file. | |
5 | |
6 from __future__ import with_statement | |
7 import StringIO | |
8 import os | |
9 import sys | |
10 | |
11 class GenerateError(Exception): | |
12 | |
13 def __init__(self, value): | |
14 self.value = value | |
15 | |
16 def __str__(self): | |
17 return repr(self.value) | |
18 | |
19 | |
20 class Generator: | |
21 | |
22 def __init__(self, base_directory, name, output, path, *excludes): | |
23 self.base_directory = base_directory | |
24 self.name = name | |
25 self.output = output | |
26 self.path = path | |
27 self.excludes = set() | |
28 for x in excludes: | |
29 self.excludes.add(x) | |
30 self.sources = [] | |
31 self.resources = [] | |
32 | |
33 def _list_files(self): | |
34 start_directory = os.path.join(self.base_directory, self.path) | |
35 for fullpath, dirs, filenames in os.walk(start_directory): | |
36 path = fullpath[len(start_directory) + 1:] | |
37 remove_me = [d for d in dirs if d.startswith('.') or | |
38 d == 'CVS' or | |
39 (d in self.excludes)] | |
40 for d in remove_me: | |
41 dirs.remove(d) | |
42 for filename in filenames: | |
43 if (filename.endswith('.java')): | |
44 self.sources.append(os.path.join(path, filename)) | |
45 elif (filename.endswith('~')): | |
46 pass | |
47 elif (filename.endswith('.pyc')): | |
48 pass | |
49 else: | |
50 self.resources.append(os.path.join(path, filename)) | |
51 self.sources.sort() | |
52 self.resources.sort() | |
53 | |
54 def _print_gypi_files(self, out, name, files): | |
55 out.write(" '%s': [\n" % name) | |
56 for filename in files: | |
57 out.write(''' r'%s/%s',%s''' % (self.path, filename,'\n')) | |
58 out.write(" ],\n") | |
59 | |
60 def _print_txt_files(self, out, files): | |
61 for filename in files: | |
62 out.write('%s\n' % os.path.join(self.path, filename)) | |
63 | |
64 def _print_ant_files(self, out, name, files): | |
65 out.write(" <filelist id='%s' dir='%s'>\n" % (name, self.path)) | |
66 for filename in files: | |
67 out.write(" <file name='%s'/>\n" % filename) | |
68 out.write(" </filelist>\n") | |
69 out.write(" <pathconvert pathsep=',' property='%s' refid='%s'>\n" | |
70 % (name, name)) | |
71 out.write(" <map from='${basedir}/%s/' to=''/>\n" % self.path) | |
72 out.write(" </pathconvert>\n") | |
73 | |
74 def _make_output(self, file_name): | |
75 if os.path.exists(file_name): | |
76 return StringIO.StringIO() | |
77 else: | |
78 return file(file_name, 'w') | |
79 | |
80 def _close(self, file_name, output): | |
81 if not isinstance(output, StringIO.StringIO): | |
82 output.close() | |
83 return | |
84 new_text = output.getvalue() | |
85 output.close() | |
86 with open(file_name, 'r') as f: | |
87 old_text = f.read() | |
88 if old_text == new_text: | |
89 return | |
90 sys.stderr.write('Updating %s\n' % file_name) | |
91 with open(file_name, 'w') as f: | |
92 f.write(new_text) | |
93 | |
94 def generate(self): | |
95 self._list_files() | |
96 | |
97 file_name = self.output + '.gypi'; | |
98 gypi = self._make_output(file_name) | |
99 gypi.write("{\n 'variables': {\n") | |
100 self._print_gypi_files(gypi, self.name + '_sources', self.sources) | |
101 self._print_gypi_files(gypi, self.name + '_resources', self.resources) | |
102 gypi.write(" },\n}\n") | |
103 self._close(file_name, gypi) | |
104 | |
105 file_name = self.output + '.xml' | |
106 ant = self._make_output(file_name) | |
107 ant.write("<project>\n") | |
108 self._print_ant_files(ant, self.name + '_sources', self.sources) | |
109 self._print_ant_files(ant, self.name + '_resources', self.resources) | |
110 ant.write("</project>\n") | |
111 self._close(file_name, ant) | |
112 | |
113 file_name = self.output + '.txt'; | |
114 txt = self._make_output(file_name) | |
115 self._print_txt_files(txt, self.sources) | |
116 self._close(file_name, txt) | |
117 | |
118 | |
119 def Main(script_name = None, name = None, output = None, path = None, | |
120 *rest): | |
121 if not path: | |
122 raise GenerateError("usage: %s NAME OUTPUT PATH EXCLUDE_DIR_NAME ..." | |
123 % script_name) | |
124 base_directory = os.path.dirname(output) | |
125 Generator(base_directory, name, output, path, *rest).generate() | |
126 | |
127 | |
128 if __name__ == '__main__': | |
129 sys.exit(Main(*sys.argv)) | |
OLD | NEW |