Last active
May 8, 2017 07:18
-
-
Save hyperair/b20d304edba3536f1fdae7e6ea474143 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import argparse | |
import itertools | |
import sys | |
import yaml | |
script_template = """ | |
#!/bin/bash | |
{functions} | |
$1 | |
""".lstrip() | |
function_template = """ | |
{name}() {{ | |
{lines} | |
}} | |
""" | |
travis_script_segments = ('before_install', 'install', 'script') | |
def generate_travis_sh(travis, indentation): | |
functions = ( | |
function_template.format( | |
name=segment, | |
lines='\n'.join(get_script_lines(travis[segment], indentation)) | |
) | |
for segment in travis_script_segments | |
) | |
script = script_template.format(functions="\n".join(functions)) | |
return script | |
def get_script_lines(lines, indentation): | |
if isinstance(lines, (str, unicode)): | |
lines = [lines] | |
lines = itertools.chain(*[line.split('\n') for line in lines]) | |
return ("{}{}".format(indentation, line) for line in lines) | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Convert .travis.yml to shell script') | |
parser.add_argument( | |
'--output', '-o', dest='outfile', | |
default=sys.stdout, type=argparse.FileType('w'), | |
help='output file') | |
parser.add_argument( | |
'infile', metavar='INPUT', nargs='?', default=sys.stdin, | |
type=argparse.FileType('r'), | |
help='path to .travis.yml') | |
parser.add_argument( | |
'--indent-str', '-i', default=' ' * 4, | |
type=unicode, help='indentation string (default: 4 spaces)') | |
args = parser.parse_args() | |
try: | |
travis = yaml.load(args.infile) | |
args.outfile.write(generate_travis_sh(travis, args.indent_str)) | |
finally: | |
args.infile.close() | |
args.outfile.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment