root/galaxy-central/eggs/GeneTrack-2.0.0_beta_1_dev_48da9e998f0caf01c5be731e926f4b0481f658f0-py2.6.egg/tests/testlib/testutil.py @ 3

リビジョン 3, 2.5 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1import pathfix
2from genetrack import conf
3from genetrack.logger import debug, info, warn, error
4
5import os, sys, unittest, re, time, shutil
6
7
8# enables tests if biopython is present
9try:
10    import Bio
11    biopython = True
12except ImportError, exc:
13    biopython = False
14
15def path_join(*args):
16    return os.path.abspath(os.path.join(*args))
17
18def stop( text ):
19    "Unrecoverable error"
20    error ( text )
21    sys.exit()
22
23class 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
42def 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
49def 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
55def 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
95if __name__ == '__main__':
96    pass
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。