Skip to content

Instantly share code, notes, and snippets.

@samdoran
Created December 11, 2024 04:59
Show Gist options
  • Save samdoran/043d221705697c083b7c3ea3b8f39d8e to your computer and use it in GitHub Desktop.
Save samdoran/043d221705697c083b7c3ea3b8f39d8e to your computer and use it in GitHub Desktop.
Save and organize Tasmota firmware
#!/usr/bin/env python
import argparse
import gzip
import shutil
import sys
from pathlib import Path
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--basename", "-b", default="tasmota")
parser.add_argument("--output", "-o", type=Path, default=Path("~/Firmware/Tasmota").expanduser())
parser.add_argument("--version", "-v", required=True)
parser.add_argument("--subdir", "-s", type=Path, default="")
parser.add_argument("--device-type", "-t", type=Path, default="")
return parser.parse_args()
def main():
args = parse_args()
firmware = Path(".pio/build/tasmota/firmware.bin")
firmware_min = Path(".pio/build/tasmota-minimal/firmware.bin")
if not all(file.exists() for file in (firmware, firmware_min)):
sys.exit("Missing firmware files")
# Create the target directories
dest = args.output / args.version / args.subdir / args.device_type / f"{args.basename}.bin"
dest_min = dest.with_stem(f"{args.basename}-minimal")
for directory in (dest, dest_min):
if not directory.parent.is_dir():
directory.parent.mkdir(parents=True)
# Copy the firmware files into place
shutil.copy(firmware, dest)
print(f"Copied to {dest}")
shutil.copy(firmware_min, dest_min)
print(f"Copied to {dest_min}")
# Compress the files
for file in (dest, dest_min):
with file.open("rb") as input:
with gzip.open(file.with_suffix(f"{file.suffix}.gz"), "wb") as output:
shutil.copyfileobj(input, output)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment