summaryrefslogtreecommitdiff
path: root/Meta/lint-ports.py
blob: a25fc109aa25da19afc50898ae2facca9a7bc76c (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
84
85
86
#!/usr/bin/env python3

import os
import re
import sys

# Matches e.g. "| [`bash`]..." and captures "bash" in group 1
PORT_TABLE_REGEX = re.compile(r'^\| \[`([^`]+)`\][^`]+$', re.MULTILINE)

PORT_TABLE_FILE = 'AvailablePorts.md'
IGNORE_FILES = {
    '.gitignore',
    '.port_include.sh',
    PORT_TABLE_FILE,
    'build_all.sh',
    'build_installed.sh',
    'README.md',
    '.hosted_defs.sh'
}


def read_port_table(filename):
    """Open a file and find all PORT_TABLE_REGEX matches.

    Args:
        filename (str): file name

    Returns:
        set: all PORT_TABLE_REGEX matches
    """
    with open(filename, 'r') as fp:
        return set(PORT_TABLE_REGEX.findall(fp.read()))


def read_port_dirs():
    """Check Ports directory for unexpected files and check each port has a package.sh file.

    Returns:
        list: all ports (set), errors encountered (bool)
    """

    ports = set()
    all_good = True
    for entry in os.listdir():
        if entry in IGNORE_FILES:
            continue
        if not os.path.isdir(entry):
            print('"Ports/{}" is neither a port (not a directory) nor an ignored file?!'.format(entry))
            all_good = False
            continue
        if not os.path.exists(entry + '/package.sh'):
            print('"Ports/{}/" is missing its package.sh?!'.format(entry))
            all_good = False
            continue
        ports.add(entry)

    return ports, all_good


def run():
    """Check Ports directory contents for errors."""

    from_table = read_port_table(PORT_TABLE_FILE)
    from_fs, all_good = read_port_dirs()

    if from_table - from_fs:
        all_good = False
        print('AvailablePorts.md lists ports that do not appear in the file system:')
        for port in sorted(from_table - from_fs):
            print('    {}'.format(port))

    if from_fs - from_table:
        all_good = False
        print('AvailablePorts.md is missing the following ports:')
        for port in sorted(from_fs - from_table):
            print('    {}'.format(port))

    if not all_good:
        sys.exit(1)

    print('No issues found.')


if __name__ == '__main__':
    os.chdir(os.path.dirname(__file__) + "/../Ports")
    run()