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