summaryrefslogtreecommitdiff
path: root/compare_mandata.py
blob: 08d4c6b3c3286e14f5c4ceee1bb6b6caaa931443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'''Check whether the manpage extensions and directories list hardcoded in brp-compress
are the same as the lists stored in pyproject_save_files.py.
There is an open issue for RPM to provide them both as macros:
https://github.com/rpm-software-management/rpm/issues/1865
Once that happens, this script can be removed.
'''

import argparse
import re
import sys

from pathlib import PosixPath

from pyproject_buildrequires import print_err
from pyproject_save_files import prepend_mandirs, MANPAGE_EXTENSIONS



def read_brp_compress(filename):

    contents = filename.read_text()
    # To avoid duplicity of the manpage extensions which are listed a few times
    # in the source file, they are stored in set and then retyped to a sorted list
    manpage_exts = sorted(
        set(re.findall(r'\(?(\w+)\\+\)?\$?', contents))
    )

    # Get rid of ${PREFIX} when extracting the manpage directories
    mandirs = [
        entry.replace('.${PREFIX}', '/PREFIX')
        for entry in contents.split()
        if entry.startswith('.${PREFIX}')
    ]

    return manpage_exts, sorted(mandirs)


def compare_mandirs(brp_compress_mandirs):
    '''
    Check whether each of brp-compress mandirs entry is present in the list
    stored in pyproject_save_files.py
    '''

    pyp_save_files_mandirs = sorted(prepend_mandirs(prefix='/PREFIX'))
    if brp_compress_mandirs == pyp_save_files_mandirs:
        return True
    else:
        print_err('Mandir lists don\'t match, update the list in pyproject_save_files.py')
        print_err('brp-compress list:', brp_compress_mandirs)
        print_err('pyproject_save_files list:', pyp_save_files_mandirs)
        return False


def compare_manpage_extensions(brp_compress_manpage_exts):
    '''
    Check whether each of brp-compress manpage extension is present in the list
    stored in pyproject_save_files.py
    '''

    if brp_compress_manpage_exts == sorted(MANPAGE_EXTENSIONS):
        return True
    else:
        print_err('Manpage extension lists don\'t match, update the list in pyproject_save_files.py')
        print_err('brp-compress list:', brp_compress_manpage_exts)
        print_err('pyproject_save_files list:', sorted(MANPAGE_EXTENSIONS))
        return False


def main(args):
    src_manpage_exts, src_mandirs = read_brp_compress(args.filename)
    extension_check_successful = compare_manpage_extensions(src_manpage_exts)
    mandir_check_successful = compare_mandirs(src_mandirs)
    if extension_check_successful and mandir_check_successful:
        sys.exit(0)
    else:
        sys.exit(1)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--filename', type=PosixPath, required=True,
                        help='Provide location of brp-compress file')
    main(parser.parse_args())