Completed
Push — develop ( b745b5...362316 )
by Wu
9s
created

get_envs()   F

Complexity

Conditions 9

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 3
cc 9
1
""" Parse travis.yml file, partly
2
"""
3
import sys
4
5
if sys.version_info[0] > 2:
6
    basestring = str
7
8
9
class TravisError(Exception):
10
    pass
11
12
13
def get_yaml_entry(yaml_dict, name):
14
    """ Get entry `name` from dict `yaml_dict`
15
16
    Parameters
17
    ----------
18
    yaml_dict : dict
19
        dict or subdict from parsing .travis.yml file
20
    name : str
21
        key to analyze and return
22
23
    Returns
24
    -------
25
    entry : None or list
26
        If `name` not in `yaml_dict` return None.  If key value is a string
27
        return a single entry list. Otherwise return the key value.
28
    """
29
    entry = yaml_dict.get(name)
30
    if entry is None:
31
        return None
32
    if isinstance(entry, basestring):
33
        return [entry]
34
    return entry
35
36
37
def get_envs(yaml_dict):
38
    """ Get first env combination from travis yaml dict
39
40
    Parameters
41
    ----------
42
    yaml_dict : dict
43
        dict or subdict from parsing .travis.yml file
44
45
    Returns
46
    -------
47
    bash_str : str
48
        bash scripting lines as string
49
    """
50
    env = get_yaml_entry(yaml_dict, 'env')
51
    if env is None:
52
        return ''
53
    # Bare string
54
    if isinstance(env, basestring):
55
        return env + '\n'
56
    # Simple list defining matrix
57
    if isinstance(env, (list, tuple)):
58
        return env[0] + '\n'
59
    # More complex dictey things
60
    globals, matrix = [get_yaml_entry(env, name)
61
                       for name in ('global', 'matrix')]
62
    if hasattr(matrix, 'keys'):
63
        raise TravisError('Oops, envs too complicated')
64
    lines = []
65
    if not globals is None:
66
        if matrix is None:
67
            raise TravisError('global section needs matrix section')
68
        lines += globals
69
    if not matrix is None:
70
        lines.append(matrix[0])
71
    return '\n'.join(lines) + '\n'
72