ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/WMCORE/setup.py
Revision: 1.32
Committed: Wed Sep 9 11:25:39 2009 UTC (15 years, 7 months ago) by metson
Content type: text/x-python
Branch: MAIN
Changes since 1.31: +9 -4 lines
Log Message:
Failed unit tests now return exit code 1

File Contents

# User Rev Content
1 fvlingen 1.1 #!/usr/bin/env python
2 metson 1.21 from distutils.core import setup, Command
3     from unittest import TextTestRunner, TestLoader, TestSuite
4     from glob import glob
5     from os.path import splitext, basename, join as pjoin, walk
6 metson 1.30 import os, sys
7 sfoulkes 1.29 try:
8     from pylint import lint
9     except:
10     pass
11 fvlingen 1.1
12 metson 1.22 """
13     Build, clean and test the WMCore package.
14     """
15    
16 metson 1.21 class TestCommand(Command):
17 metson 1.22 """
18     Handle setup.py test with this class - walk through the directory structure
19     and build up a list of tests, then build a test suite and execute it.
20    
21     TODO: Pull database URL's from environment, and skip tests where database
22     URL is not present (e.g. for a slave without Oracle connection)
23     """
24 metson 1.21 user_options = [ ]
25    
26     def initialize_options(self):
27     self._dir = os.getcwd()
28    
29     def finalize_options(self):
30     pass
31    
32     def run(self):
33     '''
34 metson 1.25 Finds all the tests modules in test/python/WMCore_t, and runs them.
35 metson 1.21 '''
36     testfiles = [ ]
37 metson 1.26
38     # Add the test and src directory to the python path
39     testspypath = '/'.join([self._dir, 'test/python/'])
40     srcpypath = '/'.join([self._dir, 'src/python/'])
41 metson 1.30 sys.path.append(testspypath)
42     sys.path.append(srcpypath)
43    
44 metson 1.21 # Walk the directory tree
45     for dirpath, dirnames, filenames in os.walk('./test/python/WMCore_t'):
46     # skipping CVS directories and their contents
47     pathelements = dirpath.split('/')
48     if not 'CVS' in pathelements:
49     # to build up a list of file names which contain tests
50     for file in filenames:
51     if file not in ['__init__.py']:
52     if file.endswith('_t.py'):
53     testmodpath = pathelements[3:]
54     testmodpath.append(file.replace('.py',''))
55     testfiles.append('.'.join(testmodpath))
56    
57     testsuite = TestSuite()
58     for test in testfiles:
59     try:
60     testsuite.addTest(TestLoader().loadTestsFromName(test))
61     except Exception, e:
62     print "Could not load %s test - fix it!\n %s" % (test, e)
63     print "Running %s tests" % testsuite.countTestCases()
64    
65     t = TextTestRunner(verbosity = 1)
66 metson 1.32 result = t.run(testsuite)
67     if not result.wasSuccessful():
68     sys.exit("Tests unsuccessful. There were %s failures and %s errors"\
69     % (len(result.failures), len(result.errors)))
70    
71 metson 1.26
72 metson 1.21 class CleanCommand(Command):
73 metson 1.22 """
74     Clean up (delete) compiled files
75     """
76 metson 1.21 user_options = [ ]
77    
78     def initialize_options(self):
79     self._clean_me = [ ]
80     for root, dirs, files in os.walk('.'):
81     for f in files:
82     if f.endswith('.pyc'):
83     self._clean_me.append(pjoin(root, f))
84    
85     def finalize_options(self):
86     pass
87    
88     def run(self):
89     for clean_me in self._clean_me:
90     try:
91     os.unlink(clean_me)
92     except:
93     pass
94    
95 metson 1.23 class LintCommand(Command):
96 metson 1.24 """
97     Lint all files in the src tree
98    
99     TODO: better format the test results, get some global result, make output
100     more buildbot friendly.
101     """
102    
103 metson 1.23 user_options = [ ]
104    
105     def initialize_options(self):
106     self._dir = os.getcwd()
107    
108     def finalize_options(self):
109     pass
110    
111     def run(self):
112     '''
113     Find the code and run lint on it
114     '''
115     files = [ ]
116 metson 1.26
117     srcpypath = '/'.join([self._dir, 'src/python/'])
118 metson 1.30 sys.path.append(srcpypath)
119 metson 1.26
120 metson 1.23 # Walk the directory tree
121     for dirpath, dirnames, filenames in os.walk('./src/python/'):
122     # skipping CVS directories and their contents
123     pathelements = dirpath.split('/')
124 metson 1.32 result = None
125 metson 1.23 if not 'CVS' in pathelements:
126     # to build up a list of file names which contain tests
127     for file in filenames:
128 metson 1.27 if file.endswith('.py'):
129     filepath = '/'.join([dirpath, file])
130     files.append(filepath)
131     # run individual tests as follows
132     try:
133 metson 1.32 result = lint.Run(['--rcfile=standards/.pylintrc',
134 metson 1.28 '--output-format=parseable',
135     filepath])
136 metson 1.27 except Exception, e:
137     print "Couldn't lint %s\n%s %s" % (file, e, type(e))
138 metson 1.23 # Could run a global test as:
139     #input = ['--rcfile=standards/.pylintrc']
140     #input.extend(files)
141 metson 1.32 #lint.Run(input)
142     print result
143 metson 1.23
144 metson 1.22 def getPackages(package_dirs = []):
145     packages = []
146     for dir in package_dirs:
147     for dirpath, dirnames, filenames in os.walk('./%s' % dir):
148     # Exclude things here
149     if dirpath not in ['./src/python/', './src/python/IMProv']:
150     pathelements = dirpath.split('/')
151     if not 'CVS' in pathelements:
152     path = pathelements[3:]
153     packages.append('.'.join(path))
154     return packages
155    
156     package_dir = {'WMCore': 'src/python/WMCore',
157     'WMComponent' : 'src/python/WMComponent',
158     'WMQuality' : 'src/python/WMQuality'}
159    
160     setup (name = 'wmcore',
161     version = '1.0',
162 metson 1.23 cmdclass = { 'test': TestCommand,
163     'clean': CleanCommand,
164     'lint': LintCommand },
165 metson 1.22 package_dir = package_dir,
166     packages = getPackages(package_dir.values()),)
167 fvlingen 1.7