2021-06-15 09:27:01 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# I could use yaml module, but it fucks ups the order and formatting.
|
|
|
|
import re
|
2021-06-29 12:35:07 +00:00
|
|
|
from os import getenv
|
2021-06-15 09:27:01 +00:00
|
|
|
from os.path import isdir, expanduser
|
|
|
|
from subprocess import check_output
|
|
|
|
|
|
|
|
path = 'ansible/requirements.yml'
|
2021-06-29 12:35:07 +00:00
|
|
|
repos_path = getenv('ANSIBLE_REPOS_PATH', '~/work')
|
2021-06-15 09:27:01 +00:00
|
|
|
|
2023-02-15 13:14:03 +00:00
|
|
|
def extractKeyValue(line, keys):
|
|
|
|
for key in keys:
|
|
|
|
matches = re.match(('[- ] %s: (.*)' % key), line)
|
|
|
|
if not matches:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
return (key, matches.group(1))
|
|
|
|
|
|
|
|
return (None, None)
|
|
|
|
|
2021-06-15 09:27:01 +00:00
|
|
|
with open(path, 'r') as f:
|
|
|
|
contents = f.readlines()
|
|
|
|
|
|
|
|
# Read file
|
|
|
|
lines = iter(contents)
|
|
|
|
entries = []
|
2023-02-15 13:14:03 +00:00
|
|
|
entry = {}
|
|
|
|
keys_possible = ['name', 'src', 'version', 'scm']
|
|
|
|
|
2021-06-15 09:27:01 +00:00
|
|
|
for line in lines:
|
2023-02-15 13:14:03 +00:00
|
|
|
if line.strip() == '---':
|
2021-06-15 09:27:01 +00:00
|
|
|
continue
|
|
|
|
|
2023-02-15 13:14:03 +00:00
|
|
|
key, value = extractKeyValue(line, keys_possible)
|
2021-06-15 09:27:01 +00:00
|
|
|
|
2023-02-15 13:14:03 +00:00
|
|
|
if key is None:
|
|
|
|
entries.append(entry.copy())
|
|
|
|
else:
|
|
|
|
entry[key] = value
|
2021-06-15 09:27:01 +00:00
|
|
|
|
2023-02-15 13:14:03 +00:00
|
|
|
# Append last entry due to lack of last newline.
|
|
|
|
entries.append(entry)
|
2021-06-15 09:27:01 +00:00
|
|
|
|
|
|
|
# Read commits from repos
|
|
|
|
for entry in entries:
|
2023-02-15 13:14:03 +00:00
|
|
|
matches = re.match('^git@github.com:[^/]+/(.+).git$', entry['src'])
|
|
|
|
if not matches:
|
|
|
|
raise Exception('Unable to find full repo name: %s' % name)
|
|
|
|
entry['full_name'] = matches.group(1)
|
|
|
|
|
2021-06-29 12:35:07 +00:00
|
|
|
cwd = expanduser('%s/%s' % (repos_path, entry['full_name']))
|
2021-06-15 09:27:01 +00:00
|
|
|
if not isdir(cwd):
|
|
|
|
print('No such repo: %s' % cwd)
|
|
|
|
continue
|
2023-02-15 13:14:03 +00:00
|
|
|
|
2021-06-15 09:27:01 +00:00
|
|
|
commit = check_output(['git', 'rev-parse', 'HEAD'], cwd=cwd)
|
|
|
|
new_version = commit.decode().strip()
|
2023-02-15 13:14:03 +00:00
|
|
|
if entry.get('version') is None:
|
|
|
|
print('untrack: %s - %s' % (new_version, entry['full_name']))
|
|
|
|
elif new_version != entry['version']:
|
2021-06-15 09:27:01 +00:00
|
|
|
entry['version'] = new_version
|
2023-02-15 13:14:03 +00:00
|
|
|
print('UPDATED: %s - %s' % (new_version, entry['full_name']))
|
|
|
|
else:
|
|
|
|
print('current: %s - %s' % (new_version, entry['full_name']))
|
2021-06-15 09:27:01 +00:00
|
|
|
|
|
|
|
lines = ['---\n']
|
|
|
|
for entry in entries:
|
|
|
|
lines.extend([
|
|
|
|
('- name: %s\n' % entry['name']),
|
|
|
|
(' src: %s\n' % entry['src']),
|
2023-02-15 13:14:03 +00:00
|
|
|
(' version: %s\n' % entry['version']) if entry.get('version') else None,
|
2021-06-15 09:27:01 +00:00
|
|
|
(' scm: %s\n' % entry['scm']),
|
|
|
|
'\n',
|
|
|
|
])
|
|
|
|
|
2023-02-15 13:14:03 +00:00
|
|
|
# remove empty lines
|
|
|
|
lines = list(filter(lambda l: l is not None, lines))
|
2021-06-15 09:27:01 +00:00
|
|
|
|
2023-02-15 13:14:03 +00:00
|
|
|
# write file
|
2021-06-15 09:27:01 +00:00
|
|
|
with open(path, 'w') as f:
|
|
|
|
contents = f.writelines(lines[:-1])
|