| 1 | """ |
|---|
| 2 | Configuration namespace. |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | """ |
|---|
| 6 | import sys, os, shutil |
|---|
| 7 | import logger |
|---|
| 8 | |
|---|
| 9 | # Python version check |
|---|
| 10 | if sys.version_info < (2, 5): |
|---|
| 11 | logger.error( 'genetrack requires python 2.5 or higher' ) |
|---|
| 12 | sys.exit() |
|---|
| 13 | |
|---|
| 14 | def path_join(*args): |
|---|
| 15 | "Builds absolute path" |
|---|
| 16 | return os.path.abspath(os.path.join(*args)) |
|---|
| 17 | |
|---|
| 18 | # set up paths relative to the location of this file |
|---|
| 19 | curr_dir = os.path.dirname( __file__ ) |
|---|
| 20 | BASE_DIR = path_join( curr_dir, '..' ) |
|---|
| 21 | TEST_DIR = path_join( BASE_DIR, 'tests' ) |
|---|
| 22 | TEST_DATA_DIR = path_join( TEST_DIR, 'testdata' ) |
|---|
| 23 | TEMP_DATA_DIR = path_join( TEST_DIR, 'tempdir' ) |
|---|
| 24 | COVERAGE_DIR = path_join( TEST_DIR, 'coverage' ) |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | def module_check(names, loglevel, exit=True): |
|---|
| 28 | "Verifies that required modules are present" |
|---|
| 29 | |
|---|
| 30 | # required modules |
|---|
| 31 | errflag = False |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | loglevel = loglevel |
|---|
| 35 | for name in names: |
|---|
| 36 | try: |
|---|
| 37 | __import__(name) |
|---|
| 38 | except ImportError, exc: |
|---|
| 39 | if not errflag: |
|---|
| 40 | loglevel('Software requirements not met!') |
|---|
| 41 | loglevel('See http://genetrack.bx.psu.edu for installation instructions') |
|---|
| 42 | loglevel('-' * 20) |
|---|
| 43 | errflag = True |
|---|
| 44 | loglevel('missing module: %s' % name) |
|---|
| 45 | |
|---|
| 46 | # missing dependecies |
|---|
| 47 | if exit and errflag: |
|---|
| 48 | sys.exit() |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | def version_check(): |
|---|
| 52 | # verify some of the versions |
|---|
| 53 | module_versions = [ ('tables', '2.0'), ('numpy', '1.1') ] |
|---|
| 54 | for name, version in module_versions: |
|---|
| 55 | try: |
|---|
| 56 | mod = __import__(name) |
|---|
| 57 | if mod.__version__ < version: |
|---|
| 58 | raise Exception('%s of version %s or higher is required' % (name, version)) |
|---|
| 59 | except Exception, exc: |
|---|
| 60 | logger.error( str(exc) ) |
|---|
| 61 | sys.exit() |
|---|
| 62 | |
|---|
| 63 | #perform the module check |
|---|
| 64 | required = ( 'numpy', 'tables', ) |
|---|
| 65 | module_check( required, loglevel=logger.error ) |
|---|
| 66 | version_check() |
|---|
| 67 | |
|---|
| 68 | def check_server(): |
|---|
| 69 | "Checks for modules that are required to run the server" |
|---|
| 70 | optional = ( 'django', 'pychartdir', 'Image' ) |
|---|
| 71 | module_check( optional, loglevel=logger.error ) |
|---|
| 72 | |
|---|
| 73 | try: |
|---|
| 74 | # monkeypath pytables to disable the Natural Name warning |
|---|
| 75 | import re |
|---|
| 76 | from tables import path |
|---|
| 77 | path._pythonIdRE = re.compile('.') |
|---|
| 78 | except: |
|---|
| 79 | logger.warn( 'could not patch the Natural Name warning' ) |
|---|
| 80 | |
|---|
| 81 | def reset_dir(path): |
|---|
| 82 | "Resets a directory path" |
|---|
| 83 | if os.path.isdir( path ): |
|---|
| 84 | shutil.rmtree( path) |
|---|
| 85 | os.mkdir( path ) |
|---|
| 86 | |
|---|
| 87 | # create the temporary data directory if not present |
|---|
| 88 | if not os.path.isdir( TEMP_DATA_DIR ): |
|---|
| 89 | os.mkdir( TEMP_DATA_DIR ) |
|---|
| 90 | |
|---|
| 91 | def testdata(*args, **kwds): |
|---|
| 92 | "Generates paths to test data" |
|---|
| 93 | path = path_join(TEST_DATA_DIR, *args) |
|---|
| 94 | if 'verify' in kwds: |
|---|
| 95 | if not os.path.isfile(path): |
|---|
| 96 | raise IOError("file '%s' not found" % path ) |
|---|
| 97 | return path |
|---|
| 98 | |
|---|
| 99 | def tempdata(*args): |
|---|
| 100 | "Generates paths to temporary data" |
|---|
| 101 | return path_join(TEMP_DATA_DIR, *args) |
|---|
| 102 | |
|---|
| 103 | def test(verbose=0): |
|---|
| 104 | "Performs module level testing" |
|---|
| 105 | import doctest |
|---|
| 106 | doctest.testmod( verbose=verbose ) |
|---|
| 107 | |
|---|
| 108 | if __name__ == "__main__": |
|---|
| 109 | test() |
|---|