ansible/update.py: handle entries without version

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2023-02-15 14:14:03 +01:00
parent bccf782469
commit 5a92982893
No known key found for this signature in database
GPG Key ID: FE65CD384D5BF7B4
1 changed files with 37 additions and 32 deletions

View File

@ -8,69 +8,74 @@ from subprocess import check_output
path = 'ansible/requirements.yml' path = 'ansible/requirements.yml'
repos_path = getenv('ANSIBLE_REPOS_PATH', '~/work') repos_path = getenv('ANSIBLE_REPOS_PATH', '~/work')
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)
with open(path, 'r') as f: with open(path, 'r') as f:
contents = f.readlines() contents = f.readlines()
# Read file # Read file
lines = iter(contents) lines = iter(contents)
entries = [] entries = []
entry = {}
keys_possible = ['name', 'src', 'version', 'scm']
for line in lines: for line in lines:
matches = re.match('- name: (.*)', line) if line.strip() == '---':
if not matches:
continue continue
name = matches.group(1)
matches = re.match(' src: (.*)', next(lines)) key, value = extractKeyValue(line, keys_possible)
if not matches:
raise Exception('Unable to find source URL: %s' % name)
src = matches.group(1)
matches = re.match('^git@github.com:[^/]+/(.+).git$', src) if key is None:
if not matches: entries.append(entry.copy())
raise Exception('Unable to find full repo name: %s' % name) else:
full_name = matches.group(1) entry[key] = value
matches = re.match(' version: (.*)', next(lines)) # Append last entry due to lack of last newline.
if not matches: entries.append(entry)
raise Exception('Unable to find current version: %s' % name)
version = matches.group(1)
matches = re.match(' scm: (.*)', next(lines))
if not matches:
raise Exception('Unable to find version control type: %s' % name)
scm = matches.group(1)
entries.append({
'name': name,
'full_name': full_name,
'src': src,
'version': version,
'scm': scm,
})
# Read commits from repos # Read commits from repos
for entry in entries: for entry in entries:
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)
cwd = expanduser('%s/%s' % (repos_path, entry['full_name'])) cwd = expanduser('%s/%s' % (repos_path, entry['full_name']))
if not isdir(cwd): if not isdir(cwd):
print('No such repo: %s' % cwd) print('No such repo: %s' % cwd)
continue continue
commit = check_output(['git', 'rev-parse', 'HEAD'], cwd=cwd) commit = check_output(['git', 'rev-parse', 'HEAD'], cwd=cwd)
new_version = commit.decode().strip() new_version = commit.decode().strip()
if new_version != entry['version']: if entry.get('version') is None:
print('untrack: %s - %s' % (new_version, entry['full_name']))
elif new_version != entry['version']:
entry['version'] = new_version entry['version'] = new_version
print('Updated: %s - %s' % (new_version, entry['full_name'])) print('UPDATED: %s - %s' % (new_version, entry['full_name']))
else:
print('current: %s - %s' % (new_version, entry['full_name']))
lines = ['---\n'] lines = ['---\n']
for entry in entries: for entry in entries:
lines.extend([ lines.extend([
('- name: %s\n' % entry['name']), ('- name: %s\n' % entry['name']),
(' src: %s\n' % entry['src']), (' src: %s\n' % entry['src']),
(' version: %s\n' % entry['version']), (' version: %s\n' % entry['version']) if entry.get('version') else None,
(' scm: %s\n' % entry['scm']), (' scm: %s\n' % entry['scm']),
'\n', '\n',
]) ])
# remove empty lines
lines = list(filter(lambda l: l is not None, lines))
# Write file # write file
with open(path, 'w') as f: with open(path, 'w') as f:
contents = f.writelines(lines[:-1]) contents = f.writelines(lines[:-1])