From f93da0234654f5b0f78236adb8caefd9fcbe4b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Tue, 15 Jun 2021 11:27:01 +0200 Subject: [PATCH] github/update_ansible.py: easier updating of requirements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using the normal python YAML module breaks formatting and order of entries. Signed-off-by: Jakub SokoĊ‚owski --- github/update_ansible.py | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 github/update_ansible.py diff --git a/github/update_ansible.py b/github/update_ansible.py new file mode 100755 index 0000000..7be971a --- /dev/null +++ b/github/update_ansible.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +# I could use yaml module, but it fucks ups the order and formatting. +import re +from os.path import isdir, expanduser +from subprocess import check_output + +path = 'ansible/requirements.yml' + +with open(path, 'r') as f: + contents = f.readlines() + +# Read file +lines = iter(contents) +entries = [] +for line in lines: + matches = re.match('- name: (.*)', line) + if not matches: + continue + name = matches.group(1) + + matches = re.match(' src: (.*)', next(lines)) + if not matches: + continue + src = matches.group(1) + + matches = re.match('^git@github.com:[^/]+/(.+).git$', src) + if not matches: + raise Exception('Unable to find full repo name!') + full_name = matches.group(1) + + matches = re.match(' version: (.*)', next(lines)) + if not matches: + continue + version = matches.group(1) + + matches = re.match(' scm: (.*)', next(lines)) + if not matches: + continue + scm = matches.group(1) + + entries.append({ + 'name': name, + 'full_name': full_name, + 'src': src, + 'version': version, + 'scm': scm, + }) + +# Read commits from repos +for entry in entries: + cwd = expanduser('~/work/%s' % entry['full_name']) + if not isdir(cwd): + print('No such repo: %s' % cwd) + continue + commit = check_output(['git', 'rev-parse', 'HEAD'], cwd=cwd) + new_version = commit.decode().strip() + if new_version != entry['version']: + entry['version'] = new_version + print('Updated: %s - %s' % (new_version, entry['full_name'])) + +lines = ['---\n'] +for entry in entries: + lines.extend([ + ('- name: %s\n' % entry['name']), + (' src: %s\n' % entry['src']), + (' version: %s\n' % entry['version']), + (' scm: %s\n' % entry['scm']), + '\n', + ]) + + +# Write file +with open(path, 'w') as f: + contents = f.writelines(lines[:-1])