Skip to content

Commit

Permalink
build: Start using more pathlib and f-string
Browse files Browse the repository at this point in the history
  • Loading branch information
photron committed Nov 2, 2024
1 parent 1e1a2b8 commit 1d99749
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
13 changes: 6 additions & 7 deletions software/pio_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 15 additions & 19 deletions software/web/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

JS_ANALYZE = False

BUILD_DIR = '../build'
BUILD_DIR = pathlib.Path('..', 'build')

HTML_MINIFIER_TERSER_OPTIONS = [
'--collapse-boolean-attributes',
Expand Down Expand Up @@ -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([
Expand All @@ -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:
Expand All @@ -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')
Expand All @@ -132,32 +132,28 @@ 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([
'npx',
'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')

Expand Down

0 comments on commit 1d99749

Please sign in to comment.