Test Setup Failed
Push — master ( c0fae0...316820 )
by Marcus
53s
created

get_maya_location()   B

Complexity

Conditions 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 22
rs 8.9197
1
"""
2
"""
3
import os
4
import platform
5
import shutil
6
import tempfile
7
import uuid
8
from contextlib import contextmanager
9
10
11
def create_and_return(*args):
12
    dir_ = os.path.join(*args)
13
    if not os.path.exists(dir_):
14
        os.makedirs(dir_)
15
    return dir_
16
17
18
@contextmanager
19
def temp_app_dir():
20
    """Contextmanager to create and remove a temporary app dir to perform
21
    our tests from.
22
    """
23
    maya_app_dir = os.environ.get('MAYA_APP_DIR', '')
24
    try:
25
        tmp_app_dir = create_and_return(
26
            tempfile.gettempdir(), 'maya_app_dir{0}'.format(str(uuid.uuid4())))
27
        os.environ['MAYA_APP_DIR'] = tmp_app_dir
28
        yield tmp_app_dir
29
    finally:
30
        shutil.rmtree(tmp_app_dir)
31
        os.environ['MAYA_APP_DIR'] = maya_app_dir
32
33
34
@contextmanager
35
def clean_maya_environment():
36
    """Contextmanager to reset necessary environment values for a clean
37
    run, then restore overwritten values.
38
    """
39
    restore = False
40
    with temp_app_dir():
41
        script_path = os.environ.get('MAYA_SCRIPT_PATH', '')
42
        module_path = os.environ.get('MAYA_MODULE_PATH', '')
43
        try:
44
            os.environ['MAYA_SCRIPT_PATH'] = ''
45
            os.environ['MAYA_MODULE_PATH'] = ''
46
            yield
47
        finally:
48
            os.environ['MAYA_SCRIPT_PATH'] = script_path
49
            os.environ['MAYA_MODULE_PATH'] = module_path
50
51
52
def get_maya_location(version):
53
    """Find the location of maya installation
54
55
    If MAYA_LOCATION is present in system and/or user variables mayatest
56
    will use whatever is stored there.
57
    """
58
    if 'MAYA_LOCATION' in os.environ.keys():
59
        return os.environ['MAYA_LOCATION']
60
61
    try:
62
        # NOTE: These paths are hardcoded after normal conventions, if
63
        #   a need to have a dynamic solution present start here.
64
        location = {
65
            'Windows': 'C:/Program Files/Autodesk/Maya{0}',
66
            'Darwin': '/Applications/Autodesk/maya{0}/Maya.app/Contents',
67
        }[platform.system()]
68
    except KeyError:
69
        location = '/usr/autodesk/maya{0}'
70
        if version < 2016:
71
            # Starting Maya 2016, the default install directory name changed.
72
            location += '-x64'
73
    return location.format(version)
74
75
76
def mayapy(version):
77
    """Find the mayapy executable path.
78
    """
79
    mayapy_executable = '{0}/bin/mayapy'.format(get_maya_location(version))
80
    if platform.system() == 'Windows':
81
        mayapy_executable += '.exe'
82
    return mayapy_executable
83