Blame SOURCES/pythondistdeps.py

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