#!/usr/libexec/platform-python
# Copyright 2010 Per Øyvind Karlsen <proyvind@moondrake.org>
# Copyright 2015 Neal Gompa <ngompa13@gmail.com>
# This program is free software. It may be redistributed and/or modified under
# the terms of the LGPL version 2.1 (or later).
# RPM python dependency generator, using .egg-info/.egg-link/.dist-info data
from __future__ import print_function
from getopt import getopt
from os.path import basename, dirname, isdir, sep
from sys import argv, stdin, version
from distutils.sysconfig import get_python_lib
from warnings import warn
['help', 'provides', 'requires', 'recommends', 'conflicts', 'extras', 'majorver-provides', 'majorver-provides-versions=', 'majorver-only', 'legacy-provides' , 'legacy'])
Provides_PyMajorVer_Variant = False
Provides_PyMajorVer_Versions = None
if o in ('-h', '--help'):
print('-h, --help\tPrint help')
print('-P, --provides\tPrint Provides')
print('-R, --requires\tPrint Requires')
print('-r, --recommends\tPrint Recommends')
print('-C, --conflicts\tPrint Conflicts')
print('-E, --extras\tPrint Extras ')
print('-M, --majorver-provides\tPrint extra Provides with Python major version only for all Python versions')
print(' --majorver-provides-versions VERSIONS\n'
' \tPrint extra Provides with Python major version only for listed Python VERSIONS (comma separated, no spaces, e.g. 2.7,3.6)')
print('-m, --majorver-only\tPrint Provides/Requires with Python major version only')
print('-L, --legacy-provides\tPrint extra legacy pythonegg Provides')
print('-l, --legacy\tPrint legacy pythonegg Provides/Requires instead')
elif o in ('-P', '--provides'):
elif o in ('-R', '--requires'):
elif o in ('-r', '--recommends'):
elif o in ('-C', '--conflicts'):
elif o in ('-E', '--extras'):
elif o in ('-M', '--majorver-provides'):
Provides_PyMajorVer_Variant = True
elif o in ('--majorver-provides-versions'):
Provides_PyMajorVer_Versions = a.split(",")
elif o in ('-m', '--majorver-only'):
elif o in ('-L', '--legacy-provides'):
elif o in ('-l', '--legacy'):
if Provides_PyMajorVer_Variant and Provides_PyMajorVer_Versions:
print("Error, options --majorver-provides and --majorver-provides-versions are mutually incompatible.")
files = stdin.readlines()
# add dependency based on path, versioned if within versioned python directory
if py_abi and (lower.endswith('.py') or lower.endswith('.pyc') or lower.endswith('.pyo')):
purelib = get_python_lib(standard_lib=0, plat_specific=0).split(version[:3])[0]
platlib = get_python_lib(standard_lib=0, plat_specific=1).split(version[:3])[0]
for lib in (purelib, platlib):
spec = ('==', f.split(lib)[1].split(sep)[0])
if spec not in py_deps[name]:
py_deps[name].append(spec)
# XXX: hack to workaround RPM internal dependency generator not passing directories
lower_dir = dirname(lower)
if lower_dir.endswith('.egg') or \
lower_dir.endswith('.egg-info') or \
lower_dir.endswith('.dist-info'):
# Determine provide, requires, conflicts & recommends based on egg/dist metadata
if lower.endswith('.egg') or \
lower.endswith('.egg-info') or \
lower.endswith('.dist-info'):
# This import is very slow, so only do it if needed
from pkg_resources import Distribution, FileMetadata, PathMetadata
metadata = PathMetadata(path_item, f)
metadata = FileMetadata(f)
dist = Distribution.from_location(path_item, dist_name, metadata)
# Check if py_version is defined in the metadata file/directory name
# Try to parse the Python version from the path the metadata
# resides at (e.g. /usr/lib/pythonX.Y/site-packages/...)
res = re.search(r"/python(?P<pyver>\d+\.\d+)/", path_item)
dist.py_version = res.group('pyver')
warn("Version for {!r} has not been found".format(dist), RuntimeWarning)
# XXX: https://github.com/pypa/setuptools/pull/1275
platform.python_version = lambda: dist.py_version
if Provides_PyMajorVer_Variant or PyMajorVer_Deps or legacy_Provides or legacy or Provides_PyMajorVer_Versions:
# Get the Python major version
pyver_major = dist.py_version.split('.')[0]
# If egg/dist metadata says package name is python, we provide python(abi)
py_deps[name].append(('==', dist.py_version))
if not legacy or not PyMajorVer_Deps:
name = 'python{}dist({})'.format(dist.py_version, dist.key)
if Provides_PyMajorVer_Variant or PyMajorVer_Deps or \
(Provides_PyMajorVer_Versions and dist.py_version in Provides_PyMajorVer_Versions):
pymajor_name = 'python{}dist({})'.format(pyver_major, dist.key)
if pymajor_name not in py_deps:
py_deps[pymajor_name] = []
if legacy or legacy_Provides:
legacy_name = 'pythonegg({})({})'.format(pyver_major, dist.key)
if legacy_name not in py_deps:
py_deps[legacy_name] = []
spec = ('==', dist.version)
if spec not in py_deps[name]:
py_deps[name].append(spec)
if Provides_PyMajorVer_Variant or \
(Provides_PyMajorVer_Versions and dist.py_version in Provides_PyMajorVer_Versions):
py_deps[pymajor_name].append(spec)
if legacy or legacy_Provides:
py_deps[legacy_name].append(spec)
if Requires or (Recommends and dist.extras):
# If egg/dist metadata says package name is python, we don't add dependency on python(abi)
elif py_abi and dist.py_version:
spec = ('==', dist.py_version)
if spec not in py_deps[name]:
py_deps[name].append(spec)
depsextras = dist.requires(extras=dist.extras)
for dep in reversed(depsextras):
# add requires/recommends based on egg/dist metadata
name = 'pythonegg({})({})'.format(pyver_major, dep.key)
name = 'python{}dist({})'.format(pyver_major, dep.key)
name = 'python{}dist({})'.format(dist.py_version, dep.key)
if spec not in py_deps[name]:
py_deps[name].append(spec)
# Unused, for automatic sub-package generation based on 'extras' from egg/dist metadata
# TODO: implement in rpm later, or...?
print('%%package\textras-{}'.format(extra))
print('Summary:\t{} extra for {} python package'.format(extra, dist.key))
print('Group:\t\tDevelopment/Python')
depsextras = dist.requires(extras=[extra])
for dep in reversed(depsextras):
print('Conflicts:\t{} {} {}'.format(dep.key, '==', spec[1]))
print('Requires:\t{} {} {}'.format(dep.key, spec[0], spec[1]))
print('%%description\t{}'.format(extra))
print('{} extra for {} python package'.format(extra, dist.key))
print('%%files\t\textras-{}\n'.format(extra))
# Should we really add conflicts for extras?
# Creating a meta package per extra with recommends on, which has
# the requires/conflicts in stead might be a better solution...
for dep in dist.requires(extras=dist.extras):
if spec not in py_deps[name]:
py_deps[name].append(spec)
names = list(py_deps.keys())
# Print out versioned provides, requires, recommends, conflicts
for spec in py_deps[name]:
print('{} {} {}'.format(name, spec[0], spec[1]))
# Print out unversioned provides, requires, recommends, conflicts