Skip to content

Commit 73078c6

Browse files
committed
NF: script to make local scripts from travis.yml
1 parent 8fb7ed6 commit 73078c6

File tree

4 files changed

+190
-2
lines changed

4 files changed

+190
-2
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ env:
2222
- INSTALL_TYPE='system' VERSION=2.7 VENV=venv
2323

2424
install:
25-
- set -vx # echo commands
2625
- source ./travis_tools.sh
2726
- get_python_environment $INSTALL_TYPE $VERSION $VENV
2827

2928
script:
30-
- echo $PWD
3129
- source test_tools.sh
30+
- pip install nose
31+
- nosetests test_travisparse.py

test_travisparse.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
""" Nosetests for travis2bashes script
2+
"""
3+
4+
from __future__ import absolute_import, print_function
5+
6+
from travisparse import get_envs, TravisError
7+
from nose.tools import assert_equal, assert_true, assert_false, assert_raises
8+
9+
10+
def test_get_envs():
11+
# Get fetch of environment from .travis.yml
12+
assert_equal(get_envs({}), '')
13+
assert_equal(get_envs(dict(install = ['something'])), '')
14+
yaml = dict(env = {'global': ['LATEST_TAG=1'],
15+
'matrix': ['VERSION=2.7.8 NUMPY_VERSION=1.6.1',
16+
'VERSION=3.3.5 NUMPY_VERSION=1.7.1',
17+
'VERSION=3.4.1 NUMPY_VERSION=1.7.1']})
18+
assert_equal(get_envs(yaml),
19+
"""LATEST_TAG=1
20+
VERSION=2.7.8 NUMPY_VERSION=1.6.1
21+
""")
22+
yaml = dict(env = {'matrix': ['VERSION=2.7.8 NUMPY_VERSION=1.6.1',
23+
'VERSION=3.3.5 NUMPY_VERSION=1.7.1',
24+
'VERSION=3.4.1 NUMPY_VERSION=1.7.1']})
25+
assert_equal(get_envs(yaml),
26+
"""VERSION=2.7.8 NUMPY_VERSION=1.6.1
27+
""")
28+
yaml = dict(env = ['ISOLATED=true', 'ISOLATED=false'])
29+
assert_equal(get_envs(yaml),
30+
"""ISOLATED=true
31+
""")
32+
# excludes too complicated
33+
yaml = dict(env = {'matrix':
34+
{'exclude':
35+
[{'gemfile': 'Gemfile', 'rvm': '2.0.0'}]}})
36+
assert_raises(TravisError, get_envs, yaml)
37+
# includes too complicated
38+
yaml = dict(env = {'matrix':
39+
{'include':
40+
[{'gemfile': 'gemfiles/Gemfile.rails-3.2.x',
41+
'rvm': 'ruby-head',
42+
'env': 'ISOLATED=false'}]}})
43+
assert_raises(TravisError, get_envs, yaml)
44+
# global implies matrix
45+
yaml = dict(env = {'global': ['LATEST_TAG=1']})
46+
assert_raises(TravisError, get_envs, yaml)
47+
# one line is OK too
48+
yaml = dict(env = {'global': 'LATEST_TAG=1',
49+
'matrix': 'VERSION=3.3.1'})
50+
assert_equal(get_envs(yaml),
51+
"""LATEST_TAG=1
52+
VERSION=3.3.1
53+
""")
54+
yaml = dict(env = 'MY_VAR=1')
55+
assert_equal(get_envs(yaml),
56+
"""MY_VAR=1
57+
""")

travis2bashes

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
""" Process .travis.yml and make bash fragments for testing
3+
4+
Dump example vars, install, testing shell scripts to files for local testing
5+
"""
6+
from __future__ import print_function
7+
8+
import os
9+
from os.path import join as pjoin
10+
import warnings
11+
12+
import argparse # Python 2.6, really?
13+
14+
# pip install pyyaml
15+
import yaml
16+
17+
from travisparse import get_envs, TravisError, get_yaml_entry
18+
19+
def main():
20+
parser = argparse.ArgumentParser(
21+
description='Extract bash fragments from .travis.yml file')
22+
parser.add_argument('--out-dir', default='working',
23+
help = 'output directory for fragments')
24+
parser.add_argument('--no-vars', action='store_true',
25+
help = 'do not write vars script file')
26+
args = parser.parse_args()
27+
out_dir = args.out_dir
28+
with open('.travis.yml', 'rt') as fobj:
29+
travis_dict = yaml.load(fobj)
30+
try:
31+
os.mkdir(out_dir)
32+
except OSError:
33+
pass
34+
# vars
35+
if not args.no_vars:
36+
try:
37+
var_str = get_envs(travis_dict)
38+
except TravisError:
39+
warnings.warn('Could not get travis vars from file')
40+
else:
41+
vars_fname = pjoin(out_dir, '_frag_travis_vars.sh')
42+
with open(vars_fname, 'wt') as fobj:
43+
fobj.write(var_str)
44+
print("Written " + vars_fname)
45+
# install
46+
install = get_yaml_entry(travis_dict, 'install')
47+
install_fname = pjoin(out_dir, '_frag_travis_install.sh')
48+
with open(install_fname, 'wt') as fobj:
49+
fobj.write('\n'.join(install) + '\n')
50+
print('Written ' + install_fname)
51+
# test
52+
test = get_yaml_entry(travis_dict, 'script')
53+
test_fname = pjoin(out_dir, '_frag_travis_test.sh')
54+
with open(test_fname, 'wt') as fobj:
55+
fobj.write('\n'.join(test) + '\n')
56+
print('Written ' + test_fname)
57+
58+
59+
if __name__ == '__main__':
60+
main()

travisparse.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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'

0 commit comments

Comments
 (0)