From b4363c73a7e7bec2380a1a2bc7b4ebdfe36a8490 Mon Sep 17 00:00:00 2001 From: Luke D Russell Date: Fri, 26 Aug 2022 21:16:13 +1000 Subject: [PATCH] Shrink ./kmk/ with `nox` & `mpy-cross`. Shrink ./kmk/ with `nox` & `mpy-cross`. The nice!nano CircuitPython doesn't leave enough storage for an uncompiled ./kmk/ folder, with main.py and Bluetooth libraries. This introduces a soft dependancy to kmk on `nox`, as a OS-agnostic alternative to `make`. Bonus is the use of native python to describe steps / options / sessions. --- noxfile.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 noxfile.py diff --git a/noxfile.py b/noxfile.py new file mode 100644 index 0000000..ac273c0 --- /dev/null +++ b/noxfile.py @@ -0,0 +1,56 @@ +import nox +import shutil +from pathlib import Path + +source_dir = Path('kmk') +build_dir = Path('build') + + +@nox.session +def black(session): + '''Format python code with `black`.''' + session.install('black') + session.run('black', source_dir) + + +@nox.session +def isort(session): + session.install('isort') + session.run('isort', source_dir) + + +@nox.session +def flake8(session): + session.install('flake8') + session.run('flake8', source_dir) + + +@nox.session +def clean(session): + build_dir.mkdir(exist_ok=True) + for child in build_dir.iterdir(): + if child.is_file(): + child.unlink() + else: + shutil.rmtree(child) + + +@nox.session +def compile(session): + + clean(session) + + shutil.copy2('boot.py', 'build/boot.py') + + # Make sure the full folder heirarchy exists + for d in source_dir.glob('**/'): + if not build_dir.joinpath(d).exists(): + Path.mkdir(build_dir.joinpath(d)) + + # Compile every python file + for x in source_dir.glob('**/*.py'): + out_path = str(build_dir.joinpath(x).with_suffix('.mpy')) + session.run('mpy-cross', f'{x}', '-o', f'{out_path}', external=True) + + +nox.options.sessions = ['black', 'isort'] # Default sessions