Passed
Push — master ( 4fca22...42b5ce )
by Marcus
40s
created

is_maya_module()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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