|
1 # bootstrapping setuptools |
|
2 import ez_setup |
|
3 ez_setup.use_setuptools() |
|
4 |
|
5 import os |
|
6 import sys |
|
7 import textwrap |
|
8 from distutils.errors import * |
|
9 from distutils.command.clean import clean as _clean |
|
10 from distutils.cmd import Command |
|
11 from setuptools import setup |
|
12 from distutils import log |
|
13 |
|
14 from distutils.core import setup |
|
15 |
|
16 |
|
17 class clean(_clean): |
|
18 """Also cleanup local temp files.""" |
|
19 |
|
20 def run(self): |
|
21 _clean.run(self) |
|
22 |
|
23 import fnmatch |
|
24 |
|
25 # kill temporary files |
|
26 patterns = [ |
|
27 # generic tempfiles |
|
28 '*~', '*.bak', '*.pyc', |
|
29 |
|
30 # tempfiles generated by ANTLR runs |
|
31 't[0-9]*Lexer.py', 't[0-9]*Parser.py', |
|
32 '*.tokens', '*__.g', |
|
33 ] |
|
34 |
|
35 for path in ('antlr3', 'unittests', 'tests'): |
|
36 path = os.path.join(os.path.dirname(__file__), path) |
|
37 if os.path.isdir(path): |
|
38 for root, dirs, files in os.walk(path, topdown=True): |
|
39 graveyard = [] |
|
40 for pat in patterns: |
|
41 graveyard.extend(fnmatch.filter(files, pat)) |
|
42 |
|
43 for name in graveyard: |
|
44 filePath = os.path.join(root, name) |
|
45 |
|
46 try: |
|
47 log.info("removing '%s'", filePath) |
|
48 os.unlink(filePath) |
|
49 except OSError, exc: |
|
50 log.warn( |
|
51 "Failed to delete '%s': %s", |
|
52 filePath, exc |
|
53 ) |
|
54 |
|
55 |
|
56 class TestError(DistutilsError): |
|
57 pass |
|
58 |
|
59 |
|
60 # grml.. the class name appears in the --help output: |
|
61 # ... |
|
62 # Options for 'CmdUnitTest' command |
|
63 # ... |
|
64 # so I have to use a rather ugly name... |
|
65 class unittest(Command): |
|
66 """Run unit tests for package""" |
|
67 |
|
68 description = "run unit tests for package" |
|
69 |
|
70 user_options = [ |
|
71 ] |
|
72 boolean_options = [] |
|
73 |
|
74 def initialize_options(self): |
|
75 pass |
|
76 |
|
77 def finalize_options(self): |
|
78 pass |
|
79 |
|
80 def run(self): |
|
81 testDir = os.path.join(os.path.dirname(__file__), 'unittests') |
|
82 if not os.path.isdir(testDir): |
|
83 raise DistutilsFileError( |
|
84 "There is not 'unittests' directory. Did you fetch the " |
|
85 "development version?", |
|
86 ) |
|
87 |
|
88 import glob |
|
89 import imp |
|
90 import unittest |
|
91 import traceback |
|
92 import StringIO |
|
93 |
|
94 suite = unittest.TestSuite() |
|
95 loadFailures = [] |
|
96 |
|
97 # collect tests from all unittests/test*.py files |
|
98 testFiles = [] |
|
99 for testPath in glob.glob(os.path.join(testDir, 'test*.py')): |
|
100 testFiles.append(testPath) |
|
101 |
|
102 testFiles.sort() |
|
103 for testPath in testFiles: |
|
104 testID = os.path.basename(testPath)[:-3] |
|
105 |
|
106 try: |
|
107 modFile, modPathname, modDescription \ |
|
108 = imp.find_module(testID, [testDir]) |
|
109 |
|
110 testMod = imp.load_module( |
|
111 testID, modFile, modPathname, modDescription |
|
112 ) |
|
113 |
|
114 suite.addTests( |
|
115 unittest.defaultTestLoader.loadTestsFromModule(testMod) |
|
116 ) |
|
117 |
|
118 except Exception: |
|
119 buf = StringIO.StringIO() |
|
120 traceback.print_exc(file=buf) |
|
121 |
|
122 loadFailures.append( |
|
123 (os.path.basename(testPath), buf.getvalue()) |
|
124 ) |
|
125 |
|
126 |
|
127 runner = unittest.TextTestRunner(verbosity=2) |
|
128 result = runner.run(suite) |
|
129 |
|
130 for testName, error in loadFailures: |
|
131 sys.stderr.write('\n' + '='*70 + '\n') |
|
132 sys.stderr.write( |
|
133 "Failed to load test module %s\n" % testName |
|
134 ) |
|
135 sys.stderr.write(error) |
|
136 sys.stderr.write('\n') |
|
137 |
|
138 if not result.wasSuccessful() or loadFailures: |
|
139 raise TestError( |
|
140 "Unit test suite failed!", |
|
141 ) |
|
142 |
|
143 |
|
144 class functest(Command): |
|
145 """Run functional tests for package""" |
|
146 |
|
147 description = "run functional tests for package" |
|
148 |
|
149 user_options = [ |
|
150 ('testcase=', None, |
|
151 "testcase to run [default: run all]"), |
|
152 ('antlr-version=', None, |
|
153 "ANTLR version to use [default: HEAD (in ../../build)]"), |
|
154 ] |
|
155 |
|
156 boolean_options = [] |
|
157 |
|
158 def initialize_options(self): |
|
159 self.testcase = None |
|
160 self.antlr_version = 'HEAD' |
|
161 |
|
162 |
|
163 def finalize_options(self): |
|
164 pass |
|
165 |
|
166 |
|
167 def run(self): |
|
168 import glob |
|
169 import imp |
|
170 import unittest |
|
171 import traceback |
|
172 import StringIO |
|
173 |
|
174 testDir = os.path.join(os.path.dirname(__file__), 'tests') |
|
175 if not os.path.isdir(testDir): |
|
176 raise DistutilsFileError( |
|
177 "There is not 'tests' directory. Did you fetch the " |
|
178 "development version?", |
|
179 ) |
|
180 |
|
181 # make sure, relative imports from testcases work |
|
182 sys.path.insert(0, testDir) |
|
183 |
|
184 rootDir = os.path.abspath( |
|
185 os.path.join(os.path.dirname(__file__), '..', '..')) |
|
186 |
|
187 if self.antlr_version == 'HEAD': |
|
188 classpath = [ |
|
189 os.path.join(rootDir, 'build', 'classes'), |
|
190 os.path.join(rootDir, 'build', 'rtclasses') |
|
191 ] |
|
192 else: |
|
193 classpath = [ |
|
194 os.path.join(rootDir, 'archive', |
|
195 'antlr-%s.jar' % self.antlr_version) |
|
196 ] |
|
197 |
|
198 classpath.extend([ |
|
199 os.path.join(rootDir, 'lib', 'antlr-2.7.7.jar'), |
|
200 os.path.join(rootDir, 'lib', 'stringtemplate-3.2.jar'), |
|
201 os.path.join(rootDir, 'lib', 'junit-4.2.jar') |
|
202 ]) |
|
203 os.environ['CLASSPATH'] = ':'.join(classpath) |
|
204 |
|
205 os.environ['ANTLRVERSION'] = self.antlr_version |
|
206 |
|
207 suite = unittest.TestSuite() |
|
208 loadFailures = [] |
|
209 |
|
210 # collect tests from all tests/t*.py files |
|
211 testFiles = [] |
|
212 for testPath in glob.glob(os.path.join(testDir, 't*.py')): |
|
213 if (testPath.endswith('Lexer.py') |
|
214 or testPath.endswith('Parser.py') |
|
215 ): |
|
216 continue |
|
217 |
|
218 # if a single testcase has been selected, filter out all other |
|
219 # tests |
|
220 if (self.testcase is not None |
|
221 and os.path.basename(testPath)[:-3] != self.testcase |
|
222 ): |
|
223 continue |
|
224 |
|
225 testFiles.append(testPath) |
|
226 |
|
227 testFiles.sort() |
|
228 for testPath in testFiles: |
|
229 testID = os.path.basename(testPath)[:-3] |
|
230 |
|
231 try: |
|
232 modFile, modPathname, modDescription \ |
|
233 = imp.find_module(testID, [testDir]) |
|
234 |
|
235 testMod = imp.load_module( |
|
236 testID, modFile, modPathname, modDescription |
|
237 ) |
|
238 |
|
239 suite.addTests( |
|
240 unittest.defaultTestLoader.loadTestsFromModule(testMod) |
|
241 ) |
|
242 |
|
243 except Exception: |
|
244 buf = StringIO.StringIO() |
|
245 traceback.print_exc(file=buf) |
|
246 |
|
247 loadFailures.append( |
|
248 (os.path.basename(testPath), buf.getvalue()) |
|
249 ) |
|
250 |
|
251 |
|
252 runner = unittest.TextTestRunner(verbosity=2) |
|
253 result = runner.run(suite) |
|
254 |
|
255 for testName, error in loadFailures: |
|
256 sys.stderr.write('\n' + '='*70 + '\n') |
|
257 sys.stderr.write( |
|
258 "Failed to load test module %s\n" % testName |
|
259 ) |
|
260 sys.stderr.write(error) |
|
261 sys.stderr.write('\n') |
|
262 |
|
263 if not result.wasSuccessful() or loadFailures: |
|
264 raise TestError( |
|
265 "Functional test suite failed!", |
|
266 ) |
|
267 |
|
268 |
|
269 setup(name='antlr_python_runtime', |
|
270 version='3.1', |
|
271 packages=['antlr3'], |
|
272 |
|
273 author="Benjamin Niemann", |
|
274 author_email="pink@odahoda.de", |
|
275 url="http://www.antlr.org/", |
|
276 download_url="http://www.antlr.org/download.html", |
|
277 license="BSD", |
|
278 description="Runtime package for ANTLR3", |
|
279 long_description=textwrap.dedent('''\ |
|
280 This is the runtime package for ANTLR3, which is required to use parsers |
|
281 generated by ANTLR3. |
|
282 '''), |
|
283 |
|
284 |
|
285 cmdclass={'unittest': unittest, |
|
286 'functest': functest, |
|
287 'clean': clean |
|
288 }, |
|
289 ) |