Blame SOURCES/compare_mandata.py

7ef706
'''Check whether the manpage extensions and directories list hardcoded in brp-compress
7ef706
are the same as the lists stored in pyproject_save_files.py.
7ef706
There is an open issue for RPM to provide them both as macros:
7ef706
https://github.com/rpm-software-management/rpm/issues/1865
7ef706
Once that happens, this script can be removed.
7ef706
'''
7ef706
7ef706
import argparse
7ef706
import re
7ef706
import sys
7ef706
7ef706
from pathlib import PosixPath
7ef706
7ef706
from pyproject_buildrequires import print_err
7ef706
from pyproject_save_files import prepend_mandirs, MANPAGE_EXTENSIONS
7ef706
7ef706
7ef706
7ef706
def read_brp_compress(filename):
7ef706
7ef706
    contents = filename.read_text()
7ef706
    # To avoid duplicity of the manpage extensions which are listed a few times
7ef706
    # in the source file, they are stored in set and then retyped to a sorted list
7ef706
    manpage_exts = sorted(
7ef706
        set(re.findall(r'\(?(\w+)\\+\)?\$?', contents))
7ef706
    )
7ef706
7ef706
    # Get rid of ${PREFIX} when extracting the manpage directories
7ef706
    mandirs = [
7ef706
        entry.replace('.${PREFIX}', '/PREFIX')
7ef706
        for entry in contents.split()
7ef706
        if entry.startswith('.${PREFIX}')
7ef706
    ]
7ef706
7ef706
    return manpage_exts, sorted(mandirs)
7ef706
7ef706
7ef706
def compare_mandirs(brp_compress_mandirs):
7ef706
    '''
7ef706
    Check whether each of brp-compress mandirs entry is present in the list
7ef706
    stored in pyproject_save_files.py
7ef706
    '''
7ef706
7ef706
    pyp_save_files_mandirs = sorted(prepend_mandirs(prefix='/PREFIX'))
7ef706
    if brp_compress_mandirs == pyp_save_files_mandirs:
7ef706
        return True
7ef706
    else:
7ef706
        print_err('Mandir lists don\'t match, update the list in pyproject_save_files.py')
7ef706
        print_err('brp-compress list:', brp_compress_mandirs)
7ef706
        print_err('pyproject_save_files list:', pyp_save_files_mandirs)
7ef706
        return False
7ef706
7ef706
7ef706
def compare_manpage_extensions(brp_compress_manpage_exts):
7ef706
    '''
7ef706
    Check whether each of brp-compress manpage extension is present in the list
7ef706
    stored in pyproject_save_files.py
7ef706
    '''
7ef706
7ef706
    if brp_compress_manpage_exts == sorted(MANPAGE_EXTENSIONS):
7ef706
        return True
7ef706
    else:
7ef706
        print_err('Manpage extension lists don\'t match, update the list in pyproject_save_files.py')
7ef706
        print_err('brp-compress list:', brp_compress_manpage_exts)
7ef706
        print_err('pyproject_save_files list:', sorted(MANPAGE_EXTENSIONS))
7ef706
        return False
7ef706
7ef706
7ef706
def main(args):
7ef706
    src_manpage_exts, src_mandirs = read_brp_compress(args.filename)
7ef706
    extension_check_successful = compare_manpage_extensions(src_manpage_exts)
7ef706
    mandir_check_successful = compare_mandirs(src_mandirs)
7ef706
    if extension_check_successful and mandir_check_successful:
7ef706
        sys.exit(0)
7ef706
    else:
7ef706
        sys.exit(1)
7ef706
7ef706
7ef706
if __name__ == '__main__':
7ef706
    parser = argparse.ArgumentParser()
7ef706
    parser.add_argument('-f', '--filename', type=PosixPath, required=True,
7ef706
                        help='Provide location of brp-compress file')
7ef706
    main(parser.parse_args())