2022-08-26 13:16:13 +02:00
|
|
|
import shutil
|
2022-08-28 11:57:13 +02:00
|
|
|
import subprocess
|
|
|
|
from os import devnull, system
|
2022-08-26 13:16:13 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
source_dir = Path('kmk')
|
|
|
|
build_dir = Path('build')
|
|
|
|
|
|
|
|
|
2022-08-28 11:57:13 +02:00
|
|
|
def clean():
|
2022-08-26 13:16:13 +02:00
|
|
|
build_dir.mkdir(exist_ok=True)
|
|
|
|
for child in build_dir.iterdir():
|
|
|
|
if child.is_file():
|
|
|
|
child.unlink()
|
|
|
|
else:
|
|
|
|
shutil.rmtree(child)
|
|
|
|
|
|
|
|
|
2022-08-28 11:57:13 +02:00
|
|
|
def compile():
|
2022-08-26 13:16:13 +02:00
|
|
|
|
|
|
|
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'))
|
2022-08-28 11:57:13 +02:00
|
|
|
system(f'mpy-cross {x} -o {out_path}')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
subprocess.run('mpy-cross', stdout=devnull, stderr=devnull)
|
|
|
|
except (FileNotFoundError):
|
|
|
|
print()
|
|
|
|
print('`mpy-cross` not found. Ensure mpy-cross is working from a shell.')
|
|
|
|
print()
|
|
|
|
clean()
|
|
|
|
compile()
|