-
Notifications
You must be signed in to change notification settings - Fork 14
/
travisparse.py
71 lines (60 loc) · 1.72 KB
/
travisparse.py
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
""" Parse travis.yml file, partly
"""
import sys
if sys.version_info[0] > 2:
basestring = str
class TravisError(Exception):
pass
def get_yaml_entry(yaml_dict, name):
""" Get entry `name` from dict `yaml_dict`
Parameters
----------
yaml_dict : dict
dict or subdict from parsing .travis.yml file
name : str
key to analyze and return
Returns
-------
entry : None or list
If `name` not in `yaml_dict` return None. If key value is a string
return a single entry list. Otherwise return the key value.
"""
entry = yaml_dict.get(name)
if entry is None:
return None
if isinstance(entry, basestring):
return [entry]
return entry
def get_envs(yaml_dict):
""" Get first env combination from travis yaml dict
Parameters
----------
yaml_dict : dict
dict or subdict from parsing .travis.yml file
Returns
-------
bash_str : str
bash scripting lines as string
"""
env = get_yaml_entry(yaml_dict, 'env')
if env is None:
return ''
# Bare string
if isinstance(env, basestring):
return env + '\n'
# Simple list defining matrix
if isinstance(env, (list, tuple)):
return env[0] + '\n'
# More complex dictey things
globals, matrix = [get_yaml_entry(env, name)
for name in ('global', 'matrix')]
if hasattr(matrix, 'keys'):
raise TravisError('Oops, envs too complicated')
lines = []
if not globals is None:
if matrix is None:
raise TravisError('global section needs matrix section')
lines += globals
if not matrix is None:
lines.append(matrix[0])
return '\n'.join(lines) + '\n'