Skip to content

Commit 3d01bd0

Browse files
authored
Add a compile_json job to replace compile_firmware. (#67)
* switch to qmk list-keyboards * add a compile_json job that will replace compile_firmware * fix the qmk list-keyboards parsing * Add a skeleton for keymap.json
1 parent be737d0 commit 3d01bd0

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

qmk_commands.py

+23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
LUFA_GIT_BRANCH = os.environ.get('LUFA_GIT_BRANCH', 'master')
3232
LUFA_GIT_URL = os.environ.get('LUFA_GIT_URL', 'https://github.com/qmk/lufa')
3333

34+
KEYMAP_DOCUMENTATION = """"This file is a QMK Configurator export. You can import this at <https://config.qmk.fm>. It can also be used directly with QMK's source code.
35+
36+
To setup your QMK environment check out the tutorial: <https://docs.qmk.fm/#/newbs>
37+
38+
You can convert this file to a keymap.c using this command: `qmk json2c {keymap}`
39+
40+
You can compile this keymap using this command: `qmk compile {keymap}`"
41+
"""
42+
3443
ZIP_EXCLUDES = {
3544
'qmk_firmware': ['qmk_firmware/.build/*', 'qmk_firmware/.git/*', 'qmk_firmware/lib/chibios/.git', 'qmk_firmware/lib/chibios-contrib/.git'],
3645
'chibios': ['chibios/.git/*'],
@@ -302,3 +311,17 @@ def write_version_txt():
302311
hash = check_output(['git', 'rev-parse', 'HEAD'], universal_newlines=True)
303312
version_txt = Path('version.txt')
304313
version_txt.write_text(hash + '\n')
314+
315+
316+
def keymap_skeleton():
317+
"""Returns the minimal structure needed for a keymap.json.
318+
"""
319+
return {
320+
'version': 1,
321+
'notes': '',
322+
'keyboard': None,
323+
'keymap': None,
324+
'layout': None,
325+
'layers': [],
326+
'documentation': KEYMAP_DOCUMENTATION,
327+
}

qmk_compiler.py

+67-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import BytesIO
44
from os import chdir, environ, path, remove
55
from subprocess import check_output, CalledProcessError, STDOUT
6-
from time import strftime
6+
from time import strftime, time
77
from traceback import format_exc
88

99
from rq import get_current_job
@@ -80,7 +80,7 @@ def compile_keymap(job, result):
8080
result['firmware_filename'] = find_firmware_file()
8181

8282
except CalledProcessError as build_error:
83-
logging.error('Could not build firmware (%s): %s', build_error.cmd, build_error.output)
83+
print('Could not build firmware (%s): %s' % (build_error.cmd, build_error.output))
8484
result['returncode'] = build_error.returncode
8585
result['cmd'] = build_error.cmd
8686
result['output'] = build_error.output
@@ -106,7 +106,6 @@ def compile_firmware(keyboard, keymap, layout, layers, source_ip=None):
106106
'author': '',
107107
'notes': '',
108108
'version': 1,
109-
'source_ip': source_ip,
110109
'documentation': 'This file is a configurator export. You can compile it directly inside QMK using the command `bin/qmk compile %s`' % (keymap_json_file,)
111110
})
112111
result = {
@@ -130,7 +129,7 @@ def compile_firmware(keyboard, keymap, layout, layers, source_ip=None):
130129

131130
# Sanity checks
132131
if not path.exists('qmk_firmware/keyboards/' + keyboard):
133-
logging.error('Unknown keyboard: %s', keyboard)
132+
print('Unknown keyboard: %s' % (keyboard,))
134133
return {'returncode': -1, 'command': '', 'output': 'Unknown keyboard!', 'firmware': None}
135134

136135
# If this keyboard needs a submodule check it out
@@ -161,8 +160,71 @@ def compile_firmware(keyboard, keymap, layout, layers, source_ip=None):
161160
return result
162161

163162

163+
@job('default', connection=redis, timeout=900)
164+
def compile_json(keyboard_keymap_data, source_ip=None):
165+
"""Compile a keymap.
166+
167+
Arguments:
168+
169+
keyboard_keymap_data
170+
A configurator export file that's been deserialized
171+
172+
source_ip
173+
The IP that submitted the compile job
174+
"""
175+
result = {
176+
'returncode': -2,
177+
'output': '',
178+
'firmware': None,
179+
'firmware_filename': '',
180+
'source_ip': source_ip,
181+
'output': 'Unknown error',
182+
}
183+
try:
184+
for key in ('keyboard', 'layout', 'keymap'):
185+
result[key] = keyboard_keymap_data[key]
186+
187+
result['keymap_archive'] = '%s-%s.json' % (result['keyboard'].replace('/', '-'), result['keymap'].replace('/', '-'))
188+
result['keymap_json'] = json.dumps(keyboard_keymap_data)
189+
result['command'] = ['bin/qmk', 'compile', result['keymap_archive']]
190+
191+
kb_data = qmk_redis.get('qmk_api_kb_' + result['keyboard'])
192+
job = get_current_job()
193+
result['id'] = job.id
194+
checkout_qmk()
195+
196+
# Sanity checks
197+
if not path.exists('qmk_firmware/keyboards/' + result['keyboard']):
198+
print('Unknown keyboard: %s' % (result['keyboard'],))
199+
return {'returncode': -1, 'command': '', 'output': 'Unknown keyboard!', 'firmware': None}
200+
201+
# If this keyboard needs a submodule check it out
202+
if kb_data.get('protocol') in ['ChibiOS', 'LUFA']:
203+
checkout_lufa()
204+
205+
if kb_data.get('protocol') == 'ChibiOS':
206+
checkout_chibios()
207+
208+
# Write the keymap file
209+
with open(path.join('qmk_firmware', result['keymap_archive']), 'w') as fd:
210+
fd.write(result['keymap_json'] + '\n')
211+
212+
# Compile the firmware
213+
store_firmware_source(result)
214+
remove(result['source_archive'])
215+
compile_keymap(job, result)
216+
store_firmware_binary(result)
217+
218+
except Exception as e:
219+
result['returncode'] = -3
220+
result['exception'] = e.__class__.__name__
221+
result['stacktrace'] = format_exc()
222+
223+
return result
224+
225+
164226
@job('default', connection=redis)
165227
def ping():
166228
"""Write a timestamp to redis to make sure at least one worker is running ok.
167229
"""
168-
return redis.set('qmk_api_last_ping', strftime('"%Y-%m-%d %H:%M:%SZ"'))
230+
return redis.set('qmk_api_last_ping', time())

update_kb_redis.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ def list_keyboards():
6767
"""
6868
chdir('qmk_firmware')
6969
try:
70-
keyboards = check_output(('make', 'list-keyboards'), stderr=STDOUT, universal_newlines=True)
70+
keyboards = check_output(('qmk', 'list-keyboards'), universal_newlines=True)
7171
keyboards = keyboards.strip()
72-
keyboards = keyboards.split('\n')[-1]
7372
finally:
7473
chdir('..')
7574
return keyboards.split()

0 commit comments

Comments
 (0)