Skip to content

Instantly share code, notes, and snippets.

@toabctl
Created February 15, 2016 09:29
Show Gist options
  • Save toabctl/0ad350c0368cb2d9efb5 to your computer and use it in GitHub Desktop.
Save toabctl/0ad350c0368cb2d9efb5 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Copyright (c) 2016 SUSE Linux GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import argparse
from collections import namedtuple
import os
from packaging import version
import re
import sys
import yaml
V = namedtuple('V', ['release', 'pkg'])
def process_args():
parser = argparse.ArgumentParser(
description='Compare rpm-packaging with OpenStack releases')
parser.add_argument('releases-git-dir',
help='Base directory of the openstack/releases '
'git repo', default='releases')
parser.add_argument('rpm-packaging-git-dir',
help='Base directory of the openstack/rpm-packaging '
'git repo', default='rpm-packaging')
parser.add_argument('release',
help='name of the release. I.e. "liberty"',
default='liberty')
parser.add_argument('--format',
help='output format', choices=('text', 'html'),
default='text')
return vars(parser.parse_args())
def find_highest_release_version(releases):
"""get a list of dicts with a version key and find the highest version
using PEP440 to compare the different versions"""
v_highest = version.parse('0')
for r in releases:
v_new = version.parse(r['version'])
if v_new > v_highest:
v_highest = v_new
return v_highest
def find_pkg_version(pkg_project_spec):
"""get a spec.j2 template and get the version"""
if os.path.exists(pkg_project_spec):
with open(pkg_project_spec) as f:
for l in f.readlines():
m = re.search('^Version:\s*(?P<version>.*)\s*$', l)
if m:
return version.parse(m.group('version'))
# no version in spec found
print('ERROR: no version in %s found' % pkg_project_spec)
return version.parse('0')
else:
return version.parse('0')
def _pretty_table(release, projects):
from prettytable import PrettyTable
tb = PrettyTable()
tb.field_names = ['name', 'release version (%s)' % release,
'pkg version (%s)' % release, 'comment']
for p_name, x in projects.items():
if x.pkg == version.parse('0'):
comment = 'needs packaging'
elif x.pkg < x.release:
comment = 'needs upgrade'
elif x.pkg == x.release:
comment = 'perfect'
elif x.pkg > x.release:
comment = 'needs downgrade'
else:
comment = ''
tb.add_row([p_name, x.release, x.pkg, comment])
return tb
def output_text(release, projects):
tb = _pretty_table(release, projects)
print(tb.get_string(sortby='name'))
def output_html(release, projects):
"""adjust the comment color a big with an ugly hack"""
from lxml import html
tb = _pretty_table(release, projects)
s = tb.get_html_string(sortby='name')
tree = html.document_fromstring(s)
tab = tree.cssselect('table')
tab[0].attrib['style'] = 'border-collapse: collapse;'
trs = tree.cssselect('tr')
for t in trs:
t.attrib['style'] = 'border-bottom:1pt solid black;'
tds = tree.cssselect('td')
for t in tds:
if t.text_content() == 'needs packaging':
t.attrib['style'] = 'background-color:yellow'
elif t.text_content() == 'needs upgrade':
t.attrib['style'] = 'background-color:LightYellow'
elif t.text_content() == 'needs downgrade':
t.attrib['style'] = 'background-color:red'
elif t.text_content() == 'perfect':
t.attrib['style'] = 'background-color:green'
print(html.tostring(tree))
def main():
args = process_args()
projects = {}
# directory which contains all yaml files from the openstack/release git dir
releases_yaml_dir = os.path.join(args['releases-git-dir'], 'deliverables',
args['release'])
for yaml_file in os.listdir(releases_yaml_dir):
project_name = yaml_file.rstrip('.yaml')
with open(os.path.join(releases_yaml_dir, yaml_file)) as f:
data = yaml.load(f.read())
v_release = find_highest_release_version(data['releases'])
# path to the corresponding .spec.j2 file
pkg_project_spec = os.path.join(args['rpm-packaging-git-dir'],
'openstack', project_name,
'%s.spec.j2' % project_name)
v_pkg = find_pkg_version(pkg_project_spec)
# add both versions to the project dict
projects[project_name] = V(v_release, v_pkg)
if args['format'] == 'text':
output_text(args['release'], projects)
elif args['format'] == 'html':
output_html(args['release'], projects)
return 0
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment