Skip to content

Commit

Permalink
Perform stricter lint checks
Browse files Browse the repository at this point in the history
  • Loading branch information
zvecr committed Jun 10, 2022
1 parent a599550 commit 9b5c0d0
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 47 deletions.
130 changes: 91 additions & 39 deletions lib/python/qmk/cli/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,45 @@
from milc import cli

from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.keyboard import keyboard_completer, list_keyboards
from qmk.keymap import locate_keymap
from qmk.info import info_json, keymap_json
from qmk.keyboard import keyboard_completer, keyboard_folder, list_keyboards
from qmk.keymap import locate_keymap, list_keymaps
from qmk.path import is_keyboard, keyboard
from qmk.git import git_get_ignored_files


def keymap_check(kb, km):
"""Perform the keymap level checks.
def _list_defaultish_keymaps(kb):
"""Return default like keymaps for a given keyboard
"""
ok = True
keymap_path = locate_keymap(kb, km)
defaultish = ['ansi', 'iso', 'via']

if not keymap_path:
keymaps = set()
for x in list_keymaps(kb):
if x in defaultish or x.startswith('default'):
keymaps.add(x)

return keymaps


def _handle_json_errors(kb, info, file):
"""Convert any json errors into lint errors
"""
ok = True
# Check for errors in the json
if info['parse_errors']:
ok = False
cli.log.error("%s: Can't find %s keymap.", kb, km)
cli.log.error(f'{kb}: Errors found when generating {file}.')

if cli.config.lint.strict and info['parse_warnings']:
ok = False
cli.log.error(f'{kb}: Warnings found when generating {file} (Strict mode enabled.)')
return ok


def rules_mk_assignment_only(keyboard_path):
def _rules_mk_assignment_only(kb):
"""Check the keyboard-level rules.mk to ensure it only has assignments.
"""
keyboard_path = keyboard(kb)
current_path = Path()
errors = []

Expand Down Expand Up @@ -58,8 +75,58 @@ def rules_mk_assignment_only(keyboard_path):
return errors


def keymap_check(kb, km):
"""Perform the keymap level checks.
"""
ok = True
keymap_path = locate_keymap(kb, km)

if not keymap_path:
ok = False
cli.log.error("%s: Can't find %s keymap.", kb, km)
return ok

km_info = keymap_json(kb, km)

if not _handle_json_errors(kb, km_info, 'keymap.json'):
ok = False

# Additional checks
invalid_files = git_get_ignored_files(keymap_path.parent)
for file in invalid_files:
cli.log.error(f'{kb}/{km}: The file "{file}" should not exist!')
ok = False

return ok


def keyboard_check(kb):
"""Perform the keyboard level checks.
"""
ok = True
kb_info = info_json(kb)

if not _handle_json_errors(kb, kb_info, 'info.json'):
ok = False

# Additional checks
rules_mk_assignment_errors = _rules_mk_assignment_only(kb)
if rules_mk_assignment_errors:
ok = False
cli.log.error('%s: Non-assignment code found in rules.mk. Move it to post_rules.mk instead.', kb)
for assignment_error in rules_mk_assignment_errors:
cli.log.error(assignment_error)

invalid_files = git_get_ignored_files(f'keyboards/{kb}/')
for file in invalid_files:
cli.log.error(f'{kb}: The file "{file}" should not exist!')
ok = False

return ok


@cli.argument('--strict', action='store_true', help='Treat warnings as errors')
@cli.argument('-kb', '--keyboard', completer=keyboard_completer, help='Comma separated list of keyboards to check')
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Comma separated list of keyboards to check')
@cli.argument('-km', '--keymap', help='The keymap to check')
@cli.argument('--all-kb', action='store_true', arg_only=True, help='Check all keyboards')
@cli.subcommand('Check keyboard and keymap for common mistakes.')
Expand All @@ -73,7 +140,7 @@ def lint(cli):
# Determine our keyboard list
if cli.args.all_kb:
if cli.args.keyboard:
cli.log.warning('Both --all-kb and --keyboard passed, --all-kb takes presidence.')
cli.log.warning('Both --all-kb and --keyboard passed, --all-kb takes precedence.')

keyboard_list = list_keyboards()
elif not cli.config.lint.keyboard:
Expand All @@ -89,38 +156,23 @@ def lint(cli):
cli.log.error('No such keyboard: %s', kb)
continue

# Gather data about the keyboard.
ok = True
keyboard_path = keyboard(kb)
keyboard_info = info_json(kb)

# Check for errors in the info.json
if keyboard_info['parse_errors']:
ok = False
cli.log.error('%s: Errors found when generating info.json.', kb)
# Determine keymaps to also check
if cli.config.lint.keymap:
keymaps = {cli.config.lint.keymap}
else:
keymaps = _list_defaultish_keymaps(kb)
# Ensure that at least a 'default' keymap always exists
keymaps.add('default')

if cli.config.lint.strict and keyboard_info['parse_warnings']:
ok = False
cli.log.error('%s: Warnings found when generating info.json (Strict mode enabled.)', kb)
ok = True

# Check the rules.mk file(s)
rules_mk_assignment_errors = rules_mk_assignment_only(keyboard_path)
if rules_mk_assignment_errors:
# keyboard level checks
if not keyboard_check(kb):
ok = False
cli.log.error('%s: Non-assignment code found in rules.mk. Move it to post_rules.mk instead.', kb)
for assignment_error in rules_mk_assignment_errors:
cli.log.error(assignment_error)

# Keymap specific checks
if cli.config.lint.keymap:
if not keymap_check(kb, cli.config.lint.keymap):
ok = False

# Check if all non-data driven macros exist in <keyboard.h>
for layout, data in keyboard_info['layouts'].items():
# Matrix data should be a list with exactly two integers: [0, 1]
if not data['c_macro'] and not all('matrix' in key_data.keys() or len(key_data) == 2 or all(isinstance(n, int) for n in key_data) for key_data in data['layout']):
cli.log.error(f'{kb}: "{layout}" has no "matrix" definition in either "info.json" or "<keyboard>.h"!')
for keymap in keymaps:
if not keymap_check(kb, keymap):
ok = False

# Report status
Expand Down
9 changes: 9 additions & 0 deletions lib/python/qmk/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,12 @@ def git_check_deviation(active_branch):
cli.run(['git', 'fetch', 'upstream', active_branch])
deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}'])
return bool(deviations.returncode)


def git_get_ignored_files(check_dir='.'):
"""Return a list of files that would be captured by the current .gitingore
"""
invalid = cli.run(['git', 'ls-files', '-c', '-o', '-i', '--exclude-standard', check_dir])
if invalid.returncode != 0:
return []
return invalid.stdout.strip().splitlines()
26 changes: 18 additions & 8 deletions lib/python/qmk/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,11 @@ def merge_info_jsons(keyboard, info_data):
for new_key, existing_key in zip(layout['layout'], info_data['layouts'][layout_name]['layout']):
existing_key.update(new_key)
else:
layout['c_macro'] = False
info_data['layouts'][layout_name] = layout
if not all('matrix' in key_data.keys() or len(key_data) == 2 or all(isinstance(n, int) for n in key_data) for key_data in layout['layout']):
_log_error(info_data, "Matrix positions for %s are missing" % layout_name)
else:
layout['c_macro'] = False
info_data['layouts'][layout_name] = layout

# Update info_data with the new data
if 'layouts' in new_info_data:
Expand Down Expand Up @@ -863,15 +866,22 @@ def keymap_json(keyboard, keymap):
keymap_rules = keymap_folder / 'rules.mk'
keymap_file = keymap_folder / 'keymap.json'

# Build the info.json file
kb_info_json = info_json(keyboard)
km_defaults = {
'keyboard_folder': f'{keyboard}/{keymap}', # bodge for error text
'parse_errors': [],
'parse_warnings': [],
}

# Merge in the data from keymap.json
# Build the keyboard overrides from keymap.json file
km_info_json = keymap_json_config(keyboard, keymap) if keymap_file.exists() else {}
deep_update(kb_info_json, km_info_json)
deep_update(km_info_json, km_defaults)

# Merge in the data from config.h, and rules.mk
_extract_rules_mk(kb_info_json, parse_rules_mk_file(keymap_rules))
_extract_config_h(kb_info_json, parse_config_h_file(keymap_config))
_extract_rules_mk(km_info_json, parse_rules_mk_file(keymap_rules))
_extract_config_h(km_info_json, parse_config_h_file(keymap_config))

# Merge in the data from keymap.json with the info.json file
kb_info_json = info_json(keyboard)
deep_update(kb_info_json, km_info_json)

return kb_info_json

0 comments on commit 9b5c0d0

Please sign in to comment.