summaryrefslogtreecommitdiffstats
path: root/google_appengine/lib/antlr3/setup.py
blob: 078deef93dcc13792ac56c4839ebd309d9c49ee7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# bootstrapping setuptools
import ez_setup
ez_setup.use_setuptools()

import os
import sys
import textwrap
from distutils.errors import *
from distutils.command.clean import clean as _clean
from distutils.cmd import Command
from setuptools import setup
from distutils import log

from distutils.core import setup


class clean(_clean):
    """Also cleanup local temp files."""

    def run(self):
        _clean.run(self)

        import fnmatch
        
        # kill temporary files
        patterns = [
            # generic tempfiles
            '*~', '*.bak', '*.pyc',

            # tempfiles generated by ANTLR runs
            't[0-9]*Lexer.py', 't[0-9]*Parser.py',
            '*.tokens', '*__.g',
            ]
            
        for path in ('antlr3', 'unittests', 'tests'):
            path = os.path.join(os.path.dirname(__file__), path)
            if os.path.isdir(path):
                for root, dirs, files in os.walk(path, topdown=True):
                    graveyard = []                    
                    for pat in patterns:
                        graveyard.extend(fnmatch.filter(files, pat))

                    for name in graveyard:
                        filePath = os.path.join(root, name)

                        try:
                            log.info("removing '%s'", filePath)
                            os.unlink(filePath)
                        except OSError, exc:
                            log.warn(
                                "Failed to delete '%s': %s",
                                filePath, exc
                                )

            
class TestError(DistutilsError):
    pass


# grml.. the class name appears in the --help output:
# ...
# Options for 'CmdUnitTest' command
# ...
# so I have to use a rather ugly name...
class unittest(Command):
    """Run unit tests for package"""

    description = "run unit tests for package"

    user_options = [
        ]
    boolean_options = []

    def initialize_options(self):
        pass
    
    def finalize_options(self):
        pass
    
    def run(self):
        testDir = os.path.join(os.path.dirname(__file__), 'unittests')
        if not os.path.isdir(testDir):
            raise DistutilsFileError(
                "There is not 'unittests' directory. Did you fetch the "
                "development version?",
                )

        import glob
        import imp
        import unittest
        import traceback
        import StringIO
        
        suite = unittest.TestSuite()
        loadFailures = []
        
        # collect tests from all unittests/test*.py files
        testFiles = []
        for testPath in glob.glob(os.path.join(testDir, 'test*.py')):
            testFiles.append(testPath)

        testFiles.sort()
        for testPath in testFiles:
            testID = os.path.basename(testPath)[:-3]

            try:
                modFile, modPathname, modDescription \
                         = imp.find_module(testID, [testDir])

                testMod = imp.load_module(
                    testID, modFile, modPathname, modDescription
                    )
                
                suite.addTests(
                    unittest.defaultTestLoader.loadTestsFromModule(testMod)
                    )
                
            except Exception:
                buf = StringIO.StringIO()
                traceback.print_exc(file=buf)
                
                loadFailures.append(
                    (os.path.basename(testPath), buf.getvalue())
                    )              
                
            
        runner = unittest.TextTestRunner(verbosity=2)
        result = runner.run(suite)

        for testName, error in loadFailures:
            sys.stderr.write('\n' + '='*70 + '\n')
            sys.stderr.write(
                "Failed to load test module %s\n" % testName
                )
            sys.stderr.write(error)
            sys.stderr.write('\n')
            
        if not result.wasSuccessful() or loadFailures:
            raise TestError(
                "Unit test suite failed!",
                )
            

class functest(Command):
    """Run functional tests for package"""

    description = "run functional tests for package"

    user_options = [
        ('testcase=', None,
         "testcase to run [default: run all]"),
        ('antlr-version=', None,
         "ANTLR version to use [default: HEAD (in ../../build)]"),
        ]
    
    boolean_options = []

    def initialize_options(self):
        self.testcase = None
        self.antlr_version = 'HEAD'

    
    def finalize_options(self):
        pass

    
    def run(self):
        import glob
        import imp
        import unittest
        import traceback
        import StringIO
        
        testDir = os.path.join(os.path.dirname(__file__), 'tests')
        if not os.path.isdir(testDir):
            raise DistutilsFileError(
                "There is not 'tests' directory. Did you fetch the "
                "development version?",
                )

        # make sure, relative imports from testcases work
        sys.path.insert(0, testDir)

        rootDir = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', '..'))

        if self.antlr_version == 'HEAD':
            classpath = [
                os.path.join(rootDir, 'build', 'classes'),
                os.path.join(rootDir, 'build', 'rtclasses')
                ]
        else:
            classpath = [
                os.path.join(rootDir, 'archive',
                             'antlr-%s.jar' % self.antlr_version)
                ]

        classpath.extend([
            os.path.join(rootDir, 'lib', 'antlr-2.7.7.jar'),
            os.path.join(rootDir, 'lib', 'stringtemplate-3.2.jar'),
            os.path.join(rootDir, 'lib', 'junit-4.2.jar')
            ])
        os.environ['CLASSPATH'] = ':'.join(classpath)

        os.environ['ANTLRVERSION'] = self.antlr_version

        suite = unittest.TestSuite()
        loadFailures = []
        
        # collect tests from all tests/t*.py files
        testFiles = []
        for testPath in glob.glob(os.path.join(testDir, 't*.py')):
            if (testPath.endswith('Lexer.py')
                or testPath.endswith('Parser.py')
                ):
                continue

            # if a single testcase has been selected, filter out all other
            # tests
            if (self.testcase is not None
                and os.path.basename(testPath)[:-3] != self.testcase
                ):
                continue
            
            testFiles.append(testPath)

        testFiles.sort()
        for testPath in testFiles:
            testID = os.path.basename(testPath)[:-3]

            try:
                modFile, modPathname, modDescription \
                         = imp.find_module(testID, [testDir])

                testMod = imp.load_module(
                    testID, modFile, modPathname, modDescription
                    )
                
                suite.addTests(
                    unittest.defaultTestLoader.loadTestsFromModule(testMod)
                    )
                
            except Exception:
                buf = StringIO.StringIO()
                traceback.print_exc(file=buf)
                
                loadFailures.append(
                    (os.path.basename(testPath), buf.getvalue())
                    )              
                
            
        runner = unittest.TextTestRunner(verbosity=2)
        result = runner.run(suite)

        for testName, error in loadFailures:
            sys.stderr.write('\n' + '='*70 + '\n')
            sys.stderr.write(
                "Failed to load test module %s\n" % testName
                )
            sys.stderr.write(error)
            sys.stderr.write('\n')
            
        if not result.wasSuccessful() or loadFailures:
            raise TestError(
                "Functional test suite failed!",
                )
            

setup(name='antlr_python_runtime',
      version='3.1.1',
      packages=['antlr3'],

      author="Benjamin Niemann",
      author_email="pink@odahoda.de",
      url="http://www.antlr.org/",
      download_url="http://www.antlr.org/download.html",
      license="BSD",
      description="Runtime package for ANTLR3",
      long_description=textwrap.dedent('''\
      This is the runtime package for ANTLR3, which is required to use parsers
      generated by ANTLR3.
      '''),
      
      
      cmdclass={'unittest': unittest,
                'functest': functest,
                'clean': clean
                },
      )