Blame SOURCES/pythonbundles.py

5cdac4
#!/usr/bin/python3 -sB
5cdac4
#                  (imports pythondistdeps from /usr/lib/rpm, hence -B)
5cdac4
#
5cdac4
# This program is free software.
5cdac4
#
5cdac4
# It is placed in the public domain or under the CC0-1.0-Universal license,
5cdac4
# whichever is more permissive.
5cdac4
#
5cdac4
# Alternatively, it may be redistributed and/or modified under the terms of
5cdac4
# the LGPL version 2.1 (or later) or GPL version 2 (or later).
5cdac4
#
5cdac4
# Use this script to generate bundled provides, e.g.:
5cdac4
# ./pythonbundles.py setuptools-47.1.1/pkg_resources/_vendor/vendored.txt
5cdac4
5cdac4
import pathlib
5cdac4
import sys
5cdac4
5cdac4
# inject parse_version import to pythondistdeps
5cdac4
# not the nicest API, but :/
5cdac4
from pkg_resources import parse_version
5cdac4
import pythondistdeps
5cdac4
pythondistdeps.parse_version = parse_version
5cdac4
5cdac4
5cdac4
def generate_bundled_provides(path, namespace):
5cdac4
    provides = set()
5cdac4
5cdac4
    for line in path.read_text().splitlines():
5cdac4
        line, _, comment = line.partition('#')
5cdac4
        if comment.startswith('egg='):
5cdac4
            # not a real comment
5cdac4
            # e.g. git+https://github.com/monty/spam.git@master#egg=spam&...
5cdac4
            egg, *_ = comment.strip().partition(' ')
5cdac4
            egg, *_ = egg.strip().partition('&')
5cdac4
            name = pythondistdeps.normalize_name(egg[4:])
5cdac4
            provides.add(f'Provides: bundled({namespace}({name}))')
5cdac4
            continue
5cdac4
        line = line.strip()
5cdac4
        if line:
5cdac4
            name, _, version = line.partition('==')
5cdac4
            name = pythondistdeps.normalize_name(name)
5cdac4
            bundled_name = f"bundled({namespace}({name}))"
5cdac4
            python_provide = pythondistdeps.convert(bundled_name, '==', version)
5cdac4
            provides.add(f'Provides: {python_provide}')
5cdac4
5cdac4
    return provides
5cdac4
5cdac4
5cdac4
def compare(expected, given):
5cdac4
    stripped = (l.strip() for l in given)
5cdac4
    no_comments = set(l for l in stripped if not l.startswith('#'))
5cdac4
    no_comments.discard('')
5cdac4
    if expected == no_comments:
5cdac4
        return True
5cdac4
    extra_expected = expected - no_comments
5cdac4
    extra_given = no_comments - expected
5cdac4
    if extra_expected:
5cdac4
        print('Missing expected provides:', file=sys.stderr)
5cdac4
        for provide in sorted(extra_expected):
5cdac4
            print(f'    - {provide}', file=sys.stderr)
5cdac4
    if extra_given:
5cdac4
        print('Redundant unexpected provides:', file=sys.stderr)
5cdac4
        for provide in sorted(extra_given):
5cdac4
            print(f'    + {provide}', file=sys.stderr)
5cdac4
    return False
5cdac4
5cdac4
5cdac4
if __name__ == '__main__':
5cdac4
    import argparse
5cdac4
5cdac4
    parser = argparse.ArgumentParser(prog=sys.argv[0],
5cdac4
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
5cdac4
    parser.add_argument('vendored', metavar='VENDORED.TXT',
5cdac4
                        help='Upstream information about vendored libraries')
5cdac4
    parser.add_argument('-c', '--compare-with', action='store',
5cdac4
                        help='A string value to compare with and verify')
5cdac4
    parser.add_argument('-n', '--namespace', action='store',
5cdac4
                        help='What namespace of provides will used', default='python3dist')
5cdac4
    args = parser.parse_args()
5cdac4
5cdac4
    provides = generate_bundled_provides(pathlib.Path(args.vendored), args.namespace)
5cdac4
5cdac4
    if args.compare_with:
5cdac4
        given = args.compare_with.splitlines()
5cdac4
        same = compare(provides, given)
5cdac4
        if not same:
5cdac4
            sys.exit(1)
5cdac4
    else:
5cdac4
        for provide in sorted(provides):
5cdac4
            print(provide)