validator.py 7.64 KB
Newer Older
Shreejoy Dash's avatar
Shreejoy Dash committed
1
import os
shreejoy's avatar
shreejoy committed
2
import sys
Shreejoy Dash's avatar
Shreejoy Dash committed
3
import json
shreejoy's avatar
shreejoy committed
4
import requests
Shreejoy Dash's avatar
Shreejoy Dash committed
5
import operator
shreejoy's avatar
shreejoy committed
6

Shreejoy Dash's avatar
Shreejoy Dash committed
7
os.chdir('official_devices')
shreejoy's avatar
shreejoy committed
8
SUGGESTIONS = []
José Henrique's avatar
José Henrique committed
9
ALLOWED_VERSIONS = ['eleven', 'eleven_plus', 'ten', 'ten_plus']
José Henrique's avatar
José Henrique committed
10
11
REQUIRED_PARAMS_DEVICE = ['name', 'brand', 'codename', 'supported_versions', 'repositories']
REQUIRED_PARAMS_DEVICE_LIST = ['supported_versions', 'repositories']
José Henrique's avatar
José Henrique committed
12
13
REQUIRED_PARAMS_TEAM_MEMBER = ['name', 'country', 'github_username', 'devices', 'ci_username']
REQUIRED_PARAMS_TEAM_MEMBER_DEVICE = ['codename', 'versions']
José Henrique's avatar
José Henrique committed
14
DEPRECATED_VERSIONS = ['ten', 'ten_plus']
shreejoy's avatar
shreejoy committed
15
16
17


def test_integrity_devices():
José Henrique's avatar
José Henrique committed
18
    global SUGGESTIONS
shreejoy's avatar
shreejoy committed
19
20
21
22
23
24
25
26
    try:
        devices = json.loads(open('devices.json', 'r').read())
    except:
        SUGGESTIONS.append('devices.json is not a valid JSON.')
        return

    for device in devices:
        device_index = devices.index(device)
José Henrique's avatar
José Henrique committed
27
28
29
30
31
        for param in REQUIRED_PARAMS_DEVICE:
            if param not in device:
                SUGGESTIONS.append(
                    f"'{param}' key not found in devices.json for device index value {device_index}")
        if 'codename' in device:
shreejoy's avatar
shreejoy committed
32
33
34
            codename = device.get('codename')
            if not os.path.isfile(f"images/{codename}.png"):
                SUGGESTIONS.append(
José Henrique's avatar
José Henrique committed
35
                    f"images/{codename}.png not found for codename {codename} in devices.json and device index value {device_index}")
shreejoy's avatar
shreejoy committed
36

José Henrique's avatar
José Henrique committed
37
38
39
40
41
42
        for param in REQUIRED_PARAMS_DEVICE_LIST:
            if not isinstance(device[param], list):
                SUGGESTIONS.append(
                    f"'{param}' key is not a valid param in devices.json for device index value {device_index}")

        if isinstance(device["supported_versions"], list):
José Henrique's avatar
José Henrique committed
43
44
45
46
            supported_versions = device['supported_versions']
            for supported_version in supported_versions:
                version_index = supported_versions.index(supported_version)
                if 'version_code' not in supported_version:
shreejoy's avatar
shreejoy committed
47
                    SUGGESTIONS.append(
José Henrique's avatar
José Henrique committed
48
49
50
51
                        f"'version_code' key not found in devices.json for device index value {device_index} and version index value {version_index}")
                elif supported_version['version_code'] not in ALLOWED_VERSIONS:
                    SUGGESTIONS.append(
                        f"'version_code' version is not allowed in devices.json for device index value {device_index} and version index value {version_index}")
shreejoy's avatar
shreejoy committed
52
53


José Henrique's avatar
José Henrique committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def test_integrity_team():
    global SUGGESTIONS
    for team_role in ['lead', 'core', 'maintainers']:
        json_file = 'team/' + team_role + '.json'
        try:
            members = json.loads(open('team/' + team_role + '.json', 'r').read())
        except:
            SUGGESTIONS.append(json_file + ' is not a valid JSON.')
            return
        if team_role == "lead":
            members = [members]
        for member in members:
            member_index = members.index(member)

            for param in REQUIRED_PARAMS_TEAM_MEMBER:
                if param not in member:
shreejoy's avatar
shreejoy committed
70
                    SUGGESTIONS.append(
José Henrique's avatar
José Henrique committed
71
                        f"'{param}' key not found in {json_file} for member index value {member_index}")
shreejoy's avatar
shreejoy committed
72

José Henrique's avatar
José Henrique committed
73
74
75
76
            if 'devices' in member:
                devices = member['devices']
                for device in devices:
                    device_index = devices.index(device)
shreejoy's avatar
shreejoy committed
77

José Henrique's avatar
José Henrique committed
78
79
80
81
                    for param in REQUIRED_PARAMS_TEAM_MEMBER_DEVICE:
                        if param not in device:
                            SUGGESTIONS.append(
                                f"{param} key not found in {json_file} for member index value {member_index} and device index value {device_index}")
shreejoy's avatar
shreejoy committed
82

José Henrique's avatar
José Henrique committed
83
84
85
86
87
88
                    if 'versions' in device:
                        versions = device['versions']
                        for version in versions:
                            if version not in ALLOWED_VERSIONS:
                                SUGGESTIONS.append(
                                    f"{version} version is not allowed in {json_file} for device index value {device_index} and member index value {member_index}")
shreejoy's avatar
shreejoy committed
89

José Henrique's avatar
José Henrique committed
90
91
def sanitize_username(username):
    return ''.join(ch for ch in username.lower() if ch.isalnum())
shreejoy's avatar
shreejoy committed
92

José Henrique's avatar
José Henrique committed
93
94
95
96
97
98
99
100
101
def is_device_deprecated(device_codename, version, devices):
    for device in devices:
        if device["codename"] == device_codename:
            for supported_version in device["supported_versions"]:
                if supported_version["version_code"] == version:
                    return "deprecated" in supported_version and supported_version["deprecated"]
    return False
            

shreejoy's avatar
shreejoy committed
102
103
104
105
106
107
108
def format_json():
    # Load JSON's
    devices = json.loads(open('devices.json', 'r').read())
    lead = json.loads(open('team/lead.json', 'r').read())
    core = json.loads(open('team/core.json', 'r').read())
    maintainers = json.loads(open('team/maintainers.json', 'r').read())

José Henrique's avatar
José Henrique committed
109
110
111
112
113
    # Sanitize ci_username
    for roles in [core, maintainers]:
        for member in roles:
            member["ci_username"] = sanitize_username(member["ci_username"])

José Henrique's avatar
José Henrique committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    # Sanitize github_username
    for roles in [core, maintainers]:
        for member in roles:
            member["github_username"] = sanitize_username(member["github_username"])

    # Sanitize deprecated versions
    for roles in [core, maintainers]:
        for member in roles:
            devices_to_remove = []
            for device in member["devices"]:
                versions_to_remove = []
                for version in device["versions"]:
                    if version not in versions_to_remove:
                        if is_device_deprecated(device["codename"], version, devices):
                            versions_to_remove.append(version)
                        elif version in DEPRECATED_VERSIONS:
                            versions_to_remove.append(version)
                for version in versions_to_remove:
                    device["versions"].remove(version)
                if not device["versions"]:
                    devices_to_remove.append(device)
            for device in devices_to_remove:
                member["devices"].remove(device)

José Henrique's avatar
José Henrique committed
138
    # Sort JSON alphabetically with selected keys
shreejoy's avatar
shreejoy committed
139
140
141
142
143
144
    devices = sorted(devices, key=operator.itemgetter('codename', 'brand'))
    core = sorted(core, key=operator.itemgetter('name', 'country'))
    maintainers = sorted(
        maintainers, key=operator.itemgetter('name', 'country'))

    # Dump them
José Henrique's avatar
José Henrique committed
145
146
147
148
    devices = json.dumps(devices, indent=3, sort_keys=False, ensure_ascii=False)
    lead = json.dumps(lead, indent=3, sort_keys=False, ensure_ascii=False)
    core = json.dumps(core, indent=3, sort_keys=False, ensure_ascii=False)
    maintainers = json.dumps(maintainers, indent=3, sort_keys=False, ensure_ascii=False)
shreejoy's avatar
shreejoy committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

    # Open JSON's
    devices_json = open('devices.json', 'w')
    lead_json = open('team/lead.json', 'w')
    core_json = open('team/core.json', 'w')
    maintainers_json = open('team/maintainers.json', 'w')

    # Write dump's
    devices_json.write(devices + '\n')
    lead_json.write(lead + '\n')
    core_json.write(core + '\n')
    maintainers_json.write(maintainers + '\n')

    # Close JSON's
    devices_json.close()
    lead_json.close()
    core_json.close()
    maintainers_json.close()

    return 0


def main():
    print('Running Integrity tests for all the JSON\'s.')
José Henrique's avatar
José Henrique committed
173
174
    test_integrity_devices()
    test_integrity_team()
shreejoy's avatar
shreejoy committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

    if len(SUGGESTIONS) > 0:
        print('Integrity test for one or more JSON\'s failed. Cannot proceed with JSON formatter.\n\n')
        print('Below might be the reasons for test failure.')
        for SUGGESTION in SUGGESTIONS:
            print(SUGGESTION)
        sys.exit(1)
    else:
        print('Integrity test passed successfully. Now formatting all the JSON\'s')
        format_json()
        sys.exit(0)


if __name__ == "__main__":
    main()