Created
September 2, 2016 23:18
-
-
Save jmcarp/699c7a2d0339edc8fc53ed6cb5535148 to your computer and use it in GitHub Desktop.
concourse extract
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/env python | |
| # encoding: utf-8 | |
| import re | |
| import sys | |
| import yaml | |
| parameter = re.compile(r'{{(.+)}}') | |
| def extract(templated, complete): | |
| if isinstance(templated, list): | |
| assert len(templated) == len(complete) | |
| for first, second in zip(templated, complete): | |
| yield from extract(first, second) | |
| if isinstance(templated, dict): | |
| for key in set(templated.keys()).intersection(complete.keys()): | |
| yield from extract(templated[key], complete[key]) | |
| if isinstance(templated, str): | |
| result = parameter.search(templated) | |
| if result: | |
| yield result.groups()[0], complete | |
| replace = re.compile(r'({{[^\s]+}})') | |
| def preprocess(stream): | |
| return replace.sub('"\\1"', stream.read()) | |
| if __name__ == '__main__': | |
| assert len(sys.argv) == 3 | |
| templated = yaml.load(preprocess(open(sys.argv[1]))) | |
| complete = yaml.load(open(sys.argv[2])) | |
| extracted = extract(templated, complete) | |
| for key, value in extract(templated, complete): | |
| print('{}: {}'.format(key, value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment