summaryrefslogtreecommitdiff
path: root/docs/shared_bindings_matrix.py
blob: 59b67b7b28153567fca2e62cd26d8ef5cec4834e (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# The MIT License (MIT)
#
# Copyright (c) 2019 Michael Schroeder
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import json
import os
import re


SUPPORTED_PORTS = ["atmel-samd", "nrf"]


def get_shared_bindings():
    """ Get a list of modules in shared-bindings based on folder names
    """
    return [item for item in os.listdir("./shared-bindings")]


def read_mpconfig():
    """ Open 'circuitpy_mpconfig.mk' and return the contents.
    """
    configs = []
    with open("py/circuitpy_mpconfig.mk") as mpconfig:
        configs = mpconfig.read()

    return configs


def build_module_map(modules, configs):
    """ Establish the base of the JSON file, based on the contents from
        `configs`. Base will contain module names, if they're part of
        the `FULL_BUILD`, or their default value (0 | 1).

    """
    base = dict()
    full_build = False
    for module in modules:
        full_name = module
        search_name = module.lstrip("_")
        re_pattern = "CIRCUITPY_{}\s=\s(.+)".format(search_name.upper())
        find_config = re.search(re_pattern, configs)
        #print(module, "|", find_config)
        if not find_config:
            continue
        full_build = int("FULL_BUILD" in find_config.group(0))
        #print(find_config[1])
        if not full_build:
            default_val = find_config.group(1)
        else:
            default_val = "None"
        base[search_name] = {
            "name": full_name,
            "full_build": str(full_build),
            "default_value": default_val,
            "excluded": []
        }

    return get_excluded_boards(base)


def get_excluded_boards(base):
    """ Cycles through each board's `mpconfigboard.mk` file to determine
        if each module is included or not. Boards are selected by existence
        in a port listed in `SUPPORTED_PORTS` (e.g. `/port/nrf/feather_52840`)
    """
    modules = list(base.keys())
    for port in SUPPORTED_PORTS:
        port_dir = "ports/{}/boards".format(port)
        for entry in os.scandir(port_dir):
            if not entry.is_dir():
                continue
            contents = ""
            board_dir = os.path.join(entry.path, "mpconfigboard.mk")
            #print(board_dir)
            with open(board_dir) as board:
                contents = board.read()
            for module in modules:
                # check if board uses `SMALL_BUILD`. if yes, and current
                # module is marked as `FULL_BUILD`, board is excluded
                small_build = re.search("CIRCUITPY_SMALL_BUILD = 1", contents)
                if small_build and base[module]["full_build"] == "1":
                    base[module]["excluded"].append(entry.name)
                    continue

                # check if module is specifically disabled for this board
                re_pattern = "CIRCUITPY_{}\s=\s(\w)".format(module.upper())
                find_module = re.search(re_pattern, contents)
                if not find_module:
                    # check if default inclusion is off ('0'). if the board doesn't
                    # have it explicitly enabled, its excluded.
                    if base[module]["default_value"] == "0":
                        base[module]["excluded"].append(entry.name)
                    continue
                if (find_module.group(1) == "0" and
                   find_module.group(1) != base[module]["default_value"]):
                        base[module]["excluded"].append(entry.name)

    return base


def support_matrix():
    modules = get_shared_bindings()
    configs = read_mpconfig()
    base = build_module_map(sorted(modules), configs)

    return base