[3] | 1 | import pathfix |
---|
| 2 | from genetrack import conf |
---|
| 3 | from genetrack.logger import debug, info, warn, error |
---|
| 4 | |
---|
| 5 | import os, sys, unittest, re, time, shutil |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | # enables tests if biopython is present |
---|
| 9 | try: |
---|
| 10 | import Bio |
---|
| 11 | biopython = True |
---|
| 12 | except ImportError, exc: |
---|
| 13 | biopython = False |
---|
| 14 | |
---|
| 15 | def path_join(*args): |
---|
| 16 | return os.path.abspath(os.path.join(*args)) |
---|
| 17 | |
---|
| 18 | def stop( text ): |
---|
| 19 | "Unrecoverable error" |
---|
| 20 | error ( text ) |
---|
| 21 | sys.exit() |
---|
| 22 | |
---|
| 23 | class Timer: |
---|
| 24 | |
---|
| 25 | def __init__(self): |
---|
| 26 | self.reset() |
---|
| 27 | |
---|
| 28 | def reset(self): |
---|
| 29 | self.start = time.time() |
---|
| 30 | |
---|
| 31 | def display(self, msg, reps=1): |
---|
| 32 | total = (time.time() - self.start ) |
---|
| 33 | |
---|
| 34 | if reps == 1: |
---|
| 35 | info( "%s takes %.3f seconds" % (msg, total) ) |
---|
| 36 | else: |
---|
| 37 | value = total/reps |
---|
| 38 | info( "%s performs at %.3f per second" % (msg, value) ) |
---|
| 39 | |
---|
| 40 | self.reset() |
---|
| 41 | |
---|
| 42 | def reset_tempdir( tempdir=None ): |
---|
| 43 | "Deletes and recreates the temporary directory" |
---|
| 44 | tempdir = tempdir or conf.temp_data_dir |
---|
| 45 | if os.path.isdir( tempdir ): |
---|
| 46 | shutil.rmtree( tempdir ) |
---|
| 47 | os.mkdir( tempdir ) |
---|
| 48 | |
---|
| 49 | def make_suite( tests ): |
---|
| 50 | "Makes a test suite from a list of TestCase classes" |
---|
| 51 | loader = unittest.TestLoader().loadTestsFromTestCase |
---|
| 52 | suites = map( loader, tests ) |
---|
| 53 | return unittest.TestSuite( suites ) |
---|
| 54 | |
---|
| 55 | def generate_coverage( func, path, *args, **kwds): |
---|
| 56 | """ |
---|
| 57 | Generates code coverage for the function |
---|
| 58 | and places the results in the path |
---|
| 59 | """ |
---|
| 60 | |
---|
| 61 | import figleaf |
---|
| 62 | from figleaf import annotate_html |
---|
| 63 | |
---|
| 64 | # Fix for figleaf misbehaving. It is adding a logger at root level |
---|
| 65 | # and that will add a handler to all subloggers (ours as well) |
---|
| 66 | # needs to be fixed in figleaf |
---|
| 67 | import logging |
---|
| 68 | root = logging.getLogger() |
---|
| 69 | # remove all root handlers |
---|
| 70 | for hand in root.handlers: |
---|
| 71 | root.removeHandler(hand) |
---|
| 72 | |
---|
| 73 | if os.path.isdir( path ): |
---|
| 74 | shutil.rmtree( path ) |
---|
| 75 | |
---|
| 76 | info( "collecting coverage information" ) |
---|
| 77 | |
---|
| 78 | figleaf.start() |
---|
| 79 | # execute the function itself |
---|
| 80 | return_vals = func( *args, **kwds) |
---|
| 81 | figleaf.stop() |
---|
| 82 | |
---|
| 83 | info( 'generating coverage' ) |
---|
| 84 | coverage = figleaf.get_data().gather_files() |
---|
| 85 | |
---|
| 86 | annotate_html.prepare_reportdir( path ) |
---|
| 87 | |
---|
| 88 | # skip python modules and the test modules |
---|
| 89 | regpatt = lambda patt: re.compile( patt, re.IGNORECASE ) |
---|
| 90 | patterns = map( regpatt, [ 'python', 'tests', 'django', 'path*' ] ) |
---|
| 91 | annotate_html.report_as_html( coverage, path, exclude_patterns=patterns, files_list='') |
---|
| 92 | |
---|
| 93 | return return_vals |
---|
| 94 | |
---|
| 95 | if __name__ == '__main__': |
---|
| 96 | pass |
---|