Created
June 7, 2018 08:41
-
-
Save hyperair/2face006e7924fcedb34165ad8bd976a to your computer and use it in GitHub Desktop.
This file contains 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): | |
functions = ( | |
function_template.format( | |
name=segment, | |
lines='\n'.join(get_script_lines(travis[segment])) | |
) | |
for segment in travis_script_segments | |
) | |
script = script_template.format(functions="\n".join(functions)) | |
return script | |
def get_script_lines(lines): | |
if isinstance(lines, (str, unicode)): | |
lines = [lines] | |
lines = itertools.chain(*[line.split('\n') for line in lines]) | |
return (" {}".format(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') | |
args = parser.parse_args() | |
try: | |
travis = yaml.load(args.infile) | |
args.outfile.write(generate_travis_sh(travis)) | |
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