2016-06-07 05:00:00 +00:00
|
|
|
#!/usr/local/bin/dib-python
|
2014-05-01 16:13:47 +00:00
|
|
|
|
|
|
|
# Copyright 2014 Red Hat Inc.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
2016-05-20 04:26:25 +00:00
|
|
|
import logging
|
2014-05-01 16:13:47 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
2017-01-18 00:21:54 +00:00
|
|
|
log = logging.getLogger()
|
2014-11-19 11:59:49 +00:00
|
|
|
|
|
|
|
|
2014-05-01 16:13:47 +00:00
|
|
|
def os_family(distro):
|
|
|
|
family = None
|
2015-02-24 18:49:47 +00:00
|
|
|
if distro in ['fedora', 'rhel', 'rhel7', 'centos', 'centos7']:
|
2014-05-01 16:13:47 +00:00
|
|
|
family = 'redhat'
|
|
|
|
elif distro in ['debian', 'ubuntu']:
|
|
|
|
family = 'debian'
|
|
|
|
elif distro == 'opensuse':
|
|
|
|
family = 'suse'
|
2016-01-04 17:00:28 +00:00
|
|
|
elif distro == 'gentoo':
|
|
|
|
family = 'gentoo'
|
2014-05-01 16:13:47 +00:00
|
|
|
|
|
|
|
return family
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
2014-11-19 11:59:49 +00:00
|
|
|
description="Translate package name to distro specific name."
|
|
|
|
" Exits with 1 if error is encountered, 2 if no pkg-map"
|
|
|
|
" file is found. Otherwise exits with 0.")
|
2014-05-01 16:13:47 +00:00
|
|
|
parser.add_argument('--element', default='',
|
|
|
|
help='The element (namespace) to use for translation.')
|
2016-05-20 04:26:25 +00:00
|
|
|
parser.add_argument('--pkg-map', default='',
|
|
|
|
help='Path to specific pkg-map file. '
|
|
|
|
'(Useful for testing)')
|
2014-05-01 16:13:47 +00:00
|
|
|
parser.add_argument('--distro', default=os.environ.get('DISTRO_NAME'),
|
|
|
|
help='The distro name to use for translation.'
|
|
|
|
' Defaults to DISTRO_NAME')
|
2016-05-20 04:26:25 +00:00
|
|
|
parser.add_argument('--release', default=os.environ.get('DIB_RELEASE'),
|
|
|
|
help='A more specfic name for distribution release')
|
2014-11-19 11:59:49 +00:00
|
|
|
parser.add_argument('--missing-ok', action="store_true",
|
|
|
|
help='Do not consider missing mappings an error.'
|
|
|
|
' Causes packages where no mapping is set to be'
|
|
|
|
' printed.')
|
2017-01-18 00:21:54 +00:00
|
|
|
# This tool has traditionally output status and debug messages on
|
|
|
|
# stderr. The problem is if a caller has stderr > stdout then
|
|
|
|
# actual output gets messed in with the logs. This allows callers
|
|
|
|
# to disambiguate actual output by specifying a unique prefix.
|
|
|
|
parser.add_argument('--prefix', default='',
|
|
|
|
help='Output mapped packages with this prefix')
|
2016-05-20 04:26:25 +00:00
|
|
|
parser.add_argument('--debug', dest='debug', action="store_true",
|
|
|
|
help="Enable debugging output")
|
|
|
|
|
2014-05-01 16:13:47 +00:00
|
|
|
args, extra = parser.parse_known_args()
|
|
|
|
|
2017-01-18 00:21:54 +00:00
|
|
|
# Logs have traditionally gone to stderr with this tool. Maintain
|
2017-05-31 04:16:17 +00:00
|
|
|
# compatibility
|
2017-01-18 00:21:54 +00:00
|
|
|
level = logging.DEBUG if args.debug else logging.INFO
|
|
|
|
logging.basicConfig(stream=sys.stderr, level=level)
|
2016-05-20 04:26:25 +00:00
|
|
|
|
|
|
|
if not args.element and not args.pkg_map:
|
2017-01-18 00:21:54 +00:00
|
|
|
log.error('Please specify an --element argument.')
|
2014-05-01 16:13:47 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2016-05-20 04:26:25 +00:00
|
|
|
if args.element and args.pkg_map:
|
2017-01-18 00:21:54 +00:00
|
|
|
log.error('Specify either --element or --pkg-map')
|
2016-05-20 04:26:25 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2014-05-01 16:13:47 +00:00
|
|
|
if not args.distro:
|
2017-01-18 00:21:54 +00:00
|
|
|
log.error('Please specify a --distro argument or set DISTRO_NAME.')
|
2014-05-01 16:13:47 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2016-05-20 04:26:25 +00:00
|
|
|
if args.pkg_map:
|
2017-01-18 00:21:54 +00:00
|
|
|
# specifying the pkg-map by hand is just for manual testing
|
|
|
|
element = "<%s>" % args.pkg_map
|
2016-05-20 04:26:25 +00:00
|
|
|
map_file = args.pkg_map
|
|
|
|
else:
|
2017-01-18 00:21:54 +00:00
|
|
|
element = args.element
|
|
|
|
map_file = '/usr/share/pkg-map/%s' % element
|
|
|
|
|
|
|
|
log.info("Mapping for %s : %s" % (element, ' '.join(extra)))
|
2016-05-20 04:26:25 +00:00
|
|
|
|
2014-05-01 16:13:47 +00:00
|
|
|
if not os.path.exists(map_file):
|
2015-02-23 18:05:35 +00:00
|
|
|
if args.missing_ok:
|
2017-01-18 00:21:54 +00:00
|
|
|
log.info("No package map for %s, done" % element)
|
2015-02-23 18:05:35 +00:00
|
|
|
for name in extra:
|
2017-01-18 00:21:54 +00:00
|
|
|
print('%s%s' % (args.prefix, name))
|
2015-02-23 18:05:35 +00:00
|
|
|
sys.exit(0)
|
2017-01-18 00:21:54 +00:00
|
|
|
else:
|
|
|
|
log.error('Required pkg-map for %s element does not exist.'
|
|
|
|
% args.element)
|
|
|
|
sys.exit(2)
|
2014-05-01 16:13:47 +00:00
|
|
|
|
|
|
|
with open(map_file) as fd:
|
2016-01-14 05:21:25 +00:00
|
|
|
try:
|
|
|
|
package_names = json.loads(fd.read())
|
2017-01-18 00:21:54 +00:00
|
|
|
# log.debug(pprint.pformat(package_names))
|
2016-01-14 05:21:25 +00:00
|
|
|
except ValueError:
|
2017-01-18 00:21:54 +00:00
|
|
|
log.error('Unable to parse %s' % map_file)
|
2016-01-14 05:21:25 +00:00
|
|
|
raise
|
2014-05-01 16:13:47 +00:00
|
|
|
|
2016-05-20 04:26:25 +00:00
|
|
|
# Parse mapping data in this form using release/distro/family/default
|
|
|
|
# Most specific takes priority; order is
|
|
|
|
# - release
|
|
|
|
# - distro
|
|
|
|
# - family
|
|
|
|
# - default
|
2014-08-11 12:35:13 +00:00
|
|
|
# An empty package list can be provided.
|
2016-05-20 04:26:25 +00:00
|
|
|
#
|
2014-08-11 12:35:13 +00:00
|
|
|
# Example for Nova and Glance (using fictitious name for Fedora and SUSE
|
|
|
|
# and package mapping for SUSE family)
|
2014-05-01 16:13:47 +00:00
|
|
|
# {
|
2016-05-20 04:26:25 +00:00
|
|
|
# "release": {
|
|
|
|
# "fedora" : {
|
|
|
|
# "23" : {
|
|
|
|
# "nova_package": "openstack-compute-foo"
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
2014-05-01 16:13:47 +00:00
|
|
|
# "distro": {
|
|
|
|
# "fedora": {
|
|
|
|
# "nova_package": "openstack-compute",
|
|
|
|
# "glance_package": "openstack-image"
|
|
|
|
# }
|
|
|
|
# },
|
|
|
|
# "family": {
|
|
|
|
# "redhat": {
|
|
|
|
# "nova_package": "openstack-nova",
|
|
|
|
# "glance_package": "openstack-glance"
|
2014-08-11 12:35:13 +00:00
|
|
|
# },
|
|
|
|
# "suse": {
|
|
|
|
# "nova_package": ""
|
|
|
|
# }
|
2014-05-01 16:13:47 +00:00
|
|
|
# },
|
|
|
|
# "default": {
|
|
|
|
# "nova_package": "nova",
|
|
|
|
# "glance_package": "glance"
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
name_map = package_names.get('default', {})
|
|
|
|
if 'family' in package_names:
|
|
|
|
family_map = package_names['family'].get(os_family(args.distro))
|
|
|
|
if family_map:
|
|
|
|
name_map.update(family_map)
|
|
|
|
if 'distro' in package_names:
|
|
|
|
distro_map = package_names['distro'].get(args.distro)
|
|
|
|
if distro_map:
|
|
|
|
name_map.update(distro_map)
|
2016-05-20 04:26:25 +00:00
|
|
|
if 'release' in package_names:
|
|
|
|
try:
|
|
|
|
# release is a sub-concept of distro
|
|
|
|
release_map = package_names['release'][args.distro][args.release]
|
|
|
|
name_map.update(release_map)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-11-19 11:59:49 +00:00
|
|
|
|
2017-01-18 00:21:54 +00:00
|
|
|
# log.debug(pprint.pformat(name_map))
|
|
|
|
|
2014-05-01 16:13:47 +00:00
|
|
|
for name in extra:
|
2014-06-03 15:37:15 +00:00
|
|
|
pkg_name = name_map.get(name)
|
2014-05-01 16:13:47 +00:00
|
|
|
if pkg_name:
|
2017-01-18 00:21:54 +00:00
|
|
|
log.debug("map %s -> %s" % (name, pkg_name))
|
|
|
|
print('%s%s' % (args.prefix, pkg_name))
|
2014-08-11 12:35:13 +00:00
|
|
|
elif name in name_map:
|
2017-01-18 00:21:54 +00:00
|
|
|
log.debug("map %s -> <skip>" % (name))
|
2014-08-11 12:35:13 +00:00
|
|
|
continue
|
2014-05-01 16:13:47 +00:00
|
|
|
else:
|
2014-11-19 11:59:49 +00:00
|
|
|
if args.missing_ok:
|
2017-01-18 00:21:54 +00:00
|
|
|
log.debug("pass -> %s" % (name))
|
|
|
|
print('%s%s' % (args.prefix, name))
|
2014-11-19 11:59:49 +00:00
|
|
|
else:
|
2017-01-18 00:21:54 +00:00
|
|
|
log.error("%s has no valid mapping for package %s" %
|
|
|
|
(element, name))
|
2014-11-19 11:59:49 +00:00
|
|
|
sys.exit(1)
|
2014-05-01 16:13:47 +00:00
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
2020-02-22 23:25:38 +00:00
|
|
|
|
2014-05-01 16:13:47 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2017-01-18 00:21:54 +00:00
|
|
|
|
|
|
|
# Tell emacs to use python-mode
|
|
|
|
# Local variables:
|
|
|
|
# mode: python
|
|
|
|
# End:
|