Blame compare_mandata.py

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