Passed
Push — master ( a407e1...fc9b54 )
by Marcus
41s
created

clean_maya_environment()   A

Complexity

Conditions 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
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
    """Create a clean maya app dir to perform tests from.
21
    """
22
    maya_app_dir = os.environ.get('MAYA_APP_DIR', '')
23
    try:
24
        tmp_app_dir = create_and_return(
25
            tempfile.gettempdir(), 'maya_app_dir{0}'.format(str(uuid.uuid4())))
26
        os.environ['MAYA_APP_DIR'] = tmp_app_dir
27
        yield tmp_app_dir
28
    finally:
29
        shutil.rmtree(tmp_app_dir)
30
        os.environ['MAYA_APP_DIR'] = maya_app_dir
31
32
33
@contextmanager
34
def clean_maya_environment():
35
    """Contextmanager to clean maya environment variables to ensure a
36
    clean run.
37
    """
38
    restore = False
39
    with temp_app_dir():
40
        script_path = os.environ.get('MAYA_SCRIPT_PATH', '')
41
        module_path = os.environ.get('MAYA_MODULE_PATH', '')
42
        try:
43
            os.environ['MAYA_SCRIPT_PATH'] = ''
44
            os.environ['MAYA_MODULE_PATH'] = ''
45
            yield
46
        finally:
47
            os.environ['MAYA_SCRIPT_PATH'] = script_path
48
            os.environ['MAYA_MODULE_PATH'] = module_path
49
50
51
def get_maya_location(version):
52
    """Find the location of maya installation
53
    """
54
    if 'MAYA_LOCATION' in os.environ.keys():
55
        return os.environ['MAYA_LOCATION']
56
57
    try:
58
        location = {
59
            'Windows': 'C:/Program Files/Autodesk/Maya{0}',
60
            'Darwin': '/Applications/Autodesk/maya{0}/Maya.app/Contents',
61
        }[platform.system()]
62
    except KeyError:
63
        location = '/usr/autodesk/maya{0}'
64
        if version < 2016:
65
            # Starting Maya 2016, the default install directory name changed.
66
            location += '-x64'
67
    return location.format(version)
68
69
70
def mayapy(version):
71
    """Find the mayapy executable path.
72
73
    """
74
    mayapy_executable = '{0}/bin/mayapy'.format(get_maya_location(version))
75
    if platform.system() == 'Windows':
76
        mayapy_executable += '.exe'
77
    return mayapy_executable
78