Skip to content

Commit

Permalink
Use argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
vidplace7 committed Oct 7, 2024
1 parent d78898d commit 0c6e9b6
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 83 deletions.
4 changes: 4 additions & 0 deletions .github/dependabot.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ updates:
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "daily"
1 change: 0 additions & 1 deletion .github/workflows/build-container.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ jobs:
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
# platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.venv
firmware
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File with Arguments",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [
"${command:pickArgs}",
"--arch", "nrf52840",
"--board", "xiao_ble",
"--build_script_path", "bin/build-nrf52.sh",
]
}
]
}
18 changes: 4 additions & 14 deletions Containerfile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
FROM python:3.12-bookworm
ENV PIP_ROOT_USER_ACTION=ignore

# Add build scripts (firmware/bin) to PATH
# RUN git clone --no-checkout --depth 1 https://github.com/meshtastic/firmware.git /meshtastic && \
# git -C /meshtastic config core.sparseCheckout true && \
# echo "bin/*" > /meshtastic/.git/info/sparse-checkout && \
# git -C /meshtastic checkout master
# ENV PATH="/meshtastic/bin:$PATH"

# Install build dependencies
RUN apt update && apt install -y \
build-essential \
cppcheck libbluetooth-dev libgpiod-dev libyaml-cpp-dev && \
# Install DRA (GitHub Releases downloader)
# wget 'https://github.com/devmatteini/dra/releases/download/0.6.2/dra_0.6.2-1_amd64.deb' && \
# dpkg --install dra_0.6.2-1_amd64.deb && rm dra_0.6.2-1_amd64.deb && \
apt clean && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements.txt .
RUN python -m pip install --upgrade pip && \
pip install -U --no-build-isolation --no-cache-dir "setuptools<72" && \
pip install -U --no-build-isolation -r requirements.txt && \
pip install -U platformio adafruit-nrfutil --no-build-isolation && \
pip install -U poetry --no-build-isolation && \
pip install -U meshtastic --pre --no-build-isolation

# Upgrade PlatformIO
RUN pio upgrade

# COPY entrypoint.sh /entrypoint.sh
# ENTRYPOINT [ "/entrypoint.sh" ]

COPY entrypoint.py /entrypoint.py
ENTRYPOINT [ "/entrypoint.py" ]
ENTRYPOINT [ "/entrypoint.py" ]
CMD [ "master" ]
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
20 changes: 12 additions & 8 deletions action.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: Setup Build Variant Composite Action
description: Variant build actions for Meshtastic Platform IO steps

inputs:
git-ref:
description: The git ref (tag/branch) of the meshtastic firmware to checkout
required: true
default: "master"
arch:
description: Processor arch name
required: true
Expand Down Expand Up @@ -36,11 +40,11 @@ outputs:
runs:
using: docker
image: docker://ghcr.io/vidplace7/build-meshtastic:main
# env:
# ARCH: ${{ inputs.arch }}
# BOARD: ${{ inputs.board }}
# BUILD_SCRIPT_PATH: ${{ inputs.build-script-path }}
# REMOVE_DEBUG_FLAGS: ${{ inputs.remove-debug-flags }}
# OTA_FIRMWARE_SOURCE: ${{ inputs.ota-firmware-source }}
# OTA_FIRMWARE_TARGET: ${{ inputs.ota-firmware-target }}
# INCLUDE_WEB_UI: ${{ inputs.include-web-ui }}
args:
- ${{ inputs.git-ref }}
- --arch
- ${{ inputs.arch }}
- --board
- ${{ inputs.board }}
- --build_script_path
- ${{ inputs.build-script-path }}
92 changes: 67 additions & 25 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,45 @@
import requests
import tarfile
import sys

inputs = {
'arch': os.getenv('INPUT_ARCH'),
'board': os.getenv('INPUT_BOARD'),
'build-script-path': os.path.normpath(os.getenv('INPUT_BUILD-SCRIPT-PATH')),
'remove-debug-flags': str(os.getenv('INPUT_REMOVE-DEBUG-FLAGS', '')).split(),
'ota-firmware-source': os.getenv('INPUT_OTA-FIRMWARE-SOURCE', ''),
'ota-firmware-target': os.getenv('INPUT_OTA-FIRMWARE-TARGET', ''),
'include-web-ui': bool(os.getenv('INPUT_INCLUDE-WEB-UI', False))
}
import shutil
import argparse

from git import Repo

# Parse arguments
# Handle CLI and GitHub Actions inputs
parser = argparse.ArgumentParser(description="")
parser.add_argument('git_ref', type=str,
default="master",
help='The tag or branch to clone from the repository.')
parser.add_argument('--git_dir', type=str, required=False,
# Use the GITHUB_WORKSPACE environment variable when running in GitHub Actions
# Otherwise, use the 'firmware' directory (local testing)
default=os.getenv('GITHUB_WORKSPACE', 'firmware'),
help='The directory to clone the meshtastic repository to.')
parser.add_argument('--arch', type=str, required=True,
help='The architecture to build for.')
parser.add_argument('--board', type=str, required=True,
help='The board to build for.')
parser.add_argument('--build_script_path', type=str, required=True,
help='The path to the build script.')
parser.add_argument('--remove_debug_flags', type=list, required=False,
default=str(os.getenv('INPUT_REMOVE-DEBUG-FLAGS', '')).split(),
help='The debug flags to remove from the build.')
parser.add_argument('--ota_firmware_source', type=str, required=False,
default=os.getenv('INPUT_OTA-FIRMWARE-SOURCE', ''),
help='The source path to download the OTA firmware.')
parser.add_argument('--ota_firmware_target', type=str, required=False,
default=os.getenv('INPUT_OTA-FIRMWARE-TARGET', ''),
help='The target path to save the OTA firmware.')
parser.add_argument('--include_web_ui', type=bool, required=False,
default=bool(os.getenv('INPUT_INCLUDE-WEB-UI', False)),
help='Whether to include the web UI in the build.')
args = parser.parse_args()

env = {
'GITHUB_ACTIONS': bool(os.getenv('GITHUB_ACTIONS')),
'XDG_CACHE_HOME': os.path.normpath(os.getenv('XDG_CACHE_HOME'))
'XDG_CACHE_HOME': os.path.normpath(os.getenv('XDG_CACHE_HOME', ''))
}

def gh_latest_release(owner, repo):
Expand Down Expand Up @@ -51,15 +76,24 @@ def extract_tar(tar_file, extract_to, remove_src=False):

# ==============

# Fix cache permissions
if os.path.exists(env['XDG_CACHE_HOME']):
os.system(f'chmod -R 777 {env['XDG_CACHE_HOME']}')
# Clone the Meshtastic repo
gh_repo = "meshtastic/firmware"
print(f"Cloning {gh_repo} {args.git_ref} to {args.git_dir}/")
if os.path.exists(args.git_dir):
shutil.rmtree(args.git_dir)
meshtastic_repo = Repo.clone_from("https://github.com/meshtastic/firmware.git", args.git_dir, single_branch=True, branch=args.git_ref, recursive=True)
print(f"..Cloned {gh_repo} {args.git_ref} to {args.git_dir}/")

# Workaround for the "safe.directory" issue
os.system("git config --system --add safe.directory /github/workspace")
if env['GITHUB_ACTIONS']:
# Fix cache permissions
if os.path.exists(env['XDG_CACHE_HOME']):
os.system(f'chmod -R 777 {env['XDG_CACHE_HOME']}')

# Workaround for the "safe.directory" issue
os.system("git config --system --add safe.directory /github/workspace")

# Web UI
if inputs['include-web-ui'] == True:
if args.include_web_ui == True:
mt_web = gh_latest_release('meshtastic', 'web')
for asset in mt_web['assets']:
if asset['name'] == 'build.tar':
Expand All @@ -69,26 +103,34 @@ def extract_tar(tar_file, extract_to, remove_src=False):
extract_tar('build.tar','data/static', remove_src=True)

# Remove debug flags for release
if len(inputs['remove-debug-flags']) > 0:
for flag in inputs['remove-debug-flags']:
if len(args.remove_debug_flags) > 0:
for flag in args.remove_debug_flags:
os.system(f"sed -i /DDEBUG_HEAP/d {flag}")

# Apply custom changes (if any)
if os.path.exists('.custom'):
os.system(f'rsync -av .custom/firmware/ {args.git_dir}')
shutil.rmtree('.custom')

# Run the Build
sys.stdout.flush()
r_build = subprocess.run([inputs['build-script-path'], inputs['board']], check=True)
sys.stdout.flush() # Fix subprocess output buffering issue
build_abspath = os.path.abspath(os.path.join(args.git_dir, args.build_script_path))
r_build = subprocess.run(
[build_abspath, args.board],
cwd=args.git_dir, check=True)

# Pull OTA firmware
if inputs['ota-firmware-source'] != '' and inputs['ota-firmware-target'] != '':
if args.ota_firmware_source != '' and args.ota_firmware_target != '':
ota_fw = gh_latest_release('meshtastic', 'firmware-ota')
for asset in ota_fw['assets']:
if asset['name'] == inputs['ota-firmware-source']:
if asset['name'] == args.ota_firmware_source:
# Download firmware.bin
download_file(asset['browser_download_url'], inputs['ota-firmware-target'])
download_file(asset['browser_download_url'], args.ota_firmware_target)

# When running in GitHub Actions
if env['GITHUB_ACTIONS']:
# Hack! - Add checked-out firmware/bin to the python module path
sys.path.append(os.path.abspath('bin'))
sys.path.append(os.path.join(build_abspath, 'bin'))
from readprops import readProps # type: ignore
# /Hack!

Expand Down
35 changes: 0 additions & 35 deletions entrypoint.sh

This file was deleted.

3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
requests
GitPython
platformio

0 comments on commit 0c6e9b6

Please sign in to comment.