Commit d9891f91 authored by José Henrique's avatar José Henrique
Browse files

Rework linter

parent 7f5f3d6d
......@@ -7,9 +7,13 @@ import operator
os.chdir('official_devices')
SUGGESTIONS = []
ALLOWED_VERSIONS = ['eleven', 'ten', 'ten_plus', 'pie', 'pie_plus']
REQUIRED_PARAMS_DEVICE = ['name', 'brand', 'codename', 'supported_versions']
REQUIRED_PARAMS_TEAM_MEMBER = ['name', 'country', 'github_username', 'devices', 'ci_username']
REQUIRED_PARAMS_TEAM_MEMBER_DEVICE = ['codename', 'versions']
def test_integrity_devices():
global SUGGESTIONS
try:
devices = json.loads(open('devices.json', 'r').read())
except:
......@@ -18,176 +22,66 @@ def test_integrity_devices():
for device in devices:
device_index = devices.index(device)
if 'name' not in device:
SUGGESTIONS.append(
f"'name' key not found in devices.json for device index value {device_index}")
if 'brand' not in device:
SUGGESTIONS.append(
f"'brand' key not found in devices.json for device index value {device_index}")
if 'codename' not in device:
SUGGESTIONS.append(
f"'codename' key not found in devices.json for device index value {device_index}")
else:
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:
codename = device.get('codename')
if not os.path.isfile(f"images/{codename}.png"):
SUGGESTIONS.append(
f"images/{codename}.png not found for codename {codename} in devices.json for device index value {device_index}")
if 'supported_versions' not in device:
SUGGESTIONS.append(
f"'supported_versions' key not found in devices.json for index value {device_index}")
continue
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:
SUGGESTIONS.append(
f"'version_code' key not found in devices.json for device index value {device_index} and version index value {version_index}")
if supported_version['version_code'] not in ALLOWED_VERSIONS:
SUGGESTIONS.append(
f"'version_code' key has a value that is not allowed in devices.json for device index value {device_index} and version index value {version_index}")
return
def test_integrity_team():
try:
lead = json.loads(open('team/lead.json', 'r').read())
except:
SUGGESTIONS.append('team/lead.json is not a valid JSON.')
return
try:
core = json.loads(open('team/core.json', 'r').read())
except:
SUGGESTIONS.append('team/core.json is not a valid JSON.')
return
try:
maintainers = json.loads(open('team/maintainers.json', 'r').read())
except:
SUGGESTIONS.append('team/maintainers.json is not a valid JSON.')
return
for member in maintainers:
member_index = maintainers.index(member)
if 'name' not in member:
SUGGESTIONS.append(
f"'name' key not found in maintainers.json for member index value {member_index}")
if 'country' not in member:
SUGGESTIONS.append(
f"'country' key not found in maintainers.json for member index value {member_index}")
if 'github_username' not in member:
SUGGESTIONS.append(
f"'github_username' key not found in maintainers.json for member index value {member_index}")
if 'devices' not in member:
SUGGESTIONS.append(
f"'devices' key not found in maintainers.json for member index value {member_index}")
continue
devices = member['devices']
for device in devices:
device_index = devices.index(device)
if 'codename' not in device:
SUGGESTIONS.append(
f"'codename' key not found in maintainers.json for member index value {member_index} and device index value {device_index}")
if 'versions' not in device:
SUGGESTIONS.append(
f"'versions' key not found in maintainers.json for member index value {member_index}")
continue
versions = device['versions']
for version in versions:
version_index = versions.index(version)
if version not in ALLOWED_VERSIONS:
if 'supported_versions' in device:
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:
SUGGESTIONS.append(
f"'versions' key has a value that is not allowed in maintainers.json for device index value {device_index} and version index value {version_index} and member index value {member_index}")
devices = lead['devices']
for device in devices:
device_index = devices.index(device)
if 'codename' not in device:
SUGGESTIONS.append(
f"'codename' key not found in lead.json for device index value {device_index}")
if 'versions' not in device:
SUGGESTIONS.append(
f"'versions' key not found in lead.json for device index value {device_index}")
continue
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}")
versions = device['versions']
for version in versions:
version_index = versions.index(version)
if version not in ALLOWED_VERSIONS:
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:
SUGGESTIONS.append(
f"'versions' key has a value that is not allowed in lead.json for device index value {device_index} and version index value {version_index}")
if 'name' not in lead:
SUGGESTIONS.append(
f"'name' key not found in lead.json")
if 'country' not in lead:
SUGGESTIONS.append(
f"'country' key not found in lead.json")
if 'github_username' not in lead:
SUGGESTIONS.append(
f"'github_username' key not found in lead.json")
if 'devices' not in lead:
SUGGESTIONS.append(
f"'devices' key not found in lead.json")
for member in core:
member_index = core.index(member)
if 'name' not in member:
SUGGESTIONS.append(
f"'name' key not found in core.json for member index value {member_index}")
if 'country' not in member:
SUGGESTIONS.append(
f"'country' key not found in core.json for member index value {member_index}")
if 'github_username' not in member:
SUGGESTIONS.append(
f"'github_username' key not found in core.json for member index value {member_index}")
if 'devices' not in member:
SUGGESTIONS.append(
f"'devices' key not found in core.json for member index value {member_index}")
continue
devices = member['devices']
for device in devices:
device_index = devices.index(device)
if 'codename' not in device:
SUGGESTIONS.append(
f"'codename' key not found in core.json for member index value {member_index} and device index value {device_index}")
f"'{param}' key not found in {json_file} for member index value {member_index}")
if 'versions' not in device:
SUGGESTIONS.append(
f"'versions' key not found in core.json for member index value {member_index}")
continue
versions = device['versions']
for version in versions:
version_index = versions.index(version)
if 'devices' in member:
devices = member['devices']
for device in devices:
device_index = devices.index(device)
if version not in ALLOWED_VERSIONS:
SUGGESTIONS.append(
f"'versions' key has a value that is not allowed in core.json for device index value {device_index} and version index value {version_index} and member index value {member_index}")
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}")
return
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}")
def sanitize_username(username):
return ''.join(ch for ch in username.lower() if ch.isalnum())
def format_json():
# Load JSON's
......@@ -196,9 +90,13 @@ def format_json():
core = json.loads(open('team/core.json', 'r').read())
maintainers = json.loads(open('team/maintainers.json', 'r').read())
# Sort JSON alphabatically with selected keys
# Sanitize ci_username
for roles in [core, maintainers]:
for member in roles:
member["ci_username"] = sanitize_username(member["ci_username"])
# Sort JSON alphabetically with selected keys
devices = sorted(devices, key=operator.itemgetter('codename', 'brand'))
#lead = sorted(lead, key=operator.itemgetter('name', 'country'))
core = sorted(core, key=operator.itemgetter('name', 'country'))
maintainers = sorted(
maintainers, key=operator.itemgetter('name', 'country'))
......@@ -232,6 +130,8 @@ def format_json():
def main():
print('Running Integrity tests for all the JSON\'s.')
test_integrity_devices()
test_integrity_team()
if len(SUGGESTIONS) > 0:
print('Integrity test for one or more JSON\'s failed. Cannot proceed with JSON formatter.\n\n')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment