diff --git a/software/pio_hooks.py b/software/pio_hooks.py index 55bdd2381..964691578 100644 --- a/software/pio_hooks.py +++ b/software/pio_hooks.py @@ -14,7 +14,7 @@ import re import json import glob -from pathlib import PurePath +import pathlib from base64 import b64encode from zlib import crc32 from collections import namedtuple @@ -33,7 +33,7 @@ def check_call(*args, **kwargs): sys.exit(1) def get_changelog_version(name): - path = os.path.join('changelog_{}.txt'.format(name)) + path = f'changelog_{name}.txt' versions = [] with open(path, 'r', encoding='utf-8') as f: @@ -102,8 +102,7 @@ def write_firmware_info(display_name, major, minor, patch, beta, build_time): buf[76] = int(beta) # since version 2 buf[4092:4096] = crc32(buf[0:4092]).to_bytes(4, byteorder='little') - with open(os.path.join(env.subst("$BUILD_DIR"), "firmware_info.bin"), "wb") as f: - f.write(buf) + pathlib.Path(env.subst('$BUILD_DIR'), 'firmware_info.bin').write_bytes(buf) def generate_module_dependencies(info_path, module, modules, all_modules_upper): if module: @@ -800,7 +799,7 @@ def main(): backend_modules.append(util.FlavoredName("Debug").get()) with tfutil.ChangedDirectory('src'): - excluded_bindings = [PurePath(x).as_posix() for x in glob.glob('bindings/brick_*') + glob.glob('bindings/bricklet_*')] + excluded_bindings = [pathlib.PurePath(x).as_posix() for x in glob.glob('bindings/brick_*') + glob.glob('bindings/bricklet_*')] excluded_bindings.remove('bindings/bricklet_unknown.h') excluded_bindings.remove('bindings/bricklet_unknown.c') @@ -849,10 +848,10 @@ def include_bindings(path): check_call([env.subst('$PYTHONEXE'), "-u", "prepare.py"], env=environ) for root, dirs, files in os.walk('src', followlinks=True): - root_path = PurePath(root) + root_path = pathlib.PurePath(root) root_parents = [root_path] + list(root_path.parents) - if PurePath('src', 'bindings') in root_parents or PurePath('src', 'modules') in root_parents: + if pathlib.PurePath('src', 'bindings') in root_parents or pathlib.PurePath('src', 'modules') in root_parents: continue for name in files: diff --git a/software/web/build.py b/software/web/build.py index 412a2ae79..f48756b40 100644 --- a/software/web/build.py +++ b/software/web/build.py @@ -13,7 +13,7 @@ JS_ANALYZE = False -BUILD_DIR = '../build' +BUILD_DIR = pathlib.Path('..', 'build') HTML_MINIFIER_TERSER_OPTIONS = [ '--collapse-boolean-attributes', @@ -68,17 +68,17 @@ def main(): print(f'Error: {e}', file=sys.stderr) exit(42) else: - os.makedirs(os.path.split(src_tfpp_path)[0], exist_ok=True) + src_tfpp_path.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(src_path, src_tfpp_path) os.chdir('src_tfpp') try: - shutil.rmtree(BUILD_DIR) + shutil.rmtree(str(BUILD_DIR)) except FileNotFoundError: pass - os.makedirs(BUILD_DIR) + BUILD_DIR.mkdir(parents=True) print('tsc...') subprocess.check_call([ @@ -93,12 +93,12 @@ def main(): 'npx', 'esbuild', 'main.tsx', - '--metafile={}'.format(os.path.join(BUILD_DIR, 'meta.json')), + f'--metafile={BUILD_DIR / "meta.json"}', '--bundle', '--target=es6', '--alias:argon2-browser=../node_modules/argon2-browser/dist/argon2-bundled.min.js', '--alias:jquery=../node_modules/jquery/dist/jquery.slim.min', - '--outfile={0}'.format(os.path.join(BUILD_DIR, 'bundle.min.js')) + f'--outfile={BUILD_DIR / "bundle.min.js"}' ] if JS_ANALYZE: @@ -123,7 +123,7 @@ def main(): scss_args += [ 'main.scss', - os.path.join(BUILD_DIR, 'main.css') + str(BUILD_DIR / 'main.css') ] subprocess.check_call(scss_args, shell=sys.platform == 'win32') @@ -132,24 +132,20 @@ def main(): subprocess.check_call([ 'npx', 'postcss', - os.path.join(BUILD_DIR, 'main.css'), + str(BUILD_DIR / 'main.css'), '-o', - os.path.join(BUILD_DIR, 'main.min.css') + str(BUILD_DIR / 'main.min.css') ], shell=sys.platform == 'win32') if args.css_source_map: - with open(os.path.join(BUILD_DIR, 'main.min.css'), 'r', encoding='utf-8') as f: - css_src = f.read() + css_src = (BUILD_DIR / 'main.min.css').read_text(encoding='utf-8') + css_map = b64encode((BUILD_DIR / 'main.min.css.map').read_bytes()).decode('ascii') - with open(os.path.join(BUILD_DIR, 'main.min.css.map'), 'rb') as f: - css_map = b64encode(f.read()).decode('ascii') + (BUILD_DIR / 'main.min.css.map').unlink() - os.remove(os.path.join(BUILD_DIR, 'main.min.css.map')) + css_src = css_src.replace('sourceMappingURL=main.min.css.map', f'sourceMappingURL=data:text/json;base64,{css_map}') - css_src = css_src.replace('sourceMappingURL=main.min.css.map', 'sourceMappingURL=data:text/json;base64,{0}'.format(css_map)) - - with open(os.path.join(BUILD_DIR, 'main.min.css'), 'w', encoding='utf-8') as f: - f.write(css_src) + (BUILD_DIR / 'main.min.css').write_text(css_src, encoding='utf-8') print('html-minifier-terser...') subprocess.check_call([ @@ -157,7 +153,7 @@ def main(): 'html-minifier-terser'] + HTML_MINIFIER_TERSER_OPTIONS + [ '-o', - os.path.join(BUILD_DIR, 'index.min.html'), + str(BUILD_DIR / 'index.min.html'), 'index.html' ], shell=sys.platform == 'win32')