Skip to content

Instantly share code, notes, and snippets.

@ekohl ekohl/example
Last active Apr 10, 2019

Embed
What would you like to do?
get_package_names
$ ./get_package_names foreman-release
foreman-nightly-nonscl-rhel7 foreman-release-1.22.0-0.1.develop.el7
foreman-client-nightly-rhel5 foreman-release-1.22.0-0.1.develop.el5
foreman-client-nightly-rhel6 foreman-release-1.22.0-0.1.develop.el6
foreman-client-nightly-fedora27 foreman-release-1.22.0-0.1.develop.fc27
foreman-client-nightly-fedora28 foreman-release-1.22.0-0.1.develop.fc28
$ ./get_package_names --dist el7 foreman-release
foreman-nightly-nonscl-rhel7 foreman-release-1.22.0-0.1.develop.el7
$ ./get_package_names foreman-release rubygem-foreman_templates
foreman-client-1.21-fedora27 foreman-release-1.21.2-1.fc27
foreman-1.21-nonscl-rhel7 foreman-release-1.21.2-1.el7
foreman-client-1.21-rhel6 foreman-release-1.21.2-1.el6
foreman-client-1.21-fedora28 foreman-release-1.21.2-1.fc28
foreman-client-1.21-rhel5 foreman-release-1.21.2-1.el5
foreman-plugins-1.21-rhel7 tfm-rubygem-foreman_templates-6.0.3-2.fm1_21.el7
#!/usr/bin/env python3
import argparse
import configparser
import logging
import os
import subprocess
from collections import defaultdict
from glob import glob
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
try:
import argcomplete
except ImportError:
argcomplete = None
def parse_props(tito_props_path):
parser = configparser.ConfigParser()
parser.read(tito_props_path)
return parser
def get_package_whitelist(tito_props, dist):
result = defaultdict(set)
for tag, section in tito_props.items():
if 'whitelist' in section and (not dist or section['disttag'] == '.{}'.format(dist)):
for short_package_name in section['whitelist'].split():
result[short_package_name].add((tag, section['disttag'], section.get('scl')))
return result
def get_packages_manifest(manifest_path):
data_loader = DataLoader()
manifest = InventoryManager(data_loader, manifest_path)
variable_manager = VariableManager(data_loader, manifest)
for host in manifest.hosts.values():
package_vars = variable_manager.get_vars(host=host)
if 'package_base_dir' in package_vars:
spec_files = glob(os.path.join(package_vars['inventory_dir'],
package_vars['package_base_dir'], host.name, '*.spec'))
yield (host.name, spec_files[0])
def get_packages(repository):
return dict(get_packages_manifest(os.path.join(repository, 'package_manifest.yaml')))
def get_spec_nvr(spec_file, dist, scl, foremandist):
command = ['rpmspec', '--srpm', '-q', '--queryformat', '%{nvr}', spec_file,
'--define', 'dist {}'.format(dist),
'--define', 'foremandist {}'.format(foremandist)]
if scl:
command += ['--define', 'scl {}'.format(scl)]
try:
return subprocess.check_output(command).decode()
except subprocess.CalledProcessError as ex:
logging.exception(ex)
return None
def main():
repository = os.path.dirname(__file__)
all_packages = get_packages(repository)
parser = argparse.ArgumentParser()
parser.add_argument('--dist')
parser.add_argument('package', nargs='+', choices=all_packages.keys())
if argcomplete:
argcomplete.autocomplete(parser)
args = parser.parse_args()
tito_file = os.path.join(repository, 'rel-eng', 'tito.props')
tito_props = parse_props(tito_file)
foremandist = tito_props.get('buildconfig', 'tag_suffix')
package_map = get_package_whitelist(tito_props, args.dist)
for package in args.package:
spec_file = all_packages[package]
for tag, disttag, scl in package_map[package]:
print(tag, get_spec_nvr(spec_file, disttag, scl, foremandist))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.