summaryrefslogtreecommitdiff
path: root/weave/tests/compiler_test_utils.py
blob: 110e7f2402e17bf0c8855e7b7550c537bf51acb1 (plain)
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
import os,sys,string
import pprint 

def remove_whitespace(in_str):
    import string
    out = string.replace(in_str," ","")
    out = string.replace(out,"\t","")
    out = string.replace(out,"\n","")
    return out
    
def print_assert_equal(test_string,actual,desired):
    """this should probably be in scipy_test
    """
    try:
        assert(actual == desired)
    except AssertionError:
        import cStringIO
        msg = cStringIO.StringIO()
        msg.write(test_string)
        msg.write(' failed\nACTUAL: \n')
        pprint.pprint(actual,msg)
        msg.write('DESIRED: \n')
        pprint.pprint(desired,msg)
        raise AssertionError, msg.getvalue()

###################################################
# mainly used by catalog tests               
###################################################
from scipy_distutils.misc_util import add_grandparent_to_path,restore_path

add_grandparent_to_path(__name__)
import catalog
restore_path()

def temp_catalog_files():
    d = catalog.default_dir()
    f = catalog.os_dependent_catalog_name()
    suffixes = ['.dat','.dir','']
    cat_files = [os.path.join(d,f+suffix) for suffix in suffixes]
    return cat_files

def clear_temp_catalog():
    """ Remove any catalog from the temp dir
    """
    cat_files = temp_catalog_files()
    for catalog_file in cat_files:
        if os.path.exists(catalog_file):
            if os.path.exists(catalog_file+'.bak'):
                os.remove(catalog_file+'.bak')
            os.rename(catalog_file,catalog_file+'.bak')

def restore_temp_catalog():
    """ Remove any catalog from the temp dir
    """
    cat_files = temp_catalog_files()
    for catalog_file in cat_files:
        if os.path.exists(catalog_file+'.bak'):
            if os.path.exists(catalog_file):            
                os.remove(catalog_file)
            os.rename(catalog_file+'.bak',catalog_file)

def empty_temp_dir():
    """ Create a sub directory in the temp directory for use in tests
    """
    import tempfile
    d = catalog.default_dir()
    for i in range(10000):
        new_d = os.path.join(d,tempfile.gettempprefix()[1:-1]+`i`)
        if not os.path.exists(new_d):
            os.mkdir(new_d)
            break
    return new_d

def cleanup_temp_dir(d):
    """ Remove a directory created by empty_temp_dir
        should probably catch errors
    """
    files = map(lambda x,d=d: os.path.join(d,x),os.listdir(d))
    for i in files:
        if os.path.isdir(i):
            cleanup_temp_dir(i)
        else:
            os.remove(i)
    os.rmdir(d)

def simple_module(directory,name,function_prefix,count=2):
    module_name = os.path.join(directory,name+'.py')
    func = "def %(function_prefix)s%(fid)d():\n    pass\n"
    code = ''
    for fid in range(count):
        code+= func % locals()
    open(module_name,'w').write(code)
    sys.path.append(directory)    
    exec "import " + name
    funcs = []
    for i in range(count):
        funcs.append(eval(name+'.'+function_prefix+`i`))
    sys.path = sys.path[:-1]    
    return module_name, funcs